Skip to content

Commit d452ea4

Browse files
suncairong163suncrCrazyHZM
authored
Feature/dubbo32. resteasy filter&intercept support (#12492)
* export path matcher & Invoker to user * export path & invoker to user * add path matcher service method compare ut * fix httpMethodMatch * fix bug * add resteasy filter & intercept * add comment * add filter & intercept ut * add filter & intercept ut * fix content-type add headers * some modify request * some change request & add response filter * add licence * some modify request * add rest easy exception mapper * add filter context ,execute filter by loop * add test dependency * response filter on class modify * fix ClassNoFoundError by add onclass & some chang request * fix change request * add response intercept ut * add response intercept ut --------- Co-authored-by: suncr <suncairong@moresec.cn> Co-authored-by: huazhongming <crazyhzm@gmail.com>
1 parent d4856e2 commit d452ea4

File tree

45 files changed

+2517
-213
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2517
-213
lines changed

dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,4 +639,7 @@ public interface CommonConstants {
639639

640640
String DUBBO_PACKABLE_METHOD_FACTORY = "dubbo.application.parameters." + PACKABLE_METHOD_FACTORY_KEY;
641641

642+
String SERVICE_DEPLOYER_ATTRIBUTE_KEY = "serviceDeployer";
643+
String RESTEASY_NETTY_HTTP_REQUEST_ATTRIBUTE_KEY = "resteasyNettyHttpRequest";
644+
642645
}

dubbo-config/dubbo-config-api/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,12 @@
233233
<scope>test</scope>
234234
</dependency>
235235

236+
<dependency>
237+
<groupId>org.jboss.resteasy</groupId>
238+
<artifactId>resteasy-jackson-provider</artifactId>
239+
<scope>test</scope>
240+
</dependency>
241+
236242

237243
</dependencies>
238244
</project>

dubbo-distribution/dubbo-all/pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,24 @@
948948
META-INF/dubbo/internal/org.apache.dubbo.registry.xds.XdsCertificateSigner
949949
</resource>
950950
</transformer>
951+
<transformer
952+
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
953+
<resource>
954+
META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.rest.filter.RestResponseInterceptor
955+
</resource>
956+
</transformer>
957+
<transformer
958+
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
959+
<resource>
960+
META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.rest.filter.RestRequestFilter
961+
</resource>
962+
</transformer>
963+
<transformer
964+
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
965+
<resource>
966+
META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.rest.filter.RestResponseFilter
967+
</resource>
968+
</transformer>
951969
<transformer
952970
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
953971
<resource>

dubbo-rpc/dubbo-rpc-rest/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<dependency>
5050
<groupId>org.jboss.resteasy</groupId>
5151
<artifactId>resteasy-jaxrs</artifactId>
52+
<scope>provided</scope>
5253
</dependency>
5354

5455
<dependency>
@@ -66,21 +67,25 @@
6667
<dependency>
6768
<groupId>org.jboss.resteasy</groupId>
6869
<artifactId>resteasy-netty4</artifactId>
70+
<scope>provided</scope>
6971
</dependency>
7072

7173
<dependency>
7274
<groupId>org.jboss.resteasy</groupId>
7375
<artifactId>resteasy-jdk-http</artifactId>
76+
<scope>provided</scope>
7477
</dependency>
7578

7679
<dependency>
7780
<groupId>org.jboss.resteasy</groupId>
7881
<artifactId>resteasy-jackson-provider</artifactId>
82+
<scope>provided</scope>
7983
</dependency>
8084

8185
<dependency>
8286
<groupId>org.jboss.resteasy</groupId>
8387
<artifactId>resteasy-jaxb-provider</artifactId>
88+
<scope>provided</scope>
8489
</dependency>
8590

8691
<!-- swagger -->

dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/NettyHttpRestServer.java

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,10 @@
2424
import io.netty.handler.codec.http.HttpResponseEncoder;
2525
import org.apache.dubbo.common.URL;
2626
import org.apache.dubbo.common.utils.NetUtils;
27-
import org.apache.dubbo.common.utils.StringUtils;
28-
import org.apache.dubbo.metadata.rest.PathMatcher;
29-
import org.apache.dubbo.metadata.rest.RestMethodMetadata;
3027
import org.apache.dubbo.metadata.rest.ServiceRestMetadata;
3128
import org.apache.dubbo.rpc.Invoker;
3229
import org.apache.dubbo.rpc.protocol.rest.constans.RestConstant;
33-
import org.apache.dubbo.rpc.protocol.rest.exception.mapper.ExceptionMapper;
34-
import org.apache.dubbo.rpc.protocol.rest.handler.NettyHttpHandler;
30+
import org.apache.dubbo.rpc.protocol.rest.deploy.ServiceDeployer;
3531
import org.apache.dubbo.rpc.protocol.rest.netty.NettyServer;
3632
import org.apache.dubbo.rpc.protocol.rest.netty.RestHttpRequestDecoder;
3733
import org.apache.dubbo.rpc.protocol.rest.netty.UnSharedHandlerCreator;
@@ -45,7 +41,6 @@
4541
import java.util.concurrent.ConcurrentHashMap;
4642

4743
import static org.apache.dubbo.common.constants.CommonConstants.BACKLOG_KEY;
48-
import static org.apache.dubbo.common.constants.CommonConstants.COMMA_SPLIT_PATTERN;
4944
import static org.apache.dubbo.common.constants.CommonConstants.IO_THREADS_KEY;
5045
import static org.apache.dubbo.remoting.Constants.BIND_IP_KEY;
5146
import static org.apache.dubbo.remoting.Constants.BIND_PORT_KEY;
@@ -56,8 +51,7 @@
5651
*/
5752
public class NettyHttpRestServer implements RestProtocolServer {
5853

59-
private final PathAndInvokerMapper pathAndInvokerMapper = new PathAndInvokerMapper();
60-
private final ExceptionMapper exceptionMapper = new ExceptionMapper();
54+
private ServiceDeployer serviceDeployer = new ServiceDeployer();
6155
private NettyServer server = getNettyServer();
6256

6357
/**
@@ -99,7 +93,7 @@ public Map<String, Object> getAttributes() {
9993
@Override
10094
public void start(URL url) {
10195

102-
registerExceptionMapper(url);
96+
registerExtension(url);
10397

10498
String bindIp = url.getParameter(BIND_IP_KEY, url.getHost());
10599
if (!url.isAnyHost() && NetUtils.isValidLocalHost(bindIp)) {
@@ -132,7 +126,7 @@ public List<ChannelHandler> getUnSharedHandlers(URL url) {
132126
url.getParameter(RestConstant.MAX_HEADER_SIZE_PARAM, RestConstant.MAX_HEADER_SIZE),
133127
url.getParameter(RestConstant.MAX_CHUNK_SIZE_PARAM, RestConstant.MAX_CHUNK_SIZE)),
134128
new HttpObjectAggregator(url.getParameter(RestConstant.MAX_REQUEST_SIZE_PARAM, RestConstant.MAX_REQUEST_SIZE)),
135-
new HttpResponseEncoder(), new RestHttpRequestDecoder(new NettyHttpHandler(pathAndInvokerMapper, exceptionMapper), url))
129+
new HttpResponseEncoder(), new RestHttpRequestDecoder(url, serviceDeployer))
136130
;
137131
}
138132
};
@@ -180,34 +174,16 @@ protected List<ChannelHandler> getChannelHandlers(URL url) {
180174

181175
@Override
182176
public void deploy(ServiceRestMetadata serviceRestMetadata, Invoker invoker) {
183-
Map<PathMatcher, RestMethodMetadata> pathToServiceMapContainPathVariable =
184-
serviceRestMetadata.getPathContainPathVariableToServiceMap();
185-
pathAndInvokerMapper.addPathAndInvoker(pathToServiceMapContainPathVariable, invoker);
186-
187-
Map<PathMatcher, RestMethodMetadata> pathToServiceMapUnContainPathVariable =
188-
serviceRestMetadata.getPathUnContainPathVariableToServiceMap();
189-
pathAndInvokerMapper.addPathAndInvoker(pathToServiceMapUnContainPathVariable, invoker);
177+
serviceDeployer.deploy(serviceRestMetadata, invoker);
190178
}
191179

192180
@Override
193181
public void undeploy(ServiceRestMetadata serviceRestMetadata) {
194-
Map<PathMatcher, RestMethodMetadata> pathToServiceMapContainPathVariable =
195-
serviceRestMetadata.getPathContainPathVariableToServiceMap();
196-
pathToServiceMapContainPathVariable.keySet().stream().forEach(pathAndInvokerMapper::removePath);
197-
198-
Map<PathMatcher, RestMethodMetadata> pathToServiceMapUnContainPathVariable =
199-
serviceRestMetadata.getPathUnContainPathVariableToServiceMap();
200-
pathToServiceMapUnContainPathVariable.keySet().stream().forEach(pathAndInvokerMapper::removePath);
201-
182+
serviceDeployer.undeploy(serviceRestMetadata);
202183
}
203184

204-
private void registerExceptionMapper(URL url) {
205-
206-
for (String clazz : COMMA_SPLIT_PATTERN.split(url.getParameter(Constants.EXTENSION_KEY, RpcExceptionMapper.class.getName()))) {
207-
if (!StringUtils.isEmpty(clazz)) {
208-
exceptionMapper.registerMapper(clazz);
209-
}
210-
}
185+
private void registerExtension(URL url) {
186+
serviceDeployer.registerExtension(url);
211187
}
212188

213189

dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestInvoker.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ protected Result doInvoke(Invocation invocation) {
7272
RequestTemplate requestTemplate = new RequestTemplate(invocation, restMethodMetadata.getRequest().getMethod(), getUrl().getAddress());
7373

7474
HttpConnectionCreateContext httpConnectionCreateContext =
75-
creatHttpConnectionCreateContext(invocation, serviceRestMetadata, restMethodMetadata, requestTemplate);
75+
createHttpConnectionCreateContext(invocation, serviceRestMetadata, restMethodMetadata, requestTemplate);
7676

7777
// fill real data
7878
for (HttpConnectionPreBuildIntercept intercept : httpConnectionPreBuildIntercepts) {
@@ -128,7 +128,7 @@ protected Result doInvoke(Invocation invocation) {
128128
* @param requestTemplate
129129
* @return
130130
*/
131-
private HttpConnectionCreateContext creatHttpConnectionCreateContext(Invocation invocation, ServiceRestMetadata serviceRestMetadata, RestMethodMetadata restMethodMetadata, RequestTemplate requestTemplate) {
131+
private HttpConnectionCreateContext createHttpConnectionCreateContext(Invocation invocation, ServiceRestMetadata serviceRestMetadata, RestMethodMetadata restMethodMetadata, RequestTemplate requestTemplate) {
132132
HttpConnectionCreateContext httpConnectionCreateContext = new HttpConnectionCreateContext();
133133
httpConnectionCreateContext.setRequestTemplate(requestTemplate);
134134
httpConnectionCreateContext.setRestMethodMetadata(restMethodMetadata);

dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestProtocol.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import org.apache.dubbo.rpc.protocol.rest.annotation.consumer.HttpConnectionPreBuildIntercept;
3232
import org.apache.dubbo.rpc.protocol.rest.annotation.metadata.MetadataResolver;
3333

34+
35+
import java.util.LinkedHashSet;
3436
import java.util.Map;
3537
import java.util.Objects;
3638
import java.util.Set;
@@ -58,7 +60,7 @@ public class RestProtocol extends AbstractProtocol {
5860

5961
public RestProtocol(FrameworkModel frameworkModel) {
6062
this.clientFactory = frameworkModel.getExtensionLoader(RestClientFactory.class).getAdaptiveExtension();
61-
this.httpConnectionPreBuildIntercepts = frameworkModel.getExtensionLoader(HttpConnectionPreBuildIntercept.class).getSupportedExtensionInstances();
63+
this.httpConnectionPreBuildIntercepts = new LinkedHashSet<>(frameworkModel.getExtensionLoader(HttpConnectionPreBuildIntercept.class).getActivateExtensions());
6264
}
6365

6466

dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestRPCInvocationUtil.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.apache.dubbo.rpc.RpcInvocation;
2828
import org.apache.dubbo.rpc.protocol.rest.annotation.ParamParserManager;
2929
import org.apache.dubbo.rpc.protocol.rest.annotation.param.parse.provider.ProviderParseContext;
30-
import org.apache.dubbo.rpc.protocol.rest.constans.RestConstant;
30+
import org.apache.dubbo.rpc.protocol.rest.deploy.ServiceDeployer;
3131
import org.apache.dubbo.rpc.protocol.rest.exception.ParamParseException;
3232
import org.apache.dubbo.rpc.protocol.rest.pair.InvokerAndRestMethodMetadataPair;
3333
import org.apache.dubbo.rpc.protocol.rest.request.RequestFacade;
@@ -38,6 +38,8 @@
3838
import java.util.Arrays;
3939
import java.util.List;
4040

41+
import static org.apache.dubbo.common.constants.CommonConstants.SERVICE_DEPLOYER_ATTRIBUTE_KEY;
42+
4143

4244
public class RestRPCInvocationUtil {
4345

@@ -136,12 +138,12 @@ public static RpcInvocation createBaseRpcInvocation(RequestFacade request, RestM
136138
*/
137139
public static InvokerAndRestMethodMetadataPair getRestMethodMetadataAndInvokerPair(PathMatcher pathMatcher) {
138140

139-
PathAndInvokerMapper pathAndInvokerMapper = (PathAndInvokerMapper) RpcContext.getServerAttachment().getObjectAttachment(RestConstant.PATH_AND_INVOKER_MAPPER);
141+
ServiceDeployer serviceDeployer = (ServiceDeployer) RpcContext.getServiceContext().getObjectAttachment(SERVICE_DEPLOYER_ATTRIBUTE_KEY);
140142

141-
if (pathAndInvokerMapper == null) {
143+
if (serviceDeployer == null) {
142144
return null;
143145
}
144-
return pathAndInvokerMapper.getRestMethodMetadata(pathMatcher);
146+
return serviceDeployer.getPathAndInvokerMapper().getRestMethodMetadata(pathMatcher);
145147
}
146148

147149
/**
@@ -190,7 +192,9 @@ public static Invoker getInvokerByServiceInvokeMethod(Method serviceMethod) {
190192
return null;
191193
}
192194

193-
InvokerAndRestMethodMetadataPair pair = getRestMethodMetadataAndInvokerPair(PathMatcher.getInvokeCreatePathMatcher(serviceMethod));
195+
PathMatcher pathMatcher = PathMatcher.getInvokeCreatePathMatcher(serviceMethod);
196+
197+
InvokerAndRestMethodMetadataPair pair = getRestMethodMetadataAndInvokerPair(pathMatcher);
194198

195199
if (pair == null) {
196200
return null;

dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/annotation/consumer/inercept/RequestHeaderIntercept.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
import java.util.Set;
3030

3131
/**
32-
* resolve method args from header
32+
* resolve method args from header
3333
*/
34-
@Activate(value = RestConstant.REQUEST_HEADER_INTERCEPT, order = 2)
34+
@Activate(value = RestConstant.REQUEST_HEADER_INTERCEPT, order = Integer.MAX_VALUE - 1)
3535
public class RequestHeaderIntercept implements HttpConnectionPreBuildIntercept {
3636

3737
@Override
@@ -45,11 +45,12 @@ public void intercept(HttpConnectionCreateContext connectionCreateContext) {
4545

4646
requestTemplate.addHeaders(RestHeaderEnum.CONTENT_TYPE.getHeader(), consumes);
4747

48+
4849
Collection<String> produces = restMethodMetadata.getRequest().getProduces();
4950
if (produces == null || produces.isEmpty()) {
5051
requestTemplate.addHeader(RestHeaderEnum.ACCEPT.getHeader(), RestConstant.DEFAULT_ACCEPT);
5152
} else {
52-
requestTemplate.addHeader(RestHeaderEnum.ACCEPT.getHeader(), produces);
53+
requestTemplate.addHeaders(RestHeaderEnum.ACCEPT.getHeader(), produces);
5354
}
5455

5556
// URL url = connectionCreateContext.getUrl();

dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/annotation/consumer/inercept/SerializeBodyIntercept.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public void intercept(HttpConnectionCreateContext connectionCreateContext) {
6060
MediaType mediaType = MediaTypeUtil.convertMediaType(requestTemplate.getBodyType(), headers.toArray(new String[0]));
6161

6262
// add mediaType by targetClass serialize
63-
if (headers.isEmpty() && mediaType != null && !mediaType.equals(MediaType.ALL_VALUE)) {
63+
if (mediaType != null && !mediaType.equals(MediaType.ALL_VALUE)) {
64+
headers.clear();
6465
headers.add(mediaType.value);
6566
}
6667
HttpMessageCodecManager.httpMessageEncode(outputStream, unSerializedBody, url, mediaType, requestTemplate.getBodyType());

0 commit comments

Comments
 (0)