@Override protected ResponseStreams finalizeHeaders(Result result, Boolean handleFlashAndSessionCookie) { // delegate cookie, session, and flash to parent super.finalizeHeaders(result, handleFlashAndSessionCookie); httpServletResponse.setStatus(result.getStatusCode()); // copy headers for (Entry<String, String> header : result.getHeaders().entrySet()) { httpServletResponse.addHeader(header.getKey(), header.getValue()); } // set content type if (result.getContentType() != null) { httpServletResponse.setContentType(result.getContentType()); } // Set charset => use utf-8 if not set // Sets correct encoding for Content-Type. But also for the output // writers. if (result.getCharset() != null) { httpServletResponse.setCharacterEncoding(result.getCharset()); } else { httpServletResponse.setCharacterEncoding(NinjaConstant.UTF_8); } // possibly ResponseStreamsServlet responseStreamsServlet = new ResponseStreamsServlet(); responseStreamsServlet.init(httpServletResponse); return responseStreamsServlet; }
public static Result status(int statusCode) { Result result = new Result(statusCode); return result; }
/** * A redirect that uses 303 see other. * * @param url * The url used as redirect target. * @return A nicely configured result with status code 303 and the url set * as Location header. */ public Result redirect(String url) { status(Result.SC_303_SEE_OTHER); addHeader(Result.LOCATION, url); return this; }
/** * A redirect that uses 303 see other. * * The redirect does NOT need a template and does NOT * render a text in the Http body by default. * * If you wish to do so please * remove the {@link NoHttpBody} that is set as renderable of * the Result. * * @param url * The url used as redirect target. * @return A nicely configured result with status code 303 and the url set * as Location header. Renders no Http body by default. */ public static Result redirect(String url) { Result result = status(Result.SC_303_SEE_OTHER); result.addHeader(Result.LOCATION, url); result.render(Result.NO_HTTP_BODY); return result; }
appendNameValue(s, "Template", result.getTemplate()); appendNameValue(s, "Charset", result.getCharset()); appendNameValue(s, "Content type", result.getContentType()); appendNameValue(s, "Status code", result.getStatusCode()+""); List<String> supportedContentTypes = result.supportedContentTypes(); if (supportedContentTypes == null || supportedContentTypes.isEmpty()) { appendNameValue(s, "Supported content types", "None set"); appendNameValue(s, "Fallback content type", result.fallbackContentType().orElse("None set")); appendNameValue(s, "Json View", (result.getJsonView() != null ? result.getJsonView().getClass().getCanonicalName() : "None")); Object renderable = result.getRenderable(); Map<String, String> headers = result.getHeaders(); if (headers != null && !headers.isEmpty()) { for (Map.Entry<String, String> entry : headers.entrySet()) {
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()); } }
DiagnosticErrorBuilder.build401UnauthorizedDiagnosticError(); return Results.unauthorized().render(diagnosticError); .addHeader(Result.WWW_AUTHENTICATE, "None") .supportedContentTypes(Result.TEXT_HTML, Result.APPLICATION_JSON, Result.APPLICATION_XML) .fallbackContentType(Result.TEXT_HTML) .render(message) .template( ninjaProperties.getWithDefault( NinjaConstant.LOCATION_VIEW_HTML_UNAUTHORIZED_KEY,
Object objectToBeRendered = result.getRenderable(); if (!result.getHeaders().containsKey(Result.CACHE_CONTROL)) { result.doNotCacheContent();
@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); } } };
httpCacheToolkit.addEtag(context, result, lastModified); if (result.getStatusCode() == Result.SC_304_NOT_MODIFIED) { result.status(200); result.contentType(mimeType);
s.append(" <p id=\"detail\">\n"); if (result.getStatusCode() != 200) { s.append ("Status code ").append(result.getStatusCode());
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(); } }
public static Result created(Optional<String> url) { Result result = status(Result.SC_201_CREATED); if (url.isPresent()) { result.addHeader(Result.LOCATION, url.get()); } return result; }
} else { assertObjectNoRenderableOrThrowException(this.renderable);
@Override public Result setLanguage(String locale, Result result) { Cookie defaultLangCookie = generateNinjaLanguageCookie(); Cookie cookie = Cookie.builder(defaultLangCookie).setValue(locale).build(); result.addCookie(cookie); return result; }
@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); } } };
/** * A redirect that uses 307 see other. * * The redirect does NOT need a template and does NOT * render a text in the Http body by default. * * If you wish to do so please * remove the {@link NoHttpBody} that is set as renderable of * the Result. * * @param url * The url used as redirect target. * @return A nicely configured result with status code 307 and the url set * as Location header. Renders no Http body by default. */ public static Result redirectTemporary(String url) { Result result = status(Result.SC_307_TEMPORARY_REDIRECT); result.addHeader(Result.LOCATION, url); result.render(Result.NO_HTTP_BODY); return result; }
/** * This function sets * * Cache-Control: no-cache, no-store * Date: (current date) * Expires: 1970 * * => it therefore effectively forces the browser and every proxy in between * not to cache content. * * See also https://devcenter.heroku.com/articles/increasing-application-performance-with-http-cache-headers * * @return this result for chaining. */ public Result doNotCacheContent() { addHeader(CACHE_CONTROL, CACHE_CONTROL_DEFAULT_NOCACHE_VALUE); addHeader(DATE, DateUtil.formatForHttpHeader(System.currentTimeMillis())); addHeader(EXPIRES, DateUtil.formatForHttpHeader(0L)); return this; }
assertObjectNoRenderableOrThrowException(this.renderable);
exchange.setStatusCode(result.getStatusCode()); for (Entry<String, String> header : result.getHeaders().entrySet()) { exchange.getResponseHeaders() .add(new HttpString(header.getKey()), header.getValue()); final String charset = Optional.fromNullable(result.getCharset()).or(NinjaConstant.UTF_8); if (result.getContentType() != null) { String contentTypeHeader = new StringBuilder() .append(result.getContentType()) .append("; charset=") .append(charset)