forked from sofastack/sofa-rpc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make resteasy execute rpc filter chain too. (sofastack#439)
* fix sofastack#376 * put static code block into @BeforeClass * fix: remove provider instance * change import format
- Loading branch information
Showing
10 changed files
with
391 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...ation/src/test/java/com/alipay/sofa/rpc/server/rest/CustomizeClientRequestTestFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...tion/src/test/java/com/alipay/sofa/rpc/server/rest/CustomizeClientResponseTestFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...on/src/test/java/com/alipay/sofa/rpc/server/rest/CustomizeContainerRequestTestFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...n/src/test/java/com/alipay/sofa/rpc/server/rest/CustomizeContainerResponseTestFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
161 changes: 161 additions & 0 deletions
161
test/test-integration/src/test/java/com/alipay/sofa/rpc/server/rest/CustomizeFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<RestService> 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<Filter> providerFilters = new ArrayList<Filter>(2); | ||
providerFilters.add(providerFilter); | ||
|
||
ServerConfig restServer = new ServerConfig() | ||
.setPort(8583) | ||
.setProtocol(RpcConstants.PROTOCOL_TYPE_REST); | ||
|
||
List<ServerConfig> servers = new ArrayList<ServerConfig>(2); | ||
servers.add(restServer); | ||
providerConfig = new ProviderConfig<RestService>() | ||
.setInterfaceId(RestService.class.getName()) | ||
.setRef(new RestServiceImpl()) | ||
.setRegister(false) | ||
.setServer(servers) | ||
.setFilterRef(providerFilters); | ||
|
||
providerConfig.export(); | ||
|
||
//rest服务 | ||
clientFilter = new CustomizeTestFilter(); | ||
List<Filter> clientFilters = new ArrayList<Filter>(2); | ||
clientFilters.add(clientFilter); | ||
|
||
ConsumerConfig<RestService> consumerConfigRest = new ConsumerConfig<RestService>() | ||
.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()); | ||
|
||
} | ||
} |
Oops, something went wrong.