1 package org
.argeo
.cms
.internal
.http
;
3 import java
.io
.Serializable
;
4 import java
.security
.PrivilegedExceptionAction
;
6 import javax
.jcr
.Repository
;
7 import javax
.jcr
.RepositoryException
;
8 import javax
.jcr
.Session
;
9 import javax
.security
.auth
.Subject
;
10 import javax
.security
.auth
.login
.LoginContext
;
11 import javax
.servlet
.ServletException
;
12 import javax
.servlet
.http
.HttpServletRequest
;
14 import org
.apache
.commons
.logging
.Log
;
15 import org
.apache
.commons
.logging
.LogFactory
;
16 import org
.apache
.jackrabbit
.server
.SessionProvider
;
17 import org
.argeo
.cms
.CmsException
;
18 import org
.argeo
.jcr
.JcrUtils
;
19 import org
.argeo
.node
.NodeConstants
;
22 * Implements an open session in view patter: a new JCR session is created for
25 class OpenInViewSessionProvider
implements SessionProvider
, Serializable
{
26 private final static Log log
= LogFactory
.getLog(OpenInViewSessionProvider
.class);
28 private static final long serialVersionUID
= 2270957712453841368L;
29 private final String alias
;
31 public OpenInViewSessionProvider(String alias
) {
35 public Session
getSession(HttpServletRequest request
, Repository rep
, String workspace
)
36 throws javax
.jcr
.LoginException
, ServletException
, RepositoryException
{
37 return login(request
, rep
, workspace
);
40 protected Session
login(HttpServletRequest request
, Repository repository
, String workspace
)
41 throws RepositoryException
{
42 if (log
.isTraceEnabled())
43 log
.trace("Repo " + alias
+ ", login to workspace " + (workspace
== null ?
"<default>" : workspace
)
44 + " in web session " + request
.getSession().getId());
45 LoginContext lc
= (LoginContext
) request
.getAttribute(NodeConstants
.LOGIN_CONTEXT_USER
);
47 throw new CmsException("No login context available");
49 // LoginContext lc = new
50 // LoginContext(NodeConstants.LOGIN_CONTEXT_USER,
51 // new HttpRequestCallbackHandler(request));
53 return Subject
.doAs(lc
.getSubject(), new PrivilegedExceptionAction
<Session
>() {
55 public Session
run() throws Exception
{
56 return repository
.login(workspace
);
59 } catch (Exception e
) {
60 throw new CmsException("Cannot log in to JCR", e
);
62 // return repository.login(workspace);
65 public void releaseSession(Session session
) {
66 JcrUtils
.logoutQuietly(session
);
67 if (log
.isTraceEnabled())
68 log
.trace("Logged out remote JCR session " + session
);