Skip to content

Commit 1653bab

Browse files
committed
Add dubbo bind host to server
1 parent ecf7420 commit 1653bab

File tree

10 files changed

+184
-122
lines changed

10 files changed

+184
-122
lines changed

dubbo-common/src/main/java/org/apache/dubbo/config/nested/OpenAPIConfig.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ public class OpenAPIConfig implements Serializable {
9191
*/
9292
private String securityScheme;
9393

94+
/**
95+
* The security.
96+
*/
97+
private String security;
98+
9499
/**
95100
* The strategy used to generate operation id and schema name.
96101
*/
@@ -245,6 +250,14 @@ public void setSecurityScheme(String securityScheme) {
245250
this.securityScheme = securityScheme;
246251
}
247252

253+
public String getSecurity() {
254+
return security;
255+
}
256+
257+
public void setSecurity(String security) {
258+
this.security = security;
259+
}
260+
248261
public String getNameStrategy() {
249262
return nameStrategy;
250263
}

dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/DefaultHttpRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public String scheme() {
300300
if (isHttp2()) {
301301
scheme = headers.getFirst(PseudoHeaderName.SCHEME.value());
302302
}
303-
return scheme == null ? HttpConstants.HTTPS : scheme;
303+
return scheme == null ? HttpConstants.HTTP : scheme;
304304
}
305305

306306
@Override

dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/openapi/Constants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,8 @@ public final class Constants {
2929
public static final String X_JAVA_METHOD = "x-java-method";
3030
public static final String X_JAVA_PARAM = "x-java-param";
3131

32+
public static final String DUBBO_SERVER = "Dubbo Server";
33+
public static final String REFERER = "referer";
34+
3235
private Constants() {}
3336
}

dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/openapi/DefaultOpenAPIService.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public HttpResult<?> handle(String path, HttpRequest httpRequest, HttpResponse h
113113
}
114114
return HttpResult.builder()
115115
.contentType(MediaType.APPLICATION + '/' + request.getFormat())
116-
.body(handleDocument(request).getBytes(StandardCharsets.UTF_8))
116+
.body(handleDocument(request, httpRequest).getBytes(StandardCharsets.UTF_8))
117117
.build();
118118
}
119119

@@ -180,7 +180,7 @@ public String getDocument(OpenAPIRequest request) {
180180

181181
HttpRequest httpRequest = RpcContext.getServiceContext().getRequest(HttpRequest.class);
182182
if (!RequestUtils.isRestRequest(httpRequest)) {
183-
return handleDocument(request);
183+
return handleDocument(request, null);
184184
}
185185

186186
path = RequestUtils.getPathVariable(httpRequest, "path");
@@ -213,12 +213,22 @@ public String getDocument(OpenAPIRequest request) {
213213
}
214214
}
215215

216-
private String handleDocument(OpenAPIRequest request) {
216+
private String handleDocument(OpenAPIRequest request, HttpRequest httpRequest) {
217217
if (Boolean.FALSE.equals(configFactory.getGlobalConfig().getCache())) {
218218
return definitionEncoder.encode(getOpenAPI(request), request);
219219
}
220220

221-
String cacheKey = request.toString();
221+
StringBuilder sb = new StringBuilder();
222+
if (httpRequest != null) {
223+
String host = httpRequest.serverHost();
224+
if (host != null) {
225+
String referer = httpRequest.header(Constants.REFERER);
226+
sb.append(referer != null && referer.contains(host) ? '/' : host);
227+
}
228+
}
229+
sb.append('|').append(request.toString());
230+
231+
String cacheKey = sb.toString();
222232
SoftReference<String> ref = cache.get(cacheKey);
223233
if (ref != null) {
224234
String value = ref.get();

dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/openapi/DefinitionFilter.java

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.apache.dubbo.rpc.protocol.tri.rest.openapi;
1818

1919
import org.apache.dubbo.remoting.http12.HttpMethods;
20+
import org.apache.dubbo.remoting.http12.HttpRequest;
2021
import org.apache.dubbo.remoting.http12.rest.OpenAPIRequest;
2122
import org.apache.dubbo.rpc.model.FrameworkModel;
2223
import org.apache.dubbo.rpc.protocol.tri.rest.openapi.model.ApiResponse;
@@ -31,6 +32,7 @@
3132
import org.apache.dubbo.rpc.protocol.tri.rest.openapi.model.RequestBody;
3233
import org.apache.dubbo.rpc.protocol.tri.rest.openapi.model.Schema;
3334
import org.apache.dubbo.rpc.protocol.tri.rest.openapi.model.SecurityScheme;
35+
import org.apache.dubbo.rpc.protocol.tri.rest.openapi.model.Server;
3436

3537
import java.util.Iterator;
3638
import java.util.List;
@@ -52,32 +54,58 @@ public DefinitionFilter(FrameworkModel frameworkModel) {
5254

5355
public OpenAPI filter(OpenAPI openAPI, OpenAPIRequest request) {
5456
OpenAPIFilter[] filters = extensionFactory.getExtensions(OpenAPIFilter.class, request.getGroup());
55-
if (filters.length == 0) {
56-
return openAPI;
57-
}
58-
5957
Context context = new ContextImpl(openAPI, schemaResolver, extensionFactory, request);
60-
for (OpenAPIFilter filter : filters) {
61-
openAPI = filter.filterOpenAPI(openAPI, context);
62-
if (openAPI == null) {
63-
return null;
58+
59+
if (filters.length > 0) {
60+
for (OpenAPIFilter filter : filters) {
61+
openAPI = filter.filterOpenAPI(openAPI, context);
62+
if (openAPI == null) {
63+
return null;
64+
}
6465
}
65-
}
6666

67-
filterPaths(openAPI, filters, context);
67+
filterPaths(openAPI, filters, context);
6868

69-
filterComponents(openAPI, filters, context);
69+
filterComponents(openAPI, filters, context);
7070

71-
for (OpenAPIFilter filter : filters) {
72-
openAPI = filter.filterOpenAPICompletion(openAPI, context);
73-
if (openAPI == null) {
74-
return null;
71+
for (OpenAPIFilter filter : filters) {
72+
openAPI = filter.filterOpenAPICompletion(openAPI, context);
73+
if (openAPI == null) {
74+
return null;
75+
}
7576
}
7677
}
7778

79+
filterServer(openAPI, context);
80+
7881
return openAPI;
7982
}
8083

84+
private static void filterServer(OpenAPI openAPI, Context context) {
85+
List<Server> servers = openAPI.getServers();
86+
if (servers == null || servers.size() != 1) {
87+
return;
88+
}
89+
Server server = servers.get(0);
90+
if (!Constants.DUBBO_SERVER.equals(server.getDescription())) {
91+
return;
92+
}
93+
HttpRequest httpRequest = context.getHttpRequest();
94+
if (httpRequest == null) {
95+
return;
96+
}
97+
String host = httpRequest.serverHost();
98+
if (host == null) {
99+
return;
100+
}
101+
String referer = httpRequest.header(Constants.REFERER);
102+
if (referer != null && referer.contains(host)) {
103+
servers.clear();
104+
} else {
105+
server.setUrl(httpRequest.scheme() + "://" + host);
106+
}
107+
}
108+
81109
private void filterPaths(OpenAPI openAPI, OpenAPIFilter[] filters, Context context) {
82110
Map<String, PathItem> paths = openAPI.getPaths();
83111
if (paths == null) {

0 commit comments

Comments
 (0)