portals-jetspeed-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From shins...@apache.org
Subject svn commit: r209904 - in /portals/jetspeed-2/trunk: applications/pam/src/java/org/apache/jetspeed/portlets/localeselector/ commons/src/java/org/apache/jetspeed/util/ components/portal/src/java/org/apache/jetspeed/localization/impl/ portal-webapp/src/we...
Date Fri, 08 Jul 2005 23:00:00 GMT
Author: shinsuke
Date: Fri Jul  8 15:59:55 2005
New Revision: 209904

URL: http://svn.apache.org/viewcvs?rev=209904&view=rev
Log:
JS2-305 http://issues.apache.org/jira/browse/JS2-305

Modified:
    portals/jetspeed-2/trunk/applications/pam/src/java/org/apache/jetspeed/portlets/localeselector/LocaleSelectorPortlet.java
    portals/jetspeed-2/trunk/commons/src/java/org/apache/jetspeed/util/JetspeedLocale.java
    portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/localization/impl/LocalizationValveImpl.java
    portals/jetspeed-2/trunk/portal-webapp/src/webapp/WEB-INF/assembly/pipelines.xml

Modified: portals/jetspeed-2/trunk/applications/pam/src/java/org/apache/jetspeed/portlets/localeselector/LocaleSelectorPortlet.java
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/applications/pam/src/java/org/apache/jetspeed/portlets/localeselector/LocaleSelectorPortlet.java?rev=209904&r1=209903&r2=209904&view=diff
==============================================================================
--- portals/jetspeed-2/trunk/applications/pam/src/java/org/apache/jetspeed/portlets/localeselector/LocaleSelectorPortlet.java
(original)
+++ portals/jetspeed-2/trunk/applications/pam/src/java/org/apache/jetspeed/portlets/localeselector/LocaleSelectorPortlet.java
Fri Jul  8 15:59:55 2005
@@ -17,6 +17,7 @@
 
 import java.io.IOException;
 import java.util.Locale;
+import java.util.prefs.Preferences;
 
 import javax.portlet.ActionRequest;
 import javax.portlet.ActionResponse;
@@ -29,24 +30,37 @@
 import javax.portlet.RenderRequest;
 import javax.portlet.RenderResponse;
 
+import org.apache.jetspeed.CommonPortletServices;
 import org.apache.jetspeed.PortalReservedParameters;
 import org.apache.jetspeed.request.RequestContext;
+import org.apache.jetspeed.security.SecurityException;
+import org.apache.jetspeed.security.User;
+import org.apache.jetspeed.security.UserManager;
+import org.apache.jetspeed.util.JetspeedLocale;
 
 /**
- * 
+ * This is the portlet to select user's preferred locale.
  * 
  * @author <a href="mailto:shinsuke@yahoo.co.jp">Shinsuke Sugaya</a>
+ * @version $Id$
  */
 public class LocaleSelectorPortlet extends GenericPortlet
 {
     public static final String PREFERED_LOCALE_SESSION_KEY = "org.apache.jetspeed.prefered.locale";
 
+    private UserManager userManager;
+    
     /* (non-Javadoc)
      * @see javax.portlet.Portlet#init(javax.portlet.PortletConfig)
      */
     public void init(PortletConfig config) throws PortletException
     {
         super.init(config);
+        userManager = (UserManager)getPortletContext().getAttribute(CommonPortletServices.CPS_USER_MANAGER_COMPONENT);
+        if (null == userManager)
+        {
+            throw new PortletException("Failed to find the User Manager on portlet initialization");
+        }
     }
 
     /* (non-Javadoc)
@@ -78,30 +92,49 @@
 
         if (language != null)
         {
-			String[] localeArray = language.split("[-|_]");
+            String[] localeArray = language.split("[-|_]");
             String country = "";
             String variant = "";
-			for(int i=0; i<localeArray.length; i++)
-			{
-				if(i == 0)
-				{
-					language = localeArray[i];
-				}
-				else if(i == 1)
-				{
-					country = localeArray[i];
-				}
-				else if(i == 2)
-				{
-					variant = localeArray[i];
-				}
-			}
-            
+            for (int i = 0; i < localeArray.length; i++)
+            {
+                if (i == 0)
+                {
+                    language = localeArray[i];
+                }
+                else if (i == 1)
+                {
+                    country = localeArray[i];
+                }
+                else if (i == 2)
+                {
+                    variant = localeArray[i];
+                }
+            }
 
-            // TODO Set the prefered locale to user's persistent storage if not anon user
             Locale preferedLocale = new Locale(language, country, variant);
-            session.setAttribute(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE, preferedLocale,PortletSession.APPLICATION_SCOPE);
-            RequestContext requestContext = (RequestContext) request.getAttribute(PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE);
+
+            if (request.getRemoteUser() != null)
+            {
+                // Set the prefered locale to user's perferences(persistent storage) if not
anon user
+                try
+                {
+                    User user = userManager.getUser(request.getRemoteUser());
+                    // TODO if preferred lang or locale is defined in PLT.D, it's better
to use it
+                    Preferences prefs = user.getPreferences();
+                    prefs.put(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE, JetspeedLocale
+                            .convertLocaleToString(preferedLocale));
+                }
+                catch (SecurityException e)
+                {
+                    // TODO Auto-generated catch block
+                    e.printStackTrace();
+                }
+            }
+
+            session.setAttribute(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE, preferedLocale,
+                    PortletSession.APPLICATION_SCOPE);
+            RequestContext requestContext = (RequestContext) request
+                    .getAttribute(PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE);
             requestContext.setLocale(preferedLocale);
             requestContext.setSessionAttribute(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE,
preferedLocale);
         }

Modified: portals/jetspeed-2/trunk/commons/src/java/org/apache/jetspeed/util/JetspeedLocale.java
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/commons/src/java/org/apache/jetspeed/util/JetspeedLocale.java?rev=209904&r1=209903&r2=209904&view=diff
==============================================================================
--- portals/jetspeed-2/trunk/commons/src/java/org/apache/jetspeed/util/JetspeedLocale.java
(original)
+++ portals/jetspeed-2/trunk/commons/src/java/org/apache/jetspeed/util/JetspeedLocale.java
Fri Jul  8 15:59:55 2005
@@ -17,20 +17,18 @@
 package org.apache.jetspeed.util;
 
 import java.util.Locale;
+import java.util.StringTokenizer;
 
 /**
  * Class to set and get Locale settings for Jetspeed.
- * 
- * NOTE:    The defaults are read from the system (java.util.Locale) but it should be
- *          enhanced so that it reads it from the properties. 
  *          
  * @author <a href="mailto:roger.ruttimann@earthlink.net">Roger Ruttimann</a>
+ * @author <a href="mailto:shinsuke@yahoo.co.jp">Shinsuke Sugaya</a>
  * @version $Id$
-
  */
 public class JetspeedLocale
 {
-    // TODO We need to get this from the properties
+    private static final String DELIM = ",";
     
     /**
      * According to PLT.21.8.1, the default locale should be English.
@@ -39,4 +37,89 @@
     {
         return Locale.ENGLISH;
     }
+
+
+    /**
+     * Converts Locale to String.
+     * 
+     * @param locale
+     * @return
+     */
+    public static String convertLocaleToString(Locale locale)
+    {
+        if (locale == null)
+        {
+            return null;
+        }
+        String country = locale.getCountry();
+        String language = locale.getLanguage();
+        String variant = locale.getVariant();
+        StringBuffer buffer = new StringBuffer(40);
+        if (language != null)
+        {
+            buffer.append(language);
+        }
+        buffer.append(DELIM);
+
+        if (country != null)
+        {
+            buffer.append(country);
+        }
+        buffer.append(DELIM);
+
+        if (variant != null)
+        {
+            buffer.append(variant);
+        }
+
+        return buffer.toString().trim();
+    }
+
+    /**
+     * Converts String to Locale.
+     * 
+     * @param localeString
+     * @return
+     */
+    public static Locale convertStringToLocale(String localeString)
+    {
+        if (localeString == null)
+        {
+            return null;
+        }
+        StringTokenizer tokenizer = new StringTokenizer(localeString, DELIM);
+
+        String language = tokenizer.nextToken().trim();
+        String country = null;
+        String variant = null;
+        if (tokenizer.hasMoreTokens())
+        {
+            country = tokenizer.nextToken().trim();
+        }
+
+        if (tokenizer.hasMoreTokens())
+        {
+            variant = tokenizer.nextToken().trim();
+        }
+
+        if (country != null && language != null && variant != null)
+        {
+            return new Locale(language, country, variant);
+        }
+        else if (country != null && language != null)
+        {
+            return new Locale(language, country);
+        }
+        else if (language != null)
+        {
+            return new Locale(language, ""); // JDK 1.3 compatibility
+        }
+        else
+        {
+            return null;
+        }
+
+    }
+
+
 }

Modified: portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/localization/impl/LocalizationValveImpl.java
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/localization/impl/LocalizationValveImpl.java?rev=209904&r1=209903&r2=209904&view=diff
==============================================================================
--- portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/localization/impl/LocalizationValveImpl.java
(original)
+++ portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/localization/impl/LocalizationValveImpl.java
Fri Jul  8 15:59:55 2005
@@ -15,11 +15,16 @@
  */
 package org.apache.jetspeed.localization.impl;
 
+import java.security.Principal;
 import java.util.Enumeration;
 import java.util.Locale;
+import java.util.prefs.Preferences;
+
+import javax.security.auth.Subject;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.jetspeed.Jetspeed;
 import org.apache.jetspeed.PortalReservedParameters;
 import org.apache.jetspeed.i18n.CurrentLocale;
 import org.apache.jetspeed.pipeline.PipelineException;
@@ -27,6 +32,12 @@
 import org.apache.jetspeed.pipeline.valve.LocalizationValve;
 import org.apache.jetspeed.pipeline.valve.ValveContext;
 import org.apache.jetspeed.request.RequestContext;
+import org.apache.jetspeed.security.SecurityException;
+import org.apache.jetspeed.security.SecurityHelper;
+import org.apache.jetspeed.security.User;
+import org.apache.jetspeed.security.UserManager;
+import org.apache.jetspeed.security.UserPrincipal;
+import org.apache.jetspeed.util.JetspeedLocale;
 
 /**
  * LocalizationValveImpl
@@ -86,11 +97,44 @@
      */
     public void invoke( RequestContext request, ValveContext context ) throws PipelineException
     {
-        // TODO Get the prefered locale from user's persistent storage if not anon user
+        Locale locale = null;
 
-        Locale locale =
-            (Locale) request.getSessionAttribute(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE);
+        // Get the prefered locale from user's preferences(persistent storage) if not anon
user
+        Subject subject = request.getSubject();
+        if (null != subject)
+        {
+            Principal userPrincipal = SecurityHelper.getPrincipal(subject, UserPrincipal.class);
+            if (null != userPrincipal)
+            {
+                log.debug("Got user principal: " + userPrincipal.getName());
+                UserManager userMgr = (UserManager) Jetspeed.getComponentManager().getComponent(UserManager.class);
+                try
+                {
+                    if (!userMgr.getAnonymousUser().equals(userPrincipal.getName())
+                            && userMgr.userExists(userPrincipal.getName()))
+                    {
+                        User user = userMgr.getUser(userPrincipal.getName());
+                        // TODO if preferred lang or locale is defined in PLT.D, it's better
to use it
+                        Preferences prefs = user.getPreferences();
+                        String localeString = prefs.get(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE,
null);
+                        if (localeString != null)
+                        {
+                            locale = JetspeedLocale.convertStringToLocale(localeString);
+                        }
+                    }
+                }
+                catch (SecurityException e)
+                {
+                    log.warn("Unexpected SecurityException in UserInfoManager", e);
+                }
+            }
+        }
 
+        if (locale == null)
+        {
+            locale = (Locale) request.getSessionAttribute(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE);
+        }
+        
         if ( locale == null && defaultLocale != null )
         {
             locale = defaultLocale;

Modified: portals/jetspeed-2/trunk/portal-webapp/src/webapp/WEB-INF/assembly/pipelines.xml
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/portal-webapp/src/webapp/WEB-INF/assembly/pipelines.xml?rev=209904&r1=209903&r2=209904&view=diff
==============================================================================
--- portals/jetspeed-2/trunk/portal-webapp/src/webapp/WEB-INF/assembly/pipelines.xml (original)
+++ portals/jetspeed-2/trunk/portal-webapp/src/webapp/WEB-INF/assembly/pipelines.xml Fri Jul
 8 15:59:55 2005
@@ -139,10 +139,10 @@
    </constructor-arg>
    <constructor-arg>
     <list>
-      <ref bean="localizationValve"/>
       <ref bean="capabilityValve"/>
       <ref bean="portalURLValve"/>
       <ref bean="securityValve"/>
+      <ref bean="localizationValve"/>
       <ref bean="passwordCredentialValve"/>
       <ref bean="loginValidationValve"/>
       <ref bean="profilerValve"/>
@@ -181,10 +181,10 @@
    </constructor-arg>
    <constructor-arg>
     <list>
-    	<ref bean="localizationValve"/>
         <ref bean="portalURLValve"/>    	    	
     	<ref bean="capabilityValve"/>
     	<ref bean="securityValve"/>    	    	
+    	<ref bean="localizationValve"/>
         <ref bean="profilerValve"/>    	    	    	
     	<ref bean="containerValve"/>
     	<ref bean="portletValve"/>    
@@ -193,4 +193,4 @@
   </bean> 
   
   
-</beans>
\ No newline at end of file
+</beans>



---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@portals.apache.org
For additional commands, e-mail: jetspeed-dev-help@portals.apache.org


Mime
View raw message