cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r769334 - in /cxf/trunk: 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/apache/cxf/jaxrs/impl/ rt/transports/http/src/main/java/org/...
Date Tue, 28 Apr 2009 11:09:45 GMT
Author: sergeyb
Date: Tue Apr 28 11:09:44 2009
New Revision: 769334

URL: http://svn.apache.org/viewvc?rev=769334&view=rev
Log:
JAXRS HttpHeadersImpl updates (Locale construction, sorting media types), adding Content-Type
to Message headers 

Modified:
    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/MediaTypeHeaderProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/NewCookieHeaderProvider.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/MediaTypeHeaderProviderTest.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/NewCookieHeaderProviderTest.java
    cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/AbstractHTTPDestination.java
    cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.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=769334&r1=769333&r2=769334&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
Tue Apr 28 11:09:44 2009
@@ -53,7 +53,9 @@
         if (lValues == null || lValues.isEmpty()) {
             return Collections.emptyList();
         }
-        return JAXRSUtils.sortMediaTypes(lValues.get(0)); 
+        List<MediaType> mediaTypes = JAXRSUtils.parseMediaTypes(lValues.get(0));
+        sortMediaTypesUsingQualityFactor(mediaTypes); 
+        return mediaTypes;
     }
 
     public Map<String, Cookie> getCookies() {
@@ -68,7 +70,7 @@
 
     public Locale getLanguage() {
         List<String> values = getListValues(HttpHeaders.CONTENT_LANGUAGE);
-        return values.size() == 0 ? null : new Locale(values.get(0));
+        return values.size() == 0 ? null : new Locale(values.get(0).trim());
     }
 
     public MediaType getMediaType() {
@@ -92,7 +94,7 @@
         for (String l : ls) {
             String[] pair = l.split(";");
             
-            Locale locale = new Locale(pair[0].trim());
+            Locale locale = createLocale(pair[0].trim());
             
             newLs.add(locale);
             if (pair.length > 1) {
@@ -149,8 +151,33 @@
         public int compare(Locale lang1, Locale lang2) {
             float p1 = prefs.get(lang1);
             float p2 = prefs.get(lang2);
-            int result = Float.compare(p1, p2);
-            return result == 0 ? result : result * -1;
+            return Float.compare(p1, p2) * -1;
         }
     }
+    
+    private Locale createLocale(String value) {
+        String[] values = value.split("-");
+        if (values.length == 0 || values.length > 2) {
+            throw new IllegalArgumentException("Illegal locale value : " + value);
+        }
+        if (values.length == 1) {
+            return new Locale(values[0]);
+        } else {
+            return new Locale(values[0], values[1]);
+        }
+        
+    }
+    
+    private void sortMediaTypesUsingQualityFactor(List<MediaType> types) {
+        if (types.size() > 1) {
+            Collections.sort(types, new Comparator<MediaType>() {
+
+                public int compare(MediaType mt1, MediaType mt2) {
+                    return JAXRSUtils.compareMediaTypesQualityFactors(mt1, mt2);
+                }
+                
+            });
+        }
+    }
+    
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MediaTypeHeaderProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MediaTypeHeaderProvider.java?rev=769334&r1=769333&r2=769334&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MediaTypeHeaderProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MediaTypeHeaderProvider.java
Tue Apr 28 11:09:44 2009
@@ -32,6 +32,10 @@
 
     public MediaType fromString(String mType) {
         
+        if (mType == null) {
+            throw new IllegalArgumentException("Media type value can not be null");
+        }
+        
         if (mType.equals(MediaType.MEDIA_TYPE_WILDCARD) || mType.startsWith("*;")) {
             return new MediaType("*", "*");
         }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/NewCookieHeaderProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/NewCookieHeaderProvider.java?rev=769334&r1=769333&r2=769334&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/NewCookieHeaderProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/NewCookieHeaderProvider.java
Tue Apr 28 11:09:44 2009
@@ -34,6 +34,10 @@
     
     public NewCookie fromString(String c) {
         
+        if (c == null) {
+            throw new IllegalArgumentException("SetCookie value can not be null");
+        }
+        
         String name = null;
         String value = null;
         String path = null;
@@ -44,23 +48,24 @@
         
         String[] tokens = c.split(";");
         for (String token : tokens) {
-            if (token.startsWith(VERSION)) {
+            String theToken = token.trim();
+            if (theToken.startsWith(VERSION)) {
                 // should we throw an exception if it's not == 1 ?
-            } else if (token.startsWith(MAX_AGE)) {
-                maxAge = Integer.parseInt(token.substring(MAX_AGE.length() + 1));
-            } else if (token.startsWith(PATH)) {
-                path = token.substring(PATH.length() + 1);
-            } else if (token.startsWith(DOMAIN)) {
-                domain = token.substring(DOMAIN.length() + 1);
-            } else if (token.startsWith(COMMENT)) {
-                comment = token.substring(COMMENT.length() + 1);
-            } else if (token.startsWith(SECURE)) {
+            } else if (theToken.startsWith(MAX_AGE)) {
+                maxAge = Integer.parseInt(theToken.substring(MAX_AGE.length() + 1));
+            } else if (theToken.startsWith(PATH)) {
+                path = theToken.substring(PATH.length() + 1);
+            } else if (theToken.startsWith(DOMAIN)) {
+                domain = theToken.substring(DOMAIN.length() + 1);
+            } else if (theToken.startsWith(COMMENT)) {
+                comment = theToken.substring(COMMENT.length() + 1);
+            } else if (theToken.startsWith(SECURE)) {
                 isSecure = true;
             } else {
-                int i = token.indexOf('=');
+                int i = theToken.indexOf('=');
                 if (i != -1) {
-                    name = token.substring(0, i);
-                    value = i == token.length()  + 1 ? "" : token.substring(i + 1);
+                    name = theToken.substring(0, i);
+                    value = i == theToken.length()  + 1 ? "" : theToken.substring(i + 1);
                 }
             }
         }

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=769334&r1=769333&r2=769334&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 Tue
Apr 28 11:09:44 2009
@@ -385,17 +385,15 @@
             return -1;
         }       
         
-        
+        return compareMediaTypesQualityFactors(mt1, mt2);
+    }
+    
+    public static int compareMediaTypesQualityFactors(MediaType mt1, MediaType mt2) {
         float q1 = getMediaTypeQualityFactor(mt1.getParameters().get("q"));
         float q2 = getMediaTypeQualityFactor(mt2.getParameters().get("q"));
-        int result = Float.compare(q1, q2);
-        if (result != 0) {
-            return result * -1;
-        }
-        
-        return 0;
-        
+        return Float.compare(q1, q2) * -1;
     }
+    
 
     public static float getMediaTypeQualityFactor(String q) {
         if (q == null) {

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=769334&r1=769333&r2=769334&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
Tue Apr 28 11:09:44 2009
@@ -58,8 +58,10 @@
         HttpHeaders h = new HttpHeadersImpl(m);
         MultivaluedMap<String, String> hs = h.getRequestHeaders();
         List<String> acceptValues = hs.get("Accept");
-        assertEquals("text/*;q=1", acceptValues.get(0));
-        assertEquals("application/xml", acceptValues.get(1));
+        assertEquals(3, acceptValues.size());
+        assertEquals("text/bar;q=0.6", acceptValues.get(0));
+        assertEquals("text/*;q=1", acceptValues.get(1));
+        assertEquals("application/xml", acceptValues.get(2));
         assertEquals(hs.getFirst("Content-Type"), "*/*");
     }
     
@@ -75,6 +77,21 @@
     }
     
     @Test
+    public void testGetMediaTypes() throws Exception {
+        
+        Message m = control.createMock(Message.class);
+        m.get(Message.PROTOCOL_HEADERS);
+        EasyMock.expectLastCall().andReturn(createHeaders());
+        control.replay();
+        HttpHeaders h = new HttpHeadersImpl(m);
+        List<MediaType> acceptValues = h.getAcceptableMediaTypes();
+        assertEquals(3, acceptValues.size());
+        assertEquals("text/*;q=1", acceptValues.get(0).toString());
+        assertEquals("application/xml", acceptValues.get(1).toString());
+        assertEquals("text/bar;q=0.6", acceptValues.get(2).toString());
+    }
+    
+    @Test
     public void testGetHeader() throws Exception {
         
         Message m = control.createMock(Message.class);
@@ -83,9 +100,10 @@
         control.replay();
         HttpHeaders h = new HttpHeadersImpl(m);
         List<String> acceptValues = h.getRequestHeader("Accept");
-        assertEquals(2, acceptValues.size());
-        assertEquals("text/*;q=1", acceptValues.get(0));
-        assertEquals("application/xml", acceptValues.get(1));
+        assertEquals(3, acceptValues.size());
+        assertEquals("text/bar;q=0.6", acceptValues.get(0));
+        assertEquals("text/*;q=1", acceptValues.get(1));
+        assertEquals("application/xml", acceptValues.get(2));
         List<String> contentValues = h.getRequestHeader("Content-Type");
         assertEquals(1, contentValues.size());
         assertEquals("*/*", contentValues.get(0));
@@ -162,14 +180,14 @@
         List<Locale> languages = h.getAcceptableLanguages();
         assertEquals(3, languages.size());
         assertEquals(new Locale("da"), languages.get(0));
-        assertEquals(new Locale("en-gb"), languages.get(1));
+        assertEquals(new Locale("en", "GB"), languages.get(1));
         assertEquals(new Locale("en"), languages.get(2));
     }
     
         
     private MetadataMap<String, String> createHeaders() {
         MetadataMap<String, String> hs = new MetadataMap<String, String>();
-        hs.putSingle("Accept", "text/*;q=1,application/xml");
+        hs.putSingle("Accept", "text/bar;q=0.6,text/*;q=1,application/xml");
         hs.putSingle("Content-Type", "*/*");
         hs.putSingle("Date", "Tue, 21 Oct 2008 17:00:00 GMT");
         return hs;

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/MediaTypeHeaderProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/MediaTypeHeaderProviderTest.java?rev=769334&r1=769333&r2=769334&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/MediaTypeHeaderProviderTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/MediaTypeHeaderProviderTest.java
Tue Apr 28 11:09:44 2009
@@ -30,6 +30,11 @@
 
 public class MediaTypeHeaderProviderTest extends Assert {
     
+    @Test(expected = IllegalArgumentException.class)
+    public void testNullValue() throws Exception {
+        MediaType.valueOf(null);
+    }
+    
     @Test
     public void testSimpleType() {
         MediaType m = MediaType.valueOf("text/html");

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/NewCookieHeaderProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/NewCookieHeaderProviderTest.java?rev=769334&r1=769333&r2=769334&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/NewCookieHeaderProviderTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/NewCookieHeaderProviderTest.java
Tue Apr 28 11:09:44 2009
@@ -26,6 +26,10 @@
 
 public class NewCookieHeaderProviderTest extends Assert {
     
+    @Test(expected = IllegalArgumentException.class)
+    public void testNullValue() throws Exception {
+        NewCookie.valueOf(null);
+    }
         
     @Test
     public void testFromSimpleString() {
@@ -48,6 +52,20 @@
                    && 10 == c.getMaxAge());
     }
     
+    
+    @Test
+    public void testFromStringWithSpaces() {
+        NewCookie c = NewCookie.valueOf(
+                      "foo=bar; Comment=comment; Path=path; Max-Age=10; Domain=domain; Secure;
Version=1");
+        assertTrue("bar".equals(c.getValue())
+                   && "foo".equals(c.getName())
+                   && 1 == c.getVersion()
+                   && "path".equals(c.getPath())
+                   && "domain".equals(c.getDomain())
+                   && "comment".equals(c.getComment())
+                   && 10 == c.getMaxAge());
+    }
+    
     @Test
     public void testToString() {
         NewCookie c = new NewCookie("foo", "bar", "path", "domain", "comment", 2, true);
@@ -55,4 +73,5 @@
                      c.toString());
                
     }
+    
 }

Modified: cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/AbstractHTTPDestination.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/AbstractHTTPDestination.java?rev=769334&r1=769333&r2=769334&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/AbstractHTTPDestination.java
(original)
+++ cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/AbstractHTTPDestination.java
Tue Apr 28 11:09:44 2009
@@ -28,6 +28,7 @@
 import java.security.Principal;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -194,6 +195,7 @@
      */
     protected void copyRequestHeaders(Message message, Map<String, List<String>>
headers) {
         HttpServletRequest req = (HttpServletRequest)message.get(HTTP_REQUEST);
+        
         //TODO how to deal with the fields        
         for (Enumeration e = req.getHeaderNames(); e.hasMoreElements();) {
             String fname = (String)e.nextElement();
@@ -210,6 +212,7 @@
                 values.add(val);
             }
         }
+        headers.put(Message.CONTENT_TYPE, Collections.singletonList(req.getContentType()));
     }
 
     /**

Modified: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java?rev=769334&r1=769333&r2=769334&view=diff
==============================================================================
--- cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java (original)
+++ cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java Tue Apr 28
11:09:44 2009
@@ -368,6 +368,13 @@
     @Produces("text/xml")
     @Consumes("application/xml")
     public Response addBook(Book book) {
+        String ct1 = httpHeaders.getMediaType().toString();
+        String ct2 = httpHeaders.getRequestHeader("Content-Type").get(0);
+        String ct3 = httpHeaders.getRequestHeaders().getFirst("Content-Type");
+        if (!("application/xml".equals(ct1) && ct1.equals(ct2) && ct1.equals(ct3)))
{
+            throw new RuntimeException("Unexpected content type");
+        }
+        
         book.setId(++bookId);
         books.put(book.getId(), book);
 



Mime
View raw message