@SuppressWarnings("unchecked") public static <T> T request() { Ctx ctx = Ctxs.get(); return ctx != null ? (T) ctx.exchange() : null; }
public static Ctx open(WithContext context) { Ctx ctx = Ctxs.open(context.tag()); ctx.setExchange(context.exchange()); ctx.setPersister(context.persister()); ctx.setUser(context.user()); Coll.assign(ctx.extras(), U.safe(context.extras())); return ctx; }
public static UserInfo user() { Ctx ctx = Ctxs.get(); UserInfo user = ctx != null ? ctx.user() : null; return U.or(user, UserInfo.ANONYMOUS); }
synchronized void close() { ensureNotClosed(); Log.debug("Closing context", "ctx", this); referenceCounter--; if (referenceCounter == 0) { clear(); } else if (referenceCounter < 0) { throw new IllegalStateException("Reference counter < 0 for context: " + this); } }
private static boolean inValidContext(Req req) { Ctx ctx = Ctxs.get(); Req ctxReq = ctx != null ? (Req) ctx.exchange() : null; U.must(ctx == null || !ctx.isClosed(), "The context for request (%s) is closed!"); U.must(req == ctxReq, "The customization request (%s) doesn't match the context request (%s)!", req, ctxReq); return true; }
public static void clearUserData(Req req) { if (Ctxs.hasContext()) { Ctxs.required().setUser(UserInfo.ANONYMOUS); } if (req.hasToken()) { Map<String, Serializable> token = req.token(); token.remove(Tokens._USER); token.remove(Tokens._SCOPE); } }
public static synchronized <T> T executeInCtx(CtxData cd, Callable<T> action) { Ctx ctx = Ctxs.open("call"); ctx.setApp(cd.app()); ctx.setExchange(null); ctx.setUser(new UserInfo(cd.username(), cd.roles())); U.assign(ctx.extras(), cd.extras()); try { return Lmbd.call(action); } finally { Ctxs.close(); } }
public String username() { return isLoggedIn() ? user().username : null; }
@SuppressWarnings("unchecked") public <T> T exchange() { ensureNotClosed(); return (T) exchange; }
public static Runnable wrap(Runnable job) { Ctx ctx = Ctxs.get(); if (ctx != null) { // U.notNull(ctx.app(), "Application wasn't attached to the context!"); Object x = ctx.exchange(); if (x instanceof JobStatusListener) { ((JobStatusListener) x).onAsync(); } // increment reference counter ctx = ctx.span(); // currently the same ctx is returned } return new ContextPreservingJobWrapper(job, ctx); }
public static EntityManager currentEntityManager() { return Ctxs.required().persister(); }
@Override public void process(Channel ctx) { T exchange = pool(ctx.helper()).get(); assert Cls.instanceOf(exchange, exchangeType); exchange.reset(); exchange.setConnection(ctx); Ctxs.open("x-request"); try { Ctxs.ctx().setExchange(exchange); process(ctx, exchange); } finally { Ctxs.close(); } }
public static ContextPreservingJobWrapper wrap(Runnable job) { Ctx ctx = Ctxs.get(); if (ctx != null) { // increment reference counter ctx = ctx.span(); // currently the same ctx is returned } return new ContextPreservingJobWrapper(job, ctx); }
public static Ctx open(String tag) { Ctx ctx = new Ctx(tag); Log.debug("Opening context", "ctx", ctx); attach(ctx); return ctx; }
public static void close() { try { Ctx ctx = get(); if (ctx != null) { ctx.close(); } else { // don't throw error here, the context might be "double-closed" on shutdown Log.warn("The context was already closed!"); } } finally { CTXS.remove(); } }
@Override public void authorize(UserInfo user) { Ctxs.required().setUser(user); req.token().put(Tokens._USER, user.username); long ttl = Conf.TOKEN.entry("ttl").or(0); long expiresOn = ttl > 0 ? U.time() + ttl : Long.MAX_VALUE; req.token().put(Tokens._EXPIRES, expiresOn); req.tokenChanged.set(true); }
private static boolean inValidContext(Req req) { Ctx ctx = Ctxs.get(); Req ctxReq = ctx != null ? (Req) ctx.exchange() : null; U.must(ctx == null || !ctx.isClosed(), "The context for request (%s) is closed!"); U.must(req == ctxReq, "The customization request (%s) doesn't match the context request (%s)!", req, ctxReq); return true; }
@Override public synchronized Map<String, Object> model() { if (model == null) { model = U.map("req", this, "data", lazyData, "files", lazyFiles, "cookies", lazyCookies, "headers", lazyHeaders); model.put("verb", verb()); model.put("uri", uri()); model.put("path", path()); model.put("home", home()); model.put("host", host()); model.put("dev", isDevMode()); WebApp app = AppCtx.app(); model.put("app", app); model.put("menu", app != null ? app.getMenu() : null); List<String> providers = U.list("google", "facebook", "linkedin", "github"); Map<String, Object> oauth = U.map("popup", true, "providers", providers); model.put("oauth", oauth); boolean loggedIn = Ctxs.ctx().isLoggedIn(); model.put("loggedIn", loggedIn); model.put("user", loggedIn ? Ctxs.ctx().user() : null); model.put("version", RapidoidInfo.version()); } return model; }
public Map<Object, Object> extras() { ensureNotClosed(); return extras; }