diff --git a/3-extensions/protocol/dubbo-samples-rest-3.2/pom.xml b/3-extensions/protocol/dubbo-samples-rest-3.2/pom.xml new file mode 100644 index 0000000000..327510f8e5 --- /dev/null +++ b/3-extensions/protocol/dubbo-samples-rest-3.2/pom.xml @@ -0,0 +1,250 @@ + + + + org.apache.dubbo + 1.0-SNAPSHOT + + 4.0.0 + + dubbo-samples-rest-3.2 + + Dubbo Samples Rest + Dubbo Samples Rest + + + 1.8 + 1.8 + 4.3.29.RELEASE + 3.2.0-beta.6 + 1.1.0.Final + 4.2.0.Final + 1.5.19 + 8.0.53 + 1.5.13.RELEASE + + + + + + org.springframework + spring-framework-bom + ${spring.version} + pom + import + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + org.apache.dubbo + dubbo-bom + ${dubbo.version} + pom + import + + + org.apache.dubbo + dubbo-dependencies-zookeeper + ${dubbo.version} + pom + + + javax.validation + validation-api + ${validation-api.version} + + + org.hibernate + hibernate-validator + ${hibernate-validator.version} + + + org.apache.tomcat.embed + tomcat-embed-core + ${tomcat.version} + + + org.apache.tomcat.embed + tomcat-embed-logging-juli + ${tomcat.version} + + + + io.swagger + swagger-annotations + ${swagger.version} + + + io.swagger + swagger-jaxrs + ${swagger.version} + + + + + + + org.apache.dubbo + dubbo + + + org.apache.dubbo + dubbo-dependencies-zookeeper + pom + + + org.slf4j + slf4j-log4j12 + + + + + org.apache.dubbo + dubbo-configcenter-zookeeper + + + io.netty + netty-all + + + org.slf4j + slf4j-simple + + + javax.validation + validation-api + + + org.jboss.resteasy + resteasy-jackson-provider + + + org.apache.dubbo + dubbo-rpc-rest + + + org.hibernate + hibernate-validator + + + org.springframework + spring-context + + + org.springframework + spring-web + + + javax.servlet + javax.servlet-api + + + org.apache.tomcat.embed + tomcat-embed-core + + + org.apache.tomcat.embed + tomcat-embed-logging-juli + + + com.alibaba.spring + spring-context-support + + + + io.swagger + swagger-annotations + + + io.swagger + swagger-jaxrs + + + + jsr311-api + javax.ws.rs + + + + + + junit + junit + test + + + + org.springframework + spring-test + test + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + ${spring-boot.version} + + + + + + org.apache.maven.plugins + maven-war-plugin + 3.3.2 + + + org.apache.maven.plugins + maven-compiler-plugin + 3.10.1 + + ${source.level} + ${target.level} + + + + + + + + + javax.annotation + + [1.11,) + + + + javax.annotation + javax.annotation-api + 1.3.2 + + + + + + diff --git a/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/EmbeddedZooKeeper.java b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/EmbeddedZooKeeper.java new file mode 100644 index 0000000000..0a13020ece --- /dev/null +++ b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/EmbeddedZooKeeper.java @@ -0,0 +1,307 @@ + +/* + * Copyright 2014 the original author or authors. + * + * Licensed 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 org.apache.dubbo.samples.rest; + +import org.apache.zookeeper.server.ServerConfig; +import org.apache.zookeeper.server.ZooKeeperServerMain; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.SmartLifecycle; +import org.springframework.util.ErrorHandler; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Method; +import java.net.ServerSocket; +import java.util.List; +import java.util.Properties; +import java.util.Random; +import java.util.UUID; +import java.util.stream.Collectors; + +/** + * from: https://github.com/spring-projects/spring-xd/blob/v1.3.1.RELEASE/spring-xd-dirt/src/main/java/org/springframework/xd/dirt/zookeeper/ZooKeeperUtils.java + *

+ * Helper class to start an embedded instance of standalone (non clustered) ZooKeeper. + *

+ * NOTE: at least an external standalone server (if not an ensemble) are recommended, even for + * {@link org.springframework.xd.dirt.server.singlenode.SingleNodeApplication} + * + * @author Patrick Peralta + * @author Mark Fisher + * @author David Turanski + */ +public class EmbeddedZooKeeper implements SmartLifecycle { + + private static final Random RANDOM = new Random(); + + /** + * Logger. + */ + private static final Logger logger = LoggerFactory.getLogger(EmbeddedZooKeeper.class); + + /** + * ZooKeeper client port. This will be determined dynamically upon startup. + */ + private final int clientPort; + + /** + * Whether to auto-start. Default is true. + */ + private boolean autoStartup = true; + + /** + * Lifecycle phase. Default is 0. + */ + private int phase = 0; + + /** + * Thread for running the ZooKeeper server. + */ + private volatile Thread zkServerThread; + + /** + * ZooKeeper server. + */ + private volatile ZooKeeperServerMain zkServer; + + /** + * {@link ErrorHandler} to be invoked if an Exception is thrown from the ZooKeeper server thread. + */ + private ErrorHandler errorHandler; + + private boolean daemon = true; + + /** + * Construct an EmbeddedZooKeeper with a random port. + */ + public EmbeddedZooKeeper() { + clientPort = findRandomPort(30000, 65535); + } + + /** + * Construct an EmbeddedZooKeeper with the provided port. + * + * @param clientPort port for ZooKeeper server to bind to + */ + public EmbeddedZooKeeper(int clientPort, boolean daemon) { + this.clientPort = clientPort; + this.daemon = daemon; + } + + /** + * Returns the port that clients should use to connect to this embedded server. + * + * @return dynamically determined client port + */ + public int getClientPort() { + return this.clientPort; + } + + /** + * Specify whether to start automatically. Default is true. + * + * @param autoStartup whether to start automatically + */ + public void setAutoStartup(boolean autoStartup) { + this.autoStartup = autoStartup; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean isAutoStartup() { + return this.autoStartup; + } + + /** + * Specify the lifecycle phase for the embedded server. + * + * @param phase the lifecycle phase + */ + public void setPhase(int phase) { + this.phase = phase; + } + + /** + * {@inheritDoc} + */ + @Override + public int getPhase() { + return this.phase; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean isRunning() { + return (zkServerThread != null); + } + + /** + * Start the ZooKeeper server in a background thread. + *

+ * Register an error handler via {@link #setErrorHandler} in order to handle + * any exceptions thrown during startup or execution. + */ + @Override + public synchronized void start() { + if (zkServerThread == null) { + zkServerThread = new Thread(new ServerRunnable(), "ZooKeeper Server Starter"); + zkServerThread.setDaemon(daemon); + zkServerThread.start(); + } + } + + /** + * Shutdown the ZooKeeper server. + */ + @Override + public synchronized void stop() { + if (zkServerThread != null) { + // The shutdown method is protected...thus this hack to invoke it. + // This will log an exception on shutdown; see + // https://issues.apache.org/jira/browse/ZOOKEEPER-1873 for details. + try { + Method shutdown = ZooKeeperServerMain.class.getDeclaredMethod("shutdown"); + shutdown.setAccessible(true); + shutdown.invoke(zkServer); + } catch (Exception e) { + throw new RuntimeException(e); + } + + // It is expected that the thread will exit after + // the server is shutdown; this will block until + // the shutdown is complete. + try { + zkServerThread.join(5000); + zkServerThread = null; + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + logger.warn("Interrupted while waiting for embedded ZooKeeper to exit"); + // abandoning zk thread + zkServerThread = null; + } + } + } + + /** + * Stop the server if running and invoke the callback when complete. + */ + @Override + public void stop(Runnable callback) { + stop(); + callback.run(); + } + + /** + * Provide an {@link ErrorHandler} to be invoked if an Exception is thrown from the ZooKeeper server thread. If none + * is provided, only error-level logging will occur. + * + * @param errorHandler the {@link ErrorHandler} to be invoked + */ + public void setErrorHandler(ErrorHandler errorHandler) { + this.errorHandler = errorHandler; + } + + /** + * Runnable implementation that starts the ZooKeeper server. + */ + private class ServerRunnable implements Runnable { + + @Override + public void run() { + try { + Properties properties = new Properties(); + File file = new File(System.getProperty("java.io.tmpdir") + + File.separator + UUID.randomUUID()); + file.deleteOnExit(); + properties.setProperty("dataDir", file.getAbsolutePath()); + properties.setProperty("clientPort", String.valueOf(clientPort)); + + QuorumPeerConfig quorumPeerConfig = new QuorumPeerConfig(); + quorumPeerConfig.parseProperties(properties); + + zkServer = new ZooKeeperServerMain(); + ServerConfig configuration = new ServerConfig(); + configuration.readFrom(quorumPeerConfig); + + zkServer.runFromConfig(configuration); + } catch (Exception e) { + if (errorHandler != null) { + errorHandler.handleError(e); + } else { + logger.error("Exception running embedded ZooKeeper", e); + } + } + } + } + + /** + * Workaround for SocketUtils.findRandomPort() deprecation. + * + * @param min min port + * @param max max port + * @return a random generated available port + */ + private static int findRandomPort(int min, int max) { + if (min < 1024) { + throw new IllegalArgumentException("Max port shouldn't be less than 1024."); + } + + if (max > 65535) { + throw new IllegalArgumentException("Max port shouldn't be greater than 65535."); + } + + if (min > max) { + throw new IllegalArgumentException("Min port shouldn't be greater than max port."); + } + + int port = 0; + int counter = 0; + + // Workaround for legacy JDK doesn't support Random.nextInt(min, max). + List randomInts = RANDOM.ints(min, max + 1) + .limit(max - min) + .mapToObj(Integer::valueOf) + .collect(Collectors.toList()); + + do { + if (counter > max - min) { + throw new IllegalStateException("Unable to find a port between " + min + "-" + max); + } + + port = randomInts.get(counter); + counter++; + } while (isPortInUse(port)); + + return port; + } + + private static boolean isPortInUse(int port) { + try (ServerSocket ignored = new ServerSocket(port)) { + return false; + } catch (IOException e) { + // continue + } + return true; + } +} diff --git a/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/ProviderConfiguration.java b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/ProviderConfiguration.java new file mode 100644 index 0000000000..270aac72ce --- /dev/null +++ b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/ProviderConfiguration.java @@ -0,0 +1,27 @@ +/* + * + * 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 org.apache.dubbo.samples.rest; + +import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; +import org.springframework.context.annotation.Configuration; + +@Configuration +@EnableDubbo(scanBasePackages = "org.apache.dubbo.samples.rest") +public class ProviderConfiguration { +} diff --git a/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/RestConsumer.java b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/RestConsumer.java new file mode 100644 index 0000000000..e987035031 --- /dev/null +++ b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/RestConsumer.java @@ -0,0 +1,31 @@ +/* + * + * 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 org.apache.dubbo.samples.rest; + +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class RestConsumer { + + public static void main(String[] args) throws Exception { + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring/rest-consumer.xml"}); + context.start(); + System.in.read(); + } + +} \ No newline at end of file diff --git a/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/RestProvider.java b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/RestProvider.java new file mode 100644 index 0000000000..6d0312a924 --- /dev/null +++ b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/RestProvider.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 org.apache.dubbo.samples.rest; + +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import java.util.concurrent.CountDownLatch; + +public class RestProvider { + + public static void main(String[] args) throws Exception { + new EmbeddedZooKeeper(2181, false).start(); + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring/rest-provider.xml"}); + context.start(); + + System.out.println("dubbo service started"); + new CountDownLatch(1).await(); + } + + + +} \ No newline at end of file diff --git a/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/api/ExceptionMapperService.java b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/api/ExceptionMapperService.java new file mode 100644 index 0000000000..9e336c5ab1 --- /dev/null +++ b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/api/ExceptionMapperService.java @@ -0,0 +1,33 @@ +/* + * + * 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 org.apache.dubbo.samples.rest.api; + + + +import javax.ws.rs.POST; +import javax.ws.rs.Path; + +@Path("/exception/mapper") +public interface ExceptionMapperService { + + @POST + @Path("/exception") + String exception(String message); + +} \ No newline at end of file diff --git a/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/api/HttpMethodService.java b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/api/HttpMethodService.java new file mode 100644 index 0000000000..e40c856b8a --- /dev/null +++ b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/api/HttpMethodService.java @@ -0,0 +1,73 @@ +/* + * + * 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 org.apache.dubbo.samples.rest.api; + + +import io.swagger.jaxrs.PATCH; + +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.HEAD; +import javax.ws.rs.OPTIONS; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.QueryParam; + +@Path("/demoService") +public interface HttpMethodService { + + @POST + @Path("/sayPost") + @Consumes({javax.ws.rs.core.MediaType.TEXT_PLAIN}) + String sayHelloPost(String hello); + + @DELETE + @Path("/sayDelete") + @Consumes({javax.ws.rs.core.MediaType.TEXT_PLAIN}) + String sayHelloDelete(@QueryParam("name") String hello); + + @HEAD + @Path("/sayHead") + @Consumes({javax.ws.rs.core.MediaType.TEXT_PLAIN}) + String sayHelloHead(@QueryParam("name") String hello); + + @GET + @Path("/sayGet") + @Consumes({javax.ws.rs.core.MediaType.TEXT_PLAIN}) + String sayHelloGet(@QueryParam("name") String hello); + + @PUT + @Path("/sayPut") + @Consumes({javax.ws.rs.core.MediaType.TEXT_PLAIN}) + String sayHelloPut(@QueryParam("name") String hello); + + @PATCH + @Path("/sayPatch") + @Consumes({javax.ws.rs.core.MediaType.TEXT_PLAIN}) + String sayHelloPatch(@QueryParam("name") String hello); + + @OPTIONS + @Path("/sayOptions") + @Consumes({javax.ws.rs.core.MediaType.TEXT_PLAIN}) + String sayHelloOptions(@QueryParam("name") String hello); + +} + diff --git a/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/api/HttpRequestAndResponseRPCContextService.java b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/api/HttpRequestAndResponseRPCContextService.java new file mode 100644 index 0000000000..5cd0f2c04e --- /dev/null +++ b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/api/HttpRequestAndResponseRPCContextService.java @@ -0,0 +1,49 @@ +/* + * + * 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 org.apache.dubbo.samples.rest.api; + + +import javax.ws.rs.Consumes; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.QueryParam; +import java.util.List; + +@Path("/demoService") +public interface HttpRequestAndResponseRPCContextService { + + @POST + @Path("/httpRequestParam") + @Consumes({javax.ws.rs.core.MediaType.TEXT_PLAIN}) + String httpRequestParam(@QueryParam("name") String hello); + + @POST + @Path("/httpRequestHeader") + @Consumes({javax.ws.rs.core.MediaType.TEXT_PLAIN}) + String httpRequestHeader(@HeaderParam("header") String hello); + + @POST + @Path("/httpResponseHeader") + @Consumes({javax.ws.rs.core.MediaType.TEXT_PLAIN}) + List httpResponseHeader(@HeaderParam("response") String hello); + + +} + diff --git a/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/api/JaxRsRestDemoService.java b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/api/JaxRsRestDemoService.java new file mode 100644 index 0000000000..bfc2e299ce --- /dev/null +++ b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/api/JaxRsRestDemoService.java @@ -0,0 +1,120 @@ +/* + * + * 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 org.apache.dubbo.samples.rest.api; + + +import javax.ws.rs.Consumes; +import javax.ws.rs.FormParam; +import javax.ws.rs.GET; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import java.util.List; +import java.util.Map; + +/** + * + * @Consumers & @Produces can be not used ,we will make sure the content-type of request by arg type + * but the Request method is forbidden disappear + * parameters which annotation are not present , it is from the body (jaxrs anntation is diffrent from spring web from param(only request param can ignore anntation)) + * + * Every method only one param from body + * + * the path annotation must present in class & method + */ + +@Path("/jaxrs/demo/service") +public interface JaxRsRestDemoService { + @GET + @Path("/hello") + Integer hello(@QueryParam("a") Integer a, @QueryParam("b") Integer b); + + @GET + @Path("/error") + String error(); + + @POST + @Path("/say") + String sayHello(String name); + + + + + + @POST + @Path("/testFormBody") + Long testFormBody(@FormParam("number") Long number); + + @POST + @Path("/testJavaBeanBody") + @Consumes({MediaType.APPLICATION_JSON}) + User testJavaBeanBody(User user); + + + + @GET + @Path("/primitive") + int primitiveInt(@QueryParam("a") int a, @QueryParam("b") int b); + + @GET + @Path("/primitiveLong") + long primitiveLong(@QueryParam("a") long a, @QueryParam("b") Long b); + + @GET + @Path("/primitiveByte") + long primitiveByte(@QueryParam("a") byte a, @QueryParam("b") Long b); + + @POST + @Path("/primitiveShort") + long primitiveShort(@QueryParam("a") short a, @QueryParam("b") Long b, int c); + + @GET + @Path("testMapParam") + @Produces({MediaType.TEXT_PLAIN}) + @Consumes({MediaType.TEXT_PLAIN}) + String testMapParam(@QueryParam("test") Map params); + + @GET + @Path("testMapHeader") + @Produces({MediaType.TEXT_PLAIN}) + @Consumes({MediaType.TEXT_PLAIN}) + String testMapHeader(@HeaderParam("test") Map headers); + + @POST + @Path("testMapForm") + @Produces({MediaType.APPLICATION_JSON}) + @Consumes({MediaType.APPLICATION_FORM_URLENCODED}) + List testMapForm(MultivaluedMap params); + + @POST + @Path("/header") + @Consumes({MediaType.TEXT_PLAIN}) + String header(@HeaderParam("header") String header); + + @POST + @Path("/headerInt") + @Consumes({MediaType.TEXT_PLAIN}) + int headerInt(@HeaderParam("header") int header); + + +} diff --git a/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/api/SpringRestDemoService.java b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/api/SpringRestDemoService.java new file mode 100644 index 0000000000..e52bc120f2 --- /dev/null +++ b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/api/SpringRestDemoService.java @@ -0,0 +1,80 @@ +/* + * + * 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 org.apache.dubbo.samples.rest.api; + + +import org.springframework.http.MediaType; +import org.springframework.util.MultiValueMap; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; + +import java.util.List; +import java.util.Map; + +@RequestMapping("/spring/demo/service") +public interface SpringRestDemoService { + + @RequestMapping(method = RequestMethod.GET, value = "/hello") + Integer hello(@RequestParam("a") Integer a, @RequestParam("b") Integer b); + + @RequestMapping(method = RequestMethod.GET, value = "/error") + String error(); + + @RequestMapping(method = RequestMethod.POST, value = "/say") + String sayHello(@RequestBody String name); + + @RequestMapping(method = RequestMethod.POST, value = "/testFormBody", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) + Long testFormBody(@RequestBody Long number); + + @RequestMapping(method = RequestMethod.POST, value = "/testJavaBeanBody", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) + User testJavaBeanBody(@RequestBody User user); + + + @RequestMapping(method = RequestMethod.GET, value = "/primitive") + int primitiveInt(@RequestParam("a") int a, @RequestParam("b") int b); + + @RequestMapping(method = RequestMethod.GET, value = "/primitiveLong") + long primitiveLong(@RequestParam("a") long a, @RequestParam("b") Long b); + + @RequestMapping(method = RequestMethod.GET, value = "/primitiveByte") + long primitiveByte(@RequestParam("a") byte a, @RequestParam("b") Long b); + + + @RequestMapping(method = RequestMethod.POST, value = "/primitiveShort") + long primitiveShort(@RequestParam("a") short a, @RequestParam("b") Long b, @RequestBody int c); + + + @RequestMapping(method = RequestMethod.GET, value = "/testMapParam") + String testMapParam(@RequestParam Map params); + + @RequestMapping(method = RequestMethod.GET, value = "/testMapHeader") + String testMapHeader(@RequestHeader Map headers); + + @RequestMapping(method = RequestMethod.POST, value = "/testMapForm", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) + List testMapForm(MultiValueMap params); + + + @RequestMapping(method = RequestMethod.GET, value = "/headerInt") + int headerInt(@RequestHeader("header") int header); + + +} diff --git a/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/api/User.java b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/api/User.java new file mode 100644 index 0000000000..a0f4c9ef29 --- /dev/null +++ b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/api/User.java @@ -0,0 +1,92 @@ +/* + * + * 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 org.apache.dubbo.samples.rest.api; + +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.XmlRootElement; +import java.io.Serializable; +import java.util.Objects; + +@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 static User getInstance() { + return new User(1l, "dubbo-rest"); + } + + 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 boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + User user = (User) o; + return Objects.equals(id, user.id) && Objects.equals(name, user.name); + } + + @Override + public int hashCode() { + return Objects.hash(id, name); + } + + @Override + public String toString() { + return "User (" + + "id=" + id + + ", name='" + name + '\'' + + ')'; + } +} diff --git a/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/exception/ExceptionMapperDemoException.java b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/exception/ExceptionMapperDemoException.java new file mode 100644 index 0000000000..d76d41605c --- /dev/null +++ b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/exception/ExceptionMapperDemoException.java @@ -0,0 +1,26 @@ +/* + * + * 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 org.apache.dubbo.samples.rest.exception; + +public class ExceptionMapperDemoException extends RuntimeException { + + public ExceptionMapperDemoException(String message) { + super(message); + } +} diff --git a/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/extension/ExceptionMapperForTest.java b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/extension/ExceptionMapperForTest.java new file mode 100644 index 0000000000..1320d4289d --- /dev/null +++ b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/extension/ExceptionMapperForTest.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 org.apache.dubbo.samples.rest.extension; + + +import org.apache.dubbo.rpc.protocol.rest.exception.mapper.ExceptionHandler; +import org.apache.dubbo.samples.rest.exception.ExceptionMapperDemoException; + +public class ExceptionMapperForTest implements ExceptionHandler { + + + @Override + public Object result(ExceptionMapperDemoException exception) { + return exception.getMessage(); + } +} diff --git a/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/impl/ExceptionMapperServiceImpl.java b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/impl/ExceptionMapperServiceImpl.java new file mode 100644 index 0000000000..cd041c7f7d --- /dev/null +++ b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/impl/ExceptionMapperServiceImpl.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 org.apache.dubbo.samples.rest.impl; + + +import org.apache.dubbo.samples.rest.api.ExceptionMapperService; +import org.apache.dubbo.samples.rest.exception.ExceptionMapperDemoException; +import org.springframework.stereotype.Service; + +@Service("exceptionMapperService") +public class ExceptionMapperServiceImpl implements ExceptionMapperService { + + @Override + public String exception(String message) { + + throw new ExceptionMapperDemoException(message); + } + +} \ No newline at end of file diff --git a/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/impl/HttpMethodServiceImpl.java b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/impl/HttpMethodServiceImpl.java new file mode 100644 index 0000000000..12a6573fcf --- /dev/null +++ b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/impl/HttpMethodServiceImpl.java @@ -0,0 +1,60 @@ +package org.apache.dubbo.samples.rest.impl;/* + * 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. + */ + + +import org.apache.dubbo.samples.rest.api.HttpMethodService; +import org.springframework.stereotype.Service; + +@Service("httpMethodService") +public class HttpMethodServiceImpl implements HttpMethodService { + + @Override + public String sayHelloPost(String hello) { + return hello; + } + + @Override + public String sayHelloDelete(String hello) { + return hello; + } + + @Override + public String sayHelloHead(String hello) { + return hello; + } + + @Override + public String sayHelloGet(String hello) { + return hello; + } + + @Override + public String sayHelloPut(String hello) { + return hello; + } + + @Override + public String sayHelloPatch(String hello) { + return hello; + } + + @Override + public String sayHelloOptions(String hello) { + return hello; + } +} + diff --git a/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/impl/HttpRequestAndResponseRPCContextServiceImpl.java b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/impl/HttpRequestAndResponseRPCContextServiceImpl.java new file mode 100644 index 0000000000..c4c6648df0 --- /dev/null +++ b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/impl/HttpRequestAndResponseRPCContextServiceImpl.java @@ -0,0 +1,55 @@ +/* + * + * 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 org.apache.dubbo.samples.rest.impl; + +import org.apache.dubbo.rpc.RpcContext; +import org.apache.dubbo.rpc.protocol.rest.netty.NettyHttpResponse; +import org.apache.dubbo.rpc.protocol.rest.request.RequestFacade; +import org.apache.dubbo.samples.rest.api.HttpRequestAndResponseRPCContextService; +import org.springframework.stereotype.Service; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +@Service("httpRequestAndResponseRPCContextService") +public class HttpRequestAndResponseRPCContextServiceImpl implements HttpRequestAndResponseRPCContextService { + @Override + public String httpRequestParam(String hello) { + Object request = RpcContext.getServerAttachment().getRequest(); + return ((RequestFacade) request).getParameter("name"); + } + + @Override + public String httpRequestHeader(String hello) { + Object request = RpcContext.getServerAttachment().getRequest(); + return ((RequestFacade) request).getHeader("header"); + } + + @Override + public List httpResponseHeader(String hello) { + Object response = RpcContext.getServerAttachment().getResponse(); + Map> outputHeaders = ((NettyHttpResponse) response).getOutputHeaders(); + String responseKey = "response"; + outputHeaders.put(responseKey, Arrays.asList(hello)); + + + return outputHeaders.get(responseKey); + } +} diff --git a/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/impl/JaxRsRestDemoServiceImpl.java b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/impl/JaxRsRestDemoServiceImpl.java new file mode 100644 index 0000000000..3611bf8e61 --- /dev/null +++ b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/impl/JaxRsRestDemoServiceImpl.java @@ -0,0 +1,107 @@ +/* + * + * 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 org.apache.dubbo.samples.rest.impl; + + +import org.apache.dubbo.samples.rest.api.JaxRsRestDemoService; +import org.apache.dubbo.samples.rest.api.User; +import org.springframework.stereotype.Service; + +import javax.ws.rs.core.MultivaluedMap; +import java.util.List; +import java.util.Map; +@Service("jaxRsRestDemoService") +public class JaxRsRestDemoServiceImpl implements JaxRsRestDemoService { + + @Override + public String sayHello(String name) { + return "Hello, " + name; + } + + @Override + public Long testFormBody(Long number) { + return number; + } + + @Override + public User testJavaBeanBody(User user) { + return user; + } + + + @Override + public int primitiveInt(int a, int b) { + return a + b; + } + + @Override + public long primitiveLong(long a, Long b) { + return a + b; + } + + @Override + public long primitiveByte(byte a, Long b) { + return a + b; + } + + @Override + public long primitiveShort(short a, Long b, int c) { + return a + b; + } + + + + @Override + public String testMapParam(Map params) { + return params.get("param"); + } + + @Override + public String testMapHeader(Map headers) { + return headers.get("header"); + } + + @Override + public List testMapForm(MultivaluedMap params) { + return params.get("form"); + } + + @Override + public String header(String header) { + return header; + } + + @Override + public int headerInt(int header) { + return header; + } + + + @Override + public Integer hello(Integer a, Integer b) { + return a + b; + } + + + @Override + public String error() { + throw new RuntimeException("test error"); + } + +} diff --git a/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/impl/SpringRestDemoServiceImpl.java b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/impl/SpringRestDemoServiceImpl.java new file mode 100644 index 0000000000..a5279e2ab6 --- /dev/null +++ b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/java/org/apache/dubbo/samples/rest/impl/SpringRestDemoServiceImpl.java @@ -0,0 +1,104 @@ +/* + * + * 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 org.apache.dubbo.samples.rest.impl; + + +import org.apache.dubbo.samples.rest.api.SpringRestDemoService; +import org.apache.dubbo.samples.rest.api.User; +import org.springframework.stereotype.Service; +import org.springframework.util.MultiValueMap; + +import java.util.List; +import java.util.Map; + +@Service("springRestDemoService") +public class SpringRestDemoServiceImpl implements SpringRestDemoService { + + @Override + public String sayHello(String name) { + return "Hello, " + name; + } + + @Override + public Long testFormBody(Long number) { + return number; + } + + @Override + public User testJavaBeanBody(User user) { + return user; + } + + + @Override + public int primitiveInt(int a, int b) { + return a + b; + } + + @Override + public long primitiveLong(long a, Long b) { + return a + b; + } + + @Override + public long primitiveByte(byte a, Long b) { + return a + b; + } + + @Override + public long primitiveShort(short a, Long b, int c) { + return a + b; + } + + + + @Override + public String testMapParam(Map params) { + return params.get("param"); + } + + @Override + public String testMapHeader(Map headers) { + return headers.get("header"); + } + + @Override + public List testMapForm(MultiValueMap params) { + return params.get("form"); + } + + + @Override + public int headerInt(int header) { + return header; + } + + + @Override + public Integer hello(Integer a, Integer b) { + return a + b; + } + + + @Override + public String error() { + throw new RuntimeException("test error"); + } + +} diff --git a/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/resources/spring/rest-consumer.xml b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/resources/spring/rest-consumer.xml new file mode 100644 index 0000000000..b726af1532 --- /dev/null +++ b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/resources/spring/rest-consumer.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/resources/spring/rest-provider.xml b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/resources/spring/rest-provider.xml new file mode 100644 index 0000000000..14ebbc52d8 --- /dev/null +++ b/3-extensions/protocol/dubbo-samples-rest-3.2/src/main/resources/spring/rest-provider.xml @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/3-extensions/protocol/dubbo-samples-rest-3.2/src/test/java/rest/ExceptionMapperTest.java b/3-extensions/protocol/dubbo-samples-rest-3.2/src/test/java/rest/ExceptionMapperTest.java new file mode 100644 index 0000000000..943521a897 --- /dev/null +++ b/3-extensions/protocol/dubbo-samples-rest-3.2/src/test/java/rest/ExceptionMapperTest.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 rest; + +import org.apache.dubbo.samples.rest.api.ExceptionMapperService; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = {"classpath:/spring/rest-consumer.xml"}) +public class ExceptionMapperTest { + @Autowired + ExceptionMapperService exceptionMapperService; + + @Test + public void exceptionMapperTest() { + String returnStr = "exception"; + String paramStr = "exception"; + Assert.assertEquals(returnStr, exceptionMapperService.exception(paramStr)); + + } + + +} diff --git a/3-extensions/protocol/dubbo-samples-rest-3.2/src/test/java/rest/HttpMethodTest.java b/3-extensions/protocol/dubbo-samples-rest-3.2/src/test/java/rest/HttpMethodTest.java new file mode 100644 index 0000000000..86096694db --- /dev/null +++ b/3-extensions/protocol/dubbo-samples-rest-3.2/src/test/java/rest/HttpMethodTest.java @@ -0,0 +1,49 @@ +/* + * + * 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 rest; + +import org.apache.dubbo.samples.rest.api.HttpMethodService; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = {"classpath:/spring/rest-consumer.xml"}) +public class HttpMethodTest { + @Autowired + HttpMethodService httpMethodService; + + @Test + public void sayHello() { + String returnStr = "hello"; + String paramStr = "hello"; + Assert.assertEquals(null, httpMethodService.sayHelloHead(paramStr)); + Assert.assertEquals(returnStr, httpMethodService.sayHelloGet(paramStr)); + Assert.assertEquals(returnStr, httpMethodService.sayHelloDelete(paramStr)); + Assert.assertEquals(returnStr, httpMethodService.sayHelloPut(paramStr)); + Assert.assertEquals(returnStr, httpMethodService.sayHelloOptions(paramStr)); +// Assert.assertEquals(returnStr, httpMethodService.sayHelloPatch(paramStr)); + Assert.assertEquals(returnStr, httpMethodService.sayHelloPost(paramStr)); + } + + +} diff --git a/3-extensions/protocol/dubbo-samples-rest-3.2/src/test/java/rest/HttpRPCContextTest.java b/3-extensions/protocol/dubbo-samples-rest-3.2/src/test/java/rest/HttpRPCContextTest.java new file mode 100644 index 0000000000..b1f09c28e7 --- /dev/null +++ b/3-extensions/protocol/dubbo-samples-rest-3.2/src/test/java/rest/HttpRPCContextTest.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 rest; + +import org.apache.dubbo.samples.rest.api.HttpRequestAndResponseRPCContextService; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = {"classpath:/spring/rest-consumer.xml"}) +public class HttpRPCContextTest { + @Autowired + HttpRequestAndResponseRPCContextService requestAndResponseRPCContextService; + + @Test + public void httpRPCContextTest() { + String returnStr = "hello"; + String paramStr = "hello"; + Assert.assertEquals(returnStr, requestAndResponseRPCContextService.httpRequestHeader(paramStr)); + Assert.assertEquals(returnStr, requestAndResponseRPCContextService.httpRequestParam(paramStr)); + Assert.assertEquals(returnStr, requestAndResponseRPCContextService.httpResponseHeader(paramStr).get(0)); + + } + + +} diff --git a/3-extensions/protocol/dubbo-samples-rest-3.2/src/test/java/rest/JaxrsDemoConsumerNettyTest.java b/3-extensions/protocol/dubbo-samples-rest-3.2/src/test/java/rest/JaxrsDemoConsumerNettyTest.java new file mode 100644 index 0000000000..25dbe1b097 --- /dev/null +++ b/3-extensions/protocol/dubbo-samples-rest-3.2/src/test/java/rest/JaxrsDemoConsumerNettyTest.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 rest; + +import org.apache.dubbo.samples.rest.api.JaxRsRestDemoService; +import org.apache.dubbo.samples.rest.api.User; +import org.jboss.resteasy.specimpl.MultivaluedMapImpl; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.util.Arrays; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = {"classpath:/spring/rest-consumer.xml"}) +public class JaxrsDemoConsumerNettyTest { + @Autowired + JaxRsRestDemoService jaxRsRestDemoService; + + @Test + public void sayHello() { + String hello = jaxRsRestDemoService.sayHello("hello"); + Assert.assertEquals("Hello, hello", hello); + } + + @Test + public void testPrimitive() { + Integer result = jaxRsRestDemoService.primitiveInt(1, 2); + Long resultLong = jaxRsRestDemoService.primitiveLong(1, 2l); + long resultByte = jaxRsRestDemoService.primitiveByte((byte) 1, 2l); + long resultShort = jaxRsRestDemoService.primitiveShort((short) 1, 2l, 1); + + assertThat(result, is(3)); + assertThat(resultShort, is(3l)); + assertThat(resultLong, is(3l)); + assertThat(resultByte, is(3l)); + } + + @Test + public void testBody() { + + Assert.assertEquals(Long.valueOf(1l), jaxRsRestDemoService.testFormBody(1l)); + + MultivaluedMapImpl forms = new MultivaluedMapImpl<>(); + forms.put("form", Arrays.asList("F1")); + + Assert.assertEquals(Arrays.asList("F1"), jaxRsRestDemoService.testMapForm(forms)); + Assert.assertEquals(User.getInstance(), jaxRsRestDemoService.testJavaBeanBody(User.getInstance())); + } + + +} diff --git a/3-extensions/protocol/dubbo-samples-rest-3.2/src/test/java/rest/SpringDemoConsumerNettyTest.java b/3-extensions/protocol/dubbo-samples-rest-3.2/src/test/java/rest/SpringDemoConsumerNettyTest.java new file mode 100644 index 0000000000..2bf9df42bf --- /dev/null +++ b/3-extensions/protocol/dubbo-samples-rest-3.2/src/test/java/rest/SpringDemoConsumerNettyTest.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 rest; + +import org.apache.dubbo.samples.rest.api.SpringRestDemoService; +import org.apache.dubbo.samples.rest.api.User; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; + +import java.util.Arrays; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = {"classpath:/spring/rest-consumer.xml"}) +public class SpringDemoConsumerNettyTest { + @Autowired + SpringRestDemoService springRestDemoService; + + @Test + public void sayHello() { + String hello = springRestDemoService.sayHello("hello"); + Assert.assertEquals("Hello, hello", hello); + } + + @Test + public void testPrimitive() { + Integer result = springRestDemoService.primitiveInt(1, 2); + Long resultLong = springRestDemoService.primitiveLong(1, 2l); + long resultByte = springRestDemoService.primitiveByte((byte) 1, 2l); + long resultShort = springRestDemoService.primitiveShort((short) 1, 2l, 1); + + assertThat(result, is(3)); + assertThat(resultShort, is(3l)); + assertThat(resultLong, is(3l)); + assertThat(resultByte, is(3l)); + } + + @Test + public void testBody() { + + Assert.assertEquals(Long.valueOf(1l), springRestDemoService.testFormBody(1l)); + + MultiValueMap forms = new LinkedMultiValueMap<>(); + forms.put("form", Arrays.asList("F1")); + + Assert.assertEquals(Arrays.asList("F1"), springRestDemoService.testMapForm(forms)); + Assert.assertEquals(User.getInstance(), springRestDemoService.testJavaBeanBody(User.getInstance())); + } + + +} diff --git a/3-extensions/protocol/pom.xml b/3-extensions/protocol/pom.xml index abe9a199a1..5f9e3fba7c 100644 --- a/3-extensions/protocol/pom.xml +++ b/3-extensions/protocol/pom.xml @@ -37,5 +37,6 @@ dubbo-samples-triple dubbo-samples-triple-reactor dubbo-samples-webservice + dubbo-samples-rest-3.2 \ No newline at end of file