/** * 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); }); }
/** * Get the EntityManager stack. * * @param threadLocalFallback if true, fall back to a ThreadLocal queue of entity managers if no HTTP.Context object is found. * @return the queue of entity managers. */ @SuppressWarnings("unchecked") public Deque<EntityManager> emStack(boolean threadLocalFallback) { return Http.Context.safeCurrent().map(context -> { Object emsObject = context.args.get(CURRENT_ENTITY_MANAGER); if (emsObject != null) { return (Deque<EntityManager>) emsObject; } else { Deque<EntityManager> ems = new ArrayDeque<>(); context.args.put(CURRENT_ENTITY_MANAGER, ems); return ems; } }).orElseGet(() -> { // Not a web request if (threadLocalFallback) { return this.get(); } else { throw new RuntimeException("No Http.Context is present. If you want to invoke this method outside of a HTTP request, you need to wrap the call with JPA.withTransaction instead."); } }); }
/** * Get the default EntityManager for this thread. * * @throws RuntimeException if no EntityManager is bound to the current Http.Context or the current Thread. * @return the EntityManager */ public EntityManager em() { Deque<EntityManager> ems = this.emStack(true); if (ems.isEmpty()) { Http.Context.safeCurrent().map(ctx -> { throw new RuntimeException("No EntityManager found in the context. Try to annotate your action method with @play.db.jpa.Transactional"); }).orElseGet(() -> { throw new RuntimeException("No EntityManager bound to this thread. Try wrapping this call in JPAApi.withTransaction, or ensure that the HTTP context is setup on this thread."); }); } return ems.peekFirst(); }
/** * @deprecated Deprecated as of 2.7.0. */ @Deprecated protected TypedMap ctxRequestAttrs() { return Http.Context.safeCurrent().map(ctx -> ctx.request().attrs()).orElseGet(() -> TypedMap.empty()); }
/** * @deprecated Deprecated as of 2.7.0. */ @Deprecated protected TypedMap ctxRequestAttrs() { return Http.Context.safeCurrent().map(ctx -> ctx.request().attrs()).orElseGet(() -> TypedMap.empty()); }
/** * @deprecated Deprecated as of 2.7.0. */ @Deprecated protected Lang ctxLang() { return Http.Context.safeCurrent().map(ctx -> ctx.messages().lang()).orElse(null); }
/** * @deprecated Deprecated as of 2.7.0. */ @Deprecated protected Lang ctxLang() { return Http.Context.safeCurrent().map(ctx -> ctx.messages().lang()).orElse(null); }
private Set<ConstraintViolation<Object>> runValidation(Lang lang, TypedMap attrs, DataBinder dataBinder, Map<String, Object> objectData) { return withRequestLocale(lang, () -> { dataBinder.bind(new MutablePropertyValues(objectData)); final ValidationPayload payload = new ValidationPayload(lang, lang != null ? new MessagesImpl(lang, this.messagesApi) : null, Http.Context.safeCurrent().map(ctx -> ctx.args).orElse(null), attrs, this.config); final Validator validator = validatorFactory.unwrap(HibernateValidatorFactory.class).usingContext().constraintValidatorPayload(payload).getValidator(); if (groups != null) { return validator.validate(dataBinder.getTarget(), groups); } else { return validator.validate(dataBinder.getTarget()); } }); }
private Set<ConstraintViolation<Object>> runValidation(Lang lang, TypedMap attrs, DataBinder dataBinder, Map<String, Object> objectData) { return withRequestLocale(lang, () -> { dataBinder.bind(new MutablePropertyValues(objectData)); final ValidationPayload payload = new ValidationPayload(lang, lang != null ? new MessagesImpl(lang, this.messagesApi) : null, Http.Context.safeCurrent().map(ctx -> ctx.args).orElse(null), attrs, this.config); final Validator validator = validatorFactory.unwrap(HibernateValidatorFactory.class).usingContext().constraintValidatorPayload(payload).getValidator(); if (groups != null) { return validator.validate(dataBinder.getTarget(), groups); } else { return validator.validate(dataBinder.getTarget()); } }); }
/** * @deprecated Deprecated as of 2.7.0. */ @Deprecated protected TypedMap ctxRequestAttrs() { return Http.Context.safeCurrent().map(ctx -> ctx.request().attrs()).orElseGet(() -> TypedMap.empty()); }
/** * @deprecated Deprecated as of 2.7.0. */ @Deprecated protected Lang ctxLang() { return Http.Context.safeCurrent().map(ctx -> ctx.messages().lang()).orElse(null); }
private Set<ConstraintViolation<Object>> runValidation(Lang lang, TypedMap attrs, DataBinder dataBinder, Map<String, Object> objectData) { return withRequestLocale(lang, () -> { dataBinder.bind(new MutablePropertyValues(objectData)); final ValidationPayload payload = new ValidationPayload(lang, lang != null ? new MessagesImpl(lang, this.messagesApi) : null, Http.Context.safeCurrent().map(ctx -> ctx.args).orElse(null), attrs, this.config); final Validator validator = validatorFactory.unwrap(HibernateValidatorFactory.class).usingContext().constraintValidatorPayload(payload).getValidator(); if (groups != null) { return validator.validate(dataBinder.getTarget(), groups); } else { return validator.validate(dataBinder.getTarget()); } }); }