You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by lu...@apache.org on 2016/12/01 19:06:49 UTC

[1/8] struts git commit: WW-4722 Renames constant for better readability

Repository: struts
Updated Branches:
  refs/heads/master a1b89842c -> 09958919c


WW-4722 Renames constant for better readability


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

Branch: refs/heads/master
Commit: bcea4c9bafb101901543c97174fb3d20d0f0c017
Parents: a1b8984
Author: Lukasz Lenart <lu...@apache.org>
Authored: Thu Dec 1 16:16:49 2016 +0100
Committer: Lukasz Lenart <lu...@apache.org>
Committed: Thu Dec 1 16:16:49 2016 +0100

----------------------------------------------------------------------
 .../java/org/apache/struts2/interceptor/I18nInterceptor.java    | 5 ++---
 .../org/apache/struts2/interceptor/I18nInterceptorTest.java     | 4 ++--
 2 files changed, 4 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/bcea4c9b/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 cec69af..62ac412 100644
--- a/core/src/main/java/org/apache/struts2/interceptor/I18nInterceptor.java
+++ b/core/src/main/java/org/apache/struts2/interceptor/I18nInterceptor.java
@@ -35,7 +35,6 @@ import org.apache.struts2.dispatcher.Parameter;
 
 import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletResponse;
-import java.util.Arrays;
 import java.util.Locale;
 import java.util.Map;
 
@@ -96,12 +95,12 @@ public class I18nInterceptor extends AbstractInterceptor {
 
     public static final String DEFAULT_SESSION_ATTRIBUTE = "WW_TRANS_I18N_LOCALE";
     public static final String DEFAULT_PARAMETER = "request_locale";
-    public static final String DEFAULT_REQUESTONLY_PARAMETER = "request_only_locale";
+    public static final String DEFAULT_REQUEST_ONLY_PARAMETER = "request_only_locale";
     public static final String DEFAULT_COOKIE_ATTRIBUTE = DEFAULT_SESSION_ATTRIBUTE;
     public static final String DEFAULT_COOKIE_PARAMETER = "request_cookie_locale";
 
     protected String parameterName = DEFAULT_PARAMETER;
-    protected String requestOnlyParameterName = DEFAULT_REQUESTONLY_PARAMETER;
+    protected String requestOnlyParameterName = DEFAULT_REQUEST_ONLY_PARAMETER;
     protected String attributeName = DEFAULT_SESSION_ATTRIBUTE;
 
     protected LocaleProvider localeProvider;

http://git-wip-us.apache.org/repos/asf/struts/blob/bcea4c9b/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 de056b9..5018815 100644
--- a/core/src/test/java/org/apache/struts2/interceptor/I18nInterceptorTest.java
+++ b/core/src/test/java/org/apache/struts2/interceptor/I18nInterceptorTest.java
@@ -78,7 +78,7 @@ public class I18nInterceptorTest extends TestCase {
     }
 
     public void testDenmarkLocaleRequestOnly() throws Exception {
-        prepare(I18nInterceptor.DEFAULT_REQUESTONLY_PARAMETER, "da_DK");
+        prepare(I18nInterceptor.DEFAULT_REQUEST_ONLY_PARAMETER, "da_DK");
         interceptor.intercept(mai);
 
         assertFalse(mai.getInvocationContext().getParameters().get(I18nInterceptor.DEFAULT_PARAMETER).isDefined()); // should have been removed
@@ -124,7 +124,7 @@ public class I18nInterceptorTest extends TestCase {
     }
 
     public void testWithVariantRequestOnly() throws Exception {
-        prepare(I18nInterceptor.DEFAULT_REQUESTONLY_PARAMETER, "ja_JP_JP");
+        prepare(I18nInterceptor.DEFAULT_REQUEST_ONLY_PARAMETER, "ja_JP_JP");
         interceptor.intercept(mai);
 
         assertFalse(mai.getInvocationContext().getParameters().get(I18nInterceptor.DEFAULT_PARAMETER).isDefined()); // should have been removed


[6/8] struts git commit: WW-4722 Prepares ActionProxy to be used with test

Posted by lu...@apache.org.
WW-4722 Prepares ActionProxy to be used with test


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

Branch: refs/heads/master
Commit: f515def5b82f29b2accac056e2148077047bdfd1
Parents: fddc658
Author: Lukasz Lenart <lu...@apache.org>
Authored: Thu Dec 1 19:50:36 2016 +0100
Committer: Lukasz Lenart <lu...@apache.org>
Committed: Thu Dec 1 19:50:36 2016 +0100

----------------------------------------------------------------------
 .../struts2/interceptor/I18nInterceptorTest.java       | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/f515def5/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 8da9f68..d8c5944 100644
--- a/core/src/test/java/org/apache/struts2/interceptor/I18nInterceptorTest.java
+++ b/core/src/test/java/org/apache/struts2/interceptor/I18nInterceptorTest.java
@@ -25,6 +25,7 @@ import com.opensymphony.xwork2.ActionContext;
 import com.opensymphony.xwork2.ActionInvocation;
 import com.opensymphony.xwork2.DefaultLocaleProvider;
 import com.opensymphony.xwork2.mock.MockActionInvocation;
+import com.opensymphony.xwork2.mock.MockActionProxy;
 import junit.framework.TestCase;
 import org.apache.struts2.ServletActionContext;
 import org.apache.struts2.StrutsStatics;
@@ -203,9 +204,8 @@ public class I18nInterceptorTest extends TestCase {
 
         EasyMock.verify(response);
 
-        Locale denmark = new Locale("da", "DK");
-        assertNotNull(session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); // should be stored here
-        assertEquals(denmark, session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); // should create a locale object
+        assertNull(session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); // should not be stored here
+        assertNull(session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); // should not create a locale object
     }
 
     private void prepare(String key, Serializable value) {
@@ -235,9 +235,16 @@ public class I18nInterceptorTest extends TestCase {
                 return SUCCESS;
             }
         };
+
+        MockActionProxy proxy = new MockActionProxy();
+        proxy.setAction(action);
+        proxy.setNamespace("i18n");
+        proxy.setActionName("anAction");
+
         mai = new MockActionInvocation();
         ((MockActionInvocation) mai).setAction(action);
         ((MockActionInvocation) mai).setInvocationContext(ac);
+        ((MockActionInvocation) mai).setProxy(proxy);
     }
 
     public void tearDown() throws Exception {


[4/8] struts git commit: WW-4722 Extracts logic to store locale in cookie into a method

Posted by lu...@apache.org.
WW-4722 Extracts logic to store locale in cookie into a method


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

Branch: refs/heads/master
Commit: 8971b5081055bd309f715aad63fd96825154c3f8
Parents: 6457f00
Author: Lukasz Lenart <lu...@apache.org>
Authored: Thu Dec 1 17:25:33 2016 +0100
Committer: Lukasz Lenart <lu...@apache.org>
Committed: Thu Dec 1 17:25:33 2016 +0100

----------------------------------------------------------------------
 .../struts2/interceptor/I18nInterceptor.java    | 28 +++++++++++++-------
 1 file changed, 19 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/8971b508/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 02409c4..cfed10a 100644
--- a/core/src/main/java/org/apache/struts2/interceptor/I18nInterceptor.java
+++ b/core/src/main/java/org/apache/struts2/interceptor/I18nInterceptor.java
@@ -234,20 +234,18 @@ public class I18nInterceptor extends AbstractInterceptor {
      */
     protected Locale readStoredLocale(ActionInvocation invocation, Map<String, Object> session) {
         Locale locale = this.readStoredLocalFromSession(invocation, session);
-
         if (locale != null) {
+            LOG.debug("Found stored Locale {} in session, using it!", locale);
             return locale;
         }
 
-        Cookie[] cookies = ServletActionContext.getRequest().getCookies();
-        if (cookies != null) {
-            for (Cookie cookie : cookies) {
-                if (DEFAULT_COOKIE_ATTRIBUTE.equals(cookie.getName())) {
-                    return getLocaleFromParam(cookie.getValue());
-                }
-            }
+        Locale cookie = readStoredLocaleFromCookie(invocation);
+        if (cookie != null) {
+            LOG.debug("Found stored Locale {} in cookies, using it!", locale);
+            return cookie;
         }
 
+        LOG.debug("Neither locale was in session nor in cookies, searching current Invocation context");
         return this.readStoredLocalFromCurrentInvocation(invocation);
     }
 
@@ -283,7 +281,7 @@ public class I18nInterceptor extends AbstractInterceptor {
         return locale;
     }
 
-    protected Locale readStoredLocalFromSession(ActionInvocation invocation, Map<String, Object> session) {
+    protected Locale readStoredLocalFromSession(ActionInvocation ignore, Map<String, Object> session) {
         // check session for saved locale
         Object sessionLocale = session.get(attributeName);
         if (sessionLocale != null && sessionLocale instanceof Locale) {
@@ -294,6 +292,18 @@ public class I18nInterceptor extends AbstractInterceptor {
         return null;
     }
 
+    protected Locale readStoredLocaleFromCookie(ActionInvocation ignore) {
+        Cookie[] cookies = ServletActionContext.getRequest().getCookies();
+        if (cookies != null) {
+            for (Cookie cookie : cookies) {
+                if (DEFAULT_COOKIE_ATTRIBUTE.equals(cookie.getName())) {
+                    return getLocaleFromParam(cookie.getValue());
+                }
+            }
+        }
+        return null;
+    }
+
     protected Locale readStoredLocalFromCurrentInvocation(ActionInvocation invocation) {
         // no overriding locale definition found, stay with current invocation (=browser) locale
         Locale locale = invocation.getInvocationContext().getLocale();


[8/8] struts git commit: WW-4722 Adds description of storage parameter

Posted by lu...@apache.org.
WW-4722 Adds description of storage parameter


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

Branch: refs/heads/master
Commit: 09958919caabc66d3b963b81af6f8c3c4b44efe0
Parents: aabc90d
Author: Lukasz Lenart <lu...@apache.org>
Authored: Thu Dec 1 19:58:03 2016 +0100
Committer: Lukasz Lenart <lu...@apache.org>
Committed: Thu Dec 1 19:58:03 2016 +0100

----------------------------------------------------------------------
 .../java/org/apache/struts2/interceptor/I18nInterceptor.java    | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/09958919/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 3fbe1f3..16ce51f 100644
--- a/core/src/main/java/org/apache/struts2/interceptor/I18nInterceptor.java
+++ b/core/src/main/java/org/apache/struts2/interceptor/I18nInterceptor.java
@@ -74,6 +74,9 @@ import java.util.Map;
   * <li>attributeName (optional) - the name of the session key to store the selected locale. By default this is
   * <b>WW_TRANS_I18N_LOCALE</b></li>
   *
+  * <li>storage (optional) - the name of storage location, it can be <b>none</b>, <b>session</b> or <b>cookie</b>.
+  * By default this is <b>session</b></li>
+  *
   * </ul>
   *
   * <!-- END SNIPPET: parameters -->
@@ -130,7 +133,7 @@ public class I18nInterceptor extends AbstractInterceptor {
             this.storage = Storage.NONE;
         } else {
             try {
-                this.storage = Storage.valueOf(storageName);
+                this.storage = Storage.valueOf(storageName.toUpperCase());
             } catch (IllegalArgumentException e) {
                 LOG.warn(new ParameterizedMessage("Wrong storage name [{{}] was defined, falling back to {}", storageName, Storage.SESSION), e);
                 this.storage = Storage.SESSION;


[7/8] struts git commit: WW-4722 Refactors store logic

Posted by lu...@apache.org.
WW-4722 Refactors store logic


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

Branch: refs/heads/master
Commit: aabc90df6b8293f597903ac2c9016270b4a37d52
Parents: f515def
Author: Lukasz Lenart <lu...@apache.org>
Authored: Thu Dec 1 19:51:14 2016 +0100
Committer: Lukasz Lenart <lu...@apache.org>
Committed: Thu Dec 1 19:51:14 2016 +0100

----------------------------------------------------------------------
 .../struts2/interceptor/I18nInterceptor.java    | 63 +++++++++++---------
 1 file changed, 36 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/aabc90df/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 5f171da..3fbe1f3 100644
--- a/core/src/main/java/org/apache/struts2/interceptor/I18nInterceptor.java
+++ b/core/src/main/java/org/apache/struts2/interceptor/I18nInterceptor.java
@@ -20,7 +20,6 @@
  */
 package org.apache.struts2.interceptor;
 
-import com.opensymphony.xwork2.ActionContext;
 import com.opensymphony.xwork2.ActionInvocation;
 import com.opensymphony.xwork2.LocaleProvider;
 import com.opensymphony.xwork2.inject.Inject;
@@ -30,7 +29,6 @@ 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.StrutsStatics;
 import org.apache.struts2.dispatcher.HttpParameters;
 import org.apache.struts2.dispatcher.Parameter;
 
@@ -191,33 +189,39 @@ public class I18nInterceptor extends AbstractInterceptor {
      * @return the locale
      */
     protected Locale storeLocale(ActionInvocation invocation, Locale locale) {
-        if (storage == Storage.COOKIE) {
-            ActionContext ac = invocation.getInvocationContext();
-            HttpServletResponse response = (HttpServletResponse) ac.get(StrutsStatics.HTTP_RESPONSE);
+        if (locale == null) {
+            storage = Storage.NONE;
+            locale = readStoredLocale(invocation);
+        }
 
-            Cookie cookie = new Cookie(DEFAULT_COOKIE_ATTRIBUTE, locale.toString());
-            cookie.setMaxAge(1209600); // two weeks
-            response.addCookie(cookie);
+        if (storage == Storage.COOKIE) {
+            storeLocaleInCookie(invocation, locale);
+        }
 
-            storage = Storage.SESSION;
+        if (Storage.SESSION == storage) {
+            storeLocaleInSession(invocation, locale);
         }
 
+        return locale;
+    }
+
+    protected void storeLocaleInCookie(ActionInvocation ignore, Locale locale) {
+        HttpServletResponse response = ServletActionContext.getResponse();
+
+        Cookie cookie = new Cookie(DEFAULT_COOKIE_ATTRIBUTE, locale.toString());
+        cookie.setMaxAge(1209600); // two weeks
+        response.addCookie(cookie);
+    }
+
+    protected void storeLocaleInSession(ActionInvocation invocation, Locale locale) {
         //save it in session
         Map<String, Object> session = invocation.getInvocationContext().getSession();
 
         if (session != null) {
             synchronized (session) {
-                if (locale == null) {
-                    storage = Storage.NONE;
-                    locale = readStoredLocale(invocation, session);
-                }
-
-                if (Storage.SESSION == storage) {
-                    session.put(attributeName, locale);
-                }
+                session.put(attributeName, locale);
             }
         }
-        return locale;
     }
 
     /**
@@ -225,11 +229,10 @@ public class I18nInterceptor extends AbstractInterceptor {
      * current invocation (=browser)
      *
      * @param invocation the current invocation
-     * @param session the current session
      * @return the read locale
      */
-    protected Locale readStoredLocale(ActionInvocation invocation, Map<String, Object> session) {
-        Locale locale = readStoredLocalFromSession(invocation, session);
+    protected Locale readStoredLocale(ActionInvocation invocation) {
+        Locale locale = readStoredLocalFromSession(invocation);
         if (locale != null) {
             LOG.debug("Found stored Locale {} in session, using it!", locale);
             return locale;
@@ -277,13 +280,19 @@ public class I18nInterceptor extends AbstractInterceptor {
         return locale;
     }
 
-    protected Locale readStoredLocalFromSession(ActionInvocation ignore, Map<String, Object> session) {
+    protected Locale readStoredLocalFromSession(ActionInvocation invocation) {
         // check session for saved locale
-        Object sessionLocale = session.get(attributeName);
-        if (sessionLocale != null && sessionLocale instanceof Locale) {
-            Locale locale = (Locale) sessionLocale;
-            LOG.debug("Applied session locale: {}", locale);
-            return locale;
+        Map<String, Object> session = invocation.getInvocationContext().getSession();
+
+        if (session != null) {
+            synchronized (session) {
+                Object sessionLocale = session.get(attributeName);
+                if (sessionLocale != null && sessionLocale instanceof Locale) {
+                    Locale locale = (Locale) sessionLocale;
+                    LOG.debug("Applied session locale: {}", locale);
+                    return locale;
+                }
+            }
         }
         return null;
     }


[2/8] struts git commit: WW-4722 Allows predefine default store location

Posted by lu...@apache.org.
WW-4722 Allows predefine default store location


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

Branch: refs/heads/master
Commit: 6e1ea61dc664c4a4e41a8a094030f2edf0088f6c
Parents: bcea4c9
Author: Lukasz Lenart <lu...@apache.org>
Authored: Thu Dec 1 16:55:42 2016 +0100
Committer: Lukasz Lenart <lu...@apache.org>
Committed: Thu Dec 1 16:55:42 2016 +0100

----------------------------------------------------------------------
 .../struts2/interceptor/I18nInterceptor.java      | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/6e1ea61d/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 62ac412..b1f8e8e 100644
--- a/core/src/main/java/org/apache/struts2/interceptor/I18nInterceptor.java
+++ b/core/src/main/java/org/apache/struts2/interceptor/I18nInterceptor.java
@@ -28,6 +28,7 @@ import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
 import com.opensymphony.xwork2.util.LocalizedTextUtil;
 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.StrutsStatics;
 import org.apache.struts2.dispatcher.HttpParameters;
@@ -102,14 +103,14 @@ public class I18nInterceptor extends AbstractInterceptor {
     protected String parameterName = DEFAULT_PARAMETER;
     protected String requestOnlyParameterName = DEFAULT_REQUEST_ONLY_PARAMETER;
     protected String attributeName = DEFAULT_SESSION_ATTRIBUTE;
+    protected String requestCookieParameterName = DEFAULT_COOKIE_PARAMETER;
+    protected Storage storage = Storage.SESSION;
 
     protected LocaleProvider localeProvider;
 
     // Request-Only = None
     protected enum Storage { COOKIE, SESSION, NONE }
 
-    protected String requestCookieParameterName = DEFAULT_COOKIE_PARAMETER;
-
     public void setParameterName(String parameterName) {
         this.parameterName = parameterName;
     }
@@ -126,6 +127,19 @@ public class I18nInterceptor extends AbstractInterceptor {
         this.attributeName = attributeName;
     }
 
+    public void setLocaleStorage(String storageName) {
+        if (storageName == null || "".equals(storageName)) {
+            this.storage = Storage.NONE;
+        } else {
+            try {
+                this.storage = Storage.valueOf(storageName);
+            } catch (IllegalArgumentException e) {
+                LOG.warn(new ParameterizedMessage("Wrong storage name [{{}] was defined, falling back to {}", storageName, Storage.SESSION), e);
+                this.storage = Storage.SESSION;
+            }
+        }
+    }
+
     @Inject
     public void setLocaleProvider(LocaleProvider localeProvider) {
         this.localeProvider = localeProvider;


[3/8] struts git commit: WW-4722 Refactors code to use predefined storage locatio

Posted by lu...@apache.org.
WW-4722 Refactors code to use predefined storage locatio


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

Branch: refs/heads/master
Commit: 6457f002d97cde76f98444ec98abdb9bc4be191f
Parents: 6e1ea61
Author: Lukasz Lenart <lu...@apache.org>
Authored: Thu Dec 1 17:21:22 2016 +0100
Committer: Lukasz Lenart <lu...@apache.org>
Committed: Thu Dec 1 17:21:22 2016 +0100

----------------------------------------------------------------------
 .../struts2/interceptor/I18nInterceptor.java    | 99 ++++++++++++--------
 .../interceptor/I18nInterceptorTest.java        | 40 ++++----
 2 files changed, 79 insertions(+), 60 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/6457f002/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 b1f8e8e..02409c4 100644
--- a/core/src/main/java/org/apache/struts2/interceptor/I18nInterceptor.java
+++ b/core/src/main/java/org/apache/struts2/interceptor/I18nInterceptor.java
@@ -115,6 +115,10 @@ public class I18nInterceptor extends AbstractInterceptor {
         this.parameterName = parameterName;
     }
 
+    public void setAttributeName(String attributeName) {
+        this.attributeName = attributeName;
+    }
+
     public void setRequestOnlyParameterName(String requestOnlyParameterName) {
         this.requestOnlyParameterName = requestOnlyParameterName;
     }
@@ -123,10 +127,6 @@ public class I18nInterceptor extends AbstractInterceptor {
         this.requestCookieParameterName = requestCookieParameterName;
     }
 
-    public void setAttributeName(String attributeName) {
-        this.attributeName = attributeName;
-    }
-
     public void setLocaleStorage(String storageName) {
         if (storageName == null || "".equals(storageName)) {
             this.storage = Storage.NONE;
@@ -152,9 +152,9 @@ public class I18nInterceptor extends AbstractInterceptor {
                 invocation.getProxy().getNamespace(), invocation.getProxy().getActionName());
         }
 
-        LocaleFinder localeFinder = new CookieLocaleFinder(invocation);
-        Locale locale = getLocaleFromParam(localeFinder.getRequestedLocale());
-        locale = storeLocale(invocation, locale, localeFinder.getStorage());
+        RequestOnlyLocaleFinder localeFinder = getLocaleFinder(invocation);
+        Locale locale = getLocaleFromParam(localeFinder.find());
+        locale = storeLocale(invocation, locale, storage);
         saveLocale(invocation, locale);
 
         if (LOG.isDebugEnabled()) {
@@ -171,6 +171,20 @@ public class I18nInterceptor extends AbstractInterceptor {
         return result;
     }
 
+    protected RequestOnlyLocaleFinder getLocaleFinder(ActionInvocation invocation) {
+        RequestOnlyLocaleFinder localeFinder;
+        if (this.storage == Storage.COOKIE) {
+            localeFinder = new CookieLocaleFinder(invocation);
+        } else if (this.storage == Storage.SESSION) {
+            localeFinder = new SessionLocaleFinder(invocation);
+        } else {
+            localeFinder = new RequestOnlyLocaleFinder(invocation);
+        }
+
+        LOG.debug("Using LocaleFinder implementation {}", localeFinder.getClass().getName());
+        return localeFinder;
+    }
+
     /**
      * Store the locale to the chosen storage, like f. e. the session
      *
@@ -308,71 +322,74 @@ public class I18nInterceptor extends AbstractInterceptor {
         invocation.getInvocationContext().setLocale(locale);
     }
 
-    protected class LocaleFinder {
-        protected Storage storage = Storage.SESSION;
-        protected Parameter requestedLocale = null;
+    protected class RequestOnlyLocaleFinder {
 
         protected ActionInvocation actionInvocation = null;
 
-        protected LocaleFinder(ActionInvocation invocation) {
+        protected RequestOnlyLocaleFinder(ActionInvocation invocation) {
             actionInvocation = invocation;
-            find();
         }
 
-        protected void find() {
-            //get requested locale
+        public String find() {
             HttpParameters params = actionInvocation.getInvocationContext().getParameters();
 
-            storage = Storage.SESSION;
-
-            requestedLocale = findLocaleParameter(params, parameterName);
-            if (requestedLocale.isDefined()) {
-                return;
-            }
-
-            requestedLocale = findLocaleParameter(params, requestOnlyParameterName);
+            Parameter requestedLocale = findLocaleParameter(params, requestOnlyParameterName);
             if (requestedLocale.isDefined()) {
                 storage = Storage.NONE;
+                return requestedLocale.getValue();
             }
+
+            return null;
         }
+    }
+
+    protected class SessionLocaleFinder extends RequestOnlyLocaleFinder {
 
-        public Storage getStorage() {
-            return storage;
+        protected SessionLocaleFinder(ActionInvocation invocation) {
+            super(invocation);
         }
 
-        public String getRequestedLocale() {
+        public String find() {
+            String requestOnlyLocale = super.find();
+
+            if (requestOnlyLocale != null) {
+                return requestOnlyLocale;
+            }
+
+            HttpParameters params = actionInvocation.getInvocationContext().getParameters();
+
+            Parameter requestedLocale = findLocaleParameter(params, parameterName);
+            if (requestedLocale.isDefined()) {
+                return requestedLocale.getValue();
+            }
+
             return requestedLocale.getValue();
         }
+
     }
 
-    protected class CookieLocaleFinder extends LocaleFinder {
+    protected class CookieLocaleFinder extends RequestOnlyLocaleFinder {
         protected CookieLocaleFinder(ActionInvocation invocation) {
             super(invocation);
         }
 
         @Override
-        protected void find() {
-            //get requested locale
-            HttpParameters params = actionInvocation.getInvocationContext().getParameters();
-            storage = Storage.SESSION;
-
-            requestedLocale = findLocaleParameter(params, parameterName);
+        public String find() {
+            String requestOnlySessionLocale = super.find();
 
-            if (requestedLocale.isDefined()) {
-                return;
+            if (requestOnlySessionLocale != null) {
+                return requestOnlySessionLocale;
             }
 
-            requestedLocale = findLocaleParameter(params, requestCookieParameterName);
-            if (requestedLocale.isDefined()) {
-                storage = Storage.COOKIE;
-                return;
-            }
+            HttpParameters params = actionInvocation.getInvocationContext().getParameters();
 
-            requestedLocale = findLocaleParameter(params, requestOnlyParameterName);
+            Parameter requestedLocale = findLocaleParameter(params, requestCookieParameterName);
             if (requestedLocale.isDefined()) {
-                storage = Storage.NONE;
+                storage = Storage.COOKIE;
+                return requestedLocale.getValue();
             }
 
+            return null;
         }
     }
 

http://git-wip-us.apache.org/repos/asf/struts/blob/6457f002/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 5018815..8da9f68 100644
--- a/core/src/test/java/org/apache/struts2/interceptor/I18nInterceptorTest.java
+++ b/core/src/test/java/org/apache/struts2/interceptor/I18nInterceptorTest.java
@@ -187,6 +187,27 @@ public class I18nInterceptorTest extends TestCase {
         assertEquals(locale1, locale);
     }
 
+    public void testCookieCreation() throws Exception {
+
+        prepare(I18nInterceptor.DEFAULT_COOKIE_PARAMETER, "da_DK");
+
+        final Cookie cookie = new Cookie(I18nInterceptor.DEFAULT_COOKIE_ATTRIBUTE, "da_DK");
+
+        HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class);
+        response.addCookie(CookieMatcher.eqCookie(cookie));
+        EasyMock.replay(response);
+
+        ac.put(StrutsStatics.HTTP_RESPONSE, response);
+        interceptor.setLocaleStorage(I18nInterceptor.Storage.COOKIE.name());
+        interceptor.intercept(mai);
+
+        EasyMock.verify(response);
+
+        Locale denmark = new Locale("da", "DK");
+        assertNotNull(session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); // should be stored here
+        assertEquals(denmark, session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); // should create a locale object
+    }
+
     private void prepare(String key, Serializable value) {
         Map<String, Serializable> params = new HashMap<>();
         params.put(key, value);
@@ -255,23 +276,4 @@ public class I18nInterceptorTest extends TestCase {
         }
     }
 
-    public void testCookieCreation() throws Exception {
-
-        prepare(I18nInterceptor.DEFAULT_COOKIE_PARAMETER, "da_DK");
-
-        final Cookie cookie = new Cookie(I18nInterceptor.DEFAULT_COOKIE_ATTRIBUTE, "da_DK");
-
-        HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class);
-        response.addCookie(CookieMatcher.eqCookie(cookie));
-        EasyMock.replay(response);
-
-        ac.put(StrutsStatics.HTTP_RESPONSE, response);
-        interceptor.intercept(mai);
-
-        EasyMock.verify(response);
-
-        Locale denmark = new Locale("da", "DK");
-        assertNotNull(session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); // should be stored here
-        assertEquals(denmark, session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); // should create a locale object
-    }
 }


[5/8] struts git commit: WW-4722 Reuses global fields and cleans up logging

Posted by lu...@apache.org.
WW-4722 Reuses global fields and cleans up logging


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

Branch: refs/heads/master
Commit: fddc6582e0a66e82b045824fb214e43d7d6c36fd
Parents: 8971b50
Author: Lukasz Lenart <lu...@apache.org>
Authored: Thu Dec 1 17:31:28 2016 +0100
Committer: Lukasz Lenart <lu...@apache.org>
Committed: Thu Dec 1 17:31:28 2016 +0100

----------------------------------------------------------------------
 .../struts2/interceptor/I18nInterceptor.java    | 24 ++++++++------------
 1 file changed, 10 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/fddc6582/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 cfed10a..5f171da 100644
--- a/core/src/main/java/org/apache/struts2/interceptor/I18nInterceptor.java
+++ b/core/src/main/java/org/apache/struts2/interceptor/I18nInterceptor.java
@@ -147,25 +147,22 @@ public class I18nInterceptor extends AbstractInterceptor {
 
     @Override
     public String intercept(ActionInvocation invocation) throws Exception {
-        if (LOG.isDebugEnabled()) {
-            LOG.debug("intercept '{}/{}' {",
-                invocation.getProxy().getNamespace(), invocation.getProxy().getActionName());
-        }
+        LOG.debug("Intercept '{}/{}'",
+            invocation.getProxy().getNamespace(), invocation.getProxy().getActionName());
 
         RequestOnlyLocaleFinder localeFinder = getLocaleFinder(invocation);
         Locale locale = getLocaleFromParam(localeFinder.find());
-        locale = storeLocale(invocation, locale, storage);
-        saveLocale(invocation, locale);
+        locale = storeLocale(invocation, locale);
+        useLocale(invocation, locale);
 
         if (LOG.isDebugEnabled()) {
-            LOG.debug("before Locale={}", invocation.getStack().findValue("locale"));
+            LOG.debug("Before action invocation Locale={}", invocation.getStack().findValue("locale"));
         }
 
         final String result = invocation.invoke();
 
         if (LOG.isDebugEnabled()) {
-            LOG.debug("after Locale={}", invocation.getStack().findValue("locale"));
-            LOG.debug("intercept } ");
+            LOG.debug("After action invocation Locale={}", invocation.getStack().findValue("locale"));
         }
 
         return result;
@@ -190,11 +187,10 @@ public class I18nInterceptor extends AbstractInterceptor {
      *
      * @param invocation the action invocation
      * @param locale the locale to store
-     * @param storage the place to store this locale (like Storage.SESSSION)
      *
      * @return the locale
      */
-    protected Locale storeLocale(ActionInvocation invocation, Locale locale, Storage storage) {
+    protected Locale storeLocale(ActionInvocation invocation, Locale locale) {
         if (storage == Storage.COOKIE) {
             ActionContext ac = invocation.getInvocationContext();
             HttpServletResponse response = (HttpServletResponse) ac.get(StrutsStatics.HTTP_RESPONSE);
@@ -233,7 +229,7 @@ public class I18nInterceptor extends AbstractInterceptor {
      * @return the read locale
      */
     protected Locale readStoredLocale(ActionInvocation invocation, Map<String, Object> session) {
-        Locale locale = this.readStoredLocalFromSession(invocation, session);
+        Locale locale = readStoredLocalFromSession(invocation, session);
         if (locale != null) {
             LOG.debug("Found stored Locale {} in session, using it!", locale);
             return locale;
@@ -246,7 +242,7 @@ public class I18nInterceptor extends AbstractInterceptor {
         }
 
         LOG.debug("Neither locale was in session nor in cookies, searching current Invocation context");
-        return this.readStoredLocalFromCurrentInvocation(invocation);
+        return readStoredLocalFromCurrentInvocation(invocation);
     }
 
     /**
@@ -328,7 +324,7 @@ public class I18nInterceptor extends AbstractInterceptor {
      * @param invocation The ActionInvocation.
      * @param locale     The locale to save.
      */
-    protected void saveLocale(ActionInvocation invocation, Locale locale) {
+    protected void useLocale(ActionInvocation invocation, Locale locale) {
         invocation.getInvocationContext().setLocale(locale);
     }