Fixed FELIX-2267 /Improved locale detection/
https://issues.apache.org/jira/browse/FELIX-2267
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@933522 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/Util.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/Util.java
index e39c4a9..c90cb63 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/Util.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/internal/Util.java
@@ -142,6 +142,43 @@
}
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
{
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/ConfigurationListener2.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/ConfigurationListener2.java
index ce31212..05c9209 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/ConfigurationListener2.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/ConfigurationListener2.java
@@ -94,6 +94,9 @@
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[]
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/OsgiManager.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/OsgiManager.java
index a187ffbc..ffdc188 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/OsgiManager.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/OsgiManager.java
@@ -18,6 +18,7 @@
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.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 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 @@
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 @@
private Dictionary configuration;
+ // See https://issues.apache.org/jira/browse/FELIX-2267
+ private Locale configuredLocale;
+
private Set enabledPlugins;
ResourceBundleManager resourceBundleManager;
@@ -313,7 +329,7 @@
}
- 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 @@
slash = pathInfo.length();
}
+ final Locale locale = getConfiguredLocale( request );
final String label = pathInfo.substring( 1, slash );
AbstractWebConsolePlugin plugin = holder.getPlugin( label );
@@ -349,7 +366,7 @@
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 @@
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 @@
}
}
+ // 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 @@
}
}
+ 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 @@
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 );