From 4215c1eb217aa6ae7d9d75c6038146a7492ee8ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=91=88=E7=86=B9?= Date: Thu, 20 Dec 2018 02:11:13 +0800 Subject: [PATCH] Make resteasy execute rpc filter chain too. (#439) * fix #376 * put static code block into @BeforeClass * fix: remove provider instance * change import format --- .../sofa/rpc/server/rest/RestServer.java | 4 +- .../rpc/server/rest/SofaResourceFactory.java | 4 +- .../server/rest/ClientRequestTestFilter.java | 4 +- .../CustomizeClientRequestTestFilter.java | 43 +++++ .../CustomizeClientResponseTestFilter.java | 44 +++++ .../CustomizeContainerRequestTestFilter.java | 43 +++++ .../CustomizeContainerResponseTestFilter.java | 45 +++++ .../rpc/server/rest/CustomizeFilterTest.java | 161 ++++++++++++++++++ .../rpc/server/rest/CustomizeTestFilter.java | 47 +++++ .../sofa/rpc/server/rest/RestTracerTest.java | 2 +- 10 files changed, 391 insertions(+), 6 deletions(-) create mode 100644 test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/CustomizeClientRequestTestFilter.java create mode 100644 test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/CustomizeClientResponseTestFilter.java create mode 100644 test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/CustomizeContainerRequestTestFilter.java create mode 100644 test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/CustomizeContainerResponseTestFilter.java create mode 100644 test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/CustomizeFilterTest.java create mode 100644 test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/CustomizeTestFilter.java diff --git a/extension-impl/remoting-resteasy/src/main/java/com/alipay/sofa/rpc/server/rest/RestServer.java b/extension-impl/remoting-resteasy/src/main/java/com/alipay/sofa/rpc/server/rest/RestServer.java index 33f26ca97..8158a2497 100644 --- a/extension-impl/remoting-resteasy/src/main/java/com/alipay/sofa/rpc/server/rest/RestServer.java +++ b/extension-impl/remoting-resteasy/src/main/java/com/alipay/sofa/rpc/server/rest/RestServer.java @@ -27,6 +27,7 @@ import com.alipay.sofa.rpc.invoke.Invoker; import com.alipay.sofa.rpc.log.Logger; import com.alipay.sofa.rpc.log.LoggerFactory; +import com.alipay.sofa.rpc.proxy.ProxyFactory; import com.alipay.sofa.rpc.server.Server; import org.jboss.resteasy.plugins.interceptors.CorsFilter; import org.jboss.resteasy.spi.PropertyInjector; @@ -204,8 +205,9 @@ public void registerProcessor(ProviderConfig providerConfig, Invoker instance) { + serverConfig.getPort() + serverConfig.getContextPath()); } try { + Object obj = ProxyFactory.buildProxy(providerConfig.getProxy(), providerConfig.getProxyClass(), instance); httpServer.getDeployment().getRegistry() - .addResourceFactory(new SofaResourceFactory(providerConfig), serverConfig.getContextPath()); + .addResourceFactory(new SofaResourceFactory(providerConfig, obj), serverConfig.getContextPath()); invokerCnt.incrementAndGet(); } catch (Exception e) { diff --git a/extension-impl/remoting-resteasy/src/main/java/com/alipay/sofa/rpc/server/rest/SofaResourceFactory.java b/extension-impl/remoting-resteasy/src/main/java/com/alipay/sofa/rpc/server/rest/SofaResourceFactory.java index f8d1b19c6..3cfa3a155 100644 --- a/extension-impl/remoting-resteasy/src/main/java/com/alipay/sofa/rpc/server/rest/SofaResourceFactory.java +++ b/extension-impl/remoting-resteasy/src/main/java/com/alipay/sofa/rpc/server/rest/SofaResourceFactory.java @@ -31,8 +31,8 @@ public class SofaResourceFactory extends SingletonResource { private final String serviceName; private final String appName; - public SofaResourceFactory(ProviderConfig providerConfig) { - super(providerConfig.getRef()); + public SofaResourceFactory(ProviderConfig providerConfig, Object object) { + super(object); this.providerConfig = providerConfig; // 缓存服务名计算和应用名计算 this.serviceName = ConfigUniqueNameGenerator.getServiceName(providerConfig); diff --git a/test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/ClientRequestTestFilter.java b/test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/ClientRequestTestFilter.java index 3fc382026..209490ca9 100644 --- a/test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/ClientRequestTestFilter.java +++ b/test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/ClientRequestTestFilter.java @@ -45,13 +45,13 @@ public class ClientRequestTestFilter implements ClientRequestFilter { public void filter(ClientRequestContext requestContext) throws IOException { LOGGER.info("客户端request filter生效"); name = "A"; - LOGGER.info("客户端customerAnnotion code:" + code); + LOGGER.info("客户端customerAnnotation code:" + code); } @CustomerAnnotation() public void setCode(String code2) { this.code2 = code2; - LOGGER.info("客户端customerAnnotion code2:" + this.code2); + LOGGER.info("客户端customerAnnotation code2:" + this.code2); } diff --git a/test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/CustomizeClientRequestTestFilter.java b/test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/CustomizeClientRequestTestFilter.java new file mode 100644 index 000000000..917311285 --- /dev/null +++ b/test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/CustomizeClientRequestTestFilter.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.rpc.server.rest; + +import javax.ws.rs.client.ClientRequestContext; +import javax.ws.rs.client.ClientRequestFilter; +import java.io.IOException; + +/** + * @author zhangchengxi + * Date 2018/12/2 + */ +public class CustomizeClientRequestTestFilter implements ClientRequestFilter { + + private static boolean invoked = false; + + public static boolean isInvoked() { + return invoked; + } + + public static void reset() { + invoked = false; + } + + @Override + public void filter(ClientRequestContext requestContext) throws IOException { + invoked = true; + } +} diff --git a/test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/CustomizeClientResponseTestFilter.java b/test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/CustomizeClientResponseTestFilter.java new file mode 100644 index 000000000..fbfb65913 --- /dev/null +++ b/test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/CustomizeClientResponseTestFilter.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.rpc.server.rest; + +import javax.ws.rs.client.ClientRequestContext; +import javax.ws.rs.client.ClientResponseContext; +import javax.ws.rs.client.ClientResponseFilter; +import java.io.IOException; + +/** + * @author zhangchengxi + * Date 2018/12/2 + */ +public class CustomizeClientResponseTestFilter implements ClientResponseFilter { + + private static boolean invoked = false; + + public static boolean isInvoked() { + return invoked; + } + + public static void reset() { + invoked = false; + } + + @Override + public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException { + invoked = true; + } +} diff --git a/test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/CustomizeContainerRequestTestFilter.java b/test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/CustomizeContainerRequestTestFilter.java new file mode 100644 index 000000000..0ea3fc27d --- /dev/null +++ b/test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/CustomizeContainerRequestTestFilter.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.rpc.server.rest; + +import javax.ws.rs.container.ContainerRequestContext; +import javax.ws.rs.container.ContainerRequestFilter; +import java.io.IOException; + +/** + * @author zhangchengxi + * Date 2018/12/2 + */ +public class CustomizeContainerRequestTestFilter implements ContainerRequestFilter { + + private static boolean invoked = false; + + public static boolean isInvoked() { + return invoked; + } + + public static void reset() { + invoked = false; + } + + @Override + public void filter(ContainerRequestContext requestContext) throws IOException { + invoked = true; + } +} diff --git a/test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/CustomizeContainerResponseTestFilter.java b/test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/CustomizeContainerResponseTestFilter.java new file mode 100644 index 000000000..cdf9ae50d --- /dev/null +++ b/test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/CustomizeContainerResponseTestFilter.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.rpc.server.rest; + +import javax.ws.rs.container.ContainerRequestContext; +import javax.ws.rs.container.ContainerResponseContext; +import javax.ws.rs.container.ContainerResponseFilter; +import java.io.IOException; + +/** + * @author zhangchengxi + * Date 2018/12/2 + */ +public class CustomizeContainerResponseTestFilter implements ContainerResponseFilter { + + private static boolean invoked = false; + + public static void reset() { + invoked = false; + } + + public static boolean isInvoked() { + return invoked; + } + + @Override + public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) + throws IOException { + invoked = true; + } +} diff --git a/test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/CustomizeFilterTest.java b/test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/CustomizeFilterTest.java new file mode 100644 index 000000000..ec7a06d39 --- /dev/null +++ b/test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/CustomizeFilterTest.java @@ -0,0 +1,161 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.rpc.server.rest; + +import com.alipay.sofa.rpc.common.RpcConstants; +import com.alipay.sofa.rpc.config.ApplicationConfig; +import com.alipay.sofa.rpc.config.ConsumerConfig; +import com.alipay.sofa.rpc.config.JAXRSProviderManager; +import com.alipay.sofa.rpc.config.ProviderConfig; +import com.alipay.sofa.rpc.config.ServerConfig; +import com.alipay.sofa.rpc.filter.Filter; +import com.alipay.sofa.rpc.test.ActivelyDestroyTest; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * @author zhangchengxi + * Date 2018/12/2 + */ +public class CustomizeFilterTest extends ActivelyDestroyTest { + + private static RestService filterRestService; + + private static CustomizeTestFilter providerFilter; + + private static CustomizeTestFilter clientFilter; + + private static ProviderConfig providerConfig; + + private static CustomizeContainerRequestTestFilter customizeContainerRequestTestFilter; + + private static CustomizeContainerResponseTestFilter customizeContainerResponseTestFilter; + + private static CustomizeClientRequestTestFilter customizeClientRequestTestFilter; + + private static CustomizeClientResponseTestFilter customizeClientResponseTestFilter; + + @BeforeClass + public static void beforeClass() { + customizeContainerRequestTestFilter = new CustomizeContainerRequestTestFilter(); + customizeContainerResponseTestFilter = new CustomizeContainerResponseTestFilter(); + customizeClientRequestTestFilter = new CustomizeClientRequestTestFilter(); + customizeClientResponseTestFilter = new CustomizeClientResponseTestFilter(); + + JAXRSProviderManager.registerCustomProviderInstance(customizeContainerRequestTestFilter); + JAXRSProviderManager.registerCustomProviderInstance(customizeContainerResponseTestFilter); + JAXRSProviderManager.registerCustomProviderInstance(customizeClientRequestTestFilter); + JAXRSProviderManager.registerCustomProviderInstance(customizeClientResponseTestFilter); + + providerFilter = new CustomizeTestFilter(); + List providerFilters = new ArrayList(2); + providerFilters.add(providerFilter); + + ServerConfig restServer = new ServerConfig() + .setPort(8583) + .setProtocol(RpcConstants.PROTOCOL_TYPE_REST); + + List servers = new ArrayList(2); + servers.add(restServer); + providerConfig = new ProviderConfig() + .setInterfaceId(RestService.class.getName()) + .setRef(new RestServiceImpl()) + .setRegister(false) + .setServer(servers) + .setFilterRef(providerFilters); + + providerConfig.export(); + + //rest服务 + clientFilter = new CustomizeTestFilter(); + List clientFilters = new ArrayList(2); + clientFilters.add(clientFilter); + + ConsumerConfig consumerConfigRest = new ConsumerConfig() + .setInterfaceId(RestService.class.getName()) + .setProtocol(RpcConstants.PROTOCOL_TYPE_REST) + .setDirectUrl("rest://127.0.0.1:8583") + .setTimeout(1000) + .setFilterRef(clientFilters) + .setApplication(new ApplicationConfig().setAppName("TestClientRest")); + filterRestService = consumerConfigRest.refer(); + } + + @AfterClass + public static void afterClass() { + JAXRSProviderManager.removeCustomProviderInstance(customizeContainerRequestTestFilter); + JAXRSProviderManager.removeCustomProviderInstance(customizeContainerResponseTestFilter); + JAXRSProviderManager.removeCustomProviderInstance(customizeClientRequestTestFilter); + JAXRSProviderManager.removeCustomProviderInstance(customizeClientResponseTestFilter); + } + + @Before + public void before() { + CustomizeContainerRequestTestFilter.reset(); + CustomizeContainerResponseTestFilter.reset(); + CustomizeClientRequestTestFilter.reset(); + CustomizeClientResponseTestFilter.reset(); + providerFilter.reset(); + clientFilter.reset(); + } + + @Test + public void testFilterInvoked() { + Assert.assertFalse(CustomizeContainerRequestTestFilter.isInvoked()); + Assert.assertFalse(CustomizeContainerResponseTestFilter.isInvoked()); + Assert.assertFalse(CustomizeClientRequestTestFilter.isInvoked()); + Assert.assertFalse(CustomizeClientResponseTestFilter.isInvoked()); + Assert.assertFalse(providerFilter.isInvoked()); + Assert.assertFalse(clientFilter.isInvoked()); + + filterRestService.get("test"); + + Assert.assertTrue(CustomizeContainerRequestTestFilter.isInvoked()); + Assert.assertTrue(CustomizeContainerResponseTestFilter.isInvoked()); + Assert.assertTrue(CustomizeClientRequestTestFilter.isInvoked()); + Assert.assertTrue(CustomizeClientResponseTestFilter.isInvoked()); + Assert.assertTrue(providerFilter.isInvoked()); + Assert.assertTrue(clientFilter.isInvoked()); + } + + @Test + public void testNormalHttpRequest() throws IOException { + Assert.assertFalse(providerFilter.isInvoked()); + Assert.assertFalse(CustomizeContainerRequestTestFilter.isInvoked()); + Assert.assertFalse(CustomizeContainerResponseTestFilter.isInvoked()); + + CloseableHttpClient httpClient = HttpClientBuilder.create().build(); + HttpGet httpGet = new HttpGet("http://127.0.0.1:8583/rest/get/abc"); + CloseableHttpResponse response = httpClient.execute(httpGet); + + Assert.assertTrue(providerFilter.isInvoked()); + Assert.assertTrue(CustomizeContainerRequestTestFilter.isInvoked()); + Assert.assertTrue(CustomizeContainerResponseTestFilter.isInvoked()); + + } +} diff --git a/test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/CustomizeTestFilter.java b/test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/CustomizeTestFilter.java new file mode 100644 index 000000000..858e6d6bf --- /dev/null +++ b/test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/CustomizeTestFilter.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.rpc.server.rest; + +import com.alipay.sofa.rpc.core.exception.SofaRpcException; +import com.alipay.sofa.rpc.core.request.SofaRequest; +import com.alipay.sofa.rpc.core.response.SofaResponse; +import com.alipay.sofa.rpc.filter.Filter; +import com.alipay.sofa.rpc.filter.FilterInvoker; + +/** + * @author zhangchengxi + * Date 2018/12/2 + */ +public class CustomizeTestFilter extends Filter { + + private boolean invoked = false; + + public boolean isInvoked() { + return invoked; + } + + @Override + public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException { + invoked = true; + return invoker.invoke(request); + } + + public void reset() { + invoked = false; + } + +} diff --git a/test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/RestTracerTest.java b/test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/RestTracerTest.java index e232afbdc..ab6cc06c7 100644 --- a/test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/RestTracerTest.java +++ b/test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/RestTracerTest.java @@ -32,7 +32,7 @@ import com.alipay.sofa.rpc.tracer.Tracers; import com.alipay.sofa.rpc.tracer.sofatracer.RpcSofaTracer; import com.alipay.sofa.rpc.tracer.sofatracer.factory.MemoryReporterImpl; -import junit.framework.Assert; +import org.junit.Assert; import org.junit.After; import org.junit.Before; import org.junit.Test;