Search in sources :

Example 1 with SessionProvider

use of org.apache.jackrabbit.server.SessionProvider in project jackrabbit by apache.

the class DavexServletService method getSession.

//-----------------------------------------------------< SessionProvider >
/**
     * Asks each available session provider in order for a session and
     * returns the first session given. The default provider is used
     * if no custom provider service is available or can provide a requested
     * session.
     */
public synchronized Session getSession(HttpServletRequest request, Repository repository, String workspace) throws LoginException, ServletException, RepositoryException {
    SessionProvider provider = null;
    Session session = null;
    for (Map.Entry<SessionProvider, Set<Session>> entry : providers.entrySet()) {
        provider = entry.getKey();
        session = provider.getSession(request, repository, workspace);
        if (session != null) {
            entry.getValue().add(session);
            break;
        }
    }
    if (session == null) {
        provider = super.getSessionProvider();
        session = provider.getSession(request, repository, workspace);
    }
    if (session != null) {
        sessions.put(session, provider);
    }
    return session;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) SessionProvider(org.apache.jackrabbit.server.SessionProvider) Session(javax.jcr.Session)

Example 2 with SessionProvider

use of org.apache.jackrabbit.server.SessionProvider in project sling by apache.

the class SlingDavExServlet method getSessionProvider.

@Override
protected SessionProvider getSessionProvider() {
    return new SessionProvider() {

        public Session getSession(final HttpServletRequest req, final Repository repository, final String workspace) throws LoginException, RepositoryException, ServletException {
            final ResourceResolver resolver = (ResourceResolver) req.getAttribute(AuthenticationSupport.REQUEST_ATTRIBUTE_RESOLVER);
            if (resolver != null) {
                final Session session = resolver.adaptTo(Session.class);
                if (session != null) {
                    final Session newSession = getLongLivedSession(session);
                    log.debug("getSession: Creating new Session ({}) for {}", newSession, newSession.getUserID());
                    return newSession;
                }
            }
            throw new ServletException("ResourceResolver missing or not providing on JCR Session");
        }

        public void releaseSession(final Session session) {
            log.debug("releaseSession: Logging out long lived Session ({})", session);
            session.logout();
        }

        /**
             * Creates a new session for the user of the slingSession in the
             * same workspace as the slingSession.
             * <p>
             * Assumption: The admin session has permission to impersonate
             * as any user without restriction. If this is not the case
             * the Session.impersonate method throws a LoginException
             * which is folded into a RepositoryException.
             *
             * @param slingSession The session provided by the Sling
             *            authentication mechanis,
             * @return a new session which may (and will) outlast the request
             * @throws RepositoryException If an error occurrs creating the
             *             session.
             */
        private Session getLongLivedSession(final Session slingSession) throws RepositoryException {
            Session adminSession = null;
            final String user = slingSession.getUserID();
            try {
                final SimpleCredentials credentials = new SimpleCredentials(user, EMPTY_PW);
                final String wsp = slingSession.getWorkspace().getName();
                adminSession = SlingDavExServlet.this.repository.loginAdministrative(wsp);
                return adminSession.impersonate(credentials);
            } catch (RepositoryException re) {
                // cause a 403/FORBIDDEN response
                throw new RepositoryException("Cannot get session for " + user, re);
            } finally {
                if (adminSession != null) {
                    adminSession.logout();
                }
            }
        }
    };
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) SimpleCredentials(javax.jcr.SimpleCredentials) SlingRepository(org.apache.sling.jcr.api.SlingRepository) Repository(javax.jcr.Repository) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) RepositoryException(javax.jcr.RepositoryException) SessionProvider(org.apache.jackrabbit.server.SessionProvider) Session(javax.jcr.Session)

Aggregations

Session (javax.jcr.Session)2 SessionProvider (org.apache.jackrabbit.server.SessionProvider)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 Repository (javax.jcr.Repository)1 RepositoryException (javax.jcr.RepositoryException)1 SimpleCredentials (javax.jcr.SimpleCredentials)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 SlingRepository (org.apache.sling.jcr.api.SlingRepository)1