Index: test/org/apache/commons/chain/web/servlet/ServletWebContextTestCase.java =================================================================== --- test/org/apache/commons/chain/web/servlet/ServletWebContextTestCase.java (revision 190998) +++ test/org/apache/commons/chain/web/servlet/ServletWebContextTestCase.java (working copy) @@ -1,5 +1,5 @@ /* - * Copyright 1999-2004 The Apache Software Foundation + * Copyright 1999-2005 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. @@ -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")); Index: test/org/apache/commons/chain/web/servlet/MockHttpServletRequest.java =================================================================== --- test/org/apache/commons/chain/web/servlet/MockHttpServletRequest.java (revision 190998) +++ test/org/apache/commons/chain/web/servlet/MockHttpServletRequest.java (working copy) @@ -1,5 +1,5 @@ /* - * Copyright 1999-2004 The Apache Software Foundation + * Copyright 1999-2005 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. @@ -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,7 +105,18 @@ 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; } Index: java/org/apache/commons/chain/web/faces/FacesWebContext.java =================================================================== --- java/org/apache/commons/chain/web/faces/FacesWebContext.java (revision 190998) +++ java/org/apache/commons/chain/web/faces/FacesWebContext.java (working copy) @@ -1,5 +1,5 @@ /* - * Copyright 1999-2004 The Apache Software Foundation + * Copyright 1999-2005 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. @@ -156,6 +156,13 @@ } + public Map getCookies() { + + return (context.getExternalContext().getRequestCookieMap()); + + } + + public Map getRequestScope() { return (context.getExternalContext().getRequestMap()); Index: java/org/apache/commons/chain/web/WebContext.java =================================================================== --- java/org/apache/commons/chain/web/WebContext.java (revision 190998) +++ java/org/apache/commons/chain/web/WebContext.java (working copy) @@ -1,5 +1,5 @@ /* - * Copyright 1999-2004 The Apache Software Foundation + * Copyright 1999-2005 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. @@ -89,6 +89,13 @@ /** + *

Return an immutable Map that maps cookie names to + * the set of cookies specified in the request. + */ + public abstract Map getCookies(); + + + /** *

Return a mutable Map that maps request scope * attribute names to their values.

*/ Index: java/org/apache/commons/chain/web/servlet/ServletCookieMap.java =================================================================== --- java/org/apache/commons/chain/web/servlet/ServletCookieMap.java (revision 0) +++ java/org/apache/commons/chain/web/servlet/ServletCookieMap.java (revision 0) @@ -0,0 +1,169 @@ +/* + * Copyright 2005 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.Enumeration; +import java.util.HashSet; +// import java.util.Iterator; +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; + + +/** + *

Private implementation of Map for servlet cookies

+ * + * @author Niall Pemberton + * @version $Revision$ $Date$ + */ + +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()); + } + } + + +} Property changes on: java/org/apache/commons/chain/web/servlet/ServletCookieMap.java ___________________________________________________________________ Name: svn:keywords + Date Author Id Revision HeadURL Name: svn:eol-style + native Index: java/org/apache/commons/chain/web/servlet/ServletWebContext.java =================================================================== --- java/org/apache/commons/chain/web/servlet/ServletWebContext.java (revision 190998) +++ java/org/apache/commons/chain/web/servlet/ServletWebContext.java (working copy) @@ -1,5 +1,5 @@ /* - * Copyright 1999-2004 The Apache Software Foundation + * Copyright 1999-2005 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. @@ -104,6 +104,12 @@ /** + *

The lazily instantiated Map of cookies.

+ */ + private Map cookieValues = null; + + + /** *

The lazily instantiated Map of request * parameter name-value.

*/ @@ -213,6 +219,7 @@ initParam = null; param = null; paramValues = null; + cookieValues = null; requestScope = null; sessionScope = null; @@ -288,6 +295,16 @@ } + public Map getCookies() { + + if ((cookieValues == null) && (request != null)) { + cookieValues = new ServletCookieMap(request); + } + return (cookieValues); + + } + + public Map getRequestScope() { if ((requestScope == null) && (request != null)) { Index: java/org/apache/commons/chain/web/portlet/PortletWebContext.java =================================================================== --- java/org/apache/commons/chain/web/portlet/PortletWebContext.java (revision 190998) +++ java/org/apache/commons/chain/web/portlet/PortletWebContext.java (working copy) @@ -1,5 +1,5 @@ /* - * Copyright 1999-2004 The Apache Software Foundation + * Copyright 1999-2005 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. @@ -291,6 +291,13 @@ } + public Map getCookies() { + + return Collections.EMPTY_MAP; + + } + + public Map getRequestScope() { if ((requestScope == null) && (request != null)) {