cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r824120 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/impl/HttpHeadersImpl.java test/java/org/apache/cxf/jaxrs/impl/HttpHeadersImplTest.java
Date Sun, 11 Oct 2009 18:25:30 GMT
Author: sergeyb
Date: Sun Oct 11 18:25:29 2009
New Revision: 824120

URL: http://svn.apache.org/viewvc?rev=824120&view=rev
Log:
JAXRS : support for complex http headers

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/HttpHeadersImpl.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/HttpHeadersImplTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/HttpHeadersImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/HttpHeadersImpl.java?rev=824120&r1=824119&r2=824120&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/HttpHeadersImpl.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/HttpHeadersImpl.java
Sun Oct 11 18:25:29 2009
@@ -24,10 +24,14 @@
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import javax.ws.rs.core.Cookie;
 import javax.ws.rs.core.HttpHeaders;
@@ -40,6 +44,24 @@
 
 public class HttpHeadersImpl implements HttpHeaders {
 
+    // TODO : it can optimized, "Mastering Regular Expressions" has the asnwers
+    private static final String COMPLEX_HEADER_EXPRESSION = 
+        "((\"(([^\"])|(?<=\\\\)\")*\")|([^\",]*))(;[\\w]+)?";
+    private static final Pattern COMPLEX_HEADER_PATTERN =
+        Pattern.compile(COMPLEX_HEADER_EXPRESSION);
+    private static final String QUOTE = "\"";
+    private static final Set<String> HEADERS_WITH_POSSIBLE_QUOTES;
+    static {
+        HEADERS_WITH_POSSIBLE_QUOTES = new HashSet<String>();
+        HEADERS_WITH_POSSIBLE_QUOTES.add(HttpHeaders.CACHE_CONTROL);
+        HEADERS_WITH_POSSIBLE_QUOTES.add(HttpHeaders.ETAG);
+        HEADERS_WITH_POSSIBLE_QUOTES.add(HttpHeaders.IF_MATCH);
+        HEADERS_WITH_POSSIBLE_QUOTES.add(HttpHeaders.IF_NONE_MATCH);
+        HEADERS_WITH_POSSIBLE_QUOTES.add(HttpHeaders.COOKIE);
+        HEADERS_WITH_POSSIBLE_QUOTES.add(HttpHeaders.SET_COOKIE);
+    }
+    
+    
     private MultivaluedMap<String, String> headers;
     
     @SuppressWarnings("unchecked")
@@ -129,16 +151,32 @@
         if (HttpUtils.isDateRelatedHeader(headerName)) {
             return values;
         }
-        String[] ls =  values.get(0).split(",");
-        if (ls.length == 1) {
-            return Collections.singletonList(ls[0].trim());
-        } else {
-            List<String> newValues = new ArrayList<String>();
-            for (String v : ls) {
-                newValues.add(v.trim());
+        return getHeaderValues(headerName, values.get(0));
+    }
+    
+    private List<String> getHeaderValues(String headerName, String originalValue) {
+        if (!originalValue.contains(QUOTE)
+            || HEADERS_WITH_POSSIBLE_QUOTES.contains(headerName)) {
+            String[] ls = originalValue.split(",");
+            if (ls.length == 1) {
+                return Collections.singletonList(ls[0].trim());
+            } else {
+                List<String> newValues = new ArrayList<String>();
+                for (String v : ls) {
+                    newValues.add(v.trim());
+                }
+                return newValues;
+            }
+        }
+        List<String> values = new ArrayList<String>(4);
+        Matcher m = COMPLEX_HEADER_PATTERN.matcher(originalValue);
+        while (m.find()) {
+            String val = m.group().trim();
+            if (val.length() > 0) {
+                values.add(val);
             }
-            return newValues;
         }
+        return values;
     }
     
     private static class AcceptLanguageComparator implements Comparator<Locale> {

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/HttpHeadersImplTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/HttpHeadersImplTest.java?rev=824120&r1=824119&r2=824120&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/HttpHeadersImplTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/HttpHeadersImplTest.java
Sun Oct 11 18:25:29 2009
@@ -47,6 +47,46 @@
     public void setUp() {
         control = EasyMock.createNiceControl();
     }
+
+    @Test
+    public void testGetHeaderWithQuotes1() throws Exception {
+        
+        Message m = control.createMock(Message.class);
+        m.get(Message.PROTOCOL_HEADERS);
+        MetadataMap<String, String> headers = 
+            createHeader("COMPLEX_HEADER", 
+                         "\"a\", \"a\";param, b, b;param, \"c, d, e\", \"c, d, e\";param");
+        EasyMock.expectLastCall().andReturn(headers);
+        control.replay();
+        HttpHeaders h = new HttpHeadersImpl(m);
+        List<String> values = h.getRequestHeader("COMPLEX_HEADER");
+        assertNotNull(values);
+        assertEquals(6, values.size());
+        assertEquals("\"a\"", values.get(0));
+        assertEquals("\"a\";param", values.get(1));
+        assertEquals("b", values.get(2));
+        assertEquals("b;param", values.get(3));
+        assertEquals("\"c, d, e\"", values.get(4));
+        assertEquals("\"c, d, e\";param", values.get(5));
+    }
+    
+    @Test
+    public void testGetHeaderWithQuotes2() throws Exception {
+        
+        Message m = control.createMock(Message.class);
+        m.get(Message.PROTOCOL_HEADERS);
+        MetadataMap<String, String> headers = 
+            createHeader("COMPLEX_HEADER", 
+                         "\"a   \\\"b\\\"\", b;param=b");
+        EasyMock.expectLastCall().andReturn(headers);
+        control.replay();
+        HttpHeaders h = new HttpHeadersImpl(m);
+        List<String> values = h.getRequestHeader("COMPLEX_HEADER");
+        assertNotNull(values);
+        assertEquals(2, values.size());
+        assertEquals("\"a   \\\"b\\\"\"", values.get(0));
+        assertEquals("b;param=b", values.get(1));
+    }
     
     @Test
     public void testGetHeaders() throws Exception {



Mime
View raw message