/** * Adds the given key and value to the session. * * @param key The key to add to this result's session * @param value The value to add to this result's session * @return A copy of this result with the key and value added to its session scope. */ public Result addingToSession(Http.Request request, String key, String value) { Map<String, String> newValues = new HashMap<>(1); newValues.put(key, value); return addingToSession(request, newValues); }
/** * Discard a cookie on the given path with no domain and not that's secure. * * @param name The name of the cookie to discard, must not be null * @param path The path of the cookie to discard, may be null */ public Result discardingCookie(String name, String path) { return discardingCookie(name, path, null, false); }
/** * Adds values to the session. * * @param values A map with values to add to this result's session * @return A copy of this result with values added to its session scope. */ public Result addingToSession(Http.Request request, Map<String, String> values) { return withSession(session(request).adding(values)); }
private Result doSendResource(Source<ByteString, ?> data, Optional<Long> contentLength, Optional<String> resourceName, boolean inline, FileMimeTypes fileMimeTypes) { // Create a Content-Disposition header StringBuilder cdBuilder = new StringBuilder(); cdBuilder.append(inline ? "inline" : "attachment"); if (resourceName.isPresent()) { cdBuilder.append("; "); HttpHeaderParameterEncoding.encodeToBuilder("filename", resourceName.get(), cdBuilder); } Map<String, String> headers = Collections.singletonMap( Http.HeaderNames.CONTENT_DISPOSITION, cdBuilder.toString() ); return new Result(status(), headers, new HttpEntity.Streamed( data, contentLength, resourceName.map(name -> fileMimeTypes.forFileName(name) .orElse(Http.MimeTypes.BINARY) ) )); }
/** * Send the given input stream. * * @param stream The input stream to send. * @param contentLength The length of the content in the stream. * @return The result. */ public Result sendInputStream(InputStream stream, long contentLength) { if (stream == null) { throw new NullPointerException("Null stream"); } return new Result(status(), new HttpEntity.Streamed(StreamConverters.fromInputStream(() -> stream, DEFAULT_CHUNK_SIZE), Optional.of(contentLength), Optional.empty())); }
/** * Places the CSRF token in the session or in a cookie (if a cookie name is configured) */ private Result placeToken(Http.Request req, final Result result, CSRF.Token token) { if (config.cookieName().isDefined()) { scala.Option<String> domain = sessionConfiguration.domain(); Http.Cookie cookie = new Http.Cookie( config.cookieName().get(), token.value(), null, sessionConfiguration.path(), domain.isDefined() ? domain.get() : null, config.secureCookie(), config.httpOnlyCookie(), OptionConverters.toJava(config.sameSiteCookie()).map(c -> c.asJava()).orElse(null)); return result.withCookies(cookie); } return result.addingToSession(req, token.name(), token.value()); } }
/** * Send a json result. * * @param json the json to send * @param encoding the encoding in which to encode the json (e.g. "UTF-8") * @return a '200 OK' result containing the json encoded with the given charset */ public Result sendJson(JsonNode json, JsonEncoding encoding) { if (json == null) { throw new NullPointerException("Null content"); } ObjectMapper mapper = Json.mapper(); ByteStringBuilder builder = ByteString$.MODULE$.newBuilder(); try { JsonGenerator jgen = mapper.getFactory().createGenerator(builder.asOutputStream(), encoding); mapper.writeValue(jgen, json); String contentType = MimeTypes.JSON; return new Result(status(), new HttpEntity.Strict(builder.result(), Optional.of(contentType))); } catch (IOException e) { throw new RuntimeException(e); } }
public Result showVersion() throws IOException { final Application application = injector.instanceOf(Application.class); final InputStream versionAsStream = application.resourceAsStream("internal/version.json"); final String versionAsString = IOUtils.toString(versionAsStream, StandardCharsets.UTF_8); return ok(versionAsString).as(Http.MimeTypes.JSON); }
/** * Generates a 307 Temporary Redirect result. * * @param url The url to redirect. * @return the result */ public static Result temporaryRedirect(String url) { return new Result(TEMPORARY_REDIRECT, Collections.singletonMap(LOCATION, url)); }
private CompletionStage<Result> handleTokenError(Http.Request req, RequestHeader taggedRequest, String msg) { CSRFErrorHandler handler = configurator.apply(this.configuration); return handler.handle(taggedRequest.asJava(), msg).thenApply(result -> { if (CSRF.getToken(taggedRequest).isEmpty()) { if (config.cookieName().isDefined()) { Option<String> domain = sessionConfiguration.domain(); return result.discardingCookie(config.cookieName().get(), sessionConfiguration.path(), domain.isDefined() ? domain.get() : null, config.secureCookie()); } return result.removingFromSession(req, config.tokenName()); } return result; }); } }
/** * Extracts the Location header of this Result value if this Result is a Redirect. * * @return the location (if it was set) */ public Optional<String> redirectLocation() { return header(LOCATION); }
/** * Discard a cookie in this result * * @param name The name of the cookie to discard, must not be null * @param path The path of the cookie te discard, may be null * @param domain The domain of the cookie to discard, may be null * @param secure Whether the cookie to discard is secure */ public Result discardingCookie(String name, String path, String domain, boolean secure) { return withCookies(new DiscardingCookie(name, path, Option.apply(domain), secure).toCookie().asJava()); }
/** * Given a Result and a Lang, return a new Result with the lang cookie set to the given Lang. * * @param result the result where the lang will be set. * @param lang the lang to set on the result * @return a new result with the lang. */ public Result setLang(Result result, Lang lang) { return messages.setLang(result.asScala(), lang).asJava(); }
/** * Extracts a Cookie value from this Result value * * @param name the cookie's name. * @return the optional cookie */ public Optional<Cookie> getCookie(String name) { return cookies().getCookie(name); }
/** * Adds the given key and value to the flash. * * @param key The key to add to this result's flash * @param value The value to add to this result's flash * @return A copy of this result with the key and value added to its flash scope. */ public Result flashing(String key, String value) { Map<String, String> newValues = new HashMap<>(1); newValues.put(key, value); return flashing(newValues); }
private Result doSendResource(Source<ByteString, ?> data, Optional<Long> contentLength, Optional<String> resourceName, boolean inline, FileMimeTypes fileMimeTypes) { // Create a Content-Disposition header StringBuilder cdBuilder = new StringBuilder(); cdBuilder.append(inline ? "inline" : "attachment"); if (resourceName.isPresent()) { cdBuilder.append("; "); HttpHeaderParameterEncoding.encodeToBuilder("filename", resourceName.get(), cdBuilder); } Map<String, String> headers = Collections.singletonMap( Http.HeaderNames.CONTENT_DISPOSITION, cdBuilder.toString() ); return new Result(status(), headers, new HttpEntity.Streamed( data, contentLength, resourceName.map(name -> fileMimeTypes.forFileName(name) .orElse(Http.MimeTypes.BINARY) ) )); }
/** * Generates a simple result. * * @param status the HTTP status for this result e.g. 200 (OK), 404 (NOT_FOUND) * @param content the result's body content * @return the result */ public static Result status(int status, ByteString content) { if (content == null) { throw new NullPointerException("Null content"); } return new Result(status, new HttpEntity.Strict(content, Optional.empty())); }
/** * Send the given input stream. * * @param stream The input stream to send. * @param contentLength The length of the content in the stream. * @return The result. */ public Result sendInputStream(InputStream stream, long contentLength) { if (stream == null) { throw new NullPointerException("Null stream"); } return new Result(status(), new HttpEntity.Streamed(StreamConverters.fromInputStream(() -> stream, DEFAULT_CHUNK_SIZE), Optional.of(contentLength), Optional.empty())); }