@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); } }
String methodName = null; Span span = null; Map<String, String> headers = gatherHttpHeaders(req); OrangeContext context = new OrangeContext(headers); HttpServletRequest blubb = new HttpServletRequestWrapper(req); rpcRequest = readRpcEnvelope(in); span = getSpan(methodName, headers, context); incrementFailureCounter(methodName, context.getRpcOriginService(), context.getRpcOriginMethod()); throw new IllegalArgumentException("Invalid method: " + Message pbRequest = readRpcBody(in, requestClass); GoTimer methodTimer = getMethodTimer(methodName, context.getRpcOriginService(), context.getRpcOriginMethod()); long startTime = methodTimer.start(); Message pbResponse = invokeHandlerChain(methodName, handler, pbRequest, context); sendSuccessfulResponse(resp, rpcRequest, pbResponse); incrementSuccessCounter(methodName, context.getRpcOriginService(), context.getRpcOriginMethod()); } catch (RpcCallException rpcEx) { sendErrorResponse(resp, rpcRequest, rpcEx.toString(), rpcEx.getCategory().getHttpStatus()); if (span != 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; }
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; }
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); } }