protected void incrementFailureCounter(String methodName, String originService, String originMethod) { handlerMetrics.incrementFailureCounter(methodName, originService, originMethod); }
protected GoTimer getMethodTimer(String methodName, String originService, String originMethod) { return handlerMetrics.getMethodTimer(methodName, originService, originMethod); }
protected void incrementSuccessCounter(String methodName, String originService, String originMethod) { handlerMetrics.incrementSuccessCounter(methodName, originService, originMethod); }
@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); } }
@Before public void setup() throws RpcCallException { handlerDictionary = new MethodHandlerDictionary(); handlerDictionary.put("a", null); ServiceMethodHandlerUnderTest mockHandlerThrowsRpcCallEx = new ServiceMethodHandlerUnderTest(); handlerDictionary.put("jsonRpcWithException", mockHandlerThrowsRpcCallEx); metricRegistry = mock(MetricRegistry.class); when(metricRegistry.counter(anyString())).thenReturn(mock(Counter.class)); when(metricRegistry.timer(anyString())).thenReturn(mock(Timer.class)); handlerMetrics = mock(RpcHandlerMetrics.class); when(handlerMetrics.getMethodTimer(any(), any(), any())).thenReturn(mock(GoTimer.class)); servlet = new JsonHandler(handlerDictionary, metricRegistry, handlerMetrics, new ServiceProperties(), null); }
private RpcEnvelope.Request readRpcEnvelope(ServletInputStream in) throws Exception { byte chunkSize[] = new byte[4]; in.read(chunkSize); int size = Ints.fromByteArray(chunkSize); if (size <= 0 || size > ProtobufUtil.MAX_HEADER_CHUNK_SIZE) { String message = "Invalid header chunk size: " + size; throw new RpcReadException(chunkSize, in, message); } byte headerData[] = readyFully(in, size); RpcEnvelope.Request rpcRequest = RpcEnvelope.Request.parseFrom(headerData); return rpcRequest; }
@Test public void parseRpcRequest_NoMethodNameGiven_ThrowIllegalArgumentExecption() { // given final String json = "{\"service\":\"x\",\"params\":[{\"data\":\"\"}],\"id\":\"dead-beef\"}"; // when Throwable thrown = catchThrowable(() -> { servlet.parseRpcRequest(json); }); // then assertThat(thrown).isInstanceOf(IllegalArgumentException.class); assertThat(thrown).hasMessage("Missing method name"); }
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); } }
public void incrementSuccessCounter(String methodName, String originService, String originMethod) { GoCounter counter = getOrCreateCounter(methodName, originService, originMethod); counter.incSuccess(); }
public Map<String, String> gatherHttpHeaders(HttpServletRequest request) { return super.gatherHttpHeaders(request); } }
@Test public void testRequestExtractionBody() throws IOException { testBodyReading("beg", "inning"); testBodyReading("", "inning"); testBodyReading("beg", ""); }
public void bootstrapComplete() throws InterruptedException { startupComplete.set(true); injector.getInstance(RpcServlet.class).serveRequests(); injector.getInstance(ReadinessCheckServer.class).serveRequests(); jettyServer.join(); }
private JsonObject getCheckDetail(HealthCheck check) { JsonObject retval = new JsonObject(); retval.add("name", new JsonPrimitive(check.getName())); retval.add("status", new JsonPrimitive(translateStatus(check.getStatus()))); String message = check.getMessage(); retval.add("reason", new JsonPrimitive(message == null ? "" : message)); return retval; }
@Test public void doHead() throws Exception { // given // when cut.doHead(mockHttpServletRequest, mockHttpServletResponse); }
@Before public void setup() { cut = new RpcServlet(mockJsonHandler, mockProtobufHandler); }
@Test public void doPost_CannotServe_ServiceUnavailable() throws Exception { // given cut.canServeRequests.set(false); // when cut.doPost(mockHttpServletRequest, mockHttpServletResponse); // then verify(mockHttpServletResponse).setStatus(Mockito.eq(HttpServletResponse.SC_SERVICE_UNAVAILABLE)); verifyNoMoreInteractions(mockJsonHandler, mockProtobufHandler); }
private Message readRpcBody(ServletInputStream in, Class<? extends Message> requestClass) throws Exception { byte chunkSize[] = new byte[4]; in.read(chunkSize); int size = Ints.fromByteArray(chunkSize); if (size == 0) { return ProtobufUtil.newEmptyMessage(requestClass); } if (size > ProtobufUtil.MAX_BODY_CHUNK_SIZE) { String message = "Invalid body chunk size: " + size; throw new RpcReadException(chunkSize, in, message); } byte bodyData[] = readyFully(in, size); Message pbRequest = ProtobufUtil.byteArrayToProtobuf(bodyData, requestClass); return pbRequest; }
@Test public void testParseRpcRequestNoId() { String input = "{\"service\":\"x\",\"method\":\"a\",\"params\":[{\"data\":\"\"}]}"; JsonRpcRequest request = servlet.parseRpcRequest(input); assertThat(request.getId()).isNull(); }
public void incrementFailureCounter(String methodName, String originService, String originMethod) { GoCounter counter = getOrCreateCounter(methodName, originService, originMethod); counter.incFailure(); }
@Test public void testParseRpcRequestNullId() { String input = "{\"service\":\"x\",\"method\":\"a\",\"params\":[{\"data\":\"\"}],\"id\":null}"; JsonRpcRequest request = servlet.parseRpcRequest(input); assertThat(request.getId()).isInstanceOf(JsonNull.class); }