/** * Send a 303 (See Other) redirect * * @param to the location to redirect to */ public void redirect(String to) { setStatus(HttpResponseStatus.SEE_OTHER); headers().set("Location", to); redirect = true; }
@Override public Response.ResponseBuilder status(int status) { response.setStatus(HttpResponseStatus.valueOf(status)); return this; }
protected void determineErrorStatus(HttpResponse res, Throwable response) { HttpResponseStatus status = null; if (response == null) { status = HttpResponseStatus.NO_CONTENT; } else if (response instanceof WebApplicationException) { status = HttpResponseStatus.valueOf(((WebApplicationException) response).getResponse().getStatus()); } res.setStatus(status); }
protected Object convertErrorToResponseObject(HttpResponse res, Throwable response) { determineErrorStatus(res, response); if (response instanceof JsonResponseError) { JsonResponseError je = (JsonResponseError) response; res.setStatus(HttpResponseStatus.valueOf(je.getResponse().getStatus())); return je.getContent(); } log.warn("Unable to convert exception to response", response); //never JSON encode an exception, user can set it as content to JsonResponseError if they want return EMPTY_JSON_OBJECT; }
protected Object convertErrorToResponseObject(HttpResponse res, Throwable response) { determineErrorStatus(res, response); if (response instanceof JsonResponseError) { JsonResponseError je = (JsonResponseError) response; res.setStatus(HttpResponseStatus.valueOf(je.getResponse().getStatus())); return je.getContent(); } log.warn("Unable to convert exception to response", response); //never JSON encode an exception, user can set it as content to JsonResponseError if they want return EMPTY_JSON_OBJECT; }
@Override public void transform(Object response, HttpRequest request, HttpResponse res, MediaType mediaType, HttpMethod method, ChannelHandlerContext ctx) { byte[] data = null; if (response == null) { data = EMPTY_JSON_OBJECT.getBytes(); } else { if (isError(response)) { response = convertErrorToResponseObject(res, (Throwable) response); } try { data = JsonDecoder.mapper.writeValueAsBytes(response); } catch (JsonProcessingException e) { log.warn("Unable to transform response to JSON", e); res.setStatus(HttpStatus.INTERNAL_SERVER_ERROR); } } setResponseContent(res, data); }
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { try { if (cause instanceof WebApplicationException) { writeResponse(ctx, cause, protocolConfig.getTransformers()); } else { log.warn(String.format("Error while processing request %s", request), cause); writeResponse(ctx, new WebApplicationException(HttpStatus.INTERNAL_SERVER_ERROR.code()), protocolConfig.getTransformers()); } } catch (Throwable t) { //at this point if an exception occurs, just log and return internal server error //internal server error log.warn(String.format("Uncaught error while processing request %s", request), cause); res.setStatus(HttpStatus.INTERNAL_SERVER_ERROR); doWrite(ctx); } } }
@Override public void transform(Object response, HttpRequest request, HttpResponse res, MediaType mediaType, HttpMethod method, ChannelHandlerContext ctx) { byte[] data = null; if (response == null) { data = EMPTY_JSON_OBJECT.getBytes(); } else { if (isError(response)) { response = convertErrorToResponseObject(res, (Throwable) response); } try { data = JsonDecoder.mapper.writeValueAsBytes(response); } catch (JsonProcessingException e) { log.warn("Unable to transform response to JSON", e); res.setStatus(HttpStatus.INTERNAL_SERVER_ERROR); } } setResponseContent(res, data); }
} else if (response instanceof WebApplicationException) { WebApplicationException ex = (WebApplicationException) response; res.setStatus(HttpResponseStatus.valueOf(ex.getResponse().getStatus())); if (ex.getMessage() != null && !ex.getMessage().isEmpty()) { byte[] msg = ex.getMessage().getBytes(); writeResponseFromStream((ResolvedFile) response, res, request, mediaType, method, ctx); } else { res.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR); if (isError(response)) { log.warn("Unexpected error to static file transformer", response);
} else if (response instanceof WebApplicationException) { WebApplicationException ex = (WebApplicationException) response; res.setStatus(HttpResponseStatus.valueOf(ex.getResponse().getStatus())); if (ex.getMessage() != null && !ex.getMessage().isEmpty()) { byte[] msg = ex.getMessage().getBytes(); writeResponseFromStream((ResolvedFile) response, res, request, mediaType, method, ctx); } else { res.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR); if (isError(response)) { log.warn("Unexpected error to static file transformer", response);
} catch (IOException e) { log.warn("Failed to write the results of a mustacheTransformer execution", e); res.setStatus(INTERNAL_SERVER_ERROR); setResponseContent(res, new byte[0]);
} catch (IOException e) { log.warn("Failed to write the results of a mustacheTransformer execution", e); res.setStatus(INTERNAL_SERVER_ERROR); setResponseContent(res, new byte[0]);
} catch (Throwable e) { log.warn("Unable to transform response to HTML using Thymeleaf transformer", e); res.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
protected ChannelFuture writeResponse(ChannelHandlerContext ctx, Object response, Queue<ResponseTransformer> t) { if (res.isRedirect()) { return doWrite(ctx); } if (response instanceof HttpResponse) { res = (HttpResponse) response; return doWrite(ctx); } List<ResponseTransformer> ts = new FixedSortedList<>(t); boolean notAcceptable = false; for (ResponseTransformer transformer : ts) { if (transformer.canTransform(response, request, request.getMatchedMediaType(), method, ctx)) { transformer.transform(response, request, res, request.getMatchedMediaType(), method, ctx); notAcceptable = false; break; } notAcceptable = true; } if (notAcceptable) { res.setStatus(HttpStatus.NOT_ACCEPTABLE); } return doWrite(ctx); }