From cf3a54e41526751b284c22189fcfad555a88a515 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Thu, 4 Jan 2018 13:37:35 +0800 Subject: [PATCH] Add test example for rest --- dubbo-test/dubbo-test-examples/pom.xml | 62 +++++++++++++ .../examples/rest/NonDubboRestConsumer.java | 91 +++++++++++++++++++ .../dubbo/examples/rest/RestConsumer.java | 43 +++++++++ .../dubbo/examples/rest/RestProvider.java | 39 ++++++++ .../alibaba/dubbo/examples/rest/api/User.java | 75 +++++++++++++++ .../dubbo/examples/rest/api/UserService.java | 23 +++++ .../rest/api/extension/ClientTraceFilter.java | 35 +++++++ .../api/extension/CustomExceptionMapper.java | 32 +++++++ .../api/extension/DynamicTraceBinding.java | 28 ++++++ .../extension/DynamicTraceInterceptor.java | 40 ++++++++ .../rest/api/extension/LogFilter.java | 30 ++++++ .../rest/api/extension/TraceFilter.java | 38 ++++++++ .../rest/api/extension/TraceInterceptor.java | 40 ++++++++ .../api/facade/AnotherUserRestService.java | 43 +++++++++ .../rest/api/facade/RegistrationResult.java | 44 +++++++++ .../rest/api/facade/UserRestService.java | 39 ++++++++ .../examples/rest/impl/UserServiceImpl.java | 39 ++++++++ .../AnnotationDrivenUserRestServiceImpl.java | 65 +++++++++++++ .../facade/AnotherUserRestServiceImpl.java | 47 ++++++++++ .../rest/impl/facade/UserRestServiceImpl.java | 74 +++++++++++++++ .../dubbo/examples/rest/rest-consumer.xml | 18 ++++ .../dubbo/examples/rest/rest-provider.xml | 57 ++++++++++++ 22 files changed, 1002 insertions(+) create mode 100644 dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/NonDubboRestConsumer.java create mode 100644 dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/RestConsumer.java create mode 100644 dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/RestProvider.java create mode 100644 dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/User.java create mode 100644 dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/UserService.java create mode 100644 dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/extension/ClientTraceFilter.java create mode 100644 dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/extension/CustomExceptionMapper.java create mode 100644 dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/extension/DynamicTraceBinding.java create mode 100644 dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/extension/DynamicTraceInterceptor.java create mode 100644 dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/extension/LogFilter.java create mode 100644 dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/extension/TraceFilter.java create mode 100644 dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/extension/TraceInterceptor.java create mode 100644 dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/facade/AnotherUserRestService.java create mode 100644 dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/facade/RegistrationResult.java create mode 100644 dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/facade/UserRestService.java create mode 100644 dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/impl/UserServiceImpl.java create mode 100644 dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/impl/facade/AnnotationDrivenUserRestServiceImpl.java create mode 100644 dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/impl/facade/AnotherUserRestServiceImpl.java create mode 100644 dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/impl/facade/UserRestServiceImpl.java create mode 100644 dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/rest-consumer.xml create mode 100644 dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/rest-provider.xml diff --git a/dubbo-test/dubbo-test-examples/pom.xml b/dubbo-test/dubbo-test-examples/pom.xml index 79db1200d..4059f9a81 100644 --- a/dubbo-test/dubbo-test-examples/pom.xml +++ b/dubbo-test/dubbo-test-examples/pom.xml @@ -110,6 +110,11 @@ limitations under the License. dubbo-rpc-redis ${project.parent.version} + + com.alibaba + dubbo-rpc-rest + ${project.parent.version} + com.alibaba dubbo-registry-default @@ -150,6 +155,63 @@ limitations under the License. javax.cache cache-api + + + org.jboss.resteasy + resteasy-jaxrs + + + + org.jboss.resteasy + resteasy-client + + + + org.jboss.resteasy + resteasy-netty4 + + + + org.jboss.resteasy + resteasy-jdk-http + + + + org.jboss.resteasy + resteasy-jackson-provider + + + + org.jboss.resteasy + resteasy-jaxb-provider + + + + org.apache.tomcat.embed + tomcat-embed-core + + + org.apache.tomcat.embed + tomcat-embed-logging-juli + + + javax.servlet + javax.servlet-api + + + org.mortbay.jetty + jetty + + + org.mortbay.jetty + servlet-api + + + + + org.apache.httpcomponents + httpclient + diff --git a/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/NonDubboRestConsumer.java b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/NonDubboRestConsumer.java new file mode 100644 index 000000000..7a22c9258 --- /dev/null +++ b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/NonDubboRestConsumer.java @@ -0,0 +1,91 @@ +/* + * 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.alibaba.dubbo.examples.rest; + +import com.alibaba.dubbo.examples.rest.api.User; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +public class NonDubboRestConsumer { + + public static void main(String[] args) { + String port = "8888"; + + registerUser("http://localhost:" + port + "/services/users/register.json", MediaType.APPLICATION_JSON_TYPE); + + registerUser("http://localhost:" + port + "/services/users/register.xml", MediaType.TEXT_XML_TYPE); + + getUser("http://localhost:" + port + "/services/users/1.json"); + + getUser("http://localhost:" + port + "/services/users/2.xml"); + + registerUser("http://localhost:" + port + "/services/u/register.json", MediaType.APPLICATION_JSON_TYPE); + + registerUser("http://localhost:" + port + "/services/u/register.xml", MediaType.TEXT_XML_TYPE); + + getUser("http://localhost:" + port + "/services/u/1.json"); + + getUser("http://localhost:" + port + "/services/u/2.xml"); + + registerUser("http://localhost:" + port + "/services/customers/register.json", MediaType.APPLICATION_JSON_TYPE); + + registerUser("http://localhost:" + port + "/services/customers/register.xml", MediaType.TEXT_XML_TYPE); + + getUser("http://localhost:" + port + "/services/customers/1.json"); + + getUser("http://localhost:" + port + "/services/customers/2.xml"); + } + + private static void registerUser(String url, MediaType mediaType) { + System.out.println("Registering user via " + url); + User user = new User(1L, "larrypage"); + Client client = ClientBuilder.newClient(); + WebTarget target = client.target(url); + Response response = target.request().post(Entity.entity(user, mediaType)); + + try { + if (response.getStatus() != 200) { + throw new RuntimeException("Failed with HTTP error code : " + response.getStatus()); + } + System.out.println("Successfully got result: " + response.readEntity(String.class)); + } finally { + response.close(); + client.close(); + } + } + + private static void getUser(String url) { + System.out.println("Getting user via " + url); + Client client = ClientBuilder.newClient(); + WebTarget target = client.target(url); + Response response = target.request().get(); + try { + if (response.getStatus() != 200) { + throw new RuntimeException("Failed with HTTP error code : " + response.getStatus()); + } + System.out.println("Successfully got result: " + response.readEntity(String.class)); + } finally { + response.close(); + client.close(); + } + } +} diff --git a/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/RestConsumer.java b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/RestConsumer.java new file mode 100644 index 000000000..4f64f6a9b --- /dev/null +++ b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/RestConsumer.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.alibaba.dubbo.examples.rest; + +import com.alibaba.dubbo.examples.rest.api.User; +import com.alibaba.dubbo.examples.rest.api.facade.AnotherUserRestService; +import com.alibaba.dubbo.rpc.RpcContext; + +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class RestConsumer { + + public static void main(String[] args) throws Exception { + String config = RestConsumer.class.getPackage().getName().replace('.', '/') + "/rest-consumer.xml"; + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config); + context.start(); + AnotherUserRestService userService = (AnotherUserRestService) context.getBean("anotherUserRestService"); + + + User user = new User(1L, "larrypage"); + System.out.println("SUCCESS: registered user with id " + userService.registerUser(user).getId()); + + RpcContext.getContext().setAttachment("clientName", "demo"); + RpcContext.getContext().setAttachment("clientImpl", "dubbox"); + System.out.println("SUCCESS: got user " + userService.getUser(1L)); + System.in.read(); + } + +} \ No newline at end of file diff --git a/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/RestProvider.java b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/RestProvider.java new file mode 100644 index 000000000..04eca05b8 --- /dev/null +++ b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/RestProvider.java @@ -0,0 +1,39 @@ +/* + * 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.alibaba.dubbo.examples.rest; + +import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class RestProvider { + + public static void main(String[] args) throws Exception { + String config = RestProvider.class.getPackage().getName().replace('.', '/') + "/rest-provider.xml"; + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config); + context.start(); + System.in.read(); + } + + @Configuration + @EnableDubbo(scanBasePackages = "com.alibaba.dubbo.examples.rest.impl.facade") + static public class ProviderConfiguration { + + } + +} \ No newline at end of file diff --git a/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/User.java b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/User.java new file mode 100644 index 000000000..3bdfd2618 --- /dev/null +++ b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/User.java @@ -0,0 +1,75 @@ +/* + * 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.alibaba.dubbo.examples.rest.api; + +import org.codehaus.jackson.annotate.JsonProperty; + +import javax.validation.constraints.Min; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import java.io.Serializable; + +@XmlRootElement +@XmlAccessorType(XmlAccessType.FIELD) +public class User implements Serializable { + + @NotNull + @Min(1L) + private Long id; + + @JsonProperty("username") + @XmlElement(name = "username") + @NotNull + @Size(min = 6, max = 50) + private String name; + + public User() { + } + + public User(Long id, String name) { + this.id = id; + this.name = name; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return "User (" + + "id=" + id + + ", name='" + name + '\'' + + ')'; + } +} diff --git a/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/UserService.java b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/UserService.java new file mode 100644 index 000000000..1c7d4a234 --- /dev/null +++ b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/UserService.java @@ -0,0 +1,23 @@ +/* + * 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.alibaba.dubbo.examples.rest.api; + +public interface UserService { + User getUser(Long id); + + Long registerUser(User user); +} diff --git a/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/extension/ClientTraceFilter.java b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/extension/ClientTraceFilter.java new file mode 100644 index 000000000..9ebf2c87d --- /dev/null +++ b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/extension/ClientTraceFilter.java @@ -0,0 +1,35 @@ +/* + * 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.alibaba.dubbo.examples.rest.api.extension; + +import javax.ws.rs.client.ClientRequestContext; +import javax.ws.rs.client.ClientRequestFilter; +import javax.ws.rs.client.ClientResponseContext; +import javax.ws.rs.client.ClientResponseFilter; +import java.io.IOException; + + +public class ClientTraceFilter implements ClientRequestFilter, ClientResponseFilter { + + public void filter(ClientRequestContext requestContext) throws IOException { + System.out.println("Client request filter invoked"); + } + + public void filter(ClientRequestContext clientRequestContext, ClientResponseContext clientResponseContext) throws IOException { + System.out.println("Client response filter invoked"); + } +} diff --git a/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/extension/CustomExceptionMapper.java b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/extension/CustomExceptionMapper.java new file mode 100644 index 000000000..398f1d8a7 --- /dev/null +++ b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/extension/CustomExceptionMapper.java @@ -0,0 +1,32 @@ +/* + * 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.alibaba.dubbo.examples.rest.api.extension; + +import com.alibaba.dubbo.rpc.RpcContext; + +import javax.ws.rs.NotFoundException; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; + +public class CustomExceptionMapper implements ExceptionMapper { + + public Response toResponse(NotFoundException e) { + System.out.println("Exception mapper successfully got an exception: " + e + ":" + e.getMessage()); + System.out.println("Client IP is " + RpcContext.getContext().getRemoteAddressString()); + return Response.status(Response.Status.NOT_FOUND).entity("Oops! the requested resource is not found!").type("text/plain").build(); + } +} diff --git a/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/extension/DynamicTraceBinding.java b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/extension/DynamicTraceBinding.java new file mode 100644 index 000000000..516f83ec7 --- /dev/null +++ b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/extension/DynamicTraceBinding.java @@ -0,0 +1,28 @@ +/* + * 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.alibaba.dubbo.examples.rest.api.extension; + +import javax.ws.rs.container.DynamicFeature; +import javax.ws.rs.container.ResourceInfo; +import javax.ws.rs.core.FeatureContext; + +public class DynamicTraceBinding implements DynamicFeature { + + public void configure(ResourceInfo resourceInfo, FeatureContext context) { + context.register(DynamicTraceInterceptor.class); + } +} \ No newline at end of file diff --git a/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/extension/DynamicTraceInterceptor.java b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/extension/DynamicTraceInterceptor.java new file mode 100644 index 000000000..47f7a71e1 --- /dev/null +++ b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/extension/DynamicTraceInterceptor.java @@ -0,0 +1,40 @@ +/* + * 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.alibaba.dubbo.examples.rest.api.extension; + +import javax.annotation.Priority; +import javax.ws.rs.Priorities; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.ext.ReaderInterceptor; +import javax.ws.rs.ext.ReaderInterceptorContext; +import javax.ws.rs.ext.WriterInterceptor; +import javax.ws.rs.ext.WriterInterceptorContext; +import java.io.IOException; + +@Priority(Priorities.USER) +public class DynamicTraceInterceptor implements ReaderInterceptor, WriterInterceptor { + + public Object aroundReadFrom(ReaderInterceptorContext readerInterceptorContext) throws IOException, WebApplicationException { + System.out.println("Dynamic reader interceptor invoked"); + return readerInterceptorContext.proceed(); + } + + public void aroundWriteTo(WriterInterceptorContext writerInterceptorContext) throws IOException, WebApplicationException { + System.out.println("Dynamic writer interceptor invoked"); + writerInterceptorContext.proceed(); + } +} diff --git a/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/extension/LogFilter.java b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/extension/LogFilter.java new file mode 100644 index 000000000..cbde2dffb --- /dev/null +++ b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/extension/LogFilter.java @@ -0,0 +1,30 @@ +/* + * 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.alibaba.dubbo.examples.rest.api.extension; + +import com.alibaba.dubbo.rpc.Filter; +import com.alibaba.dubbo.rpc.Invocation; +import com.alibaba.dubbo.rpc.Invoker; +import com.alibaba.dubbo.rpc.Result; +import com.alibaba.dubbo.rpc.RpcException; + +public class LogFilter implements Filter { + public Result invoke(Invoker invoker, Invocation invocation) throws RpcException { + System.out.println(invocation.getMethodName() + "is invoked"); + return invoker.invoke(invocation); + } +} diff --git a/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/extension/TraceFilter.java b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/extension/TraceFilter.java new file mode 100644 index 000000000..57f0158d2 --- /dev/null +++ b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/extension/TraceFilter.java @@ -0,0 +1,38 @@ +/* + * 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.alibaba.dubbo.examples.rest.api.extension; + +import javax.annotation.Priority; +import javax.ws.rs.Priorities; +import javax.ws.rs.container.ContainerRequestContext; +import javax.ws.rs.container.ContainerRequestFilter; +import javax.ws.rs.container.ContainerResponseContext; +import javax.ws.rs.container.ContainerResponseFilter; +import java.io.IOException; + +@Priority(Priorities.USER) +public class TraceFilter implements ContainerRequestFilter, ContainerResponseFilter { + + public void filter(ContainerRequestContext requestContext) throws IOException { + System.out.println("Request filter invoked: " + requestContext.getUriInfo().getAbsolutePath()); + } + + public void filter(ContainerRequestContext containerRequestContext, ContainerResponseContext containerResponseContext) throws IOException { + System.out.println("Response filter invoked."); + + } +} \ No newline at end of file diff --git a/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/extension/TraceInterceptor.java b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/extension/TraceInterceptor.java new file mode 100644 index 000000000..5fa723e83 --- /dev/null +++ b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/extension/TraceInterceptor.java @@ -0,0 +1,40 @@ +/* + * 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.alibaba.dubbo.examples.rest.api.extension; + +import javax.annotation.Priority; +import javax.ws.rs.Priorities; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.ext.ReaderInterceptor; +import javax.ws.rs.ext.ReaderInterceptorContext; +import javax.ws.rs.ext.WriterInterceptor; +import javax.ws.rs.ext.WriterInterceptorContext; +import java.io.IOException; + +@Priority(Priorities.USER) +public class TraceInterceptor implements ReaderInterceptor, WriterInterceptor { + + public Object aroundReadFrom(ReaderInterceptorContext readerInterceptorContext) throws IOException, WebApplicationException { + System.out.println("Reader interceptor invoked"); + return readerInterceptorContext.proceed(); + } + + public void aroundWriteTo(WriterInterceptorContext writerInterceptorContext) throws IOException, WebApplicationException { + System.out.println("Writer interceptor invoked"); + writerInterceptorContext.proceed(); + } +} diff --git a/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/facade/AnotherUserRestService.java b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/facade/AnotherUserRestService.java new file mode 100644 index 000000000..29b2b47f9 --- /dev/null +++ b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/facade/AnotherUserRestService.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.alibaba.dubbo.examples.rest.api.facade; + +import com.alibaba.dubbo.examples.rest.api.User; +import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType; + +import javax.validation.constraints.Min; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +@Path("u") +@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML}) +@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8}) +public interface AnotherUserRestService { + + @GET + @Path("{id : \\d+}") + User getUser(@PathParam("id") @Min(1L) Long id); + + @POST + @Path("register") + RegistrationResult registerUser(User user); +} diff --git a/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/facade/RegistrationResult.java b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/facade/RegistrationResult.java new file mode 100644 index 000000000..e888c3b40 --- /dev/null +++ b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/facade/RegistrationResult.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.alibaba.dubbo.examples.rest.api.facade; + +import javax.xml.bind.annotation.XmlRootElement; +import java.io.Serializable; + +/** + * DTO to customize the returned message + */ +@XmlRootElement +public class RegistrationResult implements Serializable { + + private Long id; + + public RegistrationResult() { + } + + public RegistrationResult(Long id) { + this.id = id; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } +} diff --git a/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/facade/UserRestService.java b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/facade/UserRestService.java new file mode 100644 index 000000000..16eface6c --- /dev/null +++ b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/api/facade/UserRestService.java @@ -0,0 +1,39 @@ +/* + * 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.alibaba.dubbo.examples.rest.api.facade; + +import com.alibaba.dubbo.examples.rest.api.User; + +import javax.validation.constraints.Min; + +/** + * This interface acts as some kind of service broker for the original UserService + *

+ * Here we want to simulate the twitter/weibo rest api, e.g. + *

+ * http://localhost:8888/user/1.json + * http://localhost:8888/user/1.xml + */ +public interface UserRestService { + + /** + * the request object is just used to test jax-rs injection. + */ + User getUser(@Min(value = 1L, message = "User ID must be greater than 1") Long id/*, HttpServletRequest request*/); + + RegistrationResult registerUser(User user); +} diff --git a/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/impl/UserServiceImpl.java b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/impl/UserServiceImpl.java new file mode 100644 index 000000000..5d8538d86 --- /dev/null +++ b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/impl/UserServiceImpl.java @@ -0,0 +1,39 @@ +/* + * 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.alibaba.dubbo.examples.rest.impl; + +import com.alibaba.dubbo.examples.rest.api.User; + +import org.springframework.stereotype.Service; + +import java.util.concurrent.atomic.AtomicLong; + +@Service("userService") +public class UserServiceImpl implements com.alibaba.dubbo.examples.rest.api.UserService { + + private final AtomicLong idGen = new AtomicLong(); + + public User getUser(Long id) { + return new User(id, "username" + id); + } + + + public Long registerUser(User user) { +// System.out.println("Username is " + user.getName()); + return idGen.incrementAndGet(); + } +} diff --git a/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/impl/facade/AnnotationDrivenUserRestServiceImpl.java b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/impl/facade/AnnotationDrivenUserRestServiceImpl.java new file mode 100644 index 000000000..f5ae44f68 --- /dev/null +++ b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/impl/facade/AnnotationDrivenUserRestServiceImpl.java @@ -0,0 +1,65 @@ +/* + * 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.alibaba.dubbo.examples.rest.impl.facade; + +import com.alibaba.dubbo.config.annotation.Service; +import com.alibaba.dubbo.examples.rest.api.User; +import com.alibaba.dubbo.examples.rest.api.UserService; +import com.alibaba.dubbo.examples.rest.api.facade.RegistrationResult; +import com.alibaba.dubbo.examples.rest.api.facade.UserRestService; +import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType; + +import org.springframework.beans.factory.annotation.Autowired; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +@Service(protocol = {"rest", "dubbo"}, group = "annotationConfig", validation = "true") +@Path("customers") +@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML}) +@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8}) +public class AnnotationDrivenUserRestServiceImpl implements UserRestService { + +// private static final Logger logger = LoggerFactory.getLogger(UserRestServiceImpl.class); + + @Autowired + private UserService userService; + + public void setUserService(UserService userService) { + this.userService = userService; + } + + @GET + @Path("{id : \\d+}") + public User getUser(@PathParam("id") Long id/*, @Context HttpServletRequest request*/) { + // test context injection +// System.out.println("Client address from @Context injection: " + (request != null ? request.getRemoteAddr() : "")); +// System.out.println("Client address from RpcContext: " + RpcContext.getContext().getRemoteAddressString()); + return userService.getUser(id); + } + + @POST + @Path("register") + public RegistrationResult registerUser(User user) { + return new RegistrationResult(userService.registerUser(user)); + } +} diff --git a/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/impl/facade/AnotherUserRestServiceImpl.java b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/impl/facade/AnotherUserRestServiceImpl.java new file mode 100644 index 000000000..c669cdc24 --- /dev/null +++ b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/impl/facade/AnotherUserRestServiceImpl.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.alibaba.dubbo.examples.rest.impl.facade; + +import com.alibaba.dubbo.examples.rest.api.User; +import com.alibaba.dubbo.examples.rest.api.UserService; +import com.alibaba.dubbo.examples.rest.api.facade.AnotherUserRestService; +import com.alibaba.dubbo.examples.rest.api.facade.RegistrationResult; +import com.alibaba.dubbo.rpc.RpcContext; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service("anotherUserRestService") +public class AnotherUserRestServiceImpl implements AnotherUserRestService { + + @Autowired + private UserService userService; + + public void setUserService(UserService userService) { + this.userService = userService; + } + + public User getUser(Long id) { + System.out.println("Client name is " + RpcContext.getContext().getAttachment("clientName")); + System.out.println("Client impl is " + RpcContext.getContext().getAttachment("clientImpl")); + return userService.getUser(id); + } + + public RegistrationResult registerUser(User user) { + return new RegistrationResult(userService.registerUser(user)); + } +} diff --git a/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/impl/facade/UserRestServiceImpl.java b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/impl/facade/UserRestServiceImpl.java new file mode 100644 index 000000000..7e4393830 --- /dev/null +++ b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/impl/facade/UserRestServiceImpl.java @@ -0,0 +1,74 @@ +/* + * 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.alibaba.dubbo.examples.rest.impl.facade; + +import com.alibaba.dubbo.examples.rest.api.User; +import com.alibaba.dubbo.examples.rest.api.UserService; +import com.alibaba.dubbo.examples.rest.api.facade.RegistrationResult; +import com.alibaba.dubbo.examples.rest.api.facade.UserRestService; +import com.alibaba.dubbo.rpc.RpcContext; +import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +@Service("userRestService") +@Path("users") +@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML}) +@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8}) +public class UserRestServiceImpl implements UserRestService { + +// private static final Logger logger = LoggerFactory.getLogger(UserRestServiceImpl.class); + + @Autowired + private UserService userService; + + public void setUserService(UserService userService) { + this.userService = userService; + } + + @GET + @Path("{id : \\d+}") + public User getUser(@PathParam("id") Long id/*, @Context HttpServletRequest request*/) { + // test context injection +// System.out.println("Client address from @Context injection: " + (request != null ? request.getRemoteAddr() : "")); + System.out.println("Client address from RpcContext: " + RpcContext.getContext().getRemoteAddressString()); + if (RpcContext.getContext().getRequest(HttpServletRequest.class) != null) { + System.out.println("Client IP address from RpcContext: " + RpcContext.getContext().getRequest(HttpServletRequest.class).getRemoteAddr()); + } + if (RpcContext.getContext().getResponse(HttpServletResponse.class) != null) { + System.out.println("Response object from RpcContext: " + RpcContext.getContext().getResponse(HttpServletResponse.class)); + } + return userService.getUser(id); + } + + @POST + @Path("register") + public RegistrationResult registerUser(User user) { + return new RegistrationResult(userService.registerUser(user)); + } +} diff --git a/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/rest-consumer.xml b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/rest-consumer.xml new file mode 100644 index 000000000..62646af2e --- /dev/null +++ b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/rest-consumer.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/rest-provider.xml b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/rest-provider.xml new file mode 100644 index 000000000..8123855e9 --- /dev/null +++ b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/rest-provider.xml @@ -0,0 +1,57 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file -- GitLab