cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1373524 - 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 Wed, 15 Aug 2012 16:43:29 GMT
Author: sergeyb
Date: Wed Aug 15 16:43:29 2012
New Revision: 1373524

URL: http://svn.apache.org/viewvc?rev=1373524&view=rev
Log:
[CXF-4455] Prototyping some more 2.0 Response implementation code

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/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=1373524&r1=1373523&r2=1373524&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
Wed Aug 15 16:43:29 2012
@@ -20,7 +20,6 @@
 package org.apache.cxf.jaxrs.impl;
 
 
-import java.text.ParseException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
@@ -126,7 +125,7 @@ public class HttpHeadersImpl implements 
     
     public Locale getLanguage() {
         List<String> values = getListValues(HttpHeaders.CONTENT_LANGUAGE);
-        return values.size() == 0 ? null : createLocale(values.get(0).trim());
+        return values.size() == 0 ? null : HttpUtils.getLocale(values.get(0).trim());
     }
 
     public MediaType getMediaType() {
@@ -150,7 +149,7 @@ public class HttpHeadersImpl implements 
         for (String l : ls) {
             String[] pair = l.split(";");
             
-            Locale locale = createLocale(pair[0].trim());
+            Locale locale = HttpUtils.getLocale(pair[0].trim());
             
             newLs.add(locale);
             if (pair.length > 1) {
@@ -251,19 +250,6 @@ public class HttpHeadersImpl implements 
         }
     }
     
-    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>() {
@@ -281,11 +267,7 @@ public class HttpHeadersImpl implements 
         if (values == null || StringUtils.isEmpty(values.get(0))) {
             return null;
         }
-        try {
-            return HttpUtils.getHttpDateFormat().parse(values.get(0));
-        } catch (ParseException ex) {
-            return null;
-        }
+        return HttpUtils.getHttpDate(values.get(0));
     }
 
     public String getHeaderString(String headerName) {
@@ -313,12 +295,7 @@ public class HttpHeadersImpl implements 
         if (values == null || values.size() != 1) {
             return -1;
         }
-        try {
-            int len = Integer.valueOf(values.get(0));
-            return len >= 0 ? len : -1;
-        } catch (Exception ex) {
-            return -1;
-        }
+        return HttpUtils.getContentLength(values.get(0));
     }
     
 }

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=1373524&r1=1373523&r2=1373524&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
Wed Aug 15 16:43:29 2012
@@ -19,9 +19,16 @@
 
 package org.apache.cxf.jaxrs.impl;
 
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
 import java.lang.annotation.Annotation;
 import java.net.URI;
+import java.util.Collections;
 import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
@@ -29,18 +36,23 @@ import java.util.Set;
 import javax.ws.rs.MessageProcessingException;
 import javax.ws.rs.core.EntityTag;
 import javax.ws.rs.core.GenericType;
+import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.Link;
 import javax.ws.rs.core.Link.Builder;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.NewCookie;
 import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status.Family;
+
+import org.apache.cxf.helpers.IOUtils;
+import org.apache.cxf.jaxrs.utils.HttpUtils;
 
 public final class ResponseImpl extends Response {
     private final int status;
-    private final Object entity;
+    private Object entity;
     private MultivaluedMap<String, Object> metadata;
-    
+    private boolean entityClosed;    
     
     ResponseImpl(int s, Object e) {
         this.status = s;
@@ -64,137 +76,174 @@ public final class ResponseImpl extends 
         return metadata;
     }
 
-    @Override
     public boolean bufferEntity() throws MessageProcessingException {
-        // TODO Auto-generated method stub
+        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;
     }
 
-    @Override
     public void close() throws MessageProcessingException {
-        // TODO Auto-generated method stub
+        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);
+            }
+            
+        }
         
     }
 
-    @Override
     public Set<String> getAllowedMethods() {
-        // TODO Auto-generated method stub
-        return null;
+        List<Object> methodValues = metadata.get(HttpHeaders.ALLOW);
+        if (methodValues == null) {
+            return Collections.emptySet();
+        } else {
+            Set<String> methods = new HashSet<String>();
+            for (Object o : methodValues) {
+                methods.add(o.toString());
+            }
+            return methods;
+        }
     }
 
-    @Override
     public Map<String, NewCookie> getCookies() {
-        // TODO Auto-generated method stub
-        return null;
+        List<Object> cookieValues = metadata.get(HttpHeaders.SET_COOKIE);
+        if (cookieValues == null) {
+            return Collections.emptyMap();
+        } else {
+            Map<String, NewCookie> cookies = new HashMap<String, NewCookie>();
+            for (Object o : cookieValues) {
+                NewCookie newCookie = NewCookie.valueOf(o.toString());
+                cookies.put(newCookie.getName(), newCookie);
+            }
+            return cookies;
+        }
     }
 
-    @Override
     public Date getDate() {
-        // TODO Auto-generated method stub
-        return null;
+        return doGetDate(HttpHeaders.DATE);
     }
 
-    @Override
+    private Date doGetDate(String dateHeader) {
+        return HttpUtils.getHttpDate(getHeader(dateHeader));
+    }
+    
     public EntityTag getEntityTag() {
-        // TODO Auto-generated method stub
-        return null;
+        String header = getHeader(HttpHeaders.ETAG);
+        return header == null ? null : EntityTag.valueOf(header);
     }
 
-    @Override
-    public String getHeader(String arg0) {
-        // TODO Auto-generated method stub
-        return null;
+    public String getHeader(String header) {
+        Object value = metadata.getFirst(header);
+        return value == null ? null : value.toString();
     }
 
-    @Override
     public Locale getLanguage() {
-        // TODO Auto-generated method stub
-        return null;
+        return HttpUtils.getLocale(getHeader(HttpHeaders.CONTENT_LANGUAGE));
     }
 
-    @Override
     public Date getLastModified() {
-        // TODO Auto-generated method stub
-        return null;
+        return doGetDate(HttpHeaders.LAST_MODIFIED);
     }
 
-    @Override
     public int getLength() {
-        // TODO Auto-generated method stub
-        return 0;
+        return HttpUtils.getContentLength(getHeader(HttpHeaders.CONTENT_LENGTH));
     }
 
-    @Override
-    public Link getLink(String arg0) {
+    public Link getLink(String relation) {
         // TODO Auto-generated method stub
         return null;
     }
 
-    @Override
     public Builder getLinkBuilder(String arg0) {
         // TODO Auto-generated method stub
         return null;
     }
 
-    @Override
     public Set<Link> getLinks() {
         // TODO Auto-generated method stub
         return null;
     }
 
-    @Override
     public URI getLocation() {
-        // TODO Auto-generated method stub
-        return null;
+        String header = getHeader(HttpHeaders.LOCATION);
+        return header == null ? null : URI.create(header);
     }
 
-    @Override
     public MediaType getMediaType() {
-        // TODO Auto-generated method stub
-        return null;
+        String header = getHeader(HttpHeaders.CONTENT_LENGTH);
+        return header == null ? null : MediaType.valueOf(header);
     }
 
-    @Override
     public StatusType getStatusInfo() {
-        // TODO Auto-generated method stub
-        return null;
+        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();
+            } 
+            
+        };
     }
 
-    @Override
     public boolean hasEntity() {
         return getEntity() != null;
     }
 
-    @Override
-    public boolean hasLink(String arg0) {
+    public boolean hasLink(String relation) {
         // TODO Auto-generated method stub
         return false;
     }
 
-    @Override
-    public <T> T readEntity(Class<T> arg0) throws MessageProcessingException,
IllegalStateException {
-        // TODO Auto-generated method stub
+    public <T> T readEntity(Class<T> cls) throws MessageProcessingException,
IllegalStateException {
+        checkEntityIsAvailable();
         return null;
     }
 
-    @Override
-    public <T> T readEntity(GenericType<T> arg0) throws MessageProcessingException,
IllegalStateException {
-        // TODO Auto-generated method stub
+    public <T> T readEntity(GenericType<T> genType) throws MessageProcessingException,
IllegalStateException {
+        checkEntityIsAvailable();
         return null;
     }
 
-    @Override
-    public <T> T readEntity(Class<T> arg0, Annotation[] arg1) throws MessageProcessingException,
+    public <T> T readEntity(Class<T> cls, Annotation[] anns) throws MessageProcessingException,
         IllegalStateException {
-        // TODO Auto-generated method stub
+        checkEntityIsAvailable();
         return null;
     }
 
-    @Override
-    public <T> T readEntity(GenericType<T> arg0, Annotation[] arg1) throws MessageProcessingException,
+    public <T> T readEntity(GenericType<T> genType, Annotation[] anns) throws
MessageProcessingException,
         IllegalStateException {
-        // TODO Auto-generated method stub
+        checkEntityIsAvailable();
         return null;
     }
     
+    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=1373524&r1=1373523&r2=1373524&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 Wed
Aug 15 16:43:29 2012
@@ -23,7 +23,9 @@ import java.io.UnsupportedEncodingExcept
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URLEncoder;
+import java.text.ParseException;
 import java.text.SimpleDateFormat;
+import java.util.Date;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
@@ -180,6 +182,46 @@ public final class HttpUtils {
         return dateFormat;
     }
     
+    public static Date getHttpDate(String value) {
+        if (value == null) {
+            return null;
+        }
+        try {
+            return HttpUtils.getHttpDateFormat().parse(value);
+        } catch (ParseException ex) {
+            return null;
+        }
+    }
+    
+    public static Locale getLocale(String value) {
+        if (value == null) {
+            return null;
+        }
+        
+        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]);
+        }
+        
+    }
+    
+    public static int getContentLength(String value) {
+        if (value == null) {
+            return -1;
+        }
+        try {
+            int len = Integer.valueOf(value);
+            return len >= 0 ? len : -1;
+        } catch (Exception ex) {
+            return -1;
+        }
+    }
+    
     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/HttpHeadersImplTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/HttpHeadersImplTest.java?rev=1373524&r1=1373523&r2=1373524&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
Wed Aug 15 16:43:29 2012
@@ -161,7 +161,7 @@ public class HttpHeadersImplTest extends
     }
     
     @Test
-    public void testGetMissingContentLegth() throws Exception {
+    public void testGetMissingContentLength() throws Exception {
         
         Message m = new MessageImpl();
         m.put(Message.PROTOCOL_HEADERS, new MetadataMap<String, String>());
@@ -170,7 +170,7 @@ public class HttpHeadersImplTest extends
     }
     
     @Test
-    public void testGetContentLegth() throws Exception {
+    public void testGetContentLength() throws Exception {
         
         Message m = new MessageImpl();
         m.put(Message.PROTOCOL_HEADERS, createHeaders());



Mime
View raw message