1 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 ; 25 import javax.naming.NamingException ; 26 27 31 public class UserContextInterceptor implements Interceptor 32 { 33 34 35 private static final Logger log = Logger.getLogger(UserContextInterceptor.class); 36 37 public Object invoke(Invocation invocation) 38 { 39 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 String remoteUser = req.getRemoteUser(); 50 51 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 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 invocation.setAttachment(AttachmentKey.USER_CONTEXT, userContext); 90 91 return invocation.invokeNext(); 93 } 94 finally 95 { 96 invocation.removeAttachment(AttachmentKey.USER_CONTEXT); 98 99 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 ().lookup("java:portal/SessionFactory"); 109 Session session = sf.getCurrentSession(); 110 return session; 111 } 112 catch (NamingException e) 113 { 114 e.printStackTrace(); throw new RuntimeException ("Cannot get session"); 116 } 117 } 118 } 119 | Popular Tags |