cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1373818 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/impl/ main/java/org/apache/cxf/jaxrs/utils/ test/java/org/apache/cxf/jaxrs/impl/
Date Thu, 16 Aug 2012 13:09:09 GMT
Author: sergeyb
Date: Thu Aug 16 13:09:09 2012
New Revision: 1373818

URL: http://svn.apache.org/viewvc?rev=1373818&view=rev
Log:
[CXF-4455] Some 2.0 Response updates and tests

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/ResponseImpl.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseImplTest.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=1373818&r1=1373817&r2=1373818&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
Thu Aug 16 13:09:09 2012
@@ -271,23 +271,7 @@ public class HttpHeadersImpl implements 
     }
 
     public String getHeaderString(String headerName) {
-        List<String> values = headers.get(headerName);
-        if (values == null) {
-            return null;
-        } else {
-            StringBuilder sb = new StringBuilder();
-            for (int i = 0; i < values.size(); i++) {
-                String value = values.get(i);
-                if (StringUtils.isEmpty(value)) {
-                    continue;
-                }
-                sb.append(value);
-                if (i + 1 < values.size()) {
-                    sb.append(",");
-                }
-            }
-            return sb.toString();
-        }
+        return HttpUtils.getHeaderString(headers.get(headerName));
     }
 
     public int getLength() {

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java?rev=1373818&r1=1373817&r2=1373818&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java
Thu Aug 16 13:09:09 2012
@@ -24,6 +24,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.lang.annotation.Annotation;
 import java.net.URI;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Date;
 import java.util.HashMap;
@@ -54,61 +55,90 @@ public final class ResponseImpl extends 
     private MultivaluedMap<String, Object> metadata;
     private boolean entityClosed;    
     
+    ResponseImpl(int s) {
+        this.status = s;
+    }
+    
     ResponseImpl(int s, Object e) {
         this.status = s;
         this.entity = e;
     }
 
-    public Object getEntity() {
-        return entity;
+    void addMetadata(MultivaluedMap<String, Object> meta) { 
+        this.metadata = meta;
     }
 
     public int getStatus() {
         return status;
     }
 
-    void addMetadata(MultivaluedMap<String, Object> meta) { 
-        this.metadata = meta;
+    public StatusType getStatusInfo() {
+        final Response.Status responseStatus = Response.Status.fromStatusCode(status);
+        return new Response.StatusType() {
+
+            public Family getFamily() {
+                return responseStatus.getFamily();
+            }
+
+            public String getReasonPhrase() {
+                return responseStatus.getReasonPhrase();
+            }
+
+            public int getStatusCode() {
+                return responseStatus.getStatusCode();
+            } 
+            
+        };
+    }
+    
+    public Object getEntity() {
+        return entity;
+    }
+
+    public boolean hasEntity() {
+        return getEntity() != null;
     }
     
     public MultivaluedMap<String, Object> getMetadata() {
-        // don't worry about cloning for now
+        return getHeaders();
+    }
+    
+    public MultivaluedMap<String, Object> getHeaders() {
         return metadata;
     }
-
-    public boolean bufferEntity() throws MessageProcessingException {
-        if (entity instanceof InputStream) {
-            if (entity instanceof ByteArrayInputStream) {
-                return false;
-            } else {
-                try {
-                    InputStream oldEntity = (InputStream)entity;
-                    entity = IOUtils.loadIntoBAIS(oldEntity);
-                    return true;
-                } catch (IOException ex) {
-                    throw new MessageProcessingException(ex);
-                }
-            }
+    
+    public MultivaluedMap<String, String> getStringHeaders() {
+        MetadataMap<String, String> headers = new MetadataMap<String, String>(metadata.size());
+        for (Map.Entry<String, List<Object>> entry : metadata.entrySet()) {
+            headers.put(entry.getKey(), toListOfStrings(entry.getValue()));
         }
-        return false;
+        return headers;
     }
 
-    public void close() throws MessageProcessingException {
-        if (!entityClosed && entity instanceof InputStream
-            && !(entity instanceof ByteArrayInputStream)) {
-            // unbuffered entity
-            try {
-                ((InputStream)entity).close();
-                entity = null;
-                entityClosed = true;
-            } catch (IOException ex) {
-                throw new MessageProcessingException(ex);
+    // TODO: Make this method private with the upgrade to the latest API snapshot
+    public String getHeader(String header) {
+        Object value = metadata.getFirst(header);
+        return value == null ? null : value.toString();
+    }
+    
+    public String getHeaderString(String header) {
+        List<Object> methodValues = metadata.get(header);
+        return HttpUtils.getHeaderString(toListOfStrings(methodValues));
+    }
+    
+    // This conversion is needed as some values may not be Strings
+    private List<String> toListOfStrings(List<Object> values) {
+        if (values == null) {
+            return null; 
+        } else {
+            List<String> stringValues = new ArrayList<String>(values.size());
+            for (Object value : values) {
+                stringValues.add(value.toString());
             }
-            
+            return stringValues;
         }
-        
     }
-
+    
     public Set<String> getAllowedMethods() {
         List<Object> methodValues = metadata.get(HttpHeaders.ALLOW);
         if (methodValues == null) {
@@ -122,6 +152,8 @@ public final class ResponseImpl extends 
         }
     }
 
+    
+    
     public Map<String, NewCookie> getCookies() {
         List<Object> cookieValues = metadata.get(HttpHeaders.SET_COOKIE);
         if (cookieValues == null) {
@@ -149,11 +181,6 @@ public final class ResponseImpl extends 
         return header == null ? null : EntityTag.valueOf(header);
     }
 
-    public String getHeader(String header) {
-        Object value = metadata.getFirst(header);
-        return value == null ? null : value.toString();
-    }
-
     public Locale getLanguage() {
         return HttpUtils.getLocale(getHeader(HttpHeaders.CONTENT_LANGUAGE));
     }
@@ -166,6 +193,16 @@ public final class ResponseImpl extends 
         return HttpUtils.getContentLength(getHeader(HttpHeaders.CONTENT_LENGTH));
     }
 
+    public URI getLocation() {
+        String header = getHeader(HttpHeaders.LOCATION);
+        return header == null ? null : URI.create(header);
+    }
+
+    public MediaType getMediaType() {
+        String header = getHeader(HttpHeaders.CONTENT_TYPE);
+        return header == null ? null : MediaType.valueOf(header);
+    }
+    
     public Link getLink(String relation) {
         // TODO Auto-generated method stub
         return null;
@@ -181,39 +218,6 @@ public final class ResponseImpl extends 
         return null;
     }
 
-    public URI getLocation() {
-        String header = getHeader(HttpHeaders.LOCATION);
-        return header == null ? null : URI.create(header);
-    }
-
-    public MediaType getMediaType() {
-        String header = getHeader(HttpHeaders.CONTENT_LENGTH);
-        return header == null ? null : MediaType.valueOf(header);
-    }
-
-    public StatusType getStatusInfo() {
-        final Response.Status responseStatus = Response.Status.fromStatusCode(status);
-        return new Response.StatusType() {
-
-            public Family getFamily() {
-                return responseStatus.getFamily();
-            }
-
-            public String getReasonPhrase() {
-                return responseStatus.getReasonPhrase();
-            }
-
-            public int getStatusCode() {
-                return responseStatus.getStatusCode();
-            } 
-            
-        };
-    }
-
-    public boolean hasEntity() {
-        return getEntity() != null;
-    }
-
     public boolean hasLink(String relation) {
         // TODO Auto-generated method stub
         return false;
@@ -241,6 +245,36 @@ public final class ResponseImpl extends 
         return null;
     }
     
+    public boolean bufferEntity() throws MessageProcessingException {
+        if (entity instanceof InputStream) {
+            if (entity instanceof ByteArrayInputStream) {
+                return false;
+            } else {
+                try {
+                    InputStream oldEntity = (InputStream)entity;
+                    entity = IOUtils.loadIntoBAIS(oldEntity);
+                    return true;
+                } catch (IOException ex) {
+                    throw new MessageProcessingException(ex);
+                }
+            }
+        }
+        return false;
+    }
+
+    public void close() throws MessageProcessingException {
+        if (!entityClosed && entity instanceof InputStream) {
+            try {
+                ((InputStream)entity).close();
+                entity = null;
+                entityClosed = true;
+            } catch (IOException ex) {
+                throw new MessageProcessingException(ex);
+            }
+        }
+        
+    }
+    
     private void checkEntityIsAvailable() throws MessageProcessingException {
         if (entity == null) {
             throw new MessageProcessingException("Entity is not available");

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java?rev=1373818&r1=1373817&r2=1373818&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java Thu
Aug 16 13:09:09 2012
@@ -44,6 +44,7 @@ import javax.ws.rs.core.Response;
 
 import org.apache.cxf.common.i18n.BundleUtils;
 import org.apache.cxf.common.logging.LogUtils;
+import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.common.util.UrlUtils;
 import org.apache.cxf.jaxrs.impl.HttpHeadersImpl;
 import org.apache.cxf.jaxrs.impl.PathSegmentImpl;
@@ -222,6 +223,25 @@ public final class HttpUtils {
         }
     }
     
+    public static String getHeaderString(List<String> values) {
+        if (values == null) {
+            return null;
+        } else {
+            StringBuilder sb = new StringBuilder();
+            for (int i = 0; i < values.size(); i++) {
+                String value = values.get(i);
+                if (StringUtils.isEmpty(value)) {
+                    continue;
+                }
+                sb.append(value);
+                if (i + 1 < values.size()) {
+                    sb.append(",");
+                }
+            }
+            return sb.toString();
+        }
+    }
+    
     public static boolean isDateRelatedHeader(String headerName) {
         return HttpHeaders.DATE.equalsIgnoreCase(headerName)
                || HttpHeaders.IF_MODIFIED_SINCE.equalsIgnoreCase(headerName)

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseImplTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseImplTest.java?rev=1373818&r1=1373817&r2=1373818&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseImplTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseImplTest.java
Thu Aug 16 13:09:09 2012
@@ -19,6 +19,16 @@
 
 package org.apache.cxf.jaxrs.impl;
 
+import java.io.ByteArrayInputStream;
+import java.util.Map;
+
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.NewCookie;
+import javax.ws.rs.core.Response;
+
+import org.apache.cxf.jaxrs.utils.HttpUtils;
+
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -36,6 +46,129 @@ public class ResponseImplTest extends As
         ri.addMetadata(meta);
         ri.getMetadata();
         assertSame("Wrong metadata", meta, ri.getMetadata());
+        assertSame("Wrong metadata", meta, ri.getHeaders());
+    }
+    
+    @Test
+    public void testHasEntity() {
+        assertTrue(new ResponseImpl(200, "").hasEntity());
+        assertFalse(new ResponseImpl(200).hasEntity());
+    }
+    
+    @Test
+    public void testHasEntityAfterClose() {
+        Response r = new ResponseImpl(200, new ByteArrayInputStream("data".getBytes()));

+        assertTrue(r.hasEntity());
+        r.close();
+        assertFalse(r.hasEntity());
+    }
+    
+    
+    @Test
+    public void testBufferEntityNoEntity() {
+        Response r = new ResponseImpl(200); 
+        assertFalse(r.bufferEntity());
+    }
+    
+    @Test
+    public void testGetHeaderString() {
+        ResponseImpl ri = new ResponseImpl(200);
+        MetadataMap<String, Object> meta = new MetadataMap<String, Object>();
+        ri.addMetadata(meta);
+        assertNull(ri.getHeader("a"));
+        meta.putSingle("a", "aValue");
+        assertEquals("aValue", ri.getHeaderString("a"));
+        meta.add("a", "aValue2");
+        assertEquals("aValue,aValue2", ri.getHeaderString("a"));
+    }
+    
+    @Test
+    public void testGetHeaderStrings() {
+        ResponseImpl ri = new ResponseImpl(200);
+        MetadataMap<String, Object> meta = new MetadataMap<String, Object>();
+        meta.add("Set-Cookie", NewCookie.valueOf("a=b"));
+        ri.addMetadata(meta);
+        MultivaluedMap<String, String> headers = ri.getStringHeaders();
+        assertEquals(1, headers.size());
+        assertEquals("a=b;Version=1", headers.getFirst("Set-Cookie"));
+    }
+    
+    @Test
+    public void testGetCookies() {
+        ResponseImpl ri = new ResponseImpl(200);
+        MetadataMap<String, Object> meta = new MetadataMap<String, Object>();
+        meta.add("Set-Cookie", NewCookie.valueOf("a=b"));
+        meta.add("Set-Cookie", NewCookie.valueOf("c=d"));
+        ri.addMetadata(meta);
+        Map<String, NewCookie> cookies = ri.getCookies();
+        assertEquals(2, cookies.size());
+        assertEquals("a=b;Version=1", cookies.get("a").toString());
+        assertEquals("c=d;Version=1", cookies.get("c").toString());
+    }
+    
+    @Test
+    public void testGetContentLength() {
+        ResponseImpl ri = new ResponseImpl(200);
+        MetadataMap<String, Object> meta = new MetadataMap<String, Object>();
+        ri.addMetadata(meta);
+        assertEquals(-1, ri.getLength());
+        meta.add("Content-Length", "10");
+        assertEquals(10, ri.getLength());
+    }
+    
+    @Test
+    public void testGetDate() {
+        doTestDate(HttpHeaders.DATE);
+    }
+    
+    @Test
+    public void testLastModified() {
+        doTestDate(HttpHeaders.LAST_MODIFIED);
+    }
+    
+    public void doTestDate(String dateHeader) {
+        boolean date = HttpHeaders.DATE.equals(dateHeader);
+        ResponseImpl ri = new ResponseImpl(200);
+        MetadataMap<String, Object> meta = new MetadataMap<String, Object>();
+        meta.add(dateHeader, "Tue, 21 Oct 2008 17:00:00 GMT");
+        ri.addMetadata(meta);
+        assertEquals(HttpUtils.getHttpDate("Tue, 21 Oct 2008 17:00:00 GMT"), 
+                     date ? ri.getDate() : ri.getLastModified());
+    }
+    
+    @Test
+    public void testEntityTag() {
+        ResponseImpl ri = new ResponseImpl(200);
+        MetadataMap<String, Object> meta = new MetadataMap<String, Object>();
+        meta.add(HttpHeaders.ETAG, "1234");
+        ri.addMetadata(meta);
+        assertEquals("\"1234\"", ri.getEntityTag().toString());
+    }
+    
+    @Test
+    public void testLocation() {
+        ResponseImpl ri = new ResponseImpl(200);
+        MetadataMap<String, Object> meta = new MetadataMap<String, Object>();
+        meta.add(HttpHeaders.LOCATION, "http://localhost:8080");
+        ri.addMetadata(meta);
+        assertEquals("http://localhost:8080", ri.getLocation().toString());
+    }
+    
+    @Test
+    public void testGetLanguage() {
+        ResponseImpl ri = new ResponseImpl(200);
+        MetadataMap<String, Object> meta = new MetadataMap<String, Object>();
+        meta.add(HttpHeaders.CONTENT_LANGUAGE, "en-US");
+        ri.addMetadata(meta);
+        assertEquals("en_US", ri.getLanguage().toString());
     }
 
+    @Test
+    public void testGetMediaType() {
+        ResponseImpl ri = new ResponseImpl(200);
+        MetadataMap<String, Object> meta = new MetadataMap<String, Object>();
+        meta.add(HttpHeaders.CONTENT_TYPE, "text/xml");
+        ri.addMetadata(meta);
+        assertEquals("text/xml", ri.getMediaType().toString());
+    }
 }



Mime
View raw message