com.googlecode.jsonrpc4j.JsonRpcServer Java Examples
The following examples show how to use
com.googlecode.jsonrpc4j.JsonRpcServer.
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: JsonRpcServerTest.java From jsonrpc4j with MIT License | 8 votes |
@Test public void testGzipRequest() throws IOException { MockHttpServletRequest request = new MockHttpServletRequest("POST", "/test-post"); request.addHeader(CONTENT_ENCODING, "gzip"); request.setContentType("application/json"); byte[] bytes = "{\"jsonrpc\":\"2.0\",\"id\":123,\"method\":\"testMethod\",\"params\":[\"Whir?inaki\"]}".getBytes(StandardCharsets.UTF_8); ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream gos = new GZIPOutputStream(baos); gos.write(bytes); gos.close(); request.setContent(baos.toByteArray()); MockHttpServletResponse response = new MockHttpServletResponse(); jsonRpcServer = new JsonRpcServer(Util.mapper, mockService, ServiceInterface.class, true); jsonRpcServer.handle(request, response); String responseContent = new String(response.getContentAsByteArray(), StandardCharsets.UTF_8); Assert.assertEquals(responseContent, "{\"jsonrpc\":\"2.0\",\"id\":123,\"result\":null}\n"); Assert.assertNull(response.getHeader(CONTENT_ENCODING)); }
Example #2
Source File: JsonRpcProtocol.java From dubbox with Apache License 2.0 | 6 votes |
protected <T> Runnable doExport(T impl, Class<T> type, URL url) throws RpcException { String addr = url.getIp() + ":" + url.getPort(); HttpServer server = serverMap.get(addr); if (server == null) { server = httpBinder.bind(url, new InternalHandler(url.getParameter("cors", false))); serverMap.put(addr, server); } final String path = url.getAbsolutePath(); JsonRpcServer skeleton = new JsonRpcServer(impl, type); skeletonMap.put(path, skeleton); return new Runnable() { public void run() { skeletonMap.remove(path); } }; }
Example #3
Source File: JsonRpcProtocol.java From dubbo-rpc-jsonrpc with Apache License 2.0 | 6 votes |
public void handle(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String uri = request.getRequestURI(); JsonRpcServer skeleton = skeletonMap.get(uri); if (cors) { response.setHeader(ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*"); response.setHeader(ACCESS_CONTROL_ALLOW_METHODS_HEADER, "POST"); response.setHeader(ACCESS_CONTROL_ALLOW_HEADERS_HEADER, "*"); } if (request.getMethod().equalsIgnoreCase("OPTIONS")) { response.setStatus(200); } else if (request.getMethod().equalsIgnoreCase("POST")) { RpcContext.getContext().setRemoteAddress(request.getRemoteAddr(), request.getRemotePort()); try { skeleton.handle(request.getInputStream(), response.getOutputStream()); } catch (Throwable e) { throw new ServletException(e); } } else { response.setStatus(500); } }
Example #4
Source File: JsonRpcProtocol.java From dubbo-rpc-jsonrpc with Apache License 2.0 | 6 votes |
protected <T> Runnable doExport(T impl, Class<T> type, URL url) throws RpcException { String addr = url.getIp() + ":" + url.getPort(); HttpServer server = serverMap.get(addr); if (server == null) { server = httpBinder.bind(url, new InternalHandler(url.getParameter("cors", false))); serverMap.put(addr, server); } final String path = url.getAbsolutePath(); JsonRpcServer skeleton = new JsonRpcServer(impl, type); skeletonMap.put(path, skeleton); return new Runnable() { public void run() { skeletonMap.remove(path); } }; }
Example #5
Source File: JsonRpcProtocol.java From dubbox with Apache License 2.0 | 6 votes |
public void handle(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String uri = request.getRequestURI(); JsonRpcServer skeleton = skeletonMap.get(uri); if (cors) { response.setHeader(ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*"); response.setHeader(ACCESS_CONTROL_ALLOW_METHODS_HEADER, "POST"); response.setHeader(ACCESS_CONTROL_ALLOW_HEADERS_HEADER, "*"); } if (request.getMethod().equalsIgnoreCase("OPTIONS")) { response.setStatus(200); } else if (request.getMethod().equalsIgnoreCase("POST")) { RpcContext.getContext().setRemoteAddress(request.getRemoteAddr(), request.getRemotePort()); try { skeleton.handle(request.getInputStream(), response.getOutputStream()); } catch (Throwable e) { throw new ServletException(e); } } else { response.setStatus(500); } }
Example #6
Source File: JsonRpcServerTest.java From jsonrpc4j with MIT License | 6 votes |
@Test public void testGzipResponse() throws IOException { MockHttpServletRequest request = new MockHttpServletRequest("POST", "/test-post"); request.addHeader(ACCEPT_ENCODING, "gzip"); request.setContentType("application/json"); request.setContent("{\"jsonrpc\":\"2.0\",\"id\":123,\"method\":\"testMethod\",\"params\":[\"Whir?inaki\"]}".getBytes(StandardCharsets.UTF_8)); MockHttpServletResponse response = new MockHttpServletResponse(); jsonRpcServer = new JsonRpcServer(Util.mapper, mockService, ServiceInterface.class, true); jsonRpcServer.handle(request, response); byte[] compressed = response.getContentAsByteArray(); String sb = getCompressedResponseContent(compressed); Assert.assertEquals(sb, "{\"jsonrpc\":\"2.0\",\"id\":123,\"result\":null}"); Assert.assertEquals("gzip", response.getHeader(CONTENT_ENCODING)); }
Example #7
Source File: JsonRpcServerTest.java From jsonrpc4j with MIT License | 6 votes |
@Test public void testGzipResponseMultipleAcceptEncoding() throws IOException { MockHttpServletRequest request = new MockHttpServletRequest("POST", "/test-post"); request.addHeader(ACCEPT_ENCODING, "gzip,deflate"); request.setContentType("application/json"); request.setContent("{\"jsonrpc\":\"2.0\",\"id\":123,\"method\":\"testMethod\",\"params\":[\"Whir?inaki\"]}".getBytes(StandardCharsets.UTF_8)); MockHttpServletResponse response = new MockHttpServletResponse(); jsonRpcServer = new JsonRpcServer(Util.mapper, mockService, ServiceInterface.class, true); jsonRpcServer.handle(request, response); byte[] compressed = response.getContentAsByteArray(); String sb = getCompressedResponseContent(compressed); Assert.assertEquals(sb, "{\"jsonrpc\":\"2.0\",\"id\":123,\"result\":null}"); Assert.assertEquals("gzip", response.getHeader(CONTENT_ENCODING)); }
Example #8
Source File: JsonRpcServerTest.java From jsonrpc4j with MIT License | 6 votes |
@Test public void testCorruptRequest() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest("POST", "/test-post"); request.setContentType("application/json"); request.setContent("{NOT JSON}".getBytes(StandardCharsets.UTF_8)); MockHttpServletResponse response = new MockHttpServletResponse(); jsonRpcServer = new JsonRpcServer(Util.mapper, mockService, ServiceInterface.class, true); jsonRpcServer.handle(request, response); String content = response.getContentAsString(); Assert.assertEquals(content, "{\"jsonrpc\":\"2.0\",\"id\":\"null\"," + "\"error\":{\"code\":-32700,\"message\":\"JSON parse error\"}}\n"); }
Example #9
Source File: JsonRpcServerTest.java From jsonrpc4j with MIT License | 6 votes |
@Test public void testGzipRequestAndResponse() throws IOException { MockHttpServletRequest request = new MockHttpServletRequest("POST", "/test-post"); request.addHeader(CONTENT_ENCODING, "gzip"); request.addHeader(ACCEPT_ENCODING, "gzip"); request.setContentType("application/json"); byte[] bytes = "{\"jsonrpc\":\"2.0\",\"id\":123,\"method\":\"testMethod\",\"params\":[\"Whir?inaki\"]}".getBytes(StandardCharsets.UTF_8); ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream gos = new GZIPOutputStream(baos); gos.write(bytes); gos.close(); request.setContent(baos.toByteArray()); MockHttpServletResponse response = new MockHttpServletResponse(); jsonRpcServer = new JsonRpcServer(Util.mapper, mockService, ServiceInterface.class, true); jsonRpcServer.handle(request, response); byte[] compressed = response.getContentAsByteArray(); String sb = getCompressedResponseContent(compressed); Assert.assertEquals(sb, "{\"jsonrpc\":\"2.0\",\"id\":123,\"result\":null}"); Assert.assertEquals("gzip", response.getHeader(CONTENT_ENCODING)); }
Example #10
Source File: DefaultHttpStatusCodeProviderTest.java From jsonrpc4j with MIT License | 5 votes |
public static void assertHttpStatusCodeForJsonRpcRequest(InputStream message, int expectedCode, JsonRpcServer server) throws Exception { MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpServletResponse res = new MockHttpServletResponse(); req.setMethod(HttpMethod.POST.name()); req.setContent(convertInputStreamToByteArray(message)); server.handle(req, res); Assert.assertEquals(expectedCode, res.getStatus()); }
Example #11
Source File: JSONRPCImporterTest.java From fuchsia with Apache License 2.0 | 5 votes |
@Override protected <T> ImportDeclaration createImportDeclaration(String endpointId, Class<T> klass, T object) { //A JsonServlet must be registered final JsonRpcServer jsonRpcServer = new JsonRpcServer(object, klass); httpServer.createContext(SERVLET_NAME + "/" + endpointId, new HttpHandler() { public void handle(HttpExchange httpExchange) throws IOException { // Get InputStream for reading the request body. // After reading the request body, the stream is close. InputStream is = httpExchange.getRequestBody(); // Get OutputStream to send the response body. // When the response body has been written, the stream must be closed to terminate the exchange. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); jsonRpcServer.handle(is, byteArrayOutputStream); byteArrayOutputStream.close(); int size = byteArrayOutputStream.size(); // send response header httpExchange.sendResponseHeaders(HttpStatus.SC_OK, size); // write response to real outputStream OutputStream realOs = httpExchange.getResponseBody(); realOs.write(byteArrayOutputStream.toByteArray(), 0, size); realOs.close(); } }); // Build associated ImportDeclaration Map<String, Object> props = new HashMap<String, Object>(); props.put(ID, endpointId); props.put(URL, "http://localhost:" + HTTP_PORT + SERVLET_NAME + "/" + endpointId); props.put(SERVICE_CLASS, klass.getName()); props.put(CONFIGS, "jsonrpc"); return ImportDeclarationBuilder.fromMetadata(props).build(); }
Example #12
Source File: Util.java From jsonrpc4j with MIT License | 5 votes |
@SuppressWarnings("serial") private static HashMap<String, Object> makeJsonRpcRequestObject(final Object id, final String methodName, final Object params) { return new HashMap<String, Object>() { { if (id != null) put(JsonRpcBasicServer.ID, id); put(JsonRpcBasicServer.JSONRPC, JsonRpcServer.VERSION); if (methodName != null) put(JsonRpcBasicServer.METHOD, methodName); if (params != null) put(JsonRpcBasicServer.PARAMS, params); } }; }
Example #13
Source File: JettyServer.java From jsonrpc4j with MIT License | 5 votes |
@Override public void init() { try { final Class<?> aClass = Class.forName(getInitParameter("class")); final Object instance = aClass.getConstructor().newInstance(); jsonRpcServer = new JsonRpcServer(instance); jsonRpcServer.setErrorResolver(AnnotationsErrorResolver.INSTANCE); } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | InvocationTargetException | IllegalAccessException e) { e.printStackTrace(); } }
Example #14
Source File: JsonRpcServerTest.java From jsonrpc4j with MIT License | 5 votes |
@Before public void setup() { jsonRpcServer = new JsonRpcServer(Util.mapper, mockService, ServiceInterface.class); jsonRpcServer.setInterceptorList(new ArrayList<JsonRpcInterceptor>() {{ add(mockInterceptor); }}); byteArrayOutputStream = new ByteArrayOutputStream(); }
Example #15
Source File: HttpStatusCodeProviderTest.java From jsonrpc4j with MIT License | 5 votes |
@Before public void setUp() throws Exception { jsonRpcServer = new JsonRpcServer(mapper, mockService, JsonRpcBasicServerTest.ServiceInterface.class); httpStatusCodeProvider = new HttpStatusCodeProvider() { @Override public int getHttpStatusCode(int resultCode) { if (resultCode == PARSE_ERROR.code) { return 1002; } else if (resultCode == INVALID_REQUEST.code) { return 1001; } else if (resultCode == METHOD_NOT_FOUND.code) { return 1003; } else if (resultCode == METHOD_PARAMS_INVALID.code) { return 1004; } else if (resultCode == INTERNAL_ERROR.code) { return 1007; } else if (resultCode == ERROR_NOT_HANDLED.code) { return 1006; } else if (resultCode == BULK_ERROR.code) { return 1005; } else { return 1000; } } @Override public Integer getJsonRpcCode(int httpStatusCode) { return null; } }; jsonRpcServer.setHttpStatusCodeProvider(httpStatusCodeProvider); }
Example #16
Source File: DefaultHttpStatusCodeProviderTest.java From jsonrpc4j with MIT License | 4 votes |
@Before public void setUp() throws Exception { jsonRpcServer = new JsonRpcServer(mapper, mockService, JsonRpcBasicServerTest.ServiceInterface.class); }
Example #17
Source File: DefaultHttpStatusCodeProviderTest.java From jsonrpc4j with MIT License | 4 votes |
@Test public void http500ForErrorNotHandled() throws Exception { JsonRpcServer server = new JsonRpcServer(mapper, mockService, JsonRpcErrorsTest.ServiceInterfaceWithoutAnnotation.class); assertHttpStatusCodeForJsonRpcRequest(messageWithListParamsStream(1, "testMethod"), 500, server); }
Example #18
Source File: HttpStatusCodeProviderTest.java From jsonrpc4j with MIT License | 4 votes |
@Test public void http1006ForErrorNotHandled() throws Exception { JsonRpcServer server = new JsonRpcServer(mapper, mockService, JsonRpcErrorsTest.ServiceInterfaceWithoutAnnotation.class); server.setHttpStatusCodeProvider(httpStatusCodeProvider); assertHttpStatusCodeForJsonRpcRequest(messageWithListParamsStream(1, "testMethod"), 1006, server); }
Example #19
Source File: LocalThreadServer.java From jsonrpc4j with MIT License | 4 votes |
public LocalThreadServer(Object service, final Class<?> remoteInterface) { this.remoteInterface = remoteInterface; this.service = service; jsonRpcServer = new JsonRpcServer(new ObjectMapper(), service, remoteInterface); start(); }
Example #20
Source File: AutomatorHttpServer.java From android-uiautomator-server with MIT License | 4 votes |
public void route(String uri, JsonRpcServer rpc) { router.put(uri, rpc); }
Example #21
Source File: JSONRPCImporterTest.java From fuchsia with Apache License 2.0 | 4 votes |
@Test public void useDeclaration() throws ServletException, NamespaceException, BinderException { ImportDeclaration declaration = spy(getValidDeclarations().get(0)); JsonRpcServer jsonRpcServer = new JsonRpcServer(serviceToBeExported, ServiceForExportation.class); Servlet gs = new RPCServlet(jsonRpcServer); Dictionary<String, Object> dic = new Hashtable<String, Object>(); http.registerServlet("/ping", gs, dic, null); fuchsiaDeclarationBinder.useDeclaration(declaration); verify(declaration, times(1)).handle(fuchsiaDeclarationBinderServiceReference); Map<String, ComponentInstance> registrations = field("registrations").ofType(Map.class).in(fuchsiaDeclarationBinder).get(); Map<String, JsonRpcHttpClient> clients = field("clients").ofType(Map.class).in(fuchsiaDeclarationBinder).get(); Assert.assertEquals(1, registrations.size()); Assert.assertEquals(1, clients.size()); }
Example #22
Source File: JSONRPCImporterTest.java From fuchsia with Apache License 2.0 | 4 votes |
@Test public void remoteInvocationCustomProxy() throws ServletException, NamespaceException, BinderException { ImportDeclaration declaration = getValidDeclarations().get(0); JsonRpcServer jsonRpcServer = new JsonRpcServer(serviceToBeExported, ServiceForExportation.class); Servlet gs = new RPCServlet(jsonRpcServer); Dictionary<String, Object> emptyDictionary = new Hashtable<String, Object>(); http.registerServlet("/ping", gs, emptyDictionary, null); fuchsiaDeclarationBinder.useDeclaration(declaration); verifyRemoteInvocation(serviceToBeExported, proxyRegistered); }
Example #23
Source File: JSONRPCImporterTest.java From fuchsia with Apache License 2.0 | 4 votes |
public RPCServlet(JsonRpcServer jsonRpcServer) { this.jsonRpcServer = jsonRpcServer; }
Example #24
Source File: JSONRPCExporter.java From fuchsia with Apache License 2.0 | 4 votes |
public RPCServlet(JsonRpcServer jsonRpcServer) { this.jsonRpcServer = jsonRpcServer; }
Example #25
Source File: JSONRPCImporterTest.java From fuchsia with Apache License 2.0 | 3 votes |
@Test public void useDeclarationDefaultProxy() throws ServletException, NamespaceException, BinderException { Map<String, Object> metadata = new HashMap<String, Object>(); metadata.put(Constants.ID, "my-id"); metadata.put(Constants.URL, "http://localhost:" + HTTP_PORT + "/ping"); //metadata.put(Constants.SERVICE_CLASS,ServiceForExportation.class.getName()); final ImportDeclaration declaration = spy(ImportDeclarationBuilder.fromMetadata(metadata).build()); declaration.bind(fuchsiaDeclarationBinderServiceReference); JsonRpcServer jsonRpcServer = new JsonRpcServer(serviceToBeExported, ServiceForExportation.class); Servlet gs = new RPCServlet(jsonRpcServer); Dictionary<String, Object> dic = new Hashtable<String, Object>(); http.registerServlet("/ping", gs, dic, null); fuchsiaDeclarationBinder.useDeclaration(declaration); verify(declaration, times(1)).handle(fuchsiaDeclarationBinderServiceReference); Map<String, ComponentInstance> registrations = field("componentInstances").ofType(Map.class).in(fuchsiaDeclarationBinder).get(); Assert.assertEquals(1, registrations.size()); }
Example #26
Source File: JSONRPCImporterTest.java From fuchsia with Apache License 2.0 | 3 votes |
@Test public void denyDeclarationCustomProxy() throws ServletException, NamespaceException, BinderException { ImportDeclaration declaration = spy(getValidDeclarations().get(0)); JsonRpcServer jsonRpcServer = new JsonRpcServer(serviceToBeExported, ServiceForExportation.class); Servlet gs = new RPCServlet(jsonRpcServer); Dictionary<String, Object> dic = new Hashtable<String, Object>(); http.registerServlet("/ping", gs, dic, null); fuchsiaDeclarationBinder.useDeclaration(declaration); verify(declaration, times(1)).handle(fuchsiaDeclarationBinderServiceReference); Map<String, ComponentInstance> registrations = field("registrations").ofType(Map.class).in(fuchsiaDeclarationBinder).get(); Map<String, JsonRpcHttpClient> clients = field("clients").ofType(Map.class).in(fuchsiaDeclarationBinder).get(); Assert.assertEquals(1, registrations.size()); Assert.assertEquals(1, clients.size()); fuchsiaDeclarationBinder.denyDeclaration(declaration); verify(declaration, times(1)).unhandle(fuchsiaDeclarationBinderServiceReference); Assert.assertEquals(0, registrations.size()); Assert.assertEquals(0, clients.size()); }
Example #27
Source File: JSONRPCImporterTest.java From fuchsia with Apache License 2.0 | 3 votes |
@Test public void denyDeclarationDefaultProxy() throws ServletException, NamespaceException, BinderException { Map<String, Object> metadata = new HashMap<String, Object>(); metadata.put(Constants.ID, "my-id"); metadata.put(Constants.URL, "http://localhost:" + HTTP_PORT + "/ping"); //metadata.put(Constants.SERVICE_CLASS,ServiceForExportation.class.getName()); ImportDeclaration declaration = spy(ImportDeclarationBuilder.fromMetadata(metadata).build()); declaration.bind(fuchsiaDeclarationBinderServiceReference); JsonRpcServer jsonRpcServer = new JsonRpcServer(serviceToBeExported, ServiceForExportation.class); Servlet gs = new RPCServlet(jsonRpcServer); Dictionary<String, Object> dic = new Hashtable<String, Object>(); http.registerServlet("/ping", gs, dic, null); fuchsiaDeclarationBinder.useDeclaration(declaration); verify(declaration, times(1)).handle(fuchsiaDeclarationBinderServiceReference); Map<String, ComponentInstance> componentInstances = field("componentInstances").ofType(Map.class).in(fuchsiaDeclarationBinder).get(); Assert.assertEquals(1, componentInstances.size()); fuchsiaDeclarationBinder.denyDeclaration(declaration); verify(declaration, times(1)).unhandle(fuchsiaDeclarationBinderServiceReference); Assert.assertEquals(0, componentInstances.size()); }
Example #28
Source File: JSONRPCImporterTest.java From fuchsia with Apache License 2.0 | 3 votes |
@Test public void gracefulStop() throws BinderException, ServletException, NamespaceException, MissingHandlerException, UnacceptableConfiguration, ConfigurationException { ImportDeclaration declaration = getValidDeclarations().get(0); Map<String, Object> metadata = new HashMap<String, Object>(); metadata.put(Constants.ID, "my-id"); metadata.put(Constants.URL, "http://localhost:" + HTTP_PORT + "/ping"); ImportDeclaration declarationForDefaultProxy = spy(ImportDeclarationBuilder.fromMetadata(metadata).build()); declarationForDefaultProxy.bind(fuchsiaDeclarationBinderServiceReference); JsonRpcServer jsonRpcServer = new JsonRpcServer(serviceToBeExported, ServiceForExportation.class); Servlet gs = new RPCServlet(jsonRpcServer); Dictionary<String, Object> emptyDictionary = new Hashtable<String, Object>(); http.registerServlet("/ping", gs, emptyDictionary, null); fuchsiaDeclarationBinder.useDeclaration(declaration); fuchsiaDeclarationBinder.useDeclaration(declarationForDefaultProxy); verifyRemoteInvocation(serviceToBeExported, proxyRegistered); Map<String, ComponentInstance> registrations = field("registrations").ofType(Map.class).in(fuchsiaDeclarationBinder).get(); Map<String, ComponentInstance> componentInstances = field("componentInstances").ofType(Map.class).in(fuchsiaDeclarationBinder).get(); Assert.assertEquals(1, registrations.size()); Assert.assertEquals(1, componentInstances.size()); method("stop").in(fuchsiaDeclarationBinder).invoke(); Assert.assertEquals(0, registrations.size()); Assert.assertEquals(0, componentInstances.size()); }