@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if (! canServeRequests.get()) { logger.info("Not processing request, service not ready yet"); resp.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE); return; } try { //TODO: refactor and clean up. we should use codecs to abstract the encoding/decoding so that // there is one handler class without a bunch of duplication String contentType = req.getHeader(CONTENT_TYPE); logger.debug("Request content-type: {}", contentType); if (isProtobuf(contentType)) { protobufHandler.doPost(req, resp); } else if (isJson(contentType)) { jsonRpcHandler.doPost(req, resp); } else { resp.setStatus(HttpServletResponse.SC_BAD_REQUEST); } } catch (Exception ex) { logger.error("Uncaught exception handling POST", ex); } }
@Test public void testRpcCallExceptionJsonRpc() throws IOException { String input = "{\"service\":\"x\",\"method\":\"jsonRpcWithException\",\"params\":[{}],\"id\":\"dead-beef\"}"; HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(MockHttpServletResponse.class); BufferedReader readerFromRequest = new BufferedReader(new StringReader(input)); when(request.getReader()).thenReturn(readerFromRequest); CharArrayWriter charArryWriter = new CharArrayWriter(512); PrintWriter writer = new PrintWriter(charArryWriter); when(response.getWriter()).thenReturn(writer); doCallRealMethod().when(response).setStatus(anyInt()); doCallRealMethod().when(response).getStatus(); servlet.doPost(request, response); String responseAsString = charArryWriter.toString(); LOGGER.debug(responseAsString); assertThat(responseAsString).contains("no p4s5!"); assertThat(responseAsString).matches(Pattern.compile(".*retriable.+true.*")); assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_OK); }
private void processRequest(Socket socket) throws IOException { MockHttpServletRequest request = new MockHttpServletRequest(socket); MockHttpServletResponse response = new MockHttpServletResponse(socket); try { String contentType = request.getContentType(); if (isProtobuf(contentType)) { protobufHandler.doPost(request, response); } else { jsonRpcHandler.doPost(request, response); } response.complete(); } catch (SocketException sockEx) { logger.warn("Caught SocketException processing request"); } catch (Exception ex) { logger.error("Uncaught exception handling POST", ex); } }
@Test public void doPost_NotValidJson_ErrorMessageBadRequest() throws IOException { // given final String json = "xxxx"; HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(MockHttpServletResponse.class); BufferedReader readerFromRequest = new BufferedReader(new StringReader(json)); when(request.getReader()).thenReturn(readerFromRequest); CharArrayWriter charArryWriter = new CharArrayWriter(512); PrintWriter writer = new PrintWriter(charArryWriter); when(response.getWriter()).thenReturn(writer); doCallRealMethod().when(response).setStatus(anyInt()); doCallRealMethod().when(response).getStatus(); // when servlet.doPost(request, response); // then final String responseAsString = charArryWriter.toString(); LOGGER.debug(responseAsString); assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_OK); }
@Test public void doPost_NotValidJson_ErrorMessageBadRequestWithHttpExposed() throws IOException { // given final String json = "xxxx"; HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(MockHttpServletResponse.class); BufferedReader readerFromRequest = new BufferedReader(new StringReader(json)); when(request.getReader()).thenReturn(readerFromRequest); CharArrayWriter charArryWriter = new CharArrayWriter(512); PrintWriter writer = new PrintWriter(charArryWriter); when(response.getWriter()).thenReturn(writer); doCallRealMethod().when(response).setStatus(anyInt()); doCallRealMethod().when(response).getStatus(); // when ServiceProperties props = new ServiceProperties(); props.addProperty(FeatureFlags.FLAG_EXPOSE_ERRORS_HTTP, "true"); servlet = new JsonHandler(handlerDictionary, metricRegistry, handlerMetrics, props, null); servlet.doPost(request, response); // then final String responseAsString = charArryWriter.toString(); LOGGER.debug(responseAsString); assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_BAD_REQUEST); }
@Test public void testRpcCallExceptionJsonRpcWithHttpExposed() throws IOException { String input = "{\"service\":\"x\",\"method\":\"jsonRpcWithException\",\"params\":[{}],\"id\":\"dead-beef\"}"; HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(MockHttpServletResponse.class); BufferedReader readerFromRequest = new BufferedReader(new StringReader(input)); when(request.getReader()).thenReturn(readerFromRequest); CharArrayWriter charArryWriter = new CharArrayWriter(512); PrintWriter writer = new PrintWriter(charArryWriter); when(response.getWriter()).thenReturn(writer); doCallRealMethod().when(response).setStatus(anyInt()); doCallRealMethod().when(response).getStatus(); ServiceProperties props = new ServiceProperties(); props.addProperty(FeatureFlags.FLAG_EXPOSE_ERRORS_HTTP, "true"); servlet = new JsonHandler(handlerDictionary, metricRegistry, handlerMetrics, props, null); servlet.doPost(request, response); String responseAsString = charArryWriter.toString(); LOGGER.debug(responseAsString); assertThat(responseAsString).contains("no p4s5!"); assertThat(responseAsString).matches(Pattern.compile(".*retriable.+true.*")); assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); }