use of org.polymap.core.runtime.session.SessionContext in project polymap4-core by Polymap4.
the class WebDavServer method createNewSession.
/**
* Initializes a new session for the given user.
* <p/>
* This method is called by {@link SecurityManagerAdapter}
*
* @param user
* @return The specified user.
*/
public static Principal createNewSession(final Principal user) {
HttpServletRequest req = io.milton.servlet.ServletRequest.getRequest();
final HttpSession session = req.getSession();
// HTTP session timeout: 30min
session.setMaxInactiveInterval(30 * 60);
FsPlugin.getDefault().sessionContextProvider.mapContext(user.getName(), true);
final SessionContext sessionContext = SessionContext.current();
// ContentManager
Locale locale = req.getLocale();
sessionContext.setAttribute("contentManager", ContentManager.forUser(user.getName(), locale, sessionContext));
// invalidate HTTP session when context is destroyed
sessionContext.addSessionListener(new ISessionListener() {
public void beforeDestroy() {
log.info("SessionContext is destroyed -> invalidating HTTP session");
try {
// sessionContext.removeSessionListener( this );
session.invalidate();
} catch (Exception e) {
log.warn("HTTP session already invalidated: " + e);
}
}
});
// session destroy listener
session.setAttribute("sessionListener", new HttpSessionBindingListener() {
public void valueBound(HttpSessionBindingEvent ev) {
}
public void valueUnbound(HttpSessionBindingEvent ev) {
//
sessionContext.execute(new Runnable() {
public void run() {
ContentManager.releaseSession(user.getName());
}
});
// prevent life-lock
if (!sessionContext.isDestroyed() && sessionContext.getAttribute("destroying") == null) {
sessionContext.setAttribute("destroying", true);
FsPlugin.getDefault().sessionContextProvider.destroyContext(sessionContext.getSessionKey());
log.info("HTTP Session destroyed: " + session.getId() + ", user: " + user);
}
}
});
log.info("New HTTP session: " + session.getId() + ", user: " + user);
return user;
}
use of org.polymap.core.runtime.session.SessionContext in project polymap4-core by Polymap4.
the class ServiceContext2 method execute.
public <E extends Exception> void execute(Task<E> task) throws E {
// XXX no GeoServerClassLoader; just one instance per JVM
// assert context.cl != null;
// ClassLoader orig = Thread.currentThread().getContextClassLoader();
// Thread.currentThread().setContextClassLoader( context.cl );
// assert Thread.currentThread().getContextClassLoader() == context.cl;
SessionContext current = SessionContext.current();
assert current == null : "Thread already mapped to a SessionContext: " + current.getSessionKey();
try {
boolean mapped = contextProvider.mapContext(sessionKey, true);
log.debug("SessionContext: " + SessionContext.current());
assert mapped : "Thread already mapped to a SessionContext: " + SessionContext.current().getSessionKey();
SecurityContext securityContext = SecurityContext.instance();
if (!securityContext.isLoggedIn()) {
// XXX this user is used to authenticate upstream mapzone services
securityContext.loginTrusted("admin");
}
task.call();
} finally {
// Thread.currentThread().setContextClassLoader( orig );
contextProvider.unmapContext();
}
}
use of org.polymap.core.runtime.session.SessionContext in project polymap4-core by Polymap4.
the class SecurityContext method tryLogin.
public boolean tryLogin() {
if (isLoggedIn()) {
throw new IllegalStateException("Already logged in for this SessionContext.");
}
try {
sc.login();
subject = sc.getSubject();
principals = new HashSet(subject.getPrincipals());
// find user
user = principals.stream().filter(p -> p instanceof UserPrincipal).findAny().orElseThrow(() -> new LoginException("Es wurde kein Nutzer in der Konfiguration gefunden"));
// allow to access the instance directly via current session (find user for example)
SessionContext.current().setAttribute("user", user);
// add roles of user to principals
Set<AuthorizationModule> authModules = subject.getPrivateCredentials(AuthorizationModule.class);
if (authModules.size() != 1) {
throw new RuntimeException("No AuthorizationModule specified. Is jaas_config.txt correct?");
}
principals.addAll(authModules.iterator().next().rolesOf(subject));
return true;
}// }
catch (LoginException e) {
// login failed
return false;
}
}
use of org.polymap.core.runtime.session.SessionContext in project polymap4-core by Polymap4.
the class EntityStateTracker method addListener.
/**
* @param handler An {@link IEntityStateListener} or any other
* {@link EventHandler annotated} object.
* @param filters
*/
public void addListener(Object handler, EventFilter... filters) {
SessionContext context = SessionContext.current();
assert context != null : "No context when registering IEntityStateListener!";
// if impl changes then check QiModule#addListener too!
EventManager.instance().subscribe(handler, filters);
}
Aggregations