KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > server > invocation > portal > 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.server.invocation.portal;
10
11 import org.jboss.portal.server.PortalRequest;
12 import org.jboss.portal.server.impl.user.UserContextImpl;
13 import org.jboss.portal.server.invocation.AttachmentKey;
14 import org.jboss.portal.server.invocation.Interceptor;
15 import org.jboss.portal.server.invocation.Invocation;
16 import org.jboss.portal.server.user.UserContext;
17
18 /**
19  * This interceptor maintains the user context object in the invocation.
20  *
21  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
22  * @version $Revision: 1.1.1.1 $
23  */

24 public class UserContextInterceptor implements Interceptor
25 {
26
27    private static final String JavaDoc SESSION_KEY_USER_CONTEXT = "USER_CONTEXT";
28
29    public Object JavaDoc invoke(Invocation invocation)
30    {
31       PortalRequest req = (PortalRequest)invocation.getAttachment(AttachmentKey.PORTAL_REQUEST);
32
33       // Get the current context or create one if needed
34
UserContext userContext = null;
35       userContext = (UserContext)req.getSession().getAttribute(SESSION_KEY_USER_CONTEXT);
36       if (userContext == null)
37       {
38          userContext = new UserContextImpl();
39          req.getSession().setAttribute(SESSION_KEY_USER_CONTEXT, userContext);
40       }
41
42       try
43       {
44          // Attach the context to the invocation
45
invocation.setAttachment(AttachmentKey.USER_CONTEXT, userContext);
46
47          // Continue the invocation
48
return invocation.invokeNext();
49       }
50       finally
51       {
52          // Remove the context to the invocation
53
invocation.removeAttachment(AttachmentKey.USER_CONTEXT);
54
55          // Store again in the session the session may have been invalidated during request
56
req.getSession().setAttribute(SESSION_KEY_USER_CONTEXT, userContext);
57       }
58    }
59 }
60
Popular Tags