public static Result TODO() { Result result = status(Result.SC_501_NOT_IMPLEMENTED); result.contentType(Result.APPLICATION_JSON); return result; }
public static Result contentType(String contentType) { Result result = status(Result.SC_200_OK); result.contentType(contentType); return result; }
public static Result html() { Result result = status(Result.SC_200_OK); result.contentType(Result.TEXT_HTML); return result; }
public static Result text() { Result result = status(Result.SC_200_OK); result.contentType(Result.TEXT_PLAIN); return result; }
public void renderResult(Context context, Result result) throws IOException { String out = render(); // set context response content type result.contentType("text/html"); result.charset("utf-8"); ResponseStreams responseStreams = context.finalizeHeaders(result); try (Writer w = responseStreams.getWriter()) { w.write(out); w.flush(); w.close(); } }
@Override public void render(Context context, Result result) { if (result.getContentType() == null) { result.contentType(Result.TEXT_PLAIN); } ResponseStreams resultJsonCustom = context .finalizeHeaders(result); try (Writer writer = resultJsonCustom.getWriter()) { writer.write(string); } catch (IOException ioException) { logger.error( "Error rendering raw String via renderRaw(...)", ioException); } } };
@Override public void render(Context context, Result result) { if (result.getContentType() == null) { result.contentType(Result.APPLICATION_OCTET_STREAM); } ResponseStreams responseStreams = context .finalizeHeaders(result); try (OutputStream outputStream = responseStreams.getOutputStream()) { outputStream.write(bytes); } catch (IOException ioException) { throw new InternalServerErrorException(ioException); } } };
result.contentType(mimeType);
private void renderWithTemplateEngineOrRaw(Context context, Result result) { // if content type is not yet set in result we copy it over from the // request accept header if (result.getContentType() == null) { if (result.supportedContentTypes().contains(context.getAcceptContentType())) { result.contentType(context.getAcceptContentType()); } else if (result.fallbackContentType().isPresent()) { result.contentType(result.fallbackContentType().get()); } else { throw new BadRequestException( "No idea how to handle incoming request with Accept:" + context.getAcceptContentType() + " at route " + context.getRequestPath()); } } // try to get a suitable rendering engine... TemplateEngine templateEngine = templateEngineManager .getTemplateEngineForContentType(result.getContentType()); if (templateEngine != null) { templateEngine.invoke(context, result); } else { throw new NinjaException( 404, "No template engine found for result content type " + result.getContentType()); } }