Java Code Examples for com.googlecode.jsonrpc4j.JsonRpcServer#handle()

The following examples show how to use com.googlecode.jsonrpc4j.JsonRpcServer#handle() . 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 vote down vote up
@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 vote down vote up
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 3
Source File: JsonRpcProtocol.java    From dubbo-rpc-jsonrpc with Apache License 2.0 6 votes vote down vote up
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: JsonRpcServerTest.java    From jsonrpc4j with MIT License 6 votes vote down vote up
@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 5
Source File: JsonRpcServerTest.java    From jsonrpc4j with MIT License 6 votes vote down vote up
@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 6
Source File: JsonRpcServerTest.java    From jsonrpc4j with MIT License 6 votes vote down vote up
@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 7
Source File: JsonRpcServerTest.java    From jsonrpc4j with MIT License 6 votes vote down vote up
@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 8
Source File: DefaultHttpStatusCodeProviderTest.java    From jsonrpc4j with MIT License 5 votes vote down vote up
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());
}