cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r756693 - in /cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ rt/frontend/jaxrs/src/test/java/org...
Date Fri, 20 Mar 2009 17:59:57 GMT
Author: sergeyb
Date: Fri Mar 20 17:59:55 2009
New Revision: 756693

URL: http://svn.apache.org/viewvc?rev=756693&view=rev
Log:
JAXRS : case-insensitive retrieval of http headers

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/HttpHeadersImpl.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MetadataMap.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/HttpHeadersImplTest.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/MetadataMapTest.java
    cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java?rev=756693&r1=756692&r2=756693&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
Fri Mar 20 17:59:55 2009
@@ -374,7 +374,8 @@
         }
         if (mbr != null) {
             try {
-                return mbr.readFrom(cls, type, anns, contentType, r.getMetadata(), conn.getInputStream());
+                return mbr.readFrom(cls, type, anns, contentType, 
+                       new MetadataMap<String, Object>(r.getMetadata(), true, true),
conn.getInputStream());
             } catch (Exception ex) {
                 throw new WebApplicationException();
             }
@@ -509,10 +510,11 @@
         public void onMessage(Message m) {
             
             Message message = conduitSelector.getEndpoint().getBinding().createMessage(m);
-            message.put(Message.REQUESTOR_ROLE, Boolean.TRUE);
+            message.put(Message.REQUESTOR_ROLE, Boolean.FALSE);
             message.put(Message.INBOUND_MESSAGE, Boolean.TRUE);
             PhaseInterceptorChain chain = setupInInterceptorChain(conduitSelector.getEndpoint());
             message.setInterceptorChain(chain);
+            message.getExchange().setInMessage(message);
             Bus origBus = BusFactory.getThreadDefaultBus(false);
             BusFactory.setThreadDefaultBus(bus);
 

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java?rev=756693&r1=756692&r2=756693&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java Fri
Mar 20 17:59:55 2009
@@ -438,8 +438,12 @@
     protected Response doInvoke(String httpMethod, Object body, Class<?> responseClass)
{
         
         MultivaluedMap<String, String> headers = getHeaders();
-        if (body != null && headers.getFirst(HttpHeaders.CONTENT_TYPE) == null) {
-            headers.putSingle(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_TYPE.toString());
+        if (body != null) {
+            if (headers.getFirst(HttpHeaders.CONTENT_TYPE) == null) {
+                headers.putSingle(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_TYPE.toString());
+            }
+        } else {
+            headers.putSingle(HttpHeaders.CONTENT_TYPE, MediaType.WILDCARD);
         }
         if (responseClass != null && headers.getFirst(HttpHeaders.ACCEPT) == null)
{
             headers.putSingle(HttpHeaders.ACCEPT, MediaType.APPLICATION_XML_TYPE.toString());

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=756693&r1=756692&r2=756693&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
Fri Mar 20 17:59:55 2009
@@ -24,6 +24,7 @@
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
@@ -38,30 +39,24 @@
 
 public class HttpHeadersImpl implements HttpHeaders {
 
-    private Message m;
     private MultivaluedMap<String, String> headers;
     
     @SuppressWarnings("unchecked")
     public HttpHeadersImpl(Message message) {
-        this.m = message;
         this.headers = new MetadataMap<String, String>(
-            (Map<String, List<String>>)message.get(Message.PROTOCOL_HEADERS));
+            (Map<String, List<String>>)message.get(Message.PROTOCOL_HEADERS),
true, true);
     }
     
     public List<MediaType> getAcceptableMediaTypes() {
-        String lValues = headers.getFirst(HttpHeaders.ACCEPT);
-        if (lValues == null) {
+        List<String> lValues = headers.get(HttpHeaders.ACCEPT);
+        if (lValues == null || lValues.isEmpty()) {
             return Collections.emptyList();
         }
-        return JAXRSUtils.sortMediaTypes(lValues); 
+        return JAXRSUtils.sortMediaTypes(lValues.get(0)); 
     }
 
     public Map<String, Cookie> getCookies() {
-        List<String> values = headers.get(HttpHeaders.COOKIE);
-        if (values == null || values.isEmpty()) {
-            return Collections.emptyMap();
-        }
-        String[] cs =  values.get(0).split(",");
+        List<String> cs = getListValues(HttpHeaders.COOKIE);
         Map<String, Cookie> cl = new HashMap<String, Cookie>(); 
         for (String c : cs) {
             Cookie cookie = Cookie.valueOf(c);
@@ -71,32 +66,27 @@
     }
 
     public Locale getLanguage() {
-        String l = headers.getFirst(HttpHeaders.CONTENT_LANGUAGE);
-        return l == null || l.length() == 0 ? null : new Locale(l);
+        List<String> values = getListValues(HttpHeaders.CONTENT_LANGUAGE);
+        return values.size() == 0 ? null : new Locale(values.get(0));
     }
 
     public MediaType getMediaType() {
-        String value = (String)m.get(Message.CONTENT_TYPE);
-        
-        return value == null ? null : MediaType.valueOf(value);
+        List<String> values = getListValues(HttpHeaders.CONTENT_TYPE);
+        return values.size() == 0 ? null : MediaType.valueOf(values.get(0));
     }
 
     public MultivaluedMap<String, String> getRequestHeaders() {
-        // should we really worry about immutability given that the Message does not ?
-        MultivaluedMap<String, String> map = new MetadataMap<String, String>();
-        for (String key : headers.keySet()) {
-            map.put(key, getRequestHeader(key));
+        Map<String, List<String>> newHeaders = new LinkedHashMap<String, List<String>>();
+        for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
+            newHeaders.put(entry.getKey(), getListValues(entry.getKey()));
         }
-        return map;
+        return new MetadataMap<String, String>(newHeaders, true, true);
     }
 
     public List<Locale> getAcceptableLanguages() {
-        List<String> values = headers.get(HttpHeaders.ACCEPT_LANGUAGE);
-        if (values == null || values.isEmpty()) {
-            return Collections.emptyList();
-        }
+        List<String> ls = getListValues(HttpHeaders.ACCEPT_LANGUAGE);
+        
         List<Locale> newLs = new ArrayList<Locale>(); 
-        String[] ls =  values.get(0).split(",");
         Map<Locale, Float> prefs = new HashMap<Locale, Float>();
         for (String l : ls) {
             String[] pair = l.split(";");
@@ -115,7 +105,7 @@
                 prefs.put(locale, 1F);
             }
         }
-        if (newLs.size() == 1) {
+        if (newLs.size() <= 1) {
             return newLs;
         }
         
@@ -125,18 +115,26 @@
     }
 
     public List<String> getRequestHeader(String name) {
-        List<String> values = headers.get(name); 
+        return getListValues(name);
+    }
+
+    private List<String> getListValues(String headerName) {
+        List<String> values = headers.get(headerName);
         if (values == null || values.isEmpty()) {
             return Collections.emptyList();
         }
-        List<String> hValues = new ArrayList<String>();
         String[] ls =  values.get(0).split(",");
-        for (String s : ls) {
-            hValues.add(s.trim());
+        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;
         }
-        return hValues;
     }
-
+    
     private static class AcceptLanguageComparator implements Comparator<Locale> {
         private Map<Locale, Float> prefs;
         

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MetadataMap.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MetadataMap.java?rev=756693&r1=756692&r2=756693&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MetadataMap.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MetadataMap.java Fri
Mar 20 17:59:55 2009
@@ -21,6 +21,7 @@
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
@@ -30,6 +31,7 @@
 
 public class MetadataMap<K, V> implements MultivaluedMap<K, V> {
 
+    private boolean caseInsensitive;
     private Map<K, List<V>> m;
     
     public MetadataMap() {
@@ -37,13 +39,29 @@
     }
     
     public MetadataMap(Map<K, List<V>> store) {
+        this(store, false, false);
+    }
+    
+    public MetadataMap(Map<K, List<V>> store, boolean readOnly, boolean caseInsensitive)
{
+        
+        if (!readOnly && caseInsensitive) {
+            throw new IllegalArgumentException(
+                "Case-insensitive keys are only supported for read-only maps at the moment");
+        }
+        this.caseInsensitive = caseInsensitive;
         
         this.m = new LinkedHashMap<K, List<V>>();
         if (store != null) {
             for (Map.Entry<K, List<V>> entry : store.entrySet()) {
-                m.put(entry.getKey(), new ArrayList<V>(entry.getValue()));
+                List<V> values = new ArrayList<V>(entry.getValue());
+                m.put(entry.getKey(), readOnly 
+                      ? Collections.unmodifiableList(values) : values);
             }
         }
+        if (readOnly) {
+            this.m = Collections.unmodifiableMap(m);
+        }
+        
     }
     
     public void add(K key, V value) {
@@ -56,7 +74,7 @@
     }
 
     public V getFirst(K key) {
-        List<V> data = m.get(key);
+        List<V> data = get(key);
         return data == null ? null : data.get(0);
     }
 
@@ -71,7 +89,15 @@
     }
 
     public boolean containsKey(Object key) {
-        return m.containsKey(key);
+        if (!caseInsensitive) {
+            return m.containsKey(key);
+        }
+        for (K theKey : m.keySet()) {
+            if (theKey.toString().toLowerCase().equals(key.toString().toLowerCase())) {
+                return true;
+            }
+        }
+        return false;
     }
 
     public boolean containsValue(Object value) {
@@ -83,7 +109,15 @@
     }
 
     public List<V> get(Object key) {
-        return m.get(key);
+        if (!caseInsensitive) {
+            return m.get(key);
+        }
+        for (Map.Entry<K, List<V>> entry : m.entrySet()) {
+            if (entry.getKey().toString().toLowerCase().equals(key.toString().toLowerCase()))
{
+                return entry.getValue();
+            }
+        }
+        return null;
     }
 
     public boolean isEmpty() {
@@ -128,5 +162,4 @@
         return m.toString();
     }
     
-     
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java?rev=756693&r1=756692&r2=756693&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java Fri
Mar 20 17:59:55 2009
@@ -622,25 +622,28 @@
     
     public static <T> T createContextValue(Message m, Type genericType, Class<T>
clazz) {
  
-        Message inbound = m.getExchange() != null ? m.getExchange().getInMessage() : m;
+        Message contextMessage = m.getExchange() != null ? m.getExchange().getInMessage()
: m;
+        if (contextMessage == null && Boolean.FALSE.equals(m.get(Message.INBOUND_MESSAGE)))
{
+            contextMessage = m;
+        }
         Object o = null;
         if (UriInfo.class.isAssignableFrom(clazz)) {
-            o = createUriInfo(inbound);
+            o = createUriInfo(contextMessage);
         } else if (HttpHeaders.class.isAssignableFrom(clazz)) {
-            o = new HttpHeadersImpl(inbound);
+            o = new HttpHeadersImpl(contextMessage);
         } else if (Request.class.isAssignableFrom(clazz)) {
-            o = new RequestImpl(inbound);
+            o = new RequestImpl(contextMessage);
         } else if (SecurityContext.class.isAssignableFrom(clazz)) {
-            o = new SecurityContextImpl(inbound);
+            o = new SecurityContextImpl(contextMessage);
         } else if (Providers.class.isAssignableFrom(clazz)) {
-            o = new ProvidersImpl(inbound);
+            o = new ProvidersImpl(contextMessage);
         } else if (ContextResolver.class.isAssignableFrom(clazz)) {
-            o = createContextResolver(genericType, inbound);
+            o = createContextResolver(genericType, contextMessage);
         } else if (MessageContext.class.isAssignableFrom(clazz)) {
             o = new MessageContextImpl(m);
         }
         
-        o = o == null ? createServletResourceValue(inbound, clazz) : o;
+        o = o == null ? createServletResourceValue(contextMessage, clazz) : o;
         return clazz.cast(o);
     }
     

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=756693&r1=756692&r2=756693&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
Fri Mar 20 17:59:55 2009
@@ -27,6 +27,7 @@
 
 import javax.ws.rs.core.Cookie;
 import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
 
 import org.apache.cxf.message.Message;
@@ -63,6 +64,17 @@
     }
     
     @Test
+    public void testMediaType() throws Exception {
+        
+        Message m = control.createMock(Message.class);
+        m.get(Message.PROTOCOL_HEADERS);
+        EasyMock.expectLastCall().andReturn(createHeaders());
+        control.replay();
+        HttpHeaders h = new HttpHeadersImpl(m);
+        assertEquals(MediaType.valueOf("*/*"), h.getMediaType());
+    }
+    
+    @Test
     public void testGetHeader() throws Exception {
         
         Message m = control.createMock(Message.class);
@@ -166,4 +178,32 @@
         hs.put(name, list);
         return hs;
     }
+    
+    @Test
+    public void testUnmodifiableRequestHeaders() throws Exception {
+        
+        Message m = control.createMock(Message.class);
+        m.get(Message.PROTOCOL_HEADERS);
+        MetadataMap<String, String> headers = 
+            createHeader(HttpHeaders.ACCEPT_LANGUAGE, 
+                         "en;q=0.7, en-gb;q=0.8, da");
+        EasyMock.expectLastCall().andReturn(headers);
+        control.replay();
+        HttpHeaders h = new HttpHeadersImpl(m);
+        List<Locale> languages = h.getAcceptableLanguages();
+        assertEquals(3, languages.size());
+        languages.clear();
+        languages = h.getAcceptableLanguages();
+        assertEquals(3, languages.size());
+        
+        MultivaluedMap<String, String> rHeaders  = h.getRequestHeaders();
+        List<String> acceptL = rHeaders.get(HttpHeaders.ACCEPT_LANGUAGE);
+        assertEquals(3, acceptL.size());
+        try {
+            rHeaders.clear();
+            fail();
+        } catch (UnsupportedOperationException ex) {
+            // expected
+        }
+    }
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/MetadataMapTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/MetadataMapTest.java?rev=756693&r1=756692&r2=756693&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/MetadataMapTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/MetadataMapTest.java
Fri Mar 20 17:59:55 2009
@@ -62,6 +62,33 @@
         assertEquals("GetFirst value is wrong", "bar", m.getFirst("baz"));
     }
     
+    @Test
+    public void testCopyAndUpdate() {
+        MetadataMap<String, Object> m = new MetadataMap<String, Object>();
+        m.add("baz", "bar");
+        MetadataMap<String, Object> m2 = new MetadataMap<String, Object>(m);
+        m.remove("baz");
+        m.add("baz", "foo");
+        assertEquals("bar", m2.getFirst("baz"));
+        assertEquals("foo", m.getFirst("baz"));
+        
+    }
+    
+    @Test(expected = UnsupportedOperationException.class)
+    public void testReadOnly() {
+        MetadataMap<String, Object> m = new MetadataMap<String, Object>();
+        m.add("baz", "bar");
+        MetadataMap<String, Object> m2 = new MetadataMap<String, Object>(m, true,
false);
+        m2.remove("baz");
+    }
     
-
+    public void testCaseInsensitive() {
+        MetadataMap<String, Object> m = new MetadataMap<String, Object>();
+        m.add("Baz", "bar");
+        MetadataMap<String, Object> m2 = new MetadataMap<String, Object>(m, true,
true);
+        assertEquals("bar", m2.getFirst("baZ"));
+        assertEquals("bar", m2.getFirst("Baz"));
+        assertTrue(m2.containsKey("BaZ"));
+        assertTrue(m2.containsKey("Baz"));
+    }
 }

Modified: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java?rev=756693&r1=756692&r2=756693&view=diff
==============================================================================
--- cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
(original)
+++ cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
Fri Mar 20 17:59:55 2009
@@ -736,7 +736,7 @@
         GetMethod get = new GetMethod(address);
         get.setRequestHeader("Accept", acceptType);
         get.setRequestHeader("Accept-Language", "da;q=0.8,en");
-        get.setRequestHeader("BOOK", "1,2,3");
+        get.setRequestHeader("Book", "1,2,3");
         HttpClient httpClient = new HttpClient();
         try {
             int result = httpClient.executeMethod(get);



Mime
View raw message