struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject [25/50] git commit: Uses newly defined Struts bean instead duplicating logic
Date Fri, 27 Jun 2014 11:23:53 GMT
Uses newly defined Struts bean instead duplicating logic


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

Branch: refs/heads/feature/WW-4295-localization
Commit: 735fd96114413181defb17cd49aa75da232a7040
Parents: 9884c49
Author: Lukasz Lenart <lukaszlenart@apache.org>
Authored: Mon May 12 08:27:30 2014 +0200
Committer: Lukasz Lenart <lukaszlenart@apache.org>
Committed: Mon May 12 08:27:30 2014 +0200

----------------------------------------------------------------------
 .../struts2/interceptor/CookieInterceptor.java  | 49 ++++++++------------
 .../interceptor/CookieInterceptorTest.java      | 11 +++++
 2 files changed, 30 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/735fd961/core/src/main/java/org/apache/struts2/interceptor/CookieInterceptor.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/struts2/interceptor/CookieInterceptor.java b/core/src/main/java/org/apache/struts2/interceptor/CookieInterceptor.java
index 340b57f..8998c5c 100644
--- a/core/src/main/java/org/apache/struts2/interceptor/CookieInterceptor.java
+++ b/core/src/main/java/org/apache/struts2/interceptor/CookieInterceptor.java
@@ -23,17 +23,18 @@ package org.apache.struts2.interceptor;
 
 import com.opensymphony.xwork2.ActionContext;
 import com.opensymphony.xwork2.ActionInvocation;
+import com.opensymphony.xwork2.inject.Inject;
 import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
-import com.opensymphony.xwork2.ExcludedPatterns;
+import com.opensymphony.xwork2.ExcludedPatternsChecker;
 import com.opensymphony.xwork2.util.TextParseUtil;
 import com.opensymphony.xwork2.util.ValueStack;
 import com.opensymphony.xwork2.util.logging.Logger;
 import com.opensymphony.xwork2.util.logging.LoggerFactory;
 import org.apache.struts2.ServletActionContext;
+import org.apache.struts2.StrutsConstants;
 
 import javax.servlet.http.Cookie;
 import java.util.Collections;
-import java.util.HashSet;
 import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.Set;
@@ -176,12 +177,12 @@ public class CookieInterceptor extends AbstractInterceptor {
 
     // Allowed names of cookies
     private Pattern acceptedPattern = Pattern.compile(ACCEPTED_PATTERN, Pattern.CASE_INSENSITIVE);
-    private Set<Pattern> excludedPatterns = new HashSet<Pattern>();
 
-    public CookieInterceptor() {
-        for (String pattern : ExcludedPatterns.EXCLUDED_PATTERNS) {
-            excludedPatterns.add(Pattern.compile(pattern, Pattern.CASE_INSENSITIVE));
-        }
+    private ExcludedPatternsChecker excludedPatternsChecker;
+
+    @Inject(StrutsConstants.STRUTS_EXCLUDED_PATTERNS_CHECKER)
+    public void setExcludedPatternsChecker(ExcludedPatternsChecker excludedPatternsChecker)
{
+        this.excludedPatternsChecker = excludedPatternsChecker;
     }
 
     /**
@@ -260,16 +261,7 @@ public class CookieInterceptor extends AbstractInterceptor {
      * @return true|false
      */
     protected boolean isAcceptableValue(String value) {
-        for (Pattern excludedPattern : excludedPatterns) {
-            boolean matches = !excludedPattern.matcher(value).matches();
-            if (!matches) {
-                if (LOG.isTraceEnabled()) {
-                    LOG.trace("Cookie value [#0] matches excludedPattern [#1]", value, excludedPattern.toString());
-                }
-                return false;
-            }
-        }
-        return true;
+        return !isExcluded(value) && isAccepted(value);
     }
 
     /**
@@ -283,7 +275,7 @@ public class CookieInterceptor extends AbstractInterceptor {
     }
 
     /**
-     * Checks if name of Cookie match {@link #acceptedPattern}
+     * Checks if name/value of Cookie is acceptable
      *
      * @param name of Cookie
      * @return true|false
@@ -303,24 +295,21 @@ public class CookieInterceptor extends AbstractInterceptor {
     }
 
     /**
-     * Checks if name of Cookie match {@link #excludedPatterns}
+     * Checks if name/value of Cookie is excluded
      *
      * @param name of Cookie
      * @return true|false
      */
     protected boolean isExcluded(String name) {
-        for (Pattern excludedPattern : excludedPatterns) {
-            boolean matches = excludedPattern.matcher(name).matches();
-            if (matches) {
-                if (LOG.isTraceEnabled()) {
-                    LOG.trace("Cookie [#0] matches excludedPattern [#1]", name, excludedPattern.toString());
-                }
-                return true;
-            } else {
-                if (LOG.isTraceEnabled()) {
-                    LOG.trace("Cookie [#0] doesn't match excludedPattern [#1]", name, excludedPattern.toString());
-                }
+        ExcludedPatternsChecker.IsExcluded excluded = excludedPatternsChecker.isExcluded(name);
+        if (excluded.isExcluded()) {
+            if (LOG.isTraceEnabled()) {
+                LOG.trace("Cookie [#0] matches excludedPattern [#1]", name, excluded.getExcludedPattern());
             }
+            return true;
+        }
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("Cookie [#0] doesn't match excludedPattern [#1]", name, excluded.getExcludedPattern());
         }
         return false;
     }

http://git-wip-us.apache.org/repos/asf/struts/blob/735fd961/core/src/test/java/org/apache/struts2/interceptor/CookieInterceptorTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/struts2/interceptor/CookieInterceptorTest.java
b/core/src/test/java/org/apache/struts2/interceptor/CookieInterceptorTest.java
index 99ba151..2bbaef9 100644
--- a/core/src/test/java/org/apache/struts2/interceptor/CookieInterceptorTest.java
+++ b/core/src/test/java/org/apache/struts2/interceptor/CookieInterceptorTest.java
@@ -27,6 +27,7 @@ import java.util.Map;
 
 import javax.servlet.http.Cookie;
 
+import com.opensymphony.xwork2.ExcludedPatternsChecker;
 import com.opensymphony.xwork2.mock.MockActionInvocation;
 import org.easymock.MockControl;
 import org.springframework.mock.web.MockHttpServletRequest;
@@ -65,6 +66,8 @@ public class CookieInterceptorTest extends StrutsInternalTestCase {
 
         // by default the interceptor doesn't accept any cookies
         CookieInterceptor interceptor = new CookieInterceptor();
+        interceptor.setExcludedPatternsChecker(new ExcludedPatternsChecker());
+
         interceptor.intercept(invocation);
 
         assertTrue(action.getCookiesMap().isEmpty());
@@ -99,6 +102,7 @@ public class CookieInterceptorTest extends StrutsInternalTestCase {
         actionInvocationControl.replay();
 
         CookieInterceptor interceptor = new CookieInterceptor();
+        interceptor.setExcludedPatternsChecker(new ExcludedPatternsChecker());
         interceptor.setCookiesName("*");
         interceptor.setCookiesValue("*");
         interceptor.intercept(invocation);
@@ -140,6 +144,7 @@ public class CookieInterceptorTest extends StrutsInternalTestCase {
         actionInvocationControl.replay();
 
         CookieInterceptor interceptor = new CookieInterceptor();
+        interceptor.setExcludedPatternsChecker(new ExcludedPatternsChecker());
         interceptor.setCookiesName("cookie1, cookie2, cookie3");
         interceptor.setCookiesValue("cookie1value, cookie2value, cookie3value");
         interceptor.intercept(invocation);
@@ -180,6 +185,7 @@ public class CookieInterceptorTest extends StrutsInternalTestCase {
         actionInvocationControl.replay();
 
         CookieInterceptor interceptor = new CookieInterceptor();
+        interceptor.setExcludedPatternsChecker(new ExcludedPatternsChecker());
         interceptor.setCookiesName("cookie1, cookie3");
         interceptor.setCookiesValue("cookie1value, cookie2value, cookie3value");
         interceptor.intercept(invocation);
@@ -220,6 +226,7 @@ public class CookieInterceptorTest extends StrutsInternalTestCase {
         actionInvocationControl.replay();
 
         CookieInterceptor interceptor = new CookieInterceptor();
+        interceptor.setExcludedPatternsChecker(new ExcludedPatternsChecker());
         interceptor.setCookiesName("cookie1, cookie3");
         interceptor.setCookiesValue("*");
         interceptor.intercept(invocation);
@@ -260,6 +267,7 @@ public class CookieInterceptorTest extends StrutsInternalTestCase {
         actionInvocationControl.replay();
 
         CookieInterceptor interceptor = new CookieInterceptor();
+        interceptor.setExcludedPatternsChecker(new ExcludedPatternsChecker());
         interceptor.setCookiesName("cookie1, cookie3");
         interceptor.setCookiesValue("");
         interceptor.intercept(invocation);
@@ -301,6 +309,7 @@ public class CookieInterceptorTest extends StrutsInternalTestCase {
         actionInvocationControl.replay();
 
         CookieInterceptor interceptor = new CookieInterceptor();
+        interceptor.setExcludedPatternsChecker(new ExcludedPatternsChecker());
         interceptor.setCookiesName("cookie1, cookie3");
         interceptor.setCookiesValue("cookie1value");
         interceptor.intercept(invocation);
@@ -361,6 +370,7 @@ public class CookieInterceptorTest extends StrutsInternalTestCase {
                 return accepted;
             }
         };
+        interceptor.setExcludedPatternsChecker(new ExcludedPatternsChecker());
         interceptor.setCookiesName("*");
 
         MockActionInvocation invocation = new MockActionInvocation();
@@ -420,6 +430,7 @@ public class CookieInterceptorTest extends StrutsInternalTestCase {
                 return accepted;
             }
         };
+        interceptor.setExcludedPatternsChecker(new ExcludedPatternsChecker());
         interceptor.setCookiesName("*");
 
         MockActionInvocation invocation = new MockActionInvocation();


Mime
View raw message