commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r411948 - in /jakarta/commons/proper/chain/trunk: src/java/org/apache/commons/chain/web/ src/java/org/apache/commons/chain/web/faces/ src/java/org/apache/commons/chain/web/portlet/ src/java/org/apache/commons/chain/web/servlet/ src/test/org...
Date Mon, 05 Jun 2006 23:29:03 GMT
Author: niallp
Date: Mon Jun  5 16:29:02 2006
New Revision: 411948

URL: http://svn.apache.org/viewvc?rev=411948&view=rev
Log:
CHAIN-28 - Provide a Map of Cookies in the WebContext

Added:
    jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/servlet/ServletCookieMap.java
  (with props)
Modified:
    jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/WebContext.java
    jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/faces/FacesWebContext.java
    jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/portlet/PortletWebContext.java
    jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/servlet/ServletWebContext.java
    jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/servlet/MockHttpServletRequest.java
    jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/servlet/ServletWebContextTestCase.java
    jakarta/commons/proper/chain/trunk/xdocs/changes.xml

Modified: jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/WebContext.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/WebContext.java?rev=411948&r1=411947&r2=411948&view=diff
==============================================================================
--- jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/WebContext.java
(original)
+++ jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/WebContext.java
Mon Jun  5 16:29:02 2006
@@ -101,6 +101,16 @@
 
 
     /**
+     * <p>Return an immutable <code>Map</code> that maps cookie names to
+     * the set of cookies specified in the request.
+     *
+     * @return Map of Cookies.
+     * @since Chain 1.1
+     */
+    public abstract Map getCookies();
+
+
+    /**
      * <p>Return a mutable <code>Map</code> that maps request scope
      * attribute names to their values.</p>
      *

Modified: jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/faces/FacesWebContext.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/faces/FacesWebContext.java?rev=411948&r1=411947&r2=411948&view=diff
==============================================================================
--- jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/faces/FacesWebContext.java
(original)
+++ jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/faces/FacesWebContext.java
Mon Jun  5 16:29:02 2006
@@ -188,6 +188,19 @@
     /**
      * See the {@link WebContext}'s Javadoc.
      *
+     * @return Map of Cookies.
+     * @since Chain 1.1
+     */
+    public Map getCookies() {
+
+        return (context.getExternalContext().getRequestCookieMap());
+
+    }
+
+
+    /**
+     * See the {@link WebContext}'s Javadoc.
+     *
      * @return Request scope Map.
      */
     public Map getRequestScope() {

Modified: jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/portlet/PortletWebContext.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/portlet/PortletWebContext.java?rev=411948&r1=411947&r2=411948&view=diff
==============================================================================
--- jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/portlet/PortletWebContext.java
(original)
+++ jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/portlet/PortletWebContext.java
Mon Jun  5 16:29:02 2006
@@ -325,6 +325,19 @@
 
 
     /**
+     * Returns an empty Map - portlets don't support Cookies.
+     *
+     * @return An empty Map.
+     * @since Chain 1.1
+     */
+    public Map getCookies() {
+
+        return Collections.EMPTY_MAP;
+
+    }
+
+
+    /**
      * See the {@link WebContext}'s Javadoc.
      *
      * @return Request scope Map.

Added: jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/servlet/ServletCookieMap.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/servlet/ServletCookieMap.java?rev=411948&view=auto
==============================================================================
--- jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/servlet/ServletCookieMap.java
(added)
+++ jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/servlet/ServletCookieMap.java
Mon Jun  5 16:29:02 2006
@@ -0,0 +1,168 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.chain.web.servlet;
+
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.Cookie;
+import org.apache.commons.chain.web.MapEntry;
+
+
+/**
+ * <p>Private implementation of <code>Map</code> for servlet cookies</p>
+ *
+ * @author Niall Pemberton
+ * @version $Revision$ $Date$
+ * @since Chain 1.1
+ */
+
+final class ServletCookieMap implements Map {
+
+
+    public ServletCookieMap(HttpServletRequest request) {
+        this.request = request;
+    }
+
+
+    private HttpServletRequest request = null;
+
+
+    public void clear() {
+        throw new UnsupportedOperationException();
+    }
+
+
+    public boolean containsKey(Object key) {
+        return (get(key) != null);
+    }
+
+
+    public boolean containsValue(Object value) {
+        Cookie[] cookies = request.getCookies();
+        if (cookies != null) {
+            for (int i = 0; i < cookies.length; i++) {
+                if (cookies[i].equals(value)) {
+                    return true;
+                }
+            }
+        }
+        return (false);
+    }
+
+
+    public Set entrySet() {
+        Set set = new HashSet();
+        Cookie[] cookies = request.getCookies();
+        if (cookies != null) {
+            for (int i = 0; i < cookies.length; i++) {
+                set.add(new MapEntry(cookies[i].getName(), cookies[i], false));
+            }
+        }
+        return (set);
+    }
+
+
+    public boolean equals(Object o) {
+        return (request.equals(o));
+    }
+
+
+    public Object get(Object key) {
+        Cookie[] cookies = request.getCookies();
+        if (cookies != null) {
+            for (int i = 0; i < cookies.length; i++) {
+                if (cookies[i].getName().equals(key(key))) {
+                    return cookies[i];
+                }
+            }
+        }
+        return null;
+    }
+
+
+    public int hashCode() {
+        return (request.hashCode());
+    }
+
+
+    public boolean isEmpty() {
+        return (size() < 1);
+    }
+
+
+    public Set keySet() {
+        Set set = new HashSet();
+        Cookie[] cookies = request.getCookies();
+        if (cookies != null) {
+            for (int i = 0; i < cookies.length; i++) {
+                set.add(cookies[i].getName());
+            }
+        }
+        return (set);
+    }
+
+
+    public Object put(Object key, Object value) {
+        throw new UnsupportedOperationException();
+    }
+
+
+    public void putAll(Map map) {
+        throw new UnsupportedOperationException();
+    }
+
+
+    public Object remove(Object key) {
+        throw new UnsupportedOperationException();
+    }
+
+
+    public int size() {
+        Cookie[] cookies = request.getCookies();
+        return (cookies == null ?  0 : cookies.length);
+    }
+
+
+    public Collection values() {
+        List list = new ArrayList(size());
+        Cookie[] cookies = request.getCookies();
+        if (cookies != null) {
+            for (int i = 0; i < cookies.length; i++) {
+                list.add(cookies[i]);
+            }
+        }
+        return (list);
+    }
+
+
+    private String key(Object key) {
+        if (key == null) {
+            throw new IllegalArgumentException();
+        } else if (key instanceof String) {
+            return ((String) key);
+        } else {
+            return (key.toString());
+        }
+    }
+
+
+}

Propchange: jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/servlet/ServletCookieMap.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/servlet/ServletCookieMap.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/servlet/ServletWebContext.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/servlet/ServletWebContext.java?rev=411948&r1=411947&r2=411948&view=diff
==============================================================================
--- jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/servlet/ServletWebContext.java
(original)
+++ jakarta/commons/proper/chain/trunk/src/java/org/apache/commons/chain/web/servlet/ServletWebContext.java
Mon Jun  5 16:29:02 2006
@@ -101,6 +101,12 @@
 
 
     /**
+     * <p>The lazily instantiated <code>Map</code> of cookies.</p>
+     */
+    private Map cookieValues = null;
+
+
+    /**
      * <p>The lazily instantiated <code>Map</code> of request
      * parameter name-value.</p>
      */
@@ -216,6 +222,7 @@
         initParam = null;
         param = null;
         paramValues = null;
+        cookieValues = null;
         requestScope = null;
         sessionScope = null;
 
@@ -317,6 +324,22 @@
             paramValues = new ServletParamValuesMap(request);
         }
         return (paramValues);
+
+    }
+
+
+    /**
+     * See the {@link WebContext}'s Javadoc.
+     *
+     * @return Map of Cookies.
+     * @since Chain 1.1
+     */
+    public Map getCookies() {
+
+        if ((cookieValues == null) && (request != null)) {
+            cookieValues = new ServletCookieMap(request);
+        }
+        return (cookieValues);
 
     }
 

Modified: jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/servlet/MockHttpServletRequest.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/servlet/MockHttpServletRequest.java?rev=411948&r1=411947&r2=411948&view=diff
==============================================================================
--- jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/servlet/MockHttpServletRequest.java
(original)
+++ jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/servlet/MockHttpServletRequest.java
Mon Jun  5 16:29:02 2006
@@ -65,6 +65,7 @@
     protected HashMap attributes = new HashMap();
     protected String contextPath = null;
     protected HashMap headers = new HashMap();
+    protected Cookie[] cookies = new Cookie[0];
     protected Locale locale = null;
     protected HashMap parameters = new HashMap();
     protected String pathInfo = null;
@@ -104,6 +105,17 @@
         parameters.put(name, results);
     }
 
+    public void addCookie(String name, String value) {
+        addCookie(new Cookie(name, value));
+    }
+
+    public void addCookie(Cookie cookie) {
+        Cookie[] newValues = new Cookie[cookies.length + 1];
+        System.arraycopy(cookies, 0, newValues, 0, cookies.length);
+        cookies = newValues;
+        cookies[cookies.length - 1] = cookie;
+    }
+
 
     public void setHttpSession(HttpSession session) {
         this.session = session;
@@ -146,7 +158,7 @@
 
 
     public Cookie[] getCookies() {
-        throw new UnsupportedOperationException();
+        return cookies;
     }
 
 

Modified: jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/servlet/ServletWebContextTestCase.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/servlet/ServletWebContextTestCase.java?rev=411948&r1=411947&r2=411948&view=diff
==============================================================================
--- jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/servlet/ServletWebContextTestCase.java
(original)
+++ jakarta/commons/proper/chain/trunk/src/test/org/apache/commons/chain/web/servlet/ServletWebContextTestCase.java
Mon Jun  5 16:29:02 2006
@@ -26,6 +26,7 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
+import javax.servlet.http.Cookie;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
@@ -92,6 +93,8 @@
         ((MockHttpServletRequest) request).addParameter("pkey1", "pvalue1");
         ((MockHttpServletRequest) request).addParameter("pkey2", "pvalue2a");
         ((MockHttpServletRequest) request).addParameter("pkey2", "pvalue2b");
+        ((MockHttpServletRequest) request).addCookie("ckey1", "cvalue1");
+        ((MockHttpServletRequest) request).addCookie("ckey2", "cvalue2");
         response = new MockHttpServletResponse();
         context = createContext();
     }
@@ -458,6 +461,53 @@
     }
 
 
+    // Test getCookies()
+    public void testCookies() {
+
+        Map map = ((WebContext) context).getCookies();
+        assertNotNull(map);
+
+        // Initial contents
+        checkMapSize(map, 2);
+        Cookie cookie1 = (Cookie)map.get("ckey1");
+        assertNotNull(cookie1);
+        assertEquals("cvalue1", cookie1.getValue());
+        Cookie cookie2 = (Cookie)map.get("ckey2");
+        assertNotNull(cookie2);
+        assertEquals("cvalue2", cookie2.getValue());
+        assertTrue(map.containsKey("ckey1"));
+        assertTrue(map.containsKey("ckey2"));
+        assertTrue(map.containsValue(cookie1));
+        assertTrue(map.containsValue(cookie2));
+
+        // Unsupported operations on read-only map
+        try {
+            map.clear();
+            fail("Should have thrown UnsupportedOperationException");
+        } catch (UnsupportedOperationException e) {
+            ; // expected result
+        }
+        try {
+            map.put("ckey3", "XXX");
+            fail("Should have thrown UnsupportedOperationException");
+        } catch (UnsupportedOperationException e) {
+            ; // expected result
+        }
+        try {
+            map.putAll(new HashMap());
+            fail("Should have thrown UnsupportedOperationException");
+        } catch (UnsupportedOperationException e) {
+            ; // expected result
+        }
+        try {
+            map.remove("ckey1");
+            fail("Should have thrown UnsupportedOperationException");
+        } catch (UnsupportedOperationException e) {
+            ; // expected result
+        }
+
+    }
+
     // Test state of newly created instance
     public void testPristine() {
 
@@ -471,6 +521,7 @@
         assertNotNull(swcontext.getInitParam());
         assertNotNull(swcontext.getParam());
         assertNotNull(swcontext.getParamValues());
+        assertNotNull(swcontext.getCookies());
         assertNotNull(swcontext.getRequestScope());
         assertNotNull(swcontext.getSessionScope());
 
@@ -487,6 +538,8 @@
                      swcontext.get("param"));
         assertTrue(swcontext.getParamValues() ==
                      swcontext.get("paramValues"));
+        assertTrue(swcontext.getCookies() ==
+                     swcontext.get("cookies"));
         assertTrue(swcontext.getRequestScope() ==
                      swcontext.get("requestScope"));
         assertTrue(swcontext.getSessionScope() ==
@@ -508,6 +561,7 @@
         assertNull(swcontext.getInitParam());
         assertNull(swcontext.getParam());
         assertNull(swcontext.getParamValues());
+        assertNull(swcontext.getCookies());
         assertNull(swcontext.getRequestScope());
         assertNull(swcontext.getSessionScope());
 
@@ -518,6 +572,7 @@
         assertNull(swcontext.get("initParam"));
         assertNull(swcontext.get("param"));
         assertNull(swcontext.get("paramValues"));
+        assertNull(swcontext.get("cookies"));
         assertNull(swcontext.get("requestScope"));
         assertNull(swcontext.get("sessionScope"));
 

Modified: jakarta/commons/proper/chain/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/chain/trunk/xdocs/changes.xml?rev=411948&r1=411947&r2=411948&view=diff
==============================================================================
--- jakarta/commons/proper/chain/trunk/xdocs/changes.xml (original)
+++ jakarta/commons/proper/chain/trunk/xdocs/changes.xml Mon Jun  5 16:29:02 2006
@@ -39,6 +39,9 @@
   <body>
 
     <release version="1.1-SNAPSHOT" date="in SVN">
+      <action dev="niallp" type="add" issue="CHAIN-28">
+        Provide a Map of Cookies in the WebContext.
+      </action>
       <action dev="niallp" type="fix" issue="CHAIN-29">
          Remove Static <code>Log</code> instances - see
          <a href="http://wiki.apache.org/jakarta-commons/Logging/StaticLog">here</a>.



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message