/** * {@inheritDoc} */ public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { jsonRpcServer.handle(request, response); response.getOutputStream().flush(); }
jsonRpcServer = new JsonRpcServer(objectMapper, service); jsonRpcServer.setErrorResolver(errorResolver); jsonRpcServer.setBackwardsCompatible(backwardsCompatible); jsonRpcServer.setRethrowExceptions(rethrowExceptions); jsonRpcServer.setAllowExtraParams(allowExtraParams); jsonRpcServer.setAllowLessParams(allowLessParams); jsonRpcServer.setInvocationListener(invocationListener); jsonRpcServer.setHttpStatusCodeProvider(httpStatusCodeProvider); jsonRpcServer.setConvertedParameterTransformer(convertedParameterTransformer); jsonRpcServer.setShouldLogInvocationErrors(shouldLogInvocationErrors); jsonRpcServer.setContentType(contentType);
private int handleRequest0(InputStream input, OutputStream output, String contentEncoding, HttpServletResponse response, ByteArrayOutputStream byteOutput) throws IOException { int result; boolean canGzipResponse = contentEncoding != null && GZIP.equalsIgnoreCase(contentEncoding); // Use gzip if client's accept-encoding is set to gzip and gzipResponses is enabled. if (gzipResponses && canGzipResponse) { response.addHeader(CONTENT_ENCODING, GZIP); try (GZIPOutputStream gos = new GZIPOutputStream(byteOutput)) { result = handleRequest(input, gos); } } else { result = handleRequest(input, byteOutput); } return result; }
/** * Handles a portlet request. * * @param request the {@link ResourceRequest} * @param response the {@link ResourceResponse} * @throws IOException on error */ public void handle(ResourceRequest request, ResourceResponse response) throws IOException { logger.debug("Handing ResourceRequest {}", request.getMethod()); response.setContentType(contentType); InputStream input = getRequestStream(request); OutputStream output = response.getPortletOutputStream(); handleRequest(input, output); // fix to not flush within handleRequest() but outside so http status code can be set output.flush(); }
@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(); } }
@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"); }
@Before public void setUp() throws Exception { jsonRpcServer = new JsonRpcServer(mapper, mockService, JsonRpcBasicServerTest.ServiceInterface.class); httpStatusCodeProvider = new HttpStatusCodeProvider() { @Override jsonRpcServer.setHttpStatusCodeProvider(httpStatusCodeProvider);
@Test public void test_contentType() throws Exception { EasyMock.expect(mockService.testMethod("Whir?inaki")).andReturn("For?est"); EasyMock.replay(mockService); MockHttpServletRequest request = new MockHttpServletRequest("POST", "/test-post"); MockHttpServletResponse response = new MockHttpServletResponse(); request.setContentType("application/json"); request.setContent("{\"jsonrpc\":\"2.0\",\"id\":123,\"method\":\"testMethod\",\"params\":[\"Whir?inaki\"]}".getBytes(StandardCharsets.UTF_8)); jsonRpcServer.setContentType("flip/flop"); jsonRpcServer.handle(request, response); assertTrue("flip/flop".equals(response.getContentType())); checkSuccessfulResponse(response); }
@Before public void setup() { jsonRpcServer = new JsonRpcServer(Util.mapper, mockService, ServiceInterface.class); jsonRpcServer.setInterceptorList(new ArrayList<JsonRpcInterceptor>() {{ add(mockInterceptor); }}); byteArrayOutputStream = new ByteArrayOutputStream(); }
public LocalThreadServer(Object service, final Class<?> remoteInterface) { this.remoteInterface = remoteInterface; this.service = service; jsonRpcServer = new JsonRpcServer(new ObjectMapper(), service, remoteInterface); start(); }
@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)); }
@Test public void http1006ForErrorNotHandled() throws Exception { JsonRpcServer server = new JsonRpcServer(mapper, mockService, JsonRpcErrorsTest.ServiceInterfaceWithoutAnnotation.class); server.setHttpStatusCodeProvider(httpStatusCodeProvider); assertHttpStatusCodeForJsonRpcRequest(messageWithListParamsStream(1, "testMethod"), 1006, server); }
@Before public void setUp() throws Exception { jsonRpcServer = new JsonRpcServer(mapper, mockService, JsonRpcBasicServerTest.ServiceInterface.class); }
jsonRpcServer = new JsonRpcServer( objectMapper, null == getServiceInterface() ? getService() : getProxyForService(), getServiceInterface()); jsonRpcServer.setErrorResolver(errorResolver); jsonRpcServer.setBackwardsCompatible(backwardsCompatible); jsonRpcServer.setRethrowExceptions(rethrowExceptions); jsonRpcServer.setAllowExtraParams(allowExtraParams); jsonRpcServer.setAllowLessParams(allowLessParams); jsonRpcServer.setInvocationListener(invocationListener); jsonRpcServer.setHttpStatusCodeProvider(httpStatusCodeProvider); jsonRpcServer.setConvertedParameterTransformer(convertedParameterTransformer); jsonRpcServer.setShouldLogInvocationErrors(shouldLogInvocationErrors); jsonRpcServer.setContentType(contentType); jsonRpcServer.setInterceptorList(interceptorList);
@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)); }
/** * {@inheritDoc} */ public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { jsonRpcServer.handle(request, response); response.getOutputStream().flush(); }
@Test public void http500ForErrorNotHandled() throws Exception { JsonRpcServer server = new JsonRpcServer(mapper, mockService, JsonRpcErrorsTest.ServiceInterfaceWithoutAnnotation.class); assertHttpStatusCodeForJsonRpcRequest(messageWithListParamsStream(1, "testMethod"), 500, server); }
@Override public void run() { logger.debug("started local thread server for interface {} and handler {}", remoteInterface, service); while (isAlive.get()) { try { final int available = serverInput.available(); if (available > 0) { jsonRpcServer.handleRequest(serverInput, serverOutput); } } catch (IOException e) { e.printStackTrace(); } } }
@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)); }
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { jsonRpcServer.handle(request, response); } }