private void sendErrorResponse(HttpServletResponse resp, RpcEnvelope.Request rpcRequest, String message, int httpStatusCode) { if (rpcRequest != null) { try { if (FeatureFlags.shouldExposeErrorsToHttp(serviceProps)) { resp.setStatus(httpStatusCode); } else { resp.setStatus(HttpServletResponse.SC_OK); } if (message == null) { message = "null"; } RpcEnvelope.Response rpcResponse = RpcEnvelope.Response.newBuilder(). setServiceMethod(rpcRequest.getServiceMethod()). setSequenceNumber(rpcRequest.getSequenceNumber()). setError(message).build(); byte responseHeader[] = rpcResponse.toByteArray(); ServletOutputStream out = resp.getOutputStream(); out.write(Ints.toByteArray(responseHeader.length)); out.write(responseHeader); out.write(Ints.toByteArray(0)); //zero-length (no) body } catch (Exception ex) { logger.warn("Error writing error response", ex); } } }
private void initialize() { httpResponse = mock(ContentResponse.class); when(httpResponse.getHeaders()).thenReturn(new HttpFields()); when(httpResponse.getStatus()).thenReturn(200); try { RpcEnvelope.Response.Builder responseBuilder = RpcEnvelope.Response.newBuilder(); responseBuilder.setServiceMethod("Test.test"); if (requestsFail) { RpcCallException callException = new RpcCallException( RpcCallException.Category.InternalServerError, "requests fail!"); responseBuilder.setError(callException.toJson().toString()); } RpcEnvelope.Response rpcResponse = responseBuilder.build(); byte[] responseHeader = rpcResponse.toByteArray(); byte[] payload = FrameworkTest.Foobar.newBuilder().build().toByteArray(); ByteArrayOutputStream out = new ByteArrayOutputStream(); out.write(Ints.toByteArray(responseHeader.length)); out.write(responseHeader); out.write(Ints.toByteArray(payload.length)); out.write(payload); out.flush(); when(httpResponse.getContent()).thenReturn(out.toByteArray()); } catch (Exception e) { e.printStackTrace(); } }
private void sendSuccessfulResponse(HttpServletResponse response, RpcEnvelope.Request rpcRequest, Message pbResponse) throws IOException { response.setStatus(HttpServletResponse.SC_OK); RpcEnvelope.Response rpcResponse = RpcEnvelope.Response.newBuilder(). setServiceMethod(rpcRequest.getServiceMethod()). setSequenceNumber(rpcRequest.getSequenceNumber()).build(); byte responseHeader[] = rpcResponse.toByteArray(); byte responseBody[]; if (pbResponse == null) { responseBody = new byte[0]; } else { responseBody = pbResponse.toByteArray(); } try { ServletOutputStream out = response.getOutputStream(); out.write(Ints.toByteArray(responseHeader.length)); out.write(responseHeader); out.write(Ints.toByteArray(responseBody.length)); out.write(responseBody); } catch (IOException ioex) { //there is nothing we can do, client probably went away logger.debug("Caught IOException, assuming client disconnected"); } }
public void setResponseException(RpcCallException responseException) { this.responseException = responseException; if (featureFlag.equals("true")) { when(httpResponse.getStatus()).thenReturn(responseException.getCategory().getHttpStatus()); } String response = "{\"error\":" + responseException.toJson() + ",\"result\":{}}"; when(httpResponse.getContentAsString()).thenReturn(response); RpcEnvelope.Response pbResponse = RpcEnvelope.Response.newBuilder(). setError(responseException.toJson().toString()).build(); byte[] responseArray = pbResponse.toByteArray(); byte[] headerLength = Ints.toByteArray(responseArray.length); byte[] bodyLength = Ints.toByteArray(0); byte[] overallPayload = concatAll(headerLength, responseArray, bodyLength); when(httpResponse.getContent()).thenReturn(overallPayload); }