felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vvalc...@apache.org
Subject svn commit: r933522 - in /felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal: Util.java servlet/ConfigurationListener2.java servlet/OsgiManager.java
Date Tue, 13 Apr 2010 08:52:24 GMT
Author: vvalchev
Date: Tue Apr 13 08:52:24 2010
New Revision: 933522

URL: http://svn.apache.org/viewvc?rev=933522&view=rev
Log:
Fixed FELIX-2267 /Improved locale detection/
https://issues.apache.org/jira/browse/FELIX-2267

Modified:
    felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/Util.java
    felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/ConfigurationListener2.java
    felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/OsgiManager.java

Modified: felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/Util.java
URL: http://svn.apache.org/viewvc/felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/Util.java?rev=933522&r1=933521&r2=933522&view=diff
==============================================================================
--- felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/Util.java (original)
+++ felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/Util.java Tue
Apr 13 08:52:24 2010
@@ -142,6 +142,43 @@ public class Util
         }
         return l;
     }
+    
+    /**
+     * This method expects a locale string in format language_COUNTRY, or
+     * language. The method will determine which is the correct form of locale
+     * string and construct a <code>Locale</code> object.
+     * 
+     * @param locale the locale string, if <code>null</code> - default locale
is
+     *          returned
+     * @return a locale object
+     * @see Locale
+     */
+    public static final Locale parseLocaleString(String locale)
+    {
+        if (locale == null)
+        {
+            return Locale.getDefault();
+        }
+        int idx = locale.indexOf('_');
+        String language;
+        String country = "";
+        if (idx < 0)
+        { // "en"
+            language = locale;
+        }
+        else
+        { // "en_US"
+            language = locale.substring(0, idx); // "en"
+            idx++; // "_"
+            int last = locale.indexOf('_', idx); // "US"
+            if (last < 0)
+            {
+                last = locale.length();
+            }
+            country = locale.substring(idx, last);
+        }
+        return new Locale(language, country);
+    }
 
     private static final class BundleNameComparator implements Comparator
     {

Modified: felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/ConfigurationListener2.java
URL: http://svn.apache.org/viewvc/felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/ConfigurationListener2.java?rev=933522&r1=933521&r2=933522&view=diff
==============================================================================
--- felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/ConfigurationListener2.java
(original)
+++ felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/ConfigurationListener2.java
Tue Apr 13 08:52:24 2010
@@ -94,6 +94,9 @@ class ConfigurationListener2 extends Con
             adList.add( new AttributeDefinitionImpl( OsgiManager.PROP_PASSWORD, "Password",
                 "The password for the user allowed to access the OSGi Management Console.",
                 OsgiManager.DEFAULT_PASSWORD ) );
+            adList.add( new AttributeDefinitionImpl( OsgiManager.PROP_LOCALE, "Locale",
+                "If set, this locale forces the localization to use this locale instead of
the one, requested by the web browser",
+                "" ) );
             adList.add( new AttributeDefinitionImpl( OsgiManager.PROP_LOG_LEVEL, "Log Level",
"Logging Level",
                 AttributeDefinition.INTEGER, new String[]
                     { String.valueOf( OsgiManager.DEFAULT_LOG_LEVEL ) }, 0, new String[]

Modified: felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/OsgiManager.java
URL: http://svn.apache.org/viewvc/felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/OsgiManager.java?rev=933522&r1=933521&r2=933522&view=diff
==============================================================================
--- felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/OsgiManager.java
(original)
+++ felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/OsgiManager.java
Tue Apr 13 08:52:24 2010
@@ -18,6 +18,7 @@ package org.apache.felix.webconsole.inte
 
 
 import java.io.IOException;
+import java.text.MessageFormat;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Dictionary;
@@ -34,13 +35,16 @@ import javax.servlet.GenericServlet;
 import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
+import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletRequestWrapper;
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.felix.webconsole.AbstractWebConsolePlugin;
 import org.apache.felix.webconsole.BrandingPlugin;
 import org.apache.felix.webconsole.WebConsoleConstants;
 import org.apache.felix.webconsole.internal.OsgiManagerPlugin;
+import org.apache.felix.webconsole.internal.Util;
 import org.apache.felix.webconsole.internal.core.BundlesServlet;
 import org.apache.felix.webconsole.internal.filter.FilteringResponseWrapper;
 import org.apache.felix.webconsole.internal.i18n.ResourceBundleManager;
@@ -90,6 +94,13 @@ public class OsgiManager extends Generic
      */
     public static final String PARAM_NO_REDIRECT_AFTER_ACTION = "_noredir_";
 
+    /**
+     * The name of the cookie storing user-configured locale
+     * See https://issues.apache.org/jira/browse/FELIX-2267
+     */
+    private static final String COOKIE_LOCALE = "felix.webconsole.locale";
+
+
     static final String PROP_MANAGER_ROOT = "manager.root";
 
     static final String PROP_DEFAULT_RENDER = "default.render";
@@ -104,6 +115,8 @@ public class OsgiManager extends Generic
 
     static final String PROP_LOG_LEVEL = "loglevel";
 
+    static final String PROP_LOCALE = "locale";
+
     public static final int DEFAULT_LOG_LEVEL = LogService.LOG_WARNING;
 
     static final String DEFAULT_PAGE = BundlesServlet.NAME;
@@ -163,6 +176,9 @@ public class OsgiManager extends Generic
 
     private Dictionary configuration;
 
+    // See https://issues.apache.org/jira/browse/FELIX-2267
+    private Locale configuredLocale;
+
     private Set enabledPlugins;
 
     ResourceBundleManager resourceBundleManager;
@@ -313,7 +329,7 @@ public class OsgiManager extends Generic
     }
 
 
-    private void service( final HttpServletRequest request, HttpServletResponse response
) throws ServletException,
+    private void service( HttpServletRequest request, HttpServletResponse response ) throws
ServletException,
         IOException
     {
         // check whether we are not at .../{webManagerRoot}
@@ -336,6 +352,7 @@ public class OsgiManager extends Generic
             slash = pathInfo.length();
         }
 
+        final Locale locale = getConfiguredLocale( request );
         final String label = pathInfo.substring( 1, slash );
         AbstractWebConsolePlugin plugin = holder.getPlugin( label );
 
@@ -349,7 +366,7 @@ public class OsgiManager extends Generic
 
         if ( plugin != null )
         {
-            final Map labelMap = holder.getLocalizedLabelMap( resourceBundleManager, request.getLocale()
);
+            final Map labelMap = holder.getLocalizedLabelMap( resourceBundleManager, locale
);
 
             // the official request attributes
             request.setAttribute( WebConsoleConstants.ATTR_LABEL_MAP, labelMap );
@@ -362,6 +379,7 @@ public class OsgiManager extends Generic
             request.setAttribute( ATTR_APP_ROOT_OLD, request.getContextPath() + request.getServletPath()
);
 
             // wrap the response for localization and template variable replacement
+            request = wrapRequest( request, locale );
             response = wrapResponse( request, response, plugin );
 
             plugin.service( request, response );
@@ -372,6 +390,30 @@ public class OsgiManager extends Generic
         }
     }
 
+    // See https://issues.apache.org/jira/browse/FELIX-2267
+    private final Locale getConfiguredLocale(HttpServletRequest request)
+    {
+        Locale locale = null;
+
+        Cookie[] cookies = request.getCookies();
+        for (int i = 0; cookies != null && i < cookies.length; i++)
+        {
+            if (COOKIE_LOCALE.equals(cookies[i].getName()))
+            {
+                locale = Util.parseLocaleString(cookies[i].getValue());
+                break;
+            }
+        }
+
+        // TODO: check UserAdmin ?
+
+        if (locale == null)
+            locale = configuredLocale;
+        if (locale == null)
+            locale = request.getLocale();
+
+        return locale;
+    }
 
     public void destroy()
     {
@@ -468,6 +510,17 @@ public class OsgiManager extends Generic
         }
     }
 
+    private HttpServletRequest wrapRequest( final HttpServletRequest request, final Locale
locale ) {
+        return new HttpServletRequestWrapper( request ) {
+            /**
+             * @see javax.servlet.ServletRequestWrapper#getLocale()
+             */
+            public Locale getLocale()
+            {
+                return locale;
+            }
+        };
+    }
 
     private HttpServletResponse wrapResponse( final HttpServletRequest request, final HttpServletResponse
response,
         final AbstractWebConsolePlugin plugin )
@@ -643,6 +696,10 @@ public class OsgiManager extends Generic
 
         configuration = config;
 
+        final Object locale = config.get( PROP_LOCALE );
+        configuredLocale = locale == null || locale.toString().trim().length() == 0 //
+            ? null : Util.parseLocaleString( locale.toString().trim() );
+
         logLevel = getProperty( config, PROP_LOG_LEVEL, DEFAULT_LOG_LEVEL );
         AbstractWebConsolePlugin.setLogLevel( logLevel );
 



Mime
View raw message