You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by rg...@apache.org on 2009/02/23 17:20:45 UTC

svn commit: r747064 - in /struts/struts2/trunk/core/src: main/java/org/apache/struts2/interceptor/CookieInterceptor.java main/java/org/apache/struts2/interceptor/CookiesAware.java test/java/org/apache/struts2/interceptor/CookieInterceptorTest.java

Author: rgielen
Date: Mon Feb 23 16:20:44 2009
New Revision: 747064

URL: http://svn.apache.org/viewvc?rev=747064&view=rev
Log:
WW-2886 - CookiesAware and CookieInterceptor are not generic
- applied patch by Mathias Bogaert

Modified:
    struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/CookieInterceptor.java
    struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/CookiesAware.java
    struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/CookieInterceptorTest.java

Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/CookieInterceptor.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/CookieInterceptor.java?rev=747064&r1=747063&r2=747064&view=diff
==============================================================================
--- struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/CookieInterceptor.java (original)
+++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/CookieInterceptor.java Mon Feb 23 16:20:44 2009
@@ -21,10 +21,7 @@
 
 package org.apache.struts2.interceptor;
 
-import java.util.Collections;
-import java.util.LinkedHashMap;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
 
 import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletRequest;
@@ -164,11 +161,11 @@
 
     private static final Logger LOG = LoggerFactory.getLogger(CookieInterceptor.class);
 
-    private Set cookiesNameSet = Collections.EMPTY_SET;
-    private Set cookiesValueSet = Collections.EMPTY_SET;
+    private Set<String> cookiesNameSet = Collections.emptySet();
+    private Set<String> cookiesValueSet = Collections.emptySet();
 
     /**
-     * Set the <code>cookiesName</code> which if matche will allow the cookie
+     * Set the <code>cookiesName</code> which if matched will allow the cookie
      * to be injected into action, could be comma-separated string.
      *
      * @param cookiesName
@@ -190,35 +187,32 @@
             this.cookiesValueSet = TextParseUtil.commaDelimitedStringToSet(cookiesValue);
     }
 
-
     public String intercept(ActionInvocation invocation) throws Exception {
-
         if (LOG.isDebugEnabled())
             LOG.debug("start interception");
 
-        final ValueStack stack = ActionContext.getContext().getValueStack();
-        HttpServletRequest request = ServletActionContext.getRequest();
-
         // contains selected cookies
-        Map cookiesMap = new LinkedHashMap();
+        final Map<String, String> cookiesMap = new LinkedHashMap<String, String>();
 
-        Cookie cookies[] = request.getCookies();
+        Cookie[] cookies = ServletActionContext.getRequest().getCookies();
         if (cookies != null) {
-            for (int a=0; a< cookies.length; a++) {
-                String name = cookies[a].getName();
-                String value = cookies[a].getValue();
+            final ValueStack stack = ActionContext.getContext().getValueStack();
+
+            for (Cookie cookie : cookies) {
+                String name = cookie.getName();
+                String value = cookie.getValue();
 
                 if (cookiesNameSet.contains("*")) {
                     if (LOG.isDebugEnabled())
-                        LOG.debug("contains cookie name [*] in configured cookies name set, cookie with name ["+name+"] with value ["+value+"] will be injected");
+                        LOG.debug("contains cookie name [*] in configured cookies name set, cookie with name [" + name + "] with value [" + value + "] will be injected");
                     populateCookieValueIntoStack(name, value, cookiesMap, stack);
-                }
-                else if (cookiesNameSet.contains(cookies[a].getName())) {
+                } else if (cookiesNameSet.contains(cookie.getName())) {
                     populateCookieValueIntoStack(name, value, cookiesMap, stack);
                 }
             }
         }
 
+        // inject the cookiesMap, even if we don't have any cookies
         injectIntoCookiesAwareAction(invocation.getAction(), cookiesMap);
 
         return invocation.invoke();
@@ -233,7 +227,7 @@
      * @param cookiesMap
      * @param stack
      */
-    protected void populateCookieValueIntoStack(String cookieName, String cookieValue, Map cookiesMap, ValueStack stack) {
+    protected void populateCookieValueIntoStack(String cookieName, String cookieValue, Map<String, String> cookiesMap, ValueStack stack) {
         if (cookiesValueSet.isEmpty() || cookiesValueSet.contains("*")) {
             // If the interceptor is configured to accept any cookie value
             // OR
@@ -267,12 +261,11 @@
      * @param action
      * @param cookiesMap
      */
-    protected void injectIntoCookiesAwareAction(Object action, Map cookiesMap) {
+    protected void injectIntoCookiesAwareAction(Object action, Map<String, String> cookiesMap) {
         if (action instanceof CookiesAware) {
             if (LOG.isDebugEnabled())
                 LOG.debug("action ["+action+"] implements CookiesAware, injecting cookies map ["+cookiesMap+"]");
             ((CookiesAware)action).setCookiesMap(cookiesMap);
         }
     }
-
 }

Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/CookiesAware.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/CookiesAware.java?rev=747064&r1=747063&r2=747064&view=diff
==============================================================================
--- struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/CookiesAware.java (original)
+++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/CookiesAware.java Mon Feb 23 16:20:44 2009
@@ -24,5 +24,5 @@
 import java.util.Map;
 
 public interface CookiesAware {
-    void setCookiesMap(Map cookies);
-}
+    void setCookiesMap(Map<String, String> cookies);
+}
\ No newline at end of file

Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/CookieInterceptorTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/CookieInterceptorTest.java?rev=747064&r1=747063&r2=747064&view=diff
==============================================================================
--- struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/CookieInterceptorTest.java (original)
+++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/CookieInterceptorTest.java Mon Feb 23 16:20:44 2009
@@ -326,9 +326,10 @@
         private String cookie2;
         private String cookie3;
 
-        public void setCookiesMap(Map cookies) {
+        public void setCookiesMap(Map<String, String> cookies) {
             this.cookies = cookies;
         }
+
         public Map getCookiesMap() {
             return this.cookies;
         }