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:51 UTC

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

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
-    }
 }