wink-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b...@apache.org
Subject svn commit: r1005110 - in /incubator/wink/trunk/wink-server/src: main/java/org/apache/wink/server/internal/contexts/HttpHeadersImpl.java test/java/org/apache/wink/server/internal/contexts/HttpHeadersImplTest.java
Date Wed, 06 Oct 2010 16:27:18 GMT
Author: bluk
Date: Wed Oct  6 16:27:18 2010
New Revision: 1005110

URL: http://svn.apache.org/viewvc?rev=1005110&view=rev
Log:
Fix issue with multiple cookies in single header

See [WINK-317]

Thanks to Chris Francuz for reporting the issue.

Modified:
    incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/contexts/HttpHeadersImpl.java
    incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/contexts/HttpHeadersImplTest.java

Modified: incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/contexts/HttpHeadersImpl.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/contexts/HttpHeadersImpl.java?rev=1005110&r1=1005109&r2=1005110&view=diff
==============================================================================
--- incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/contexts/HttpHeadersImpl.java
(original)
+++ incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/contexts/HttpHeadersImpl.java
Wed Oct  6 16:27:18 2010
@@ -43,6 +43,7 @@ import org.apache.wink.common.internal.C
 import org.apache.wink.common.internal.WinkConfiguration;
 import org.apache.wink.common.internal.http.Accept;
 import org.apache.wink.common.internal.http.AcceptLanguage;
+import org.apache.wink.common.internal.i18n.Messages;
 import org.apache.wink.common.internal.utils.HeaderUtils;
 import org.apache.wink.common.internal.utils.StringUtils;
 import org.apache.wink.common.internal.utils.UnmodifiableMultivaluedMap;
@@ -162,8 +163,10 @@ public class HttpHeadersImpl implements 
             List<String> cookiesHeaders = getRequestHeaderInternal(HttpHeaders.COOKIE);
             if (cookiesHeaders != null) {
                 for (String cookieHeader : cookiesHeaders) {
-                    Cookie cookie = Cookie.valueOf(cookieHeader);
-                    cookies.put(cookie.getName(), cookie);
+                    List<Cookie> currentCookies = parseCookieHeader(cookieHeader);
+                    for (Cookie c : currentCookies) {
+                        cookies.put(c.getName(), c);
+                    }
                 }
             }
         }
@@ -171,6 +174,73 @@ public class HttpHeadersImpl implements 
         return cookies;
     }
 
+    private static final String VERSION    = "$Version";            //$NON-NLS-1$
+    private static final String DOMAIN     = "$Domain";             //$NON-NLS-1$
+    private static final String PATH       = "$Path";               //$NON-NLS-1$
+
+    private static class ModifiableCookie {
+        public String name;
+        public String value;
+        public int    version = 0;
+        public String path;
+        public String domain;
+    }
+
+    private List<Cookie> parseCookieHeader(String cookieHeader) {
+        String tokens[] = cookieHeader.split("[;,]"); //$NON-NLS-1$
+
+        if (tokens.length <= 0) {
+            return Collections.emptyList();
+        }
+
+        List<Cookie> cookies = new ArrayList<Cookie>();
+
+        ModifiableCookie currentCookie = null;
+        int version = 0;
+
+        for (String token : tokens) {
+            String[] subTokens = token.trim().split("=", 2); //$NON-NLS-1$
+            String name = subTokens.length > 0 ? subTokens[0] : null;
+            String value = subTokens.length > 1 ? subTokens[1] : null;
+            if (value != null && value.startsWith("\"") //$NON-NLS-1$
+                && value.endsWith("\"") //$NON-NLS-1$
+                && value.length() > 1) {
+                value = value.substring(1, value.length() - 1);
+            }
+
+            if (!name.startsWith("$")) { //$NON-NLS-1$
+                // this is the start of a new cookie
+                if (currentCookie != null) {
+                    if (currentCookie.name != null && currentCookie.value != null)
{
+                        cookies.add(new Cookie(currentCookie.name, currentCookie.value,
+                                               currentCookie.path, currentCookie.domain,
+                                               currentCookie.version));
+                    }
+                }
+                currentCookie = new ModifiableCookie();
+                currentCookie.name = name;
+                currentCookie.value = value;
+                currentCookie.version = version;
+            } else if (name.startsWith(VERSION)) {
+                version = Integer.parseInt(value);
+            } else if (name.startsWith(PATH) && currentCookie != null) {
+                currentCookie.path = value;
+            } else if (name.startsWith(DOMAIN) && currentCookie != null) {
+                currentCookie.domain = value;
+            }
+        }
+
+        if (currentCookie != null) {
+            if (currentCookie.name != null && currentCookie.value != null) {
+                cookies.add(new Cookie(currentCookie.name, currentCookie.value,
+                                       currentCookie.path, currentCookie.domain,
+                                       currentCookie.version));
+            }
+        }
+        
+        return cookies;
+    }
+
     public Locale getLanguage() {
         if (language == null) {
             String languageStr = headers.getFirst(HttpHeaders.CONTENT_LANGUAGE);
@@ -304,7 +374,7 @@ public class HttpHeadersImpl implements 
 
         public String getFirst(String key) {
             List<String> headers = getRequestHeaderInternal(key);
-            if(headers == null || headers.isEmpty()) {
+            if (headers == null || headers.isEmpty()) {
                 return null;
             }
             return headers.get(0);

Modified: incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/contexts/HttpHeadersImplTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/contexts/HttpHeadersImplTest.java?rev=1005110&r1=1005109&r2=1005110&view=diff
==============================================================================
--- incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/contexts/HttpHeadersImplTest.java
(original)
+++ incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/contexts/HttpHeadersImplTest.java
Wed Oct  6 16:27:18 2010
@@ -19,31 +19,22 @@
 
 package org.apache.wink.server.internal.contexts;
 
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.io.UnsupportedEncodingException;
-import java.security.Principal;
-import java.util.Collections;
-import java.util.Enumeration;
 import java.util.List;
-import java.util.Locale;
 import java.util.Map;
 import java.util.Vector;
 
-import javax.servlet.RequestDispatcher;
-import javax.servlet.ServletInputStream;
-import javax.servlet.ServletOutputStream;
-import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-import javax.servlet.http.HttpSession;
+import javax.ws.rs.core.Cookie;
+import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
 
 import junit.framework.TestCase;
 
 import org.apache.wink.server.internal.DeploymentConfiguration;
 import org.apache.wink.server.internal.handlers.ServerMessageContext;
+import org.jmock.Expectations;
+import org.jmock.Mockery;
 
 public class HttpHeadersImplTest extends TestCase {
 
@@ -53,397 +44,113 @@ public class HttpHeadersImplTest extends
      */
     public void testRequestHeaderReturnsNull() {
 
-        ServerMessageContext context = new ServerMessageContext(new HttpServletRequest()
{
-
-            public void setCharacterEncoding(String arg0) throws UnsupportedEncodingException
{
-
-            }
-
-            public void setAttribute(String arg0, Object arg1) {
-
-            }
-
-            public void removeAttribute(String arg0) {
-
-            }
-
-            public boolean isSecure() {
-
-                return false;
-            }
-
-            public int getServerPort() {
-
-                return 0;
-            }
-
-            public String getServerName() {
-
-                return null;
-            }
-
-            public String getScheme() {
-
-                return null;
-            }
-
-            public RequestDispatcher getRequestDispatcher(String arg0) {
-
-                return null;
-            }
-
-            public int getRemotePort() {
-
-                return 0;
-            }
-
-            public String getRemoteHost() {
-
-                return null;
-            }
-
-            public String getRemoteAddr() {
-
-                return null;
-            }
-
-            public String getRealPath(String arg0) {
-
-                return null;
-            }
-
-            public BufferedReader getReader() throws IOException {
-
-                return null;
-            }
-
-            public String getProtocol() {
-
-                return null;
-            }
-
-            public String[] getParameterValues(String arg0) {
-
-                return null;
-            }
-
-            public Enumeration getParameterNames() {
-
-                return null;
-            }
-
-            public Map getParameterMap() {
-
-                return null;
-            }
-
-            public String getParameter(String arg0) {
-
-                return null;
-            }
-
-            public Enumeration getLocales() {
-
-                return null;
-            }
-
-            public Locale getLocale() {
-
-                return null;
-            }
-
-            public int getLocalPort() {
-
-                return 0;
-            }
-
-            public String getLocalName() {
-
-                return null;
-            }
-
-            public String getLocalAddr() {
-                return null;
-            }
-
-            public ServletInputStream getInputStream() throws IOException {
-                return null;
-            }
-
-            public String getContentType() {
-                return null;
-            }
-
-            public int getContentLength() {
-                return 0;
-            }
-
-            public String getCharacterEncoding() {
-                return null;
-            }
-
-            public Enumeration getAttributeNames() {
-                return null;
-            }
-
-            public Object getAttribute(String arg0) {
-                return null;
-            }
-
-            public boolean isUserInRole(String arg0) {
-                return false;
-            }
-
-            public boolean isRequestedSessionIdValid() {
-                return false;
-            }
-
-            public boolean isRequestedSessionIdFromUrl() {
-                return false;
-            }
-
-            public boolean isRequestedSessionIdFromURL() {
-                return false;
-            }
-
-            public boolean isRequestedSessionIdFromCookie() {
-                return false;
-            }
-
-            public Principal getUserPrincipal() {
-                return null;
-            }
-
-            public HttpSession getSession(boolean arg0) {
-                return null;
-            }
-
-            public HttpSession getSession() {
-                return null;
-            }
-
-            public String getServletPath() {
-                return null;
-            }
-
-            public String getRequestedSessionId() {
-                return null;
-            }
-
-            public StringBuffer getRequestURL() {
-                return null;
-            }
-
-            public String getRequestURI() {
-                return null;
-            }
-
-            public String getRemoteUser() {
-                return null;
-            }
-
-            public String getQueryString() {
-                return null;
-            }
-
-            public String getPathTranslated() {
-                return null;
-            }
-
-            public String getPathInfo() {
-                return null;
-            }
-
-            public String getMethod() {
-                return null;
-            }
-
-            public int getIntHeader(String arg0) {
-                return 0;
-            }
-
-            public Enumeration getHeaders(String arg0) {
-                if ("Accept".equals(arg0)) {
-                    Vector<String> values = new Vector<String>();
-                    values.add("text/xml");
-                    values.add(null);
-                    return values.elements();
-                }
-                return null;
-            }
-
-            public Enumeration getHeaderNames() {
-                Vector<String> headers = new Vector<String>(Collections.singletonList("Accept"));
-                return headers.elements();
-            }
-
-            public String getHeader(String arg0) {
-                return null;
-            }
-
-            public long getDateHeader(String arg0) {
-                return 0;
-            }
-
-            public Cookie[] getCookies() {
-                return null;
-            }
-
-            public String getContextPath() {
-                return null;
-            }
-
-            public String getAuthType() {
-                return null;
-            }
-        }, new HttpServletResponse() {
-
-            public void setLocale(Locale arg0) {
-
-            }
-
-            public void setContentType(String arg0) {
-
-            }
-
-            public void setContentLength(int arg0) {
-
-            }
-
-            public void setCharacterEncoding(String arg0) {
-
-            }
-
-            public void setBufferSize(int arg0) {
-
-            }
-
-            public void resetBuffer() {
-
-            }
-
-            public void reset() {
-
-            }
-
-            public boolean isCommitted() {
-
-                return false;
-            }
-
-            public PrintWriter getWriter() throws IOException {
-
-                return null;
-            }
-
-            public ServletOutputStream getOutputStream() throws IOException {
-
-                return null;
-            }
-
-            public Locale getLocale() {
-
-                return null;
-            }
-
-            public String getContentType() {
-
-                return null;
-            }
-
-            public String getCharacterEncoding() {
-
-                return null;
-            }
-
-            public int getBufferSize() {
-
-                return 0;
-            }
-
-            public void flushBuffer() throws IOException {
-
-            }
-
-            public void setStatus(int arg0, String arg1) {
-
-            }
-
-            public void setStatus(int arg0) {
-
-            }
-
-            public void setIntHeader(String arg0, int arg1) {
-
-            }
-
-            public void setHeader(String arg0, String arg1) {
-
-            }
-
-            public void setDateHeader(String arg0, long arg1) {
-
-            }
-
-            public void sendRedirect(String arg0) throws IOException {
-
-            }
-
-            public void sendError(int arg0, String arg1) throws IOException {
-
-            }
-
-            public void sendError(int arg0) throws IOException {
-
-            }
-
-            public String encodeUrl(String arg0) {
-
-                return null;
-            }
-
-            public String encodeURL(String arg0) {
-
-                return null;
-            }
-
-            public String encodeRedirectUrl(String arg0) {
-
-                return null;
-            }
-
-            public String encodeRedirectURL(String arg0) {
-
-                return null;
-            }
-
-            public boolean containsHeader(String arg0) {
-
-                return false;
-            }
-
-            public void addIntHeader(String arg0, int arg1) {
-
+        final Vector<String> values = new Vector<String>();
+        values.add("text/xml");
+        values.add(null);
+
+        Mockery mockery = new Mockery();
+        final HttpServletRequest requestMock = mockery.mock(HttpServletRequest.class);
+        final HttpServletResponse responseMock = mockery.mock(HttpServletResponse.class);
+        mockery.checking(new Expectations() {
+            {
+                oneOf(requestMock).getMethod();
+                will(returnValue(null));
+                oneOf(requestMock).getQueryString();
+                will(returnValue(null));
+                oneOf(requestMock).getHeaders("Accept");
+                will(returnValue(values.elements()));
             }
+        });
+        ServerMessageContext context =
+            new ServerMessageContext(requestMock, responseMock, new DeploymentConfiguration());
+        HttpHeadersImpl headers = new HttpHeadersImpl(context);
+        List<MediaType> mediaTypes = headers.getAcceptableMediaTypes();
+        assertEquals(1, mediaTypes.size());
+        assertEquals(MediaType.TEXT_XML_TYPE, headers.getAcceptableMediaTypes().get(0));
+        mockery.assertIsSatisfied();
 
-            public void addHeader(String arg0, String arg1) {
+    }
 
+    /**
+     * See [WINK-317]
+     */
+    public void testSingleCookieValuesInSingleHeader() {
+        final Vector<String> values = new Vector<String>();
+        values.add("$Version=\"1\"; MyName=\"MyValue\"; $Path=\"/somewhere\"");
+
+        Mockery mockery = new Mockery();
+        final HttpServletRequest requestMock = mockery.mock(HttpServletRequest.class);
+        final HttpServletResponse responseMock = mockery.mock(HttpServletResponse.class);
+        mockery.checking(new Expectations() {
+            {
+                oneOf(requestMock).getMethod();
+                will(returnValue(null));
+                oneOf(requestMock).getHeaders(HttpHeaders.COOKIE);
+                will(returnValue(values.elements()));
             }
+        });
+        ServerMessageContext context =
+            new ServerMessageContext(requestMock, responseMock, new DeploymentConfiguration());
+        HttpHeadersImpl headers = new HttpHeadersImpl(context);
+        Map<String, Cookie> cookies = headers.getCookies();
+        assertEquals(new Cookie("MyName", "MyValue", "/somewhere", null), cookies.get("MyName"));
+        assertEquals(1, cookies.size());
+        mockery.assertIsSatisfied();
+    }
 
-            public void addDateHeader(String arg0, long arg1) {
+    /**
+     * See [WINK-317]
+     */
+    public void testNoCookieValuesInSingleHeader() {
+        final Vector<String> values = new Vector<String>();
 
+        Mockery mockery = new Mockery();
+        final HttpServletRequest requestMock = mockery.mock(HttpServletRequest.class);
+        final HttpServletResponse responseMock = mockery.mock(HttpServletResponse.class);
+        mockery.checking(new Expectations() {
+            {
+                oneOf(requestMock).getMethod();
+                will(returnValue(null));
+                oneOf(requestMock).getHeaders(HttpHeaders.COOKIE);
+                will(returnValue(values.elements()));
             }
+        });
+        ServerMessageContext context =
+            new ServerMessageContext(requestMock, responseMock, new DeploymentConfiguration());
+        HttpHeadersImpl headers = new HttpHeadersImpl(context);
+        Map<String, Cookie> cookies = headers.getCookies();
+        assertNull(cookies.get("MyName"));
+        assertEquals(0, cookies.size());
+        mockery.assertIsSatisfied();
+    }
 
-            public void addCookie(Cookie arg0) {
-
+    /**
+     * See [WINK-317]
+     */
+    public void testMultipleCookieValuesInSingleHeader() {
+        final Vector<String> values = new Vector<String>();
+        values
+            .add("$Version=\"1\";MyOtherName=\"MyOtherValue\"; $Path=\"/else\"; $Domain=\"mydomain.com\";
MyName=\"MyValue\"; $Path=\"/somewhere\"; ");
+
+        Mockery mockery = new Mockery();
+        final HttpServletRequest requestMock = mockery.mock(HttpServletRequest.class);
+        final HttpServletResponse responseMock = mockery.mock(HttpServletResponse.class);
+        mockery.checking(new Expectations() {
+            {
+                oneOf(requestMock).getMethod();
+                will(returnValue(null));
+                oneOf(requestMock).getHeaders(HttpHeaders.COOKIE);
+                will(returnValue(values.elements()));
             }
-        }, new DeploymentConfiguration() {
         });
-
+        ServerMessageContext context =
+            new ServerMessageContext(requestMock, responseMock, new DeploymentConfiguration());
         HttpHeadersImpl headers = new HttpHeadersImpl(context);
-        List<MediaType> mediaTypes = headers.getAcceptableMediaTypes();
-        assertEquals(1, mediaTypes.size());
-        assertEquals(MediaType.TEXT_XML_TYPE, headers.getAcceptableMediaTypes().get(0));
+        Map<String, Cookie> cookies = headers.getCookies();
+        assertEquals(new Cookie("MyName", "MyValue", "/somewhere", null), cookies.get("MyName"));
+        assertEquals(new Cookie("MyOtherName", "MyOtherValue", "/else", "mydomain.com"),
cookies
+            .get("MyOtherName"));
+        assertEquals(2, cookies.size());
+        mockery.assertIsSatisfied();
     }
-
 }



Mime
View raw message