KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > core > invocation > UserContextInterceptor


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.core.invocation;
10
11 import org.jboss.portal.core.CoreConstants;
12 import org.jboss.portal.core.impl.user.UserContextImpl;
13 import org.jboss.portal.core.impl.user.UserImpl;
14 import org.jboss.portal.server.PortalRequest;
15 import org.jboss.portal.server.invocation.AttachmentKey;
16 import org.jboss.portal.server.invocation.Interceptor;
17 import org.jboss.portal.server.invocation.Invocation;
18 import org.jboss.portal.server.plugins.preferences.AbstractPreferenceStore;
19 import org.apache.log4j.Logger;
20 import org.hibernate.Session;
21 import org.hibernate.SessionFactory;
22 import org.hibernate.Query;
23
24 import javax.naming.InitialContext JavaDoc;
25 import javax.naming.NamingException JavaDoc;
26
27 /**
28  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
29  * @version $Revision: 1.9 $
30  */

31 public class UserContextInterceptor implements Interceptor
32 {
33
34    /** Our logger. */
35    private static final Logger log = Logger.getLogger(UserContextInterceptor.class);
36
37    public Object JavaDoc invoke(Invocation invocation)
38    {
39       // Get the current context or create one if needed
40
PortalRequest req = (PortalRequest)invocation.getAttachment(AttachmentKey.PORTAL_REQUEST);
41       UserContextImpl userContext = (UserContextImpl)req.getSession().getAttribute(CoreConstants.SES_USER_CONTEXT);
42       if (userContext == null)
43       {
44          userContext = new UserContextImpl();
45          req.getSession().setAttribute(CoreConstants.SES_USER_CONTEXT, userContext);
46       }
47
48       // Get the id
49
String JavaDoc remoteUser = req.getRemoteUser();
50
51       // Fetch user
52
UserImpl user = null;
53       try
54       {
55          if (remoteUser != null)
56          {
57             Session session = getSession();
58             Query query = session.createQuery("from UserImpl as u where u.userName=?");
59             query.setString(0, remoteUser);
60             user = (UserImpl)query.uniqueResult();
61          }
62       }
63       catch (Exception JavaDoc e)
64       {
65          log.error("Cannot fetch user from hibernate", e);
66       }
67
68       if (user != null)
69       {
70          userContext.setUser(user);
71          userContext.setSignedIn(true);
72          userContext.setInformations(user.getProperties());
73          userContext.setPreferenceStore(user.getPreferenceStore());
74       }
75       else
76       {
77          if (userContext.getPreferenceStore() == null)
78          {
79             userContext.setPreferenceStore(new AbstractPreferenceStore());
80          }
81          userContext.setUser(null);
82          userContext.setInformations(null);
83          userContext.setSignedIn(false);
84       }
85
86       try
87       {
88          // Attach the context to the invocation
89
invocation.setAttachment(AttachmentKey.USER_CONTEXT, userContext);
90
91          // Continue the invocation
92
return invocation.invokeNext();
93       }
94       finally
95       {
96          // Remove the context to the invocation
97
invocation.removeAttachment(AttachmentKey.USER_CONTEXT);
98
99          // Store again in the session the session may have been invalidated during request
100
req.getSession().setAttribute(CoreConstants.SES_USER_CONTEXT, userContext);
101       }
102    }
103
104    protected Session getSession()
105    {
106       try
107       {
108          SessionFactory sf = (SessionFactory)new InitialContext JavaDoc().lookup("java:portal/SessionFactory");
109          Session session = sf.getCurrentSession();
110          return session;
111       }
112       catch (NamingException JavaDoc e)
113       {
114          e.printStackTrace(); // FIXME
115
throw new RuntimeException JavaDoc("Cannot get session");
116       }
117    }
118 }
119
Popular Tags