com.googlecode.jsonrpc4j.ProxyUtil Java Examples

The following examples show how to use com.googlecode.jsonrpc4j.ProxyUtil. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JSONRPCImporter.java    From fuchsia with Apache License 2.0 6 votes vote down vote up
private void createProxyFromKlass(String klassName, JsonRpcHttpClient client, Dictionary<String, Object> props, ImportDeclaration importDeclaration) throws BinderException {
    final Class<?> klass;
    // Use given klass
    try {
        klass = FuchsiaUtils.loadClass(context, klassName);
    } catch (ClassNotFoundException e) {
        throw new BinderException(
                "Cannot create a proxy for the ImportDeclaration : " + importDeclaration
                        + " unable to find a bundle which export the service class.", e
        );
    }

    // create the proxy !
    final Object proxy = ProxyUtil.createClientProxy(klass.getClassLoader(), klass, client);
    ServiceRegistration sReg = context.registerService(klassName, proxy, props);

    handleImportDeclaration(importDeclaration);

    String id = (String) importDeclaration.getMetadata().get(ID);
    // Add the registration to the registration list
    registrations.put(id, sReg);
}
 
Example #2
Source File: JsonRPC.java    From WeEvent with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    System.out.println("This is WeEvent json rpc sample.");
    try {
        URL remote = new URL("http://localhost:7000/weevent-broker/jsonrpc");
        // init json rpc client
        JsonRpcHttpClient client = new JsonRpcHttpClient(remote);
        // init IBrokerRpc object
        IBrokerRpc rpc = ProxyUtil.createClientProxy(client.getClass().getClassLoader(), IBrokerRpc.class, client);

        // ensure topic
        rpc.open("com.weevent.test", WeEvent.DEFAULT_GROUP_ID);

        // publish event
        SendResult sendResult = rpc.publish("com.weevent.test", WeEvent.DEFAULT_GROUP_ID, "hello WeEvent".getBytes(StandardCharsets.UTF_8), new HashMap<>());
        System.out.println(sendResult);
    } catch (MalformedURLException | BrokerException e) {
        e.printStackTrace();
    }
}
 
Example #3
Source File: StreamServerTest.java    From jsonrpc4j with MIT License 6 votes vote down vote up
@Test
public void testStopWhileClientsWorking() throws Exception {
	
	StreamServer streamServer = createAndStartServer();
	Socket socket = new Socket(serverSocket.getInetAddress(), serverSocket.getLocalPort());
	final Service service1 = ProxyUtil.createClientProxy(this.getClass().getClassLoader(), Service.class, jsonRpcClient, socket);
	
	Thread t = new Thread(new Runnable() {
		@Override
		public void run() {
			// noinspection InfiniteLoopStatement
			while (true) {
				service1.inc();
			}
			
		}
	});
	t.start();
	
	while (service.val < 10) {
		Thread.yield();
	}
	streamServer.stop();
}
 
Example #4
Source File: ServerClientTest.java    From jsonrpc4j with MIT License 5 votes vote down vote up
@Test
public void testAllMethodsViaCompositeProxy() throws Throwable {
	Object compositeService = ProxyUtil.createCompositeServiceProxy(ClassLoader.getSystemClassLoader(), new Object[]{client},
			new Class<?>[]{Service.class}, true);
	Service clientService = (Service) compositeService;
	testCommon(clientService);
}
 
Example #5
Source File: TimeoutTest.java    From jsonrpc4j with MIT License 5 votes vote down vote up
@Test
public void testTimingOutRequests() throws Exception {
	JsonRpcHttpClient client = getHttpClient(true, false);
	client.setReadTimeoutMillis(1);
	expectedEx.expectCause(isA(SocketTimeoutException.class));
	service = ProxyUtil.createClientProxy(this.getClass().getClassLoader(), FakeTimingOutService.class, client);
	service.doTimeout();
}
 
Example #6
Source File: HttpCodeTest.java    From jsonrpc4j with MIT License 5 votes vote down vote up
@Test
public void http405OnInvalidUrl() throws MalformedURLException {
	expectedEx.expectMessage(anyOf(
			equalTo("405 HTTP method POST is not supported by this URL"),
			equalTo("404 Not Found"),
			equalTo("HTTP method POST is not supported by this URL"),
			startsWith("Server returned HTTP response code: 405 for URL: http://127.0.0.1")));
	expectedEx.expect(Exception.class);
	FakeServiceInterface service = ProxyUtil.createClientProxy(FakeServiceInterface.class, getClient("error"));
	service.doSomething();
}
 
Example #7
Source File: HttpCodeTest.java    From jsonrpc4j with MIT License 5 votes vote down vote up
@Test
public void httpCustomStatus() throws MalformedURLException {
	expectedEx.expectMessage(equalTo("Server Error"));
	expectedEx.expect(JsonRpcClientException.class);

	RestTemplate restTemplate = new RestTemplate();

	JsonRpcRestClient client = getClient(JettyServer.SERVLET, restTemplate);

	// Overwrite error handler for error check.
	restTemplate.setErrorHandler(new DefaultResponseErrorHandler());

	FakeServiceInterface service = ProxyUtil.createClientProxy(FakeServiceInterface.class, client);
	service.throwSomeException("function error");
}
 
Example #8
Source File: StreamServerTest.java    From jsonrpc4j with MIT License 5 votes vote down vote up
CreateClients invoke() throws IOException {
	services = new Service[5];
	sockets = new Socket[5];
	for (int i = 0; i < services.length; i++) {
		sockets[i] = new Socket(serverSocket.getInetAddress(), serverSocket.getLocalPort());
		services[i] = ProxyUtil.createClientProxy(this.getClass().getClassLoader(), Service.class, jsonRpcClient, sockets[i]);
	}
	return this;
}
 
Example #9
Source File: StreamServerTest.java    From jsonrpc4j with MIT License 5 votes vote down vote up
@Test
public void testClientDisconnectsCausingExceptionOnServer() throws Exception {
	StreamServer streamServer = createAndStartServer();
	final Socket socket = new Socket(serverSocket.getInetAddress(), serverSocket.getLocalPort());
	final Service service1 = ProxyUtil.createClientProxy(this.getClass().getClassLoader(), Service.class, jsonRpcClient, socket);
	Thread t = new Thread(new Runnable() {
		@Override
		public void run() {
			while (true) {
				if (service1.inc() > 5) {
					try {
						socket.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
					return;
				}
			}
		}
	});
	
	Thread.sleep(1000);
	assertEquals(1, streamServer.getNumberOfConnections());
	Server server = streamServer.getServers().iterator().next();
	assertNotNull(server);
	t.start();
	
	while (streamServer.getNumberOfConnections() > 0) {
		Thread.yield();
	}
	assertEquals(0, server.getNumberOfErrors());
	streamServer.stop();
}
 
Example #10
Source File: StreamServerTest.java    From jsonrpc4j with MIT License 5 votes vote down vote up
@Test
public void testBasicConnection() throws Exception {
	
	StreamServer streamServer = createAndStartServer();
	Socket socket = new Socket(serverSocket.getInetAddress(), serverSocket.getLocalPort());
	Service client = ProxyUtil.createClientProxy(this.getClass().getClassLoader(), Service.class, jsonRpcClient, socket);
	for (int i = 0; i < 100; i++) {
		assertEquals(i, client.inc());
	}
	assertEquals("hello dude", client.hello("dude"));
	socket.close();
	streamServer.stop();
}
 
Example #11
Source File: HttpClientTest.java    From jsonrpc4j with MIT License 4 votes vote down vote up
@Test
public void testGZIPRequestAndResponse() throws MalformedURLException {
	service = ProxyUtil.createClientProxy(this.getClass().getClassLoader(), FakeServiceInterface.class, getHttpClient(true, true));
	int i = service.returnPrimitiveInt(2);
	Assert.assertEquals(2, i);
}
 
Example #12
Source File: JSONRPCExporterTest.java    From fuchsia with Apache License 2.0 4 votes vote down vote up
@Test
public void remoteServiceInvoked() throws Exception {

    ExportDeclaration declaration = getValidDeclarations().get(0);

    JSONRPCExportDeclarationWrapper pojo = JSONRPCExportDeclarationWrapper.create(declaration);

    fuchsiaDeclarationBinder.useDeclaration(declaration);

    JsonRpcHttpClient client = new JsonRpcHttpClient(new java.net.URL("http://localhost:" + HTTP_PORT + pojo.getUrlContext() + "/" + pojo.getInstanceName()));

    Object proxy = ProxyUtil.createClientProxy(this.getClass().getClassLoader(), ServiceForExportation.class, client);

    ServiceForExportation remoteObject = (ServiceForExportation) proxy;

    remoteObject.ping();

    verify(serviceToBeExported, times(1)).ping();

}
 
Example #13
Source File: LocalThreadServer.java    From jsonrpc4j with MIT License 4 votes vote down vote up
public T client(Class<T> clazz) throws IOException {
	InputStream clientInput = new PipedInputStream((PipedOutputStream) serverOutput);
	OutputStream clientOutput = new PipedOutputStream((PipedInputStream) serverInput);
	return ProxyUtil.createClientProxy(ClassLoader.getSystemClassLoader(), clazz, new JsonRpcClient(), clientInput, clientOutput);
}
 
Example #14
Source File: HttpClientTest.java    From jsonrpc4j with MIT License 4 votes vote down vote up
@Test
public void testRequestAndResponse() throws MalformedURLException {
	service = ProxyUtil.createClientProxy(this.getClass().getClassLoader(), FakeServiceInterface.class, getHttpClient(false, false));
	int i = service.returnPrimitiveInt(2);
	Assert.assertEquals(2, i);
}
 
Example #15
Source File: HttpClientTest.java    From jsonrpc4j with MIT License 4 votes vote down vote up
@Test
public void testGZIPRequest() throws MalformedURLException {
	service = ProxyUtil.createClientProxy(this.getClass().getClassLoader(), FakeServiceInterface.class, getHttpClient(true, false));
	int i = service.returnPrimitiveInt(2);
	Assert.assertEquals(2, i);
}
 
Example #16
Source File: HttpCodeTest.java    From jsonrpc4j with MIT License 4 votes vote down vote up
@Test
public void http200() throws MalformedURLException {
	FakeServiceInterface service = ProxyUtil.createClientProxy(FakeServiceInterface.class, getClient());
	service.doSomething();
}
 
Example #17
Source File: SimpleTest.java    From jsonrpc4j with MIT License 4 votes vote down vote up
@Before
@Override
public void setup() throws Exception {
	super.setup();
	service = ProxyUtil.createClientProxy(FakeServiceInterface.class, getClient());
}
 
Example #18
Source File: ApplicationConfig.java    From tools-journey with Apache License 2.0 4 votes vote down vote up
@Bean
public ExampleClientAPI exampleClientAPI(JsonRpcHttpClient jsonRpcHttpClient) {
    return ProxyUtil.createClientProxy(getClass().getClassLoader(), ExampleClientAPI.class, jsonRpcHttpClient);
}