Author: julien(a)jboss.com
Date: 2007-03-13 19:29:15 -0400 (Tue, 13 Mar 2007)
New Revision: 6658
Removed:
trunk/portlet/src/main/org/jboss/portal/portlet/test/UserInterceptor.java
Modified:
trunk/core-cms/src/main/org/jboss/portal/core/cms/aspect/IdentityBindingInterceptor.java
trunk/core/src/main/org/jboss/portal/core/aspects/server/LocaleInterceptor.java
trunk/core/src/main/org/jboss/portal/core/aspects/server/UserInterceptor.java
trunk/core/src/main/org/jboss/portal/core/controller/ControllerContext.java
trunk/core/src/main/org/jboss/portal/core/controller/portlet/CachedUserImpl.java
trunk/core/src/main/org/jboss/portal/core/controller/portlet/ControllerUserContext.java
trunk/portlet/src/main/org/jboss/portal/portlet/test/PortletController.java
trunk/server/src/main/org/jboss/portal/server/ServerInvocation.java
trunk/server/src/main/org/jboss/portal/server/ServerRequest.java
Log:
simplified user/profile access in the code base, having the logic to fetch user for
request in one place.
Modified: trunk/core/src/main/org/jboss/portal/core/aspects/server/LocaleInterceptor.java
===================================================================
---
trunk/core/src/main/org/jboss/portal/core/aspects/server/LocaleInterceptor.java 2007-03-13
22:28:49 UTC (rev 6657)
+++
trunk/core/src/main/org/jboss/portal/core/aspects/server/LocaleInterceptor.java 2007-03-13
23:29:15 UTC (rev 6658)
@@ -23,18 +23,15 @@
package org.jboss.portal.core.aspects.server;
import org.jboss.portal.common.invocation.InvocationException;
-import org.jboss.portal.identity.User;
-import org.jboss.portal.identity.UserProfileModule;
-import org.jboss.portal.identity.UserModule;
import org.jboss.portal.server.ServerInterceptor;
import org.jboss.portal.server.ServerInvocation;
import org.jboss.portal.server.ServerRequest;
+import org.jboss.portal.identity.User;
-import javax.naming.InitialContext;
-import javax.naming.NameNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
+import java.util.Map;
/**
* If the user is authenticated and has a preferred locale then this one is chosen.
Otherwhise the locale used is the
@@ -52,25 +49,13 @@
// The locales
List locales = new ArrayList(4);
- // Try to get the locale from the user
- User user = (User)invocation.getRequest().getUser();
+ //
+ Map profile = (Map)invocation.getAttribute(ServerInvocation.PRINCIPAL_SCOPE,
UserInterceptor.PROFILE_KEY);
- UserProfileModule userProfileModule = null;
-
- //TODO: identity code change
- try
+ //
+ if (profile != null)
{
- userProfileModule = (UserProfileModule)new
InitialContext().lookup("java:portal/UserProfileModule");
- }
- catch (NameNotFoundException ignore)
- {
- // Name is not bound anymore, it could happen during a shutdown, we don't do
anything
- }
-
- if (user != null && userProfileModule != null)
- {
- //Locale locale = user.getPreferredLocale();
- Object lc = userProfileModule.getProperty(user,User.INFO_USER_LOCALE);
+ Object lc = profile.get(User.INFO_USER_LOCALE);
Locale locale = null;
if (lc instanceof Locale)
{
Modified: trunk/core/src/main/org/jboss/portal/core/aspects/server/UserInterceptor.java
===================================================================
---
trunk/core/src/main/org/jboss/portal/core/aspects/server/UserInterceptor.java 2007-03-13
22:28:49 UTC (rev 6657)
+++
trunk/core/src/main/org/jboss/portal/core/aspects/server/UserInterceptor.java 2007-03-13
23:29:15 UTC (rev 6658)
@@ -24,6 +24,7 @@
import org.jboss.logging.Logger;
import org.jboss.portal.common.invocation.InvocationException;
+import org.jboss.portal.common.invocation.AttributeResolver;
import org.jboss.portal.core.CoreConstants;
import org.jboss.portal.core.controller.portlet.CachedUserImpl;
import org.jboss.portal.identity.NoSuchUserException;
@@ -44,6 +45,7 @@
import java.security.Principal;
import java.util.Date;
import java.util.Map;
+import java.util.HashMap;
/**
* The interceptor is responsible for managing the user identity lifecycle based on the
principal name returned by the
@@ -58,6 +60,12 @@
public class UserInterceptor extends ServerInterceptor
{
+ /** . */
+ public static final String PROFILE_KEY = "profile";
+
+ /** . */
+ public static final String USER_KEY = "user";
+
/** Our logger. */
private static final Logger log = Logger.getLogger(UserInterceptor.class);
@@ -67,6 +75,7 @@
/** UserProfile */
protected UserProfileModule userProfileModule = null;
+ /** . */
protected boolean cacheUser = true;
public UserModule getUserModule()
@@ -113,19 +122,22 @@
HttpServletRequest req = invocation.getServerContext().getClientRequest();
HttpSession httpSession = req.getSession();
+ // Get scope
+ AttributeResolver principalScopeResolver =
invocation.getContext().getAttributeResolver(ServerInvocation.PRINCIPAL_SCOPE);
+
// Get the id
Principal userPrincipal = req.getUserPrincipal();
- // The user for the request
+ // The user and its profile
User user = null;
+ Map profile = null;
+
// Fetch user if we can
if (userPrincipal != null)
{
String userName = userPrincipal.getName();
-
-
//
try
{
@@ -134,27 +146,31 @@
log.trace("About to fetch user=" + userName);
}
- if (isCacheUser())
+ // Try to obtain cached user
+ user = (User)principalScopeResolver.getAttribute(USER_KEY);
+
+ //
+ if (user == null)
{
- //try to obtain cached user
- user =
(User)invocation.getContext().getAttributeResolver(ServerInvocation.PRINCIPAL_SCOPE).getAttribute("cachedUser");
+ // Fetch user info
+ user = getUserModule().findUserByUserName(userName);
- //create new cached one
- if (user == null)
- {
- user = getUserModule().findUserByUserName(userName);
- Map props = getUserProfileModule().getProperties(user);
- user = new CachedUserImpl(user.getId(), user.getUserName(), props);
+ // Get profile
+ profile = getUserProfileModule().getProperties(user);
- //cache user
- invocation.getContext().setAttribute(ServerInvocation.PRINCIPAL_SCOPE,
"cachedUser", user);
- }
+ // Build detached pojo
+ user = new CachedUserImpl(user.getId(), user.getUserName());
+
+ // Cache
+ invocation.getContext().setAttribute(ServerInvocation.PRINCIPAL_SCOPE,
USER_KEY, user);
+
+ // Get a detached object
+ profile = new HashMap(profile);
+
+ // Cache
+ invocation.getContext().setAttribute(ServerInvocation.PRINCIPAL_SCOPE,
PROFILE_KEY, profile);
}
- else
- {
- // Lookup
- user = getUserModule().findUserByUserName(userName);
- }
+
//
if (trace)
{
@@ -225,16 +241,16 @@
try
{
- // Attach the request user to the invocation
- invocation.getRequest().setUser(user);
-
// Continue the invocation
invocation.invokeNext();
}
finally
{
- // Clean up the request user
- invocation.getRequest().setUser(null);
+ if (!cacheUser)
+ {
+ principalScopeResolver.setAttribute(USER_KEY, null);
+ principalScopeResolver.setAttribute(PROFILE_KEY, null);
+ }
}
}
Modified: trunk/core/src/main/org/jboss/portal/core/controller/ControllerContext.java
===================================================================
--- trunk/core/src/main/org/jboss/portal/core/controller/ControllerContext.java 2007-03-13
22:28:49 UTC (rev 6657)
+++ trunk/core/src/main/org/jboss/portal/core/controller/ControllerContext.java 2007-03-13
23:29:15 UTC (rev 6658)
@@ -30,7 +30,10 @@
import org.jboss.portal.server.request.URLContext;
import org.jboss.portal.server.request.URLFormat;
import org.jboss.portal.identity.User;
+import org.jboss.portal.core.aspects.server.UserInterceptor;
+import java.util.Map;
+
/**
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision$
@@ -115,6 +118,11 @@
public User getUser()
{
- return (User)serverInvocation.getRequest().getUser();
+ return (User)getAttribute(ServerInvocation.PRINCIPAL_SCOPE,
UserInterceptor.USER_KEY);
}
+
+ public Map getUserProfile()
+ {
+ return (Map)getAttribute(ServerInvocation.PRINCIPAL_SCOPE,
UserInterceptor.PROFILE_KEY);
+ }
}
Modified:
trunk/core/src/main/org/jboss/portal/core/controller/portlet/CachedUserImpl.java
===================================================================
---
trunk/core/src/main/org/jboss/portal/core/controller/portlet/CachedUserImpl.java 2007-03-13
22:28:49 UTC (rev 6657)
+++
trunk/core/src/main/org/jboss/portal/core/controller/portlet/CachedUserImpl.java 2007-03-13
23:29:15 UTC (rev 6658)
@@ -27,31 +27,27 @@
import java.util.Map;
/**
- * Simple POJO to cache user data
+ * Simple POJO to cache user data.
*
* @author <a href="mailto:boleslaw dot dawidowicz at redhat anotherdot
com">Boleslaw Dawidowicz</a>
* @version $Revision: 0.1 $
*/
public class CachedUserImpl implements User
{
+
+ /** . */
private static final org.jboss.logging.Logger log =
org.jboss.logging.Logger.getLogger(CachedUserImpl.class);
+ /** . */
private Object id;
+ /** . */
private String name;
- private Map profile;
-
- //restrict constructor
- private CachedUserImpl()
+ public CachedUserImpl(Object id, String name)
{
- }
-
- public CachedUserImpl(Object id, String name, Map profile)
- {
this.id = id;
this.name = name;
- this.profile = profile;
}
public Object getId()
@@ -66,17 +62,11 @@
public void updatePassword(String password)
{
- log.error("Cached user. Password cannot be updated using this object. Obtain
User using UserModule.");
+ throw new UnsupportedOperationException("Cached user. Password cannot be
updated using this object. Obtain User using UserModule.");
}
public boolean validatePassword(String password)
{
- log.error("Cached user. Password cannot be validated using this object. Obtain
User using UserModule.");
- return false;
+ throw new UnsupportedOperationException("Cached user. Password cannot be
validated using this object. Obtain User using UserModule.");
}
-
- public Map getProfile()
- {
- return profile;
- }
}
Modified:
trunk/core/src/main/org/jboss/portal/core/controller/portlet/ControllerUserContext.java
===================================================================
---
trunk/core/src/main/org/jboss/portal/core/controller/portlet/ControllerUserContext.java 2007-03-13
22:28:49 UTC (rev 6657)
+++
trunk/core/src/main/org/jboss/portal/core/controller/portlet/ControllerUserContext.java 2007-03-13
23:29:15 UTC (rev 6658)
@@ -24,14 +24,10 @@
import org.jboss.portal.common.util.Tools;
import org.jboss.portal.identity.User;
-import org.jboss.portal.identity.UserProfileModule;
-import org.jboss.portal.identity.IdentityException;
import org.jboss.portal.portlet.spi.UserContext;
import org.jboss.portal.server.ServerInvocation;
import org.jboss.portal.core.controller.ControllerContext;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@@ -51,13 +47,16 @@
/** . */
private final User user;
+ /** . */
+ private final Map profile;
+
public ControllerUserContext(ControllerContext controllerContext)
{
this.invocation = controllerContext.getServerInvocation();
this.user = controllerContext.getUser();
+ this.profile = controllerContext.getUserProfile();
}
- /** This implementation returns the remote user value from the underlying http
request. */
public String getId()
{
return user != null ? user.getUserName() : null;
@@ -65,44 +64,7 @@
public Map getInformations()
{
- User user = getUser();
-
- //check if user has been cached and obtain profile from it.
- if (user instanceof CachedUserImpl)
- {
- return ((CachedUserImpl)user).getProfile();
- }
-
- //if user was not cached - obtain profile in traditional way.
-
- UserProfileModule userProfileModule = null;
-
- //MARK: identity code change
- try
- {
- userProfileModule = (UserProfileModule)new
InitialContext().lookup("java:portal/UserProfileModule");
- }
- catch (NamingException ignore)
- {
- // Name is not bound anymore, it could happen during a shutdown, we don't do
anything
- }
-
- if (user != null && userProfileModule != null)
- {
- try
- {
- return userProfileModule.getProperties(user);
- }
- catch (IdentityException e)
- {
- log.error("Cannot obtain user profile information: ", e);
- return null;
- }
- }
- else
- {
- return null;
- }
+ return profile;
}
public User getUser()
Modified:
trunk/core-cms/src/main/org/jboss/portal/core/cms/aspect/IdentityBindingInterceptor.java
===================================================================
---
trunk/core-cms/src/main/org/jboss/portal/core/cms/aspect/IdentityBindingInterceptor.java 2007-03-13
22:28:49 UTC (rev 6657)
+++
trunk/core-cms/src/main/org/jboss/portal/core/cms/aspect/IdentityBindingInterceptor.java 2007-03-13
23:29:15 UTC (rev 6658)
@@ -27,6 +27,7 @@
import org.jboss.portal.server.ServerInterceptor;
import org.jboss.portal.server.ServerInvocation;
import org.jboss.portal.common.invocation.InvocationException;
+import org.jboss.portal.core.aspects.server.UserInterceptor;
/**
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
@@ -41,7 +42,7 @@
{
// setup the portal user information to be used by the CMS Business Layer
// for fine grained access control enforcement
- User user = (User)invocation.getRequest().getUser();
+ User user = (User)invocation.getAttribute(ServerInvocation.PRINCIPAL_SCOPE,
UserInterceptor.USER_KEY);
JCRCMS.getUserInfo().set(user);
invocation.invokeNext();
}
Modified: trunk/portlet/src/main/org/jboss/portal/portlet/test/PortletController.java
===================================================================
--- trunk/portlet/src/main/org/jboss/portal/portlet/test/PortletController.java 2007-03-13
22:28:49 UTC (rev 6657)
+++ trunk/portlet/src/main/org/jboss/portal/portlet/test/PortletController.java 2007-03-13
23:29:15 UTC (rev 6658)
@@ -82,6 +82,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
+import java.util.HashMap;
/**
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
@@ -542,7 +543,7 @@
public Map getInformations()
{
- return
((UserInterceptor.User)serverInvocation.getRequest().getUser()).getInformations();
+ return new HashMap();
}
public Locale getLocale()
Deleted: trunk/portlet/src/main/org/jboss/portal/portlet/test/UserInterceptor.java
===================================================================
--- trunk/portlet/src/main/org/jboss/portal/portlet/test/UserInterceptor.java 2007-03-13
22:28:49 UTC (rev 6657)
+++ trunk/portlet/src/main/org/jboss/portal/portlet/test/UserInterceptor.java 2007-03-13
23:29:15 UTC (rev 6658)
@@ -1,81 +0,0 @@
-/******************************************************************************
- * JBoss, a division of Red Hat *
- * Copyright 2006, Red Hat Middleware, LLC, and individual *
- * contributors as indicated by the @authors tag. See the *
- * copyright.txt in the distribution for a full listing of *
- * individual contributors. *
- * *
- * This is free software; you can redistribute it and/or modify it *
- * under the terms of the GNU Lesser General Public License as *
- * published by the Free Software Foundation; either version 2.1 of *
- * the License, or (at your option) any later version. *
- * *
- * This software is distributed in the hope that it will be useful, *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
- * Lesser General Public License for more details. *
- * *
- * You should have received a copy of the GNU Lesser General Public *
- * License along with this software; if not, write to the Free *
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
- ******************************************************************************/
-package org.jboss.portal.portlet.test;
-
-import org.jboss.portal.common.invocation.InvocationException;
-import org.jboss.portal.server.ServerInterceptor;
-import org.jboss.portal.server.ServerInvocation;
-import org.jboss.portal.server.ServerRequest;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpSession;
-import java.io.Serializable;
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * The user interceptor is a simple implementation that is used to run the portlet
controller.
- *
- * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
- * @version $Revision$
- */
-public class UserInterceptor extends ServerInterceptor
-{
-
- protected void invoke(ServerInvocation invocation) throws Exception,
InvocationException
- {
- ServerRequest sreq = invocation.getRequest();
- HttpServletRequest hreq = invocation.getServerContext().getClientRequest();
- HttpSession session = hreq.getSession();
-
- //
- User ctx =
(User)session.getAttribute("org.jboss.portal.server.aspects.server#UserContext");
- if (ctx == null)
- {
- ctx = new User();
-
session.setAttribute("org.jboss.portal.server.aspects.server#UserContext",
ctx);
- }
-
- //
- try
- {
- sreq.setUser(ctx);
- invocation.invokeNext();
- }
- finally
- {
- sreq.setUser(null);
- }
- }
-
- public static class User implements Serializable
- {
- /** . */
- private Map informations = new HashMap();
-
- public Map getInformations()
- {
- return informations;
- }
- }
-}
Modified: trunk/server/src/main/org/jboss/portal/server/ServerInvocation.java
===================================================================
--- trunk/server/src/main/org/jboss/portal/server/ServerInvocation.java 2007-03-13
22:28:49 UTC (rev 6657)
+++ trunk/server/src/main/org/jboss/portal/server/ServerInvocation.java 2007-03-13
23:29:15 UTC (rev 6658)
@@ -45,9 +45,6 @@
/** . */
public static final Scope NAVIGATIONAL_STATE_SCOPE = new
Scope("navigationalstate");
- /** . */
- public static final String IDENTITY_KEY = "identity";
-
/** The request. */
private ServerRequest req;
Modified: trunk/server/src/main/org/jboss/portal/server/ServerRequest.java
===================================================================
--- trunk/server/src/main/org/jboss/portal/server/ServerRequest.java 2007-03-13 22:28:49
UTC (rev 6657)
+++ trunk/server/src/main/org/jboss/portal/server/ServerRequest.java 2007-03-13 23:29:15
UTC (rev 6658)
@@ -31,9 +31,6 @@
public class ServerRequest
{
- /** The request user. */
- protected Object user;
-
/** The locales for the scope of the current request. */
protected Locale[] locales;
@@ -47,26 +44,6 @@
this.invocationContext = invocationContext;
}
- public Object getUser()
- {
- return user;
- }
-
- /**
- * Associate a user object with the current request. If the user is null, it is
equivalent to remove it.
- * The underlying server invocation will have the user object bound into the
<code>ServerInvocation.REQUEST_SCOPE</code>
- * using the <code>ServerInvocation.USER_IDENTITY_KEY</code> name.
- *
- * @param user the request user
- */
- public void setUser(Object user)
- {
- this.user = user;
-
- //
- invocationContext.setAttribute(ServerInvocation.REQUEST_SCOPE,
ServerInvocation.IDENTITY_KEY, user);
- }
-
public Server getServer()
{
return server;