/** * Calls a Callable which invokes a Controller or some other method with a Context */ public static <V> V invokeWithContext(RequestBuilder requestBuilder, Callable<V> callable) { try { Context.current.set(new Context(requestBuilder)); return callable.call(); } catch (Exception e) { throw new RuntimeException(e); } finally { Context.current.remove(); } }
/** * Calls a Callable which invokes a Controller or some other method with a Context. * * @param requestBuilder the request builder to invoke in this context. * @param contextComponents the context components to run. * @param callable the callable block to run. * @param <V> the return type. * @return the value from {@code callable}. * * @deprecated Deprecated as of 2.7.0. See <a href="https://www.playframework.com/documentation/latest/JavaHttpContextMigration27">migration guide</a>. */ @Deprecated public static <V> V invokeWithContext(RequestBuilder requestBuilder, JavaContextComponents contextComponents, Callable<V> callable) { try { Context.current.set(new Context(requestBuilder, contextComponents)); return callable.call(); } catch (Exception e) { throw new RuntimeException(e); } finally { Context.current.remove(); } }
/** * Calls a Callable which invokes a Controller or some other method with a Context. * * @param requestBuilder the request builder to invoke in this context. * @param contextComponents the context components to run. * @param callable the callable block to run. * @param <V> the return type. * @return the value from {@code callable}. * * @deprecated Deprecated as of 2.7.0. See <a href="https://www.playframework.com/documentation/latest/JavaHttpContextMigration27">migration guide</a>. */ @Deprecated public static <V> V invokeWithContext(RequestBuilder requestBuilder, JavaContextComponents contextComponents, Callable<V> callable) { try { Context.current.set(new Context(requestBuilder, contextComponents)); return callable.call(); } catch (Exception e) { throw new RuntimeException(e); } finally { Context.current.remove(); } }
/** * 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); }); }
/** * Create a new context with the given request. * * The id, session, flash and args remain unchanged. * * This method is intended for use within a Java action, to create a new Context to pass to a delegate action. * * @param request The request to create the new header from. * @return The new context. * * @deprecated Deprecated as of 2.7.0. <a href="https://www.playframework.com/documentation/latest/JavaHttpContextMigration27">See migration guide.</a>. */ @Deprecated public Context withRequest(Request request) { return new Context(id, request.asScala(), request, response, session, flash, args, lang, components); } }
/** * Create a new context with the given request. * * The id, session, flash and args remain unchanged. * * This method is intended for use within a Java action, to create a new Context to pass to a delegate action. * * @param request The request to create the new header from. * @return The new context. * * @deprecated Deprecated as of 2.7.0. <a href="https://www.playframework.com/documentation/latest/JavaHttpContextMigration27">See migration guide.</a>. */ @Deprecated public Context withRequest(Request request) { return new Context(id, request.asScala(), request, response, session, flash, args, lang, components); } }
/** * Create a new context with the given request. * * The id, session, flash and args remain unchanged. * * This method is intended for use within a Java action, to create a new Context to pass to a delegate action. * * @param request The request to create the new header from. * @return The new context. * * @deprecated Deprecated as of 2.7.0. <a href="https://www.playframework.com/documentation/latest/JavaHttpContextMigration27">See migration guide.</a>. */ @Deprecated public Context withRequest(Request request) { return new Context(id, request.asScala(), request, response, session, flash, args, lang, components); } }
/** * Create a new context with the given request. * * The id, Scala RequestHeader, session, flash and args remain unchanged. * * @param request The request to create the new header from. * @return The new context. */ public Context withRequest(Request request) { return new Context(id, header, request, session, flash, args); } }
/** * Builds a new Http.Context for a specific request * @param request the Request you want to use for this Context * @return a new Http.Context for this request * * @deprecated Deprecated as of 2.7.0. See <a href="https://www.playframework.com/documentation/latest/JavaHttpContextMigration27">migration guide</a>. */ @Deprecated public static Http.Context httpContext(Http.Request request) { return new Http.Context(request, contextComponents()); }
/** * Builds a new Http.Context for a specific request * @param request the Request you want to use for this Context * @return a new Http.Context for this request * * @deprecated Deprecated as of 2.7.0. See <a href="https://www.playframework.com/documentation/latest/JavaHttpContextMigration27">migration guide</a>. */ @Deprecated public static Http.Context httpContext(Http.Request request) { return new Http.Context(request, contextComponents()); }