Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,26 @@
*/
package org.apache.dubbo.rpc.protocol.rest;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

public class DemoService implements IDemoService {
@Override
public Integer hello(Integer a, Integer b) {
return a + b;
}

@Override
public String error() {
throw new RuntimeException();
}
@Path("/demoService")
public interface DemoService {
@GET
@Path("/hello")
Integer hello(@QueryParam("a") Integer a, @QueryParam("b") Integer b);

@GET
@Path("/error")
String error();

@POST
@Path("/say")
@Consumes({MediaType.TEXT_PLAIN})
String sayHello(String name);
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
/*
* 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.rpc.protol.rest;

/**
* RestServiceImpl
*/

public class RestServiceImpl implements RestService {

private boolean called;

public String sayHello(String name) {
called = true;
return "Hello, " + name;
}


public boolean isCalled() {
return called;
}
}
/*
* 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.rpc.protocol.rest;


public class DemoServiceImpl implements DemoService {
private boolean called;

public String sayHello(String name) {
called = true;
return "Hello, " + name;
}


public boolean isCalled() {
return called;
}

@Override
public Integer hello(Integer a, Integer b) {
return a + b;
}

@Override
public String error() {
throw new RuntimeException();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.dubbo.rpc.ProxyFactory;
import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.RpcInvocation;
import org.apache.dubbo.rpc.model.ApplicationModel;
Expand All @@ -49,16 +50,55 @@ public void tearDown() {
protocol.destroy();
}

@Test
public void testRestProtocol() {
URL url = URL.valueOf("rest://127.0.0.1:5342/rest/say?version=1.0.0");
DemoServiceImpl server = new DemoServiceImpl();
ProviderModel providerModel = new ProviderModel(url.getServiceKey(), server, DemoService.class);
ApplicationModel.initProviderModel(url.getServiceKey(), providerModel);

Exporter<DemoService> exporter = protocol.export(proxy.getInvoker(server, DemoService.class, url));
Invoker<DemoService> invoker = protocol.refer(DemoService.class, url);
Assertions.assertFalse(server.isCalled());

Comment thread
ralf0131 marked this conversation as resolved.
DemoService client = proxy.getProxy(invoker);
String result = client.sayHello("haha");
Assertions.assertTrue(server.isCalled());
Assertions.assertEquals("Hello, haha", result);
invoker.destroy();
exporter.unexport();
}

@Test
public void testRestProtocolWithContextPath() {
DemoServiceImpl server = new DemoServiceImpl();
Assertions.assertFalse(server.isCalled());
URL url = URL.valueOf("rest://127.0.0.1:5341/a/b/c?version=1.0.0");
ProviderModel providerModel = new ProviderModel(url.getServiceKey(), server, DemoService.class);
ApplicationModel.initProviderModel(url.getServiceKey(), providerModel);

Exporter<DemoService> exporter = protocol.export(proxy.getInvoker(server, DemoService.class, url));

url = URL.valueOf("rest://127.0.0.1:5341/a/b/c/?version=1.0.0");
Invoker<DemoService> invoker = protocol.refer(DemoService.class, url);
DemoService client = proxy.getProxy(invoker);
String result = client.sayHello("haha");
Assertions.assertTrue(server.isCalled());
Assertions.assertEquals("Hello, haha", result);
invoker.destroy();
exporter.unexport();
}

@Test
public void testExport() {
IDemoService server = new DemoService();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, IDemoService.class);
DemoService server = new DemoServiceImpl();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, DemoService.class);
ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel);

RpcContext.getContext().setAttachment("timeout", "200");
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(server, IDemoService.class, exportUrl));
Exporter<DemoService> exporter = protocol.export(proxy.getInvoker(server, DemoService.class, exportUrl));

IDemoService demoService = this.proxy.getProxy(protocol.refer(IDemoService.class, exportUrl));
DemoService demoService = this.proxy.getProxy(protocol.refer(DemoService.class, exportUrl));

Integer echoString = demoService.hello(1, 2);
assertThat(echoString, is(3));
Expand All @@ -68,14 +108,14 @@ public void testExport() {

@Test
public void testNettyServer() {
IDemoService server = new DemoService();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, IDemoService.class);
DemoService server = new DemoServiceImpl();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, DemoService.class);
ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel);

URL nettyUrl = exportUrl.addParameter(Constants.SERVER_KEY, "netty");
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, nettyUrl));
Exporter<DemoService> exporter = protocol.export(proxy.getInvoker(new DemoServiceImpl(), DemoService.class, nettyUrl));

IDemoService demoService = this.proxy.getProxy(protocol.refer(IDemoService.class, nettyUrl));
DemoService demoService = this.proxy.getProxy(protocol.refer(DemoService.class, nettyUrl));

Integer echoString = demoService.hello(10, 10);
assertThat(echoString, is(20));
Expand All @@ -86,40 +126,40 @@ public void testNettyServer() {
@Test
public void testServletWithoutWebConfig() {
Assertions.assertThrows(RpcException.class, () -> {
IDemoService server = new DemoService();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, IDemoService.class);
DemoService server = new DemoServiceImpl();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, DemoService.class);
ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel);

URL servletUrl = exportUrl.addParameter(Constants.SERVER_KEY, "servlet");

protocol.export(proxy.getInvoker(server, IDemoService.class, servletUrl));
protocol.export(proxy.getInvoker(server, DemoService.class, servletUrl));
});
}

@Test
public void testErrorHandler() {
Assertions.assertThrows(RpcException.class, () -> {
IDemoService server = new DemoService();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, IDemoService.class);
DemoService server = new DemoServiceImpl();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, DemoService.class);
ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel);

URL nettyUrl = exportUrl.addParameter(Constants.SERVER_KEY, "netty");
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(server, IDemoService.class, nettyUrl));
Exporter<DemoService> exporter = protocol.export(proxy.getInvoker(server, DemoService.class, nettyUrl));

IDemoService demoService = this.proxy.getProxy(protocol.refer(IDemoService.class, nettyUrl));
DemoService demoService = this.proxy.getProxy(protocol.refer(DemoService.class, nettyUrl));

demoService.error();
});
}

@Test
public void testInvoke() {
IDemoService server = new DemoService();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, IDemoService.class);
DemoService server = new DemoServiceImpl();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, DemoService.class);
ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel);


Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(server, IDemoService.class, exportUrl));
Exporter<DemoService> exporter = protocol.export(proxy.getInvoker(server, DemoService.class, exportUrl));

RpcInvocation rpcInvocation = new RpcInvocation("hello", new Class[]{Integer.class, Integer.class}, new Integer[]{2, 3});

Expand All @@ -129,15 +169,15 @@ public void testInvoke() {

@Test
public void testFilter() {
IDemoService server = new DemoService();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, IDemoService.class);
DemoService server = new DemoServiceImpl();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, DemoService.class);
ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel);

URL nettyUrl = exportUrl.addParameter(Constants.SERVER_KEY, "netty")
.addParameter(Constants.EXTENSION_KEY, "org.apache.dubbo.rpc.protocol.rest.support.LoggingFilter");
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(server, IDemoService.class, nettyUrl));
Exporter<DemoService> exporter = protocol.export(proxy.getInvoker(server, DemoService.class, nettyUrl));

IDemoService demoService = this.proxy.getProxy(protocol.refer(IDemoService.class, nettyUrl));
DemoService demoService = this.proxy.getProxy(protocol.refer(DemoService.class, nettyUrl));

Integer result = demoService.hello(1, 2);

Expand All @@ -148,16 +188,16 @@ public void testFilter() {

@Test
public void testRpcContextFilter() {
IDemoService server = new DemoService();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, IDemoService.class);
DemoService server = new DemoServiceImpl();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, DemoService.class);
ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel);

// use RpcContextFilter
URL nettyUrl = exportUrl.addParameter(Constants.SERVER_KEY, "netty")
.addParameter(Constants.EXTENSION_KEY, "org.apache.dubbo.rpc.protocol.rest.RpcContextFilter");
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(server, IDemoService.class, nettyUrl));
Exporter<DemoService> exporter = protocol.export(proxy.getInvoker(server, DemoService.class, nettyUrl));

IDemoService demoService = this.proxy.getProxy(protocol.refer(IDemoService.class, nettyUrl));
DemoService demoService = this.proxy.getProxy(protocol.refer(DemoService.class, nettyUrl));

String value = null;
// put a null value into attachment.
Expand All @@ -172,12 +212,12 @@ public void testRpcContextFilter() {
@Test
public void testRegFail() {
Assertions.assertThrows(RuntimeException.class, () -> {
IDemoService server = new DemoService();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, IDemoService.class);
DemoService server = new DemoServiceImpl();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, DemoService.class);
ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel);

URL nettyUrl = exportUrl.addParameter(Constants.EXTENSION_KEY, "com.not.existing.Filter");
protocol.export(proxy.getInvoker(server, IDemoService.class, nettyUrl));
protocol.export(proxy.getInvoker(server, DemoService.class, nettyUrl));
});
}

Expand Down
Loading