@Test public void customStatusMessageIsMaintainedWithPayload() throws Exception { when(request.getAction()).thenReturn(HttpMethods.GET); Payload detailedReason = new StringPayload("Please authenticate and try again", "text/plain"); Response response = new Response.Builder() .status(Status.failure(FORBIDDEN, "You can't see this")) .payload(detailedReason).build(); underTest.send(request, response, httpServletResponse); verify(httpServletResponse).setStatus(403, "You can't see this"); }
@Override public void send(@Nullable final Request request, final Response response, final HttpServletResponse httpResponse) throws ServletException, IOException { log.debug("Sending response: {}", response); for (Map.Entry<String, String> header : response.getHeaders()) { httpResponse.addHeader(header.getKey(), header.getValue()); } Status status = response.getStatus(); String statusMessage = status.getMessage(); try (Payload payload = response.getPayload()) { if (status.isSuccessful() || payload != null) { if (statusMessage == null) { httpResponse.setStatus(status.getCode()); } else { httpResponse.setStatus(status.getCode(), statusMessage); } if (payload != null) { log.trace("Attaching payload: {}", payload); if (payload.getContentType() != null) { httpResponse.setContentType(payload.getContentType()); } if (payload.getSize() != Payload.UNKNOWN_SIZE) { httpResponse.setContentLengthLong(payload.getSize()); } if (request != null && !HttpMethods.HEAD.equals(request.getAction())) { try (InputStream input = payload.openInputStream(); OutputStream output = httpResponse.getOutputStream()) { payload.copy(input, output); } } } } else { httpResponse.sendError(status.getCode(), statusMessage); } } }
DefaultHttpResponseSender extends ComponentSupport implements HttpResponseSender { @Override public void send(@Nullable final Request request, final Response response, final HttpServletResponse httpResponse) throws ServletException, IOException { log.debug("Sending response: {}", response); for (Map.Entry<String, String> header : response.getHeaders()) { httpResponse.addHeader(header.getKey(), header.getValue()); } Status status = response.getStatus(); String statusMessage = status.getMessage(); try (Payload payload = response.getPayload()) { if (status.isSuccessful() || payload != null) { if (statusMessage == null) { httpResponse.setStatus(status.getCode()); } else { httpResponse.setStatus(status.getCode(), statusMessage); } if (payload != null) { log.trace("Attaching payload: {}", payload); if (payload.getContentType() != null) { httpResponse.setContentType(payload.getContentType()); } if (payload.getSize() != Payload.UNKNOWN_SIZE) { httpResponse.setContentLengthLong(payload.getSize()); } if (request != null && !HttpMethods.HEAD.equals(request.getAction())) { try (InputStream input = payload.openInputStream(); OutputStream output = httpResponse.getOutputStream()) { payload.copy(input, output); } } } } else { httpResponse.sendError(status.getCode(), statusMessage); } } } }
DefaultHttpResponseSender extends ComponentSupport implements HttpResponseSender { @Override public void send(@Nullable final Request request, final Response response, final HttpServletResponse httpResponse) throws ServletException, IOException { log.debug("Sending response: {}", response); for (Map.Entry<String, String> header : response.getHeaders()) { httpResponse.addHeader(header.getKey(), header.getValue()); } Status status = response.getStatus(); String statusMessage = status.getMessage(); try (Payload payload = response.getPayload()) { if (status.isSuccessful() || payload != null) { if (statusMessage == null) { httpResponse.setStatus(status.getCode()); } else { httpResponse.setStatus(status.getCode(), statusMessage); } if (payload != null) { log.trace("Attaching payload: {}", payload); if (payload.getContentType() != null) { httpResponse.setContentType(payload.getContentType()); } if (payload.getSize() != Payload.UNKNOWN_SIZE) { httpResponse.setContentLengthLong(payload.getSize()); } if (request != null && !HttpMethods.HEAD.equals(request.getAction())) { try (InputStream input = payload.openInputStream(); OutputStream output = httpResponse.getOutputStream()) { payload.copy(input, output); } } } } else { httpResponse.sendError(status.getCode(), statusMessage); } } }  }
DefaultHttpResponseSender extends ComponentSupport implements HttpResponseSender { @Override public void send(@Nullable final Request request, final Response response, final HttpServletResponse httpResponse) throws ServletException, IOException { log.debug("Sending response: {}", response); for (Map.Entry<String, String> header : response.getHeaders()) { httpResponse.addHeader(header.getKey(), header.getValue()); } Status status = response.getStatus(); String statusMessage = status.getMessage(); try (Payload payload = response.getPayload()) { if (status.isSuccessful() || payload != null) { if (statusMessage == null) { httpResponse.setStatus(status.getCode()); } else { httpResponse.setStatus(status.getCode(), statusMessage); } if (payload != null) { log.trace("Attaching payload: {}", payload); if (payload.getContentType() != null) { httpResponse.setContentType(payload.getContentType()); } if (payload.getSize() != Payload.UNKNOWN_SIZE) { httpResponse.setContentLengthLong(payload.getSize()); } if (request != null && !HttpMethods.HEAD.equals(request.getAction())) { try (InputStream input = payload.openInputStream(); OutputStream output = httpResponse.getOutputStream()) { payload.copy(input, output); } } } } else { httpResponse.sendError(status.getCode(), statusMessage); } } }  @Override void send(@Nullable final Request request, final Response response, final HttpServletResponse httpResponse); }
DefaultHttpResponseSender extends ComponentSupport implements HttpResponseSender { @Override public void send(@Nullable final Request request, final Response response, final HttpServletResponse httpResponse) throws ServletException, IOException { log.debug("Sending response: {}", response); for (Map.Entry<String, String> header : response.getHeaders()) { httpResponse.addHeader(header.getKey(), header.getValue()); } Status status = response.getStatus(); String statusMessage = status.getMessage(); try (Payload payload = response.getPayload()) { if (status.isSuccessful() || payload != null) { if (statusMessage == null) { httpResponse.setStatus(status.getCode()); } else { httpResponse.setStatus(status.getCode(), statusMessage); } if (payload != null) { log.trace("Attaching payload: {}", payload); if (payload.getContentType() != null) { httpResponse.setContentType(payload.getContentType()); } if (payload.getSize() != Payload.UNKNOWN_SIZE) { httpResponse.setContentLengthLong(payload.getSize()); } if (request != null && !HttpMethods.HEAD.equals(request.getAction())) { try (InputStream input = payload.openInputStream(); OutputStream output = httpResponse.getOutputStream()) { payload.copy(input, output); } } } } else { httpResponse.sendError(status.getCode(), statusMessage); } } }  @Override void send(@Nullable final Request request, final Response response, final HttpServletResponse httpResponse);  }