struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject struts git commit: WW-4791 Stops using DefaultLocalizedTextProvider#localeFromString
Date Wed, 26 Apr 2017 08:38:41 GMT
Repository: struts
Updated Branches:
  refs/heads/master 8f75e1a19 -> f06caf7ca


WW-4791 Stops using DefaultLocalizedTextProvider#localeFromString


Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/f06caf7c
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/f06caf7c
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/f06caf7c

Branch: refs/heads/master
Commit: f06caf7ca7fc38c5c834e129ac18a483ef37c58d
Parents: 8f75e1a
Author: Lukasz Lenart <lukaszlenart@apache.org>
Authored: Wed Apr 26 10:38:32 2017 +0200
Committer: Lukasz Lenart <lukaszlenart@apache.org>
Committed: Wed Apr 26 10:38:32 2017 +0200

----------------------------------------------------------------------
 .../xwork2/DefaultLocaleProvider.java           | 11 +++++--
 .../util/DefaultLocalizedTextProvider.java      |  3 ++
 .../apache/struts2/dispatcher/Dispatcher.java   | 32 ++++++++++++--------
 .../struts2/interceptor/I18nInterceptor.java    |  6 ++--
 .../PropertiesConfigurationProviderTest.java    | 10 +++---
 .../struts2/dispatcher/DispatcherTest.java      |  1 -
 .../interceptor/I18nInterceptorTest.java        | 13 +-------
 .../portlet/dispatcher/Jsr168Dispatcher.java    | 29 ++++++++++++------
 8 files changed, 61 insertions(+), 44 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/f06caf7c/core/src/main/java/com/opensymphony/xwork2/DefaultLocaleProvider.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/opensymphony/xwork2/DefaultLocaleProvider.java b/core/src/main/java/com/opensymphony/xwork2/DefaultLocaleProvider.java
index 7fbc2bc..bdb6d7c 100644
--- a/core/src/main/java/com/opensymphony/xwork2/DefaultLocaleProvider.java
+++ b/core/src/main/java/com/opensymphony/xwork2/DefaultLocaleProvider.java
@@ -15,9 +15,10 @@
  */
 package com.opensymphony.xwork2;
 
-import com.opensymphony.xwork2.util.DefaultLocalizedTextProvider;
+import org.apache.commons.lang3.LocaleUtils;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.message.ParameterizedMessage;
 
 import java.util.Arrays;
 import java.util.Locale;
@@ -42,7 +43,13 @@ public class DefaultLocaleProvider implements LocaleProvider {
 
     @Override
     public boolean isValidLocaleString(String localeStr) {
-        return isValidLocale(DefaultLocalizedTextProvider.localeFromString(localeStr, getLocale()));
+        Locale locale = null;
+        try {
+            locale = LocaleUtils.toLocale(localeStr);
+        } catch (IllegalArgumentException e) {
+            LOG.warn(new ParameterizedMessage("Cannot convert [{}] to proper locale", localeStr,
e));
+        }
+        return isValidLocale(locale);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/struts/blob/f06caf7c/core/src/main/java/com/opensymphony/xwork2/util/DefaultLocalizedTextProvider.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/opensymphony/xwork2/util/DefaultLocalizedTextProvider.java
b/core/src/main/java/com/opensymphony/xwork2/util/DefaultLocalizedTextProvider.java
index b7d5314..61a8317 100644
--- a/core/src/main/java/com/opensymphony/xwork2/util/DefaultLocalizedTextProvider.java
+++ b/core/src/main/java/com/opensymphony/xwork2/util/DefaultLocalizedTextProvider.java
@@ -152,7 +152,10 @@ public class DefaultLocalizedTextProvider implements LocalizedTextProvider
{
      * @param localeStr     The locale String to parse.
      * @param defaultLocale The locale to use if localeStr is <tt>null</tt>.
      * @return requested Locale
+     *
+     * @deprecated please use {@link org.apache.commons.lang3.LocaleUtils#toLocale(String)}
      */
+    @Deprecated
     public static Locale localeFromString(String localeStr, Locale defaultLocale) {
         if ((localeStr == null) || (localeStr.trim().length() == 0) || ("_".equals(localeStr)))
{
             if (defaultLocale != null) {

http://git-wip-us.apache.org/repos/asf/struts/blob/f06caf7c/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java b/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java
index 3645241..667f37d 100644
--- a/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java
+++ b/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java
@@ -39,10 +39,12 @@ import com.opensymphony.xwork2.util.location.LocatableProperties;
 import com.opensymphony.xwork2.util.location.Location;
 import com.opensymphony.xwork2.util.location.LocationUtils;
 import com.opensymphony.xwork2.util.profiling.UtilTimerStack;
+import org.apache.commons.lang3.LocaleUtils;
 import org.apache.commons.lang3.BooleanUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.message.ParameterizedMessage;
 import org.apache.struts2.ServletActionContext;
 import org.apache.struts2.StrutsConstants;
 import org.apache.struts2.StrutsException;
@@ -672,14 +674,7 @@ public class Dispatcher {
         extraContext.put(ActionContext.SESSION, sessionMap);
         extraContext.put(ActionContext.APPLICATION, applicationMap);
 
-        Locale locale;
-        if (defaultLocale != null) {
-            locale = DefaultLocalizedTextProvider.localeFromString(defaultLocale, request.getLocale());
-        } else {
-            locale = request.getLocale();
-        }
-
-        extraContext.put(ActionContext.LOCALE, locale);
+        extraContext.put(ActionContext.LOCALE, getLocale(request));
 
         extraContext.put(StrutsStatics.HTTP_REQUEST, request);
         extraContext.put(StrutsStatics.HTTP_RESPONSE, response);
@@ -697,6 +692,22 @@ public class Dispatcher {
         return extraContext;
     }
 
+    protected Locale getLocale(HttpServletRequest request) {
+        Locale locale;
+        if (defaultLocale != null) {
+            try {
+                locale = LocaleUtils.toLocale(defaultLocale);
+            } catch (IllegalArgumentException e) {
+                LOG.warn(new ParameterizedMessage("Cannot convert 'struts.locale' = [{}]
to proper locale, defaulting to request locale [{}]",
+                                defaultLocale, request.getLocale()), e);
+                locale = request.getLocale();
+            }
+        } else {
+            locale = request.getLocale();
+        }
+        return locale;
+    }
+
     /**
      * Return the path to save uploaded files to (this is configurable).
      *
@@ -754,10 +765,7 @@ public class Dispatcher {
             encoding = "UTF-8";
         }
 
-        Locale locale = null;
-        if (defaultLocale != null) {
-            locale = DefaultLocalizedTextProvider.localeFromString(defaultLocale, request.getLocale());
-        }
+        Locale locale = getLocale(request);
 
         if (encoding != null) {
             applyEncoding(request, encoding);

http://git-wip-us.apache.org/repos/asf/struts/blob/f06caf7c/core/src/main/java/org/apache/struts2/interceptor/I18nInterceptor.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/struts2/interceptor/I18nInterceptor.java b/core/src/main/java/org/apache/struts2/interceptor/I18nInterceptor.java
index 722beb0..61a158c 100644
--- a/core/src/main/java/org/apache/struts2/interceptor/I18nInterceptor.java
+++ b/core/src/main/java/org/apache/struts2/interceptor/I18nInterceptor.java
@@ -25,7 +25,7 @@ import com.opensymphony.xwork2.LocaleProvider;
 import com.opensymphony.xwork2.LocaleProviderFactory;
 import com.opensymphony.xwork2.inject.Inject;
 import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
-import com.opensymphony.xwork2.util.DefaultLocalizedTextProvider;
+import org.apache.commons.lang.LocaleUtils;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.message.ParameterizedMessage;
@@ -166,7 +166,9 @@ public class I18nInterceptor extends AbstractInterceptor {
             } else {
                 String localeStr = requestedLocale.toString();
                 if (localeProvider.isValidLocaleString(localeStr)) {
-                    locale = DefaultLocalizedTextProvider.localeFromString(requestedLocale.toString(),
null);
+                    locale = LocaleUtils.toLocale(localeStr);
+                } else {
+                    locale = localeProvider.getLocale();
                 }
             }
             if (locale != null) {

http://git-wip-us.apache.org/repos/asf/struts/blob/f06caf7c/core/src/test/java/org/apache/struts2/config/PropertiesConfigurationProviderTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/struts2/config/PropertiesConfigurationProviderTest.java
b/core/src/test/java/org/apache/struts2/config/PropertiesConfigurationProviderTest.java
index 05606ce..b4fd408 100644
--- a/core/src/test/java/org/apache/struts2/config/PropertiesConfigurationProviderTest.java
+++ b/core/src/test/java/org/apache/struts2/config/PropertiesConfigurationProviderTest.java
@@ -26,6 +26,7 @@ import com.opensymphony.xwork2.inject.ContainerBuilder;
 import com.opensymphony.xwork2.util.DefaultLocalizedTextProvider;
 import com.opensymphony.xwork2.util.location.LocatableProperties;
 import junit.framework.TestCase;
+import org.apache.commons.lang3.LocaleUtils;
 import org.apache.struts2.StrutsConstants;
 
 import java.util.Locale;
@@ -41,7 +42,7 @@ public class PropertiesConfigurationProviderTest extends TestCase {
 
         ContainerBuilder builder = new ContainerBuilder();
         builder.constant("foo", "bar");
-        builder.constant("struts.locale", "DE_de");
+        builder.constant("struts.locale", "de_DE");
 
         PropertiesConfigurationProvider prov = new PropertiesConfigurationProvider();
         prov.register(builder, new LocatableProperties());
@@ -49,7 +50,7 @@ public class PropertiesConfigurationProviderTest extends TestCase {
         Container container = builder.create(true);
 
         String localeStr = container.getInstance(String.class, StrutsConstants.STRUTS_LOCALE);
-        Locale locale = DefaultLocalizedTextProvider.localeFromString(localeStr, Locale.FRANCE);
+        Locale locale = LocaleUtils.toLocale(localeStr);
 
         assertNotNull(locale);
         assertEquals("DE", locale.getCountry());
@@ -68,11 +69,8 @@ public class PropertiesConfigurationProviderTest extends TestCase {
         Container container = builder.create(true);
 
         String localeStr = container.getInstance(String.class, StrutsConstants.STRUTS_LOCALE);
-        Locale locale = DefaultLocalizedTextProvider.localeFromString(localeStr, Locale.getDefault());
 
-        assertNotNull(locale);
-        Locale vmLocale = Locale.getDefault();
-        assertEquals(locale, vmLocale);
+        assertNull(localeStr);
     }
 
     public void testDefaultSettings() throws Exception {

http://git-wip-us.apache.org/repos/asf/struts/blob/f06caf7c/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java b/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java
index 8001bd2..0f430de 100644
--- a/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java
+++ b/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java
@@ -102,7 +102,6 @@ public class DispatcherTest extends StrutsInternalTestCase {
         Mock mock = new Mock(HttpServletRequest.class);
         mock.expectAndReturn("getCharacterEncoding", "utf-8");
         mock.expectAndReturn("getHeader", "X-Requested-With", "");
-        mock.expectAndReturn("getLocale", Locale.getDefault());
         mock.expectAndReturn("getCharacterEncoding", "utf-8");
         HttpServletRequest req = (HttpServletRequest) mock.proxy();
         HttpServletResponse res = new MockHttpServletResponse();

http://git-wip-us.apache.org/repos/asf/struts/blob/f06caf7c/core/src/test/java/org/apache/struts2/interceptor/I18nInterceptorTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/struts2/interceptor/I18nInterceptorTest.java b/core/src/test/java/org/apache/struts2/interceptor/I18nInterceptorTest.java
index 4bbaa46..a38b8c4 100644
--- a/core/src/test/java/org/apache/struts2/interceptor/I18nInterceptorTest.java
+++ b/core/src/test/java/org/apache/struts2/interceptor/I18nInterceptorTest.java
@@ -90,19 +90,8 @@ public class I18nInterceptorTest extends TestCase {
         assertEquals(denmark, mai.getInvocationContext().getLocale()); // should create a
locale object
     }
 
-    public void testCountryOnlyLocale() throws Exception {
-        prepare(I18nInterceptor.DEFAULT_PARAMETER, "NL");
-        interceptor.intercept(mai);
-
-        assertFalse(mai.getInvocationContext().getParameters().get(I18nInterceptor.DEFAULT_PARAMETER).isDefined());
// should have been removed
-
-        Locale denmark = new Locale("NL");
-        assertNotNull(session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); // should
be stored here
-        assertEquals(denmark, session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); //
should create a locale object
-    }
-
     public void testLanguageOnlyLocale() throws Exception {
-        prepare(I18nInterceptor.DEFAULT_PARAMETER, "da_");
+        prepare(I18nInterceptor.DEFAULT_PARAMETER, "da");
         interceptor.intercept(mai);
 
         assertFalse(mai.getInvocationContext().getParameters().get(I18nInterceptor.DEFAULT_PARAMETER).isDefined());
// should have been removed

http://git-wip-us.apache.org/repos/asf/struts/blob/f06caf7c/plugins/portlet/src/main/java/org/apache/struts2/portlet/dispatcher/Jsr168Dispatcher.java
----------------------------------------------------------------------
diff --git a/plugins/portlet/src/main/java/org/apache/struts2/portlet/dispatcher/Jsr168Dispatcher.java
b/plugins/portlet/src/main/java/org/apache/struts2/portlet/dispatcher/Jsr168Dispatcher.java
index 9a15b87..aab7d9f 100644
--- a/plugins/portlet/src/main/java/org/apache/struts2/portlet/dispatcher/Jsr168Dispatcher.java
+++ b/plugins/portlet/src/main/java/org/apache/struts2/portlet/dispatcher/Jsr168Dispatcher.java
@@ -26,10 +26,11 @@ import com.opensymphony.xwork2.ActionProxy;
 import com.opensymphony.xwork2.ActionProxyFactory;
 import com.opensymphony.xwork2.config.ConfigurationException;
 import com.opensymphony.xwork2.inject.Container;
-import com.opensymphony.xwork2.util.DefaultLocalizedTextProvider;
+import org.apache.commons.lang3.LocaleUtils;
 import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.LogManager;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.logging.log4j.message.ParameterizedMessage;
 import org.apache.struts2.StrutsConstants;
 import org.apache.struts2.StrutsException;
 import org.apache.struts2.StrutsStatics;
@@ -393,14 +394,7 @@ public class Jsr168Dispatcher extends GenericPortlet implements StrutsStatics
{
         extraContext.put(ActionContext.SESSION, sessionMap);
         extraContext.put(ActionContext.APPLICATION, applicationMap);
 
-        String defaultLocale = container.getInstance(String.class, StrutsConstants.STRUTS_LOCALE);
-        Locale locale;
-        if (defaultLocale != null) {
-            locale = DefaultLocalizedTextProvider.localeFromString(defaultLocale, request.getLocale());
-        } else {
-            locale = request.getLocale();
-        }
-        extraContext.put(ActionContext.LOCALE, locale);
+        extraContext.put(ActionContext.LOCALE, getLocale(request));
 
         extraContext.put(StrutsStatics.STRUTS_PORTLET_CONTEXT, getPortletContext());
         extraContext.put(REQUEST, request);
@@ -424,6 +418,23 @@ public class Jsr168Dispatcher extends GenericPortlet implements StrutsStatics
{
         return extraContext;
     }
 
+    protected Locale getLocale(PortletRequest request) {
+        String defaultLocale = container.getInstance(String.class, StrutsConstants.STRUTS_LOCALE);
+        Locale locale;
+        if (defaultLocale != null) {
+            try {
+                locale = LocaleUtils.toLocale(defaultLocale);
+            } catch (IllegalArgumentException e) {
+                LOG.warn(new ParameterizedMessage("Cannot convert 'struts.locale' = [{}]
to proper locale, defaulting to request locale [{}]",
+                        defaultLocale, request.getLocale()), e);
+                locale = request.getLocale();
+            }
+        } else {
+            locale = request.getLocale();
+        }
+        return locale;
+    }
+
     /**
      * Loads the action and executes it. This method first creates the action
      * context from the given parameters then loads an <tt>ActionProxy</tt>


Mime
View raw message