本文整理汇总了Java中com.googlecode.jsonrpc4j.JsonRpcServer.handle方法的典型用法代码示例。如果您正苦于以下问题:Java JsonRpcServer.handle方法的具体用法?Java JsonRpcServer.handle怎么用?Java JsonRpcServer.handle使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.googlecode.jsonrpc4j.JsonRpcServer
的用法示例。
在下文中一共展示了JsonRpcServer.handle方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: handle
import com.googlecode.jsonrpc4j.JsonRpcServer; //导入方法依赖的package包/类
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);
}
}
示例2: testGzipResponse
import com.googlecode.jsonrpc4j.JsonRpcServer; //导入方法依赖的package包/类
@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));
}
示例3: testCorruptRequest
import com.googlecode.jsonrpc4j.JsonRpcServer; //导入方法依赖的package包/类
@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");
}
示例4: testGzipRequest
import com.googlecode.jsonrpc4j.JsonRpcServer; //导入方法依赖的package包/类
@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));
}
示例5: testGzipRequestAndResponse
import com.googlecode.jsonrpc4j.JsonRpcServer; //导入方法依赖的package包/类
@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));
}
示例6: assertHttpStatusCodeForJsonRpcRequest
import com.googlecode.jsonrpc4j.JsonRpcServer; //导入方法依赖的package包/类
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());
}