2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org
.argeo
.server
.jcr
.mvc
;
19 import javax
.jcr
.Session
;
21 import org
.apache
.commons
.logging
.Log
;
22 import org
.apache
.commons
.logging
.LogFactory
;
23 import org
.springframework
.ui
.ModelMap
;
24 import org
.springframework
.web
.context
.request
.RequestAttributes
;
25 import org
.springframework
.web
.context
.request
.WebRequest
;
26 import org
.springframework
.web
.context
.request
.WebRequestInterceptor
;
28 public class OpenSessionInViewJcrInterceptor
implements WebRequestInterceptor
,
30 private final static Log log
= LogFactory
31 .getLog(OpenSessionInViewJcrInterceptor
.class);
33 private Session session
;
35 public void preHandle(WebRequest request
) throws Exception
{
36 if (log
.isTraceEnabled())
37 log
.trace("preHandle: " + request
);
38 // Authentication auth = SecurityContextHolder.getContext()
39 // .getAuthentication();
41 // log.debug("auth=" + auth + ", authenticated="
42 // + auth.isAuthenticated() + ", name=" + auth.getName());
44 // log.debug("No auth");
46 // FIXME: find a safer way to initialize
47 // FIXME: not really needed to initialize here
48 // session.getRepository();
49 request
.setAttribute(REQUEST_ATTR_SESSION
, session
,
50 RequestAttributes
.SCOPE_REQUEST
);
53 public void postHandle(WebRequest request
, ModelMap model
) throws Exception
{
54 // if (log.isDebugEnabled())
55 // log.debug("postHandle: " + request);
58 public void afterCompletion(WebRequest request
, Exception ex
)
60 if (log
.isTraceEnabled())
61 log
.trace("afterCompletion: " + request
);
62 // FIXME: only close session that were open
66 public void setSession(Session session
) {
67 this.session
= session
;