cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1460724 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/impl/ResponseBuilderImpl.java test/java/org/apache/cxf/jaxrs/impl/ResponseBuilderImplTest.java
Date Mon, 25 Mar 2013 15:46:43 GMT
Author: sergeyb
Date: Mon Mar 25 15:46:43 2013
New Revision: 1460724

URL: http://svn.apache.org/r1460724
Log:
CXF-4914 implemented empty responsebuilder methods

allow(..)
allow(Set)
encoding()
entity(Object, Annotation[])
replaceAll()
variants(..)

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

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseBuilderImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseBuilderImpl.java?rev=1460724&r1=1460723&r2=1460724&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseBuilderImpl.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseBuilderImpl.java
Mon Mar 25 15:46:43 2013
@@ -22,6 +22,7 @@ package org.apache.cxf.jaxrs.impl;
 import java.lang.annotation.Annotation;
 import java.net.URI;
 import java.text.SimpleDateFormat;
+import java.util.Arrays;
 import java.util.Date;
 import java.util.List;
 import java.util.Locale;
@@ -47,6 +48,7 @@ public final class ResponseBuilderImpl e
     private int status = 200;
     private Object entity;
     private MultivaluedMap<String, Object> metadata = new MetadataMap<String, Object>();
+    private Annotation[] annotations;
 
     public ResponseBuilderImpl() {
     }
@@ -58,10 +60,11 @@ public final class ResponseBuilderImpl e
     }
        
     public Response build() {
-        ResponseImpl r = new ResponseImpl(status, entity);
+        ResponseImpl r = new ResponseImpl(status);
         MetadataMap<String, Object> m = 
             new MetadataMap<String, Object>(metadata, false, true);
         r.addMetadata(m);
+        r.setEntity(entity, annotations);
         reset();
         return r;
     }
@@ -160,7 +163,6 @@ public final class ResponseBuilderImpl e
         type(variant == null ? null : variant.getMediaType());
         language(variant == null ? null : variant.getLanguage());
         setHeader(HttpHeaders.CONTENT_ENCODING, variant == null ? null : variant.getEncoding());
-        
         return this;
     }
 
@@ -220,6 +222,7 @@ public final class ResponseBuilderImpl e
     private void reset() {
         metadata.clear();
         entity = null;
+        annotations = null;
         status = 200;
     }
     
@@ -256,27 +259,25 @@ public final class ResponseBuilderImpl e
     }
 
     @Override
-    public ResponseBuilder allow(String... arg0) {
-        // TODO: Implement
-        throw new UnsupportedOperationException("Not implemented");
+    public ResponseBuilder allow(String... methods) {
+        return addHeader(HttpHeaders.ALLOW, (Object[])methods);
     }
 
     @Override
-    public ResponseBuilder allow(Set<String> arg0) {
-        // TODO: Implement
-        throw new UnsupportedOperationException("Not implemented");
+    public ResponseBuilder allow(Set<String> methods) {
+        return allow(methods.toArray(new String[methods.size()]));
     }
 
     @Override
-    public ResponseBuilder encoding(String arg0) {
-        // TODO: Implement
-        throw new UnsupportedOperationException("Not implemented");
+    public ResponseBuilder encoding(String encoding) {
+        return setHeader(HttpHeaders.CONTENT_ENCODING, encoding);
     }
 
     @Override
-    public ResponseBuilder entity(Object arg0, Annotation[] arg1) {
-        // TODO: Implement
-        throw new UnsupportedOperationException("Not implemented");
+    public ResponseBuilder entity(Object entity, Annotation[] annotations) {
+        this.annotations = annotations;
+        this.entity = entity;
+        return this;
     }
 
     @Override
@@ -297,14 +298,19 @@ public final class ResponseBuilderImpl e
     }
 
     @Override
-    public ResponseBuilder replaceAll(MultivaluedMap<String, Object> arg0) {
-        // TODO: Implement
-        throw new UnsupportedOperationException("Not implemented");
+    public ResponseBuilder replaceAll(MultivaluedMap<String, Object> map) {
+        metadata.clear();
+        if (map != null) {
+            metadata.putAll(map);
+        }
+        return this;
     }
 
     @Override
-    public ResponseBuilder variants(Variant... arg0) {
-        // TODO: Implement
-        throw new UnsupportedOperationException("Not implemented");
+    public ResponseBuilder variants(Variant... variants) {
+        if (variants == null) {
+            return variants((List<Variant>)null);
+        }
+        return variants(Arrays.asList(variants));
     }
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseBuilderImplTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseBuilderImplTest.java?rev=1460724&r1=1460723&r2=1460724&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseBuilderImplTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ResponseBuilderImplTest.java
Mon Mar 25 15:46:43 2013
@@ -19,12 +19,16 @@
 
 package org.apache.cxf.jaxrs.impl;
 
+import java.lang.annotation.Annotation;
 import java.net.URI;
 import java.text.SimpleDateFormat;
 import java.util.Date;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Locale;
+import java.util.Set;
 
+import javax.ws.rs.Produces;
 import javax.ws.rs.core.EntityTag;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.Link;
@@ -41,6 +45,76 @@ import org.junit.Test;
 public class ResponseBuilderImplTest extends Assert {
 
     @Test
+    public void testAllow() throws Exception {
+        MetadataMap<String, Object> m = new MetadataMap<String, Object>();
+        m.add("Allow", "HEAD");
+        m.add("Allow", "GET");
+        checkBuild(Response.ok().allow("HEAD").allow("GET").build(), 200, null, m);
+    }
+    
+    @Test
+    public void testEncoding() throws Exception {
+        MetadataMap<String, Object> m = new MetadataMap<String, Object>();
+        m.add("Content-Encoding", "gzip");
+        checkBuild(Response.ok().encoding("gzip").build(), 200, null, m);
+    }
+
+    @Test
+    public void testEntity() throws Exception {
+        MetadataMap<String, Object> m = new MetadataMap<String, Object>();
+        checkBuild(Response.ok().entity("Hello").build(), 200, "Hello", m);
+    }
+
+    @Test
+    public void testEntityAnnotations() throws Exception {
+        MetadataMap<String, Object> m = new MetadataMap<String, Object>();
+        Annotation[] annotations = new Annotation[1];
+        Annotation produces = new Produces() {
+            @Override
+            public Class<? extends Annotation> annotationType() {
+                return Produces.class;
+            }
+            @Override
+            public String[] value() {
+                return new String[] {
+                    "text/turtle"
+                };
+            }
+        };
+        annotations[0] = produces;
+        Response response = Response.ok().entity("<> a <#test>", annotations).build();
+        checkBuild(response, 200, "<> a <#test>", m);
+        assertArrayEquals(annotations, ((ResponseImpl)response).getEntityAnnotations());
+    }
+
+    @Test
+    public void testReplaceAll() throws Exception {
+        MetadataMap<String, Object> m = new MetadataMap<String, Object>();
+        m.add("Content-Type", "text/plain");
+        checkBuild(Response.ok().type("image/png").tag("removeme").replaceAll(m).build(),
200, null, m);
+
+    }
+
+    @Test
+    public void testAllowReset() throws Exception {
+        MetadataMap<String, Object> m = new MetadataMap<String, Object>();
+        m.add("Allow", "POST");
+        checkBuild(Response.ok().allow("HEAD").allow("GET").allow().allow("POST").build(),
200, null, m);
+    }
+
+    @Test
+    public void testAllowSet() throws Exception {
+        MetadataMap<String, Object> m = new MetadataMap<String, Object>();
+        m.add("Allow", "HEAD");
+        m.add("Allow", "GET");
+        // LinkedHashSet so we get a predictable order
+        Set<String> methods = new LinkedHashSet<String>();
+        methods.add("HEAD");
+        methods.add("GET");
+        checkBuild(Response.ok().allow(methods).build(), 200, null, m);
+    }
+
+    @Test
     public void testValidStatus() {
         assertEquals(100, Response.status(100).build().getStatus());
         assertEquals(101, Response.status(101).build().getStatus());
@@ -260,9 +334,22 @@ public class ResponseBuilderImplTest ext
     }
     
     @Test
-    public void testVariants() throws Exception {
+    public void testVariantsArray() throws Exception {
         
         MetadataMap<String, Object> m = new MetadataMap<String, Object>();
+        m.add("Accept", "application/json");
+        m.add("Accept", "application/xml");
+        m.add("Vary", "Accept");
+
+        Variant json = new Variant(MediaType.APPLICATION_JSON_TYPE, (String)null, null);
+        Variant xml = new Variant(MediaType.APPLICATION_XML_TYPE, (String)null, null);
+
+        checkBuild(Response.ok().variants(json, xml).build(), 200, null, m);
+    }
+
+    @Test
+    public void testVariantsList() throws Exception {
+        MetadataMap<String, Object> m = new MetadataMap<String, Object>();
         m.add("Accept", "text/xml");
         m.add("Accept", "application/xml");
         m.add("Accept-Language", "en_UK");
@@ -281,5 +368,4 @@ public class ResponseBuilderImplTest ext
                    200, null, m);
     }
     
-    
 }



Mime
View raw message