Return-Path: X-Original-To: apmail-struts-commits-archive@minotaur.apache.org Delivered-To: apmail-struts-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 731E8117D4 for ; Fri, 27 Jun 2014 11:23:38 +0000 (UTC) Received: (qmail 95037 invoked by uid 500); 27 Jun 2014 11:23:37 -0000 Delivered-To: apmail-struts-commits-archive@struts.apache.org Received: (qmail 94971 invoked by uid 500); 27 Jun 2014 11:23:37 -0000 Mailing-List: contact commits-help@struts.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@struts.apache.org Delivered-To: mailing list commits@struts.apache.org Received: (qmail 94921 invoked by uid 99); 27 Jun 2014 11:23:37 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 27 Jun 2014 11:23:37 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id AA9A598D6BE; Fri, 27 Jun 2014 11:23:36 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: lukaszlenart@apache.org To: commits@struts.apache.org Date: Fri, 27 Jun 2014 11:23:53 -0000 Message-Id: <25d409b8aa6f4e298f44175bcbc32b12@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [25/50] git commit: Uses newly defined Struts bean instead duplicating logic 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 Authored: Mon May 12 08:27:30 2014 +0200 Committer: Lukasz Lenart 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 excludedPatterns = new HashSet(); - 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();