/** * The transient language will be taken into account when using {@link MessagesApi#preferred(RequestHeader)}} (It will take precedence over any other language). * * @return The current transient language of this request. */ default Optional<Lang> transientLang() { return attrs().getOptional(Messages.Attrs.CurrentLang).map(play.api.i18n.Lang::asJava); }
/** * The transient language will be taken into account when using {@link MessagesApi#preferred(RequestHeader)}} (It will take precedence over any other language). * * @return The current transient language of this request. */ default Optional<Lang> transientLang() { return attrs().getOptional(Messages.Attrs.CurrentLang).map(play.api.i18n.Lang::asJava); }
/** * The transient language will be taken into account when using {@link MessagesApi#preferred(RequestHeader)}} (It will take precedence over any other language). * * @return The current transient language of this request. */ default Optional<Lang> transientLang() { return attrs().getOptional(Messages.Attrs.CurrentLang).map(play.api.i18n.Lang::asJava); }
/** * Executes this action with the given HTTP request and returns the result. * * @param req the http request with which to execute this action * @return a promise to the action's result */ public CompletionStage<Result> call(Request req) { // TODO: Make this method abstract after removing call(Context) return Context.safeCurrent().map(threadLocalCtx -> { // A previous action did explicitly set a context onto the thread local (via Http.Context.current.set(...)) // Let's use that context so the user doesn't loose data he/she set onto that ctx (args,...) Context newCtx = threadLocalCtx.withRequest(req.removeAttr(CTX_ARGS)); Context.setCurrent(newCtx); return call(newCtx); }).orElseGet(() -> { // A previous action did not set a context explicitly, we simply create a new one to pass on the request Context ctx = new Context(req.removeAttr(CTX_ARGS), contextComponents); ctx.args = req.attrs().getOptional(CTX_ARGS).orElse(new HashMap<>()); return call(ctx); }); }
/** * Executes this action with the given HTTP request and returns the result. * * @param req the http request with which to execute this action * @return a promise to the action's result */ public CompletionStage<Result> call(Request req) { // TODO: Make this method abstract after removing call(Context) return Context.safeCurrent().map(threadLocalCtx -> { // A previous action did explicitly set a context onto the thread local (via Http.Context.current.set(...)) // Let's use that context so the user doesn't loose data he/she set onto that ctx (args,...) Context newCtx = threadLocalCtx.withRequest(req.removeAttr(CTX_ARGS)); Context.setCurrent(newCtx); return call(newCtx); }).orElseGet(() -> { // A previous action did not set a context explicitly, we simply create a new one to pass on the request Context ctx = new Context(req.removeAttr(CTX_ARGS), contextComponents); ctx.args = req.attrs().getOptional(CTX_ARGS).orElse(new HashMap<>()); return call(ctx); }); }
/** * Executes this action with the given HTTP request and returns the result. * * @param req the http request with which to execute this action * @return a promise to the action's result */ public CompletionStage<Result> call(Request req) { // TODO: Make this method abstract after removing call(Context) return Context.safeCurrent().map(threadLocalCtx -> { // A previous action did explicitly set a context onto the thread local (via Http.Context.current.set(...)) // Let's use that context so the user doesn't loose data he/she set onto that ctx (args,...) Context newCtx = threadLocalCtx.withRequest(req.removeAttr(CTX_ARGS)); Context.setCurrent(newCtx); return call(newCtx); }).orElseGet(() -> { // A previous action did not set a context explicitly, we simply create a new one to pass on the request Context ctx = new Context(req.removeAttr(CTX_ARGS), contextComponents); ctx.args = req.attrs().getOptional(CTX_ARGS).orElse(new HashMap<>()); return call(ctx); }); }
/** * Checks if an action is authorised. This allows controller-level annotations to cede control to method-level annotations. * * @param request the request * @return true if a more-specific annotation has authorised access, otherwise false */ protected static boolean isAuthorised(final Http.RequestHeader request) { return request.attrs().getOptional(ACTION_AUTHORISED).orElse(false); }
@Override public Object getRequestAttribute(final String name) { Object value = context.args.get(name); // for the user profiles, if we don't get a value from the context.args, we try from the attributes (call after the SecurityFilter) if (Pac4jConstants.USER_PROFILES.equals(name) && value == null) { final Optional<Object> optionalValue = request.attrs().getOptional(PAC4J_USER_PROFILES); if (optionalValue.isPresent()) { value = optionalValue.get(); } } return value; }
@Override public Object getRequestAttribute(final String name) { Object value = context.args.get(name); // for the user profiles, if we don't get a value from the context.args, we try from the attributes (call after the SecurityFilter) if (Pac4jConstants.USER_PROFILES.equals(name) && value == null) { final Optional<Object> optionalValue = request.attrs().getOptional(PAC4J_USER_PROFILES); if (optionalValue.isPresent()) { value = optionalValue.get(); } } return value; }
/** * Checks if an action is authorised. This allows controller-level annotations to cede control to method-level annotations. * * @param request the request * @return true if a more-specific annotation has authorised access, otherwise false */ protected static boolean isAuthorised(final Http.RequestHeader request) { return request.attrs().getOptional(ACTION_AUTHORISED).orElse(false); }
@Override public Object getAttribute(final String name) { return ofNullable(attributes.get(name)) .orElseGet(() -> playDelegate.attrs().getOptional(TypedKey.create(name)).orElse(null)); }
/** * Get the deferred action from the request. * * @param request the request * @return a tuple containing the deferred action (or null if it doesn't exist) and the cleaned up request you should pass on */ @SuppressWarnings("unchecked") public F.Tuple<AbstractDeadboltAction<?>, Http.RequestHeader> getDeferredAction(final Http.RequestHeader request) { return request.attrs().getOptional(ACTION_DEFERRED).map(action -> { action.delegate = this; return F.<AbstractDeadboltAction<?>, Http.RequestHeader>Tuple(action, request.removeAttr(ACTION_DEFERRED).addAttr(IGNORE_DEFERRED_FLAG, true)); }).orElseGet(() -> F.Tuple(null, request)); }
/** * Get the deferred action from the request. * * @param request the request * @return a tuple containing the deferred action (or null if it doesn't exist) and the cleaned up request you should pass on */ @SuppressWarnings("unchecked") public F.Tuple<AbstractDeadboltAction<?>, Http.RequestHeader> getDeferredAction(final Http.RequestHeader request) { return request.attrs().getOptional(ACTION_DEFERRED).map(action -> { action.delegate = this; return F.<AbstractDeadboltAction<?>, Http.RequestHeader>Tuple(action, request.removeAttr(ACTION_DEFERRED).addAttr(IGNORE_DEFERRED_FLAG, true)); }).orElseGet(() -> F.Tuple(null, request)); }
@Override public CompletionStage<F.Tuple<Optional<? extends Subject>, Http.RequestHeader>> apply(final DeadboltHandler deadboltHandler, final Http.RequestHeader requestHeader) { if (cacheUserPerRequestEnabled) { final TypedKey<Subject> deadboltHandlerCacheId = this.typedKeyCache.computeIfAbsent(deadboltHandler.getId(), k -> TypedKey.create("deadbolt.java.cache-user." + k)); final Optional<? extends Subject> cachedUser = requestHeader.attrs().getOptional(deadboltHandlerCacheId); if (cachedUser.isPresent()) { return CompletableFuture.completedFuture(F.Tuple(cachedUser, requestHeader)); } else { return deadboltHandler.getSubject(requestHeader).thenApply(subjectOption -> subjectOption.map(s -> F.<Optional<? extends Subject>, Http.RequestHeader>Tuple(Optional.of(s), requestHeader.addAttr(deadboltHandlerCacheId, s))).orElseGet(() -> F.Tuple(Optional.empty(), requestHeader))); } } return deadboltHandler.getSubject(requestHeader).thenApply(subjectOption -> subjectOption.map(s -> F.<Optional<? extends Subject>, Http.RequestHeader>Tuple(Optional.of(s), requestHeader)).orElseGet(() -> F.Tuple(Optional.empty(), requestHeader))); } }
@Override public CompletionStage<F.Tuple<Optional<? extends Subject>, Http.RequestHeader>> apply(final DeadboltHandler deadboltHandler, final Http.RequestHeader requestHeader) { if (cacheUserPerRequestEnabled) { final TypedKey<Subject> deadboltHandlerCacheId = this.typedKeyCache.computeIfAbsent(deadboltHandler.getId(), k -> TypedKey.create("deadbolt.java.cache-user." + k)); final Optional<? extends Subject> cachedUser = requestHeader.attrs().getOptional(deadboltHandlerCacheId); if (cachedUser.isPresent()) { return CompletableFuture.completedFuture(F.Tuple(cachedUser, requestHeader)); } else { return deadboltHandler.getSubject(requestHeader).thenApply(subjectOption -> subjectOption.map(s -> F.<Optional<? extends Subject>, Http.RequestHeader>Tuple(Optional.of(s), requestHeader.addAttr(deadboltHandlerCacheId, s))).orElseGet(() -> F.Tuple(Optional.empty(), requestHeader))); } } return deadboltHandler.getSubject(requestHeader).thenApply(subjectOption -> subjectOption.map(s -> F.<Optional<? extends Subject>, Http.RequestHeader>Tuple(Optional.of(s), requestHeader)).orElseGet(() -> F.Tuple(Optional.empty(), requestHeader))); } }