]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jcr.mvc/src/main/java/org/argeo/jcr/mvc/OpenSessionInViewJcrInterceptor.java
f32acaa690077452b3d29f008e692220af1cc047
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jcr.mvc / src / main / java / org / argeo / jcr / mvc / OpenSessionInViewJcrInterceptor.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 package org.argeo.jcr.mvc;
18
19 import javax.jcr.Session;
20
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;
27
28 public class OpenSessionInViewJcrInterceptor implements WebRequestInterceptor,
29 JcrMvcConstants {
30 private final static Log log = LogFactory
31 .getLog(OpenSessionInViewJcrInterceptor.class);
32
33 private Session session;
34
35 public void preHandle(WebRequest request) throws Exception {
36 if (log.isTraceEnabled())
37 log.trace("preHandle: " + request);
38 // Authentication auth = SecurityContextHolder.getContext()
39 // .getAuthentication();
40 // if (auth != null)
41 // log.debug("auth=" + auth + ", authenticated="
42 // + auth.isAuthenticated() + ", name=" + auth.getName());
43 // else
44 // log.debug("No auth");
45
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);
51 }
52
53 public void postHandle(WebRequest request, ModelMap model) throws Exception {
54 // if (log.isDebugEnabled())
55 // log.debug("postHandle: " + request);
56 }
57
58 public void afterCompletion(WebRequest request, Exception ex)
59 throws Exception {
60 if (log.isTraceEnabled())
61 log.trace("afterCompletion: " + request);
62 // FIXME: only close session that were open
63 session.logout();
64 }
65
66 public void setSession(Session session) {
67 this.session = session;
68 }
69
70 }