Skip to content

Commit

Permalink
Make resteasy execute rpc filter chain too. (sofastack#439)
Browse files Browse the repository at this point in the history
* fix sofastack#376

* put static code block into @BeforeClass

* fix: remove provider instance

* change import format
  • Loading branch information
OrezzerO authored and JervyShi committed Dec 19, 2018
1 parent d86ffa8 commit 4215c1e
Show file tree
Hide file tree
Showing 10 changed files with 391 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}

Expand Down
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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());

}
}
Loading

0 comments on commit 4215c1e

Please sign in to comment.