Skip to content

Commit

Permalink
Add dubbo bind host to server
Browse files Browse the repository at this point in the history
  • Loading branch information
oxsean committed Nov 27, 2024
1 parent ecf7420 commit 1653bab
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public class OpenAPIConfig implements Serializable {
*/
private String securityScheme;

/**
* The security.
*/
private String security;

/**
* The strategy used to generate operation id and schema name.
*/
Expand Down Expand Up @@ -245,6 +250,14 @@ public void setSecurityScheme(String securityScheme) {
this.securityScheme = securityScheme;
}

public String getSecurity() {
return security;
}

public void setSecurity(String security) {
this.security = security;
}

public String getNameStrategy() {
return nameStrategy;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public String scheme() {
if (isHttp2()) {
scheme = headers.getFirst(PseudoHeaderName.SCHEME.value());
}
return scheme == null ? HttpConstants.HTTPS : scheme;
return scheme == null ? HttpConstants.HTTP : scheme;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@ public final class Constants {
public static final String X_JAVA_METHOD = "x-java-method";
public static final String X_JAVA_PARAM = "x-java-param";

public static final String DUBBO_SERVER = "Dubbo Server";
public static final String REFERER = "referer";

private Constants() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public HttpResult<?> handle(String path, HttpRequest httpRequest, HttpResponse h
}
return HttpResult.builder()
.contentType(MediaType.APPLICATION + '/' + request.getFormat())
.body(handleDocument(request).getBytes(StandardCharsets.UTF_8))
.body(handleDocument(request, httpRequest).getBytes(StandardCharsets.UTF_8))
.build();
}

Expand Down Expand Up @@ -180,7 +180,7 @@ public String getDocument(OpenAPIRequest request) {

HttpRequest httpRequest = RpcContext.getServiceContext().getRequest(HttpRequest.class);
if (!RequestUtils.isRestRequest(httpRequest)) {
return handleDocument(request);
return handleDocument(request, null);
}

path = RequestUtils.getPathVariable(httpRequest, "path");
Expand Down Expand Up @@ -213,12 +213,22 @@ public String getDocument(OpenAPIRequest request) {
}
}

private String handleDocument(OpenAPIRequest request) {
private String handleDocument(OpenAPIRequest request, HttpRequest httpRequest) {
if (Boolean.FALSE.equals(configFactory.getGlobalConfig().getCache())) {
return definitionEncoder.encode(getOpenAPI(request), request);
}

String cacheKey = request.toString();
StringBuilder sb = new StringBuilder();
if (httpRequest != null) {
String host = httpRequest.serverHost();
if (host != null) {
String referer = httpRequest.header(Constants.REFERER);
sb.append(referer != null && referer.contains(host) ? '/' : host);
}
}
sb.append('|').append(request.toString());

String cacheKey = sb.toString();
SoftReference<String> ref = cache.get(cacheKey);
if (ref != null) {
String value = ref.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.dubbo.rpc.protocol.tri.rest.openapi;

import org.apache.dubbo.remoting.http12.HttpMethods;
import org.apache.dubbo.remoting.http12.HttpRequest;
import org.apache.dubbo.remoting.http12.rest.OpenAPIRequest;
import org.apache.dubbo.rpc.model.FrameworkModel;
import org.apache.dubbo.rpc.protocol.tri.rest.openapi.model.ApiResponse;
Expand All @@ -31,6 +32,7 @@
import org.apache.dubbo.rpc.protocol.tri.rest.openapi.model.RequestBody;
import org.apache.dubbo.rpc.protocol.tri.rest.openapi.model.Schema;
import org.apache.dubbo.rpc.protocol.tri.rest.openapi.model.SecurityScheme;
import org.apache.dubbo.rpc.protocol.tri.rest.openapi.model.Server;

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

public OpenAPI filter(OpenAPI openAPI, OpenAPIRequest request) {
OpenAPIFilter[] filters = extensionFactory.getExtensions(OpenAPIFilter.class, request.getGroup());
if (filters.length == 0) {
return openAPI;
}

Context context = new ContextImpl(openAPI, schemaResolver, extensionFactory, request);
for (OpenAPIFilter filter : filters) {
openAPI = filter.filterOpenAPI(openAPI, context);
if (openAPI == null) {
return null;

if (filters.length > 0) {
for (OpenAPIFilter filter : filters) {
openAPI = filter.filterOpenAPI(openAPI, context);
if (openAPI == null) {
return null;
}
}
}

filterPaths(openAPI, filters, context);
filterPaths(openAPI, filters, context);

filterComponents(openAPI, filters, context);
filterComponents(openAPI, filters, context);

for (OpenAPIFilter filter : filters) {
openAPI = filter.filterOpenAPICompletion(openAPI, context);
if (openAPI == null) {
return null;
for (OpenAPIFilter filter : filters) {
openAPI = filter.filterOpenAPICompletion(openAPI, context);
if (openAPI == null) {
return null;
}
}
}

filterServer(openAPI, context);

return openAPI;
}

private static void filterServer(OpenAPI openAPI, Context context) {
List<Server> servers = openAPI.getServers();
if (servers == null || servers.size() != 1) {
return;
}
Server server = servers.get(0);
if (!Constants.DUBBO_SERVER.equals(server.getDescription())) {
return;
}
HttpRequest httpRequest = context.getHttpRequest();
if (httpRequest == null) {
return;
}
String host = httpRequest.serverHost();
if (host == null) {
return;
}
String referer = httpRequest.header(Constants.REFERER);
if (referer != null && referer.contains(host)) {
servers.clear();
} else {
server.setUrl(httpRequest.scheme() + "://" + host);
}
}

private void filterPaths(OpenAPI openAPI, OpenAPIFilter[] filters, Context context) {
Map<String, PathItem> paths = openAPI.getPaths();
if (paths == null) {
Expand Down
Loading

0 comments on commit 1653bab

Please sign in to comment.