public static RequestContext current() { RequestContext c = tryGetCurrent(); if(null == c) { throw new IllegalStateException("Current request context must be initialized"); } return c; }
/** * Returns the instance of {@link SecurityContext} in current request of returns <code>null</code> if the context not exists. */ public static SecurityContext tryGetCurrent(){ RequestContext rc = RequestContext.tryGetCurrent(); if(null == rc) { return null; }else{ return (SecurityContext)rc.getAttribute(CONTEXT_ATTRIBUTE_NAME); } }
/** * Returns current request's locale or use {@link Locales#DEFAULT_LOCALE} if no current locale. */ public static Locale locale() { RequestContext rc = tryGetCurrent(); if(null != rc && null != rc.getLocale()) { return rc.getLocale(); }else{ return Locales.DEFAULT_LOCALE; } }
@Override public Object require() { RequestContext rc = RequestContext.tryGetCurrent(); if(null == rc) { OAuth2ModuleImpl module = (OAuth2ModuleImpl)rc.getAttribute(OAuth2ModuleImpl.class.getName()); if(null != module) { return module; } } SecurityContext sc = SecurityContext.tryGetCurrent(); if(null == sc) { return EMPTY; } Authentication authc = sc.getAuthentication(); if(!(authc instanceof OAuth2Authentication)) { return EMPTY; } OAuth2ModuleImpl module = new OAuth2ModuleImpl(this, (OAuth2Authentication)authc); rc.setAttribute(OAuth2ModuleImpl.class.getName(), module); return module; }