cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1090267 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/client/ main/java/org/apache/cxf/jaxrs/ext/form/ main/java/org/apache/cxf/jaxrs/provider/ main/java/org/apache/cxf/jaxrs/utils/ test/java/org/apache/cxf/jaxrs/pr...
Date Fri, 08 Apr 2011 14:29:50 GMT
Author: sergeyb
Date: Fri Apr  8 14:29:50 2011
New Revision: 1090267

URL: http://svn.apache.org/viewvc?rev=1090267&view=rev
Log:
[CXF-3392] Updating FormEncodingProvider to support Form extensions

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/form/Form.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/FormEncodingProviderTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java?rev=1090267&r1=1090266&r2=1090267&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
Fri Apr  8 14:29:50 2011
@@ -374,10 +374,10 @@ public class ClientProxyImpl extends Abs
         }
     }
 
-    private MultivaluedMap<String, Object> handleForm(MultivaluedMap<ParameterType,
Parameter> map, 
+    private MultivaluedMap<String, String> handleForm(MultivaluedMap<ParameterType,
Parameter> map, 
                                                       Object[] params) {
         
-        MultivaluedMap<String, Object> form = new MetadataMap<String, Object>();
+        MultivaluedMap<String, String> form = new MetadataMap<String, String>();
         
         List<Parameter> fm = getParameters(map, ParameterType.FORM);
         for (Parameter p : fm) {

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/form/Form.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/form/Form.java?rev=1090267&r1=1090266&r2=1090267&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/form/Form.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/form/Form.java Fri
Apr  8 14:29:50 2011
@@ -29,8 +29,16 @@ import org.apache.cxf.jaxrs.utils.FormUt
  *
  */
 public class Form {
-    private MultivaluedMap<String, Object> map = 
-        new MetadataMap<String, Object>();
+    private MultivaluedMap<String, String> map = 
+        new MetadataMap<String, String>();
+    
+    public Form() {
+        
+    }
+    
+    public Form(MultivaluedMap<String, String> map) {
+        this.map = map;
+    }
     
     public Form set(String name, Object value) {
         
@@ -38,8 +46,8 @@ public class Form {
         return this;
     }
     
-    public MultivaluedMap<String, Object> getData() {
-        return new MetadataMap<String, Object>(map);
+    public MultivaluedMap<String, String> getData() {
+        return map;
     }
     
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingProvider.java?rev=1090267&r1=1090266&r2=1090267&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingProvider.java
Fri Apr  8 14:29:50 2011
@@ -43,6 +43,7 @@ import javax.ws.rs.ext.Provider;
 
 import org.apache.cxf.attachment.AttachmentUtil;
 import org.apache.cxf.jaxrs.ext.MessageContext;
+import org.apache.cxf.jaxrs.ext.form.Form;
 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
 import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition;
 import org.apache.cxf.jaxrs.ext.multipart.MultipartBody;
@@ -103,7 +104,7 @@ public class FormEncodingProvider implem
             
             persistParamsOnMessage(params);
             
-            return params;
+            return getFormObject(clazz, params);
         } catch (WebApplicationException e) {
             throw e;
         } catch (Exception e) {
@@ -120,12 +121,17 @@ public class FormEncodingProvider implem
     
     @SuppressWarnings("unchecked")
     protected MultivaluedMap<String, String> createMap(Class<?> clazz) throws
Exception {
-        if (clazz == MultivaluedMap.class) {
+        if (clazz == MultivaluedMap.class || clazz == Form.class) {
             return new MetadataMap<String, String>();
         }
         return (MultivaluedMap<String, String>)clazz.newInstance();
     }
     
+    @SuppressWarnings("unchecked")
+    private Object getFormObject(Class<?> clazz, MultivaluedMap<String, String>
params) {
+        return Form.class.isAssignableFrom(clazz) ? new Form((MultivaluedMap)params) : params;
+    }
+    
     /**
      * Retrieve map of parameters from the passed in message
      *
@@ -167,10 +173,10 @@ public class FormEncodingProvider implem
 
     private boolean isSupported(Class<?> type, Type genericType, Annotation[] annotations,

                                 MediaType mt) {
-        return MultivaluedMap.class.isAssignableFrom(type)
-            || mt.getType().equalsIgnoreCase("multipart") 
+        return (MultivaluedMap.class.isAssignableFrom(type) || Form.class.isAssignableFrom(type))

+            || (mt.getType().equalsIgnoreCase("multipart") 
             && mt.isCompatible(MediaType.MULTIPART_FORM_DATA_TYPE)
-            && (MultipartBody.class.isAssignableFrom(type) || Attachment.class.isAssignableFrom(type));
+            && (MultipartBody.class.isAssignableFrom(type) || Attachment.class.isAssignableFrom(type)));
     }
     
     @SuppressWarnings("unchecked")
@@ -189,7 +195,8 @@ public class FormEncodingProvider implem
             provider.setMessageContext(mc);
             provider.writeTo(body, body.getClass(), body.getClass(), anns, mt, headers, os);
         } else {
-            MultivaluedMap<String, String> map = (MultivaluedMap<String, String>)obj;
+            MultivaluedMap<String, String> map = 
+                (MultivaluedMap<String, String>)(obj instanceof Form ? ((Form)obj).getData()
: obj);
             boolean encoded = AnnotationUtils.getAnnotation(anns, Encoded.class) != null;
             
             String encoding = HttpUtils.getSetEncoding(mt, headers, "UTF-8");

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java?rev=1090267&r1=1090266&r2=1090267&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java Fri
Apr  8 14:29:50 2011
@@ -47,7 +47,7 @@ public final class FormUtils {
         
     }
     
-    public static void addPropertyToForm(MultivaluedMap<String, Object> map, String
name, Object value) {
+    public static void addPropertyToForm(MultivaluedMap<String, String> map, String
name, Object value) {
         if (!"".equals(name)) {
             map.add(name, value.toString());
         } else {

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/FormEncodingProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/FormEncodingProviderTest.java?rev=1090267&r1=1090266&r2=1090267&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/FormEncodingProviderTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/FormEncodingProviderTest.java
Fri Apr  8 14:29:50 2011
@@ -31,6 +31,7 @@ import javax.ws.rs.WebApplicationExcepti
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
 
+import org.apache.cxf.jaxrs.ext.form.Form;
 import org.apache.cxf.jaxrs.impl.MetadataMap;
 import org.apache.cxf.jaxrs.utils.HttpUtils;
 
@@ -61,6 +62,18 @@ public class FormEncodingProviderTest ex
     
     @SuppressWarnings("unchecked")
     @Test
+    public void testReadFromForm() throws Exception {
+        InputStream is = getClass().getResourceAsStream("singleValPostBody.txt");
+        Form form = (Form)ferp.readFrom((Class)Form.class, null, 
+                new Annotation[]{}, MediaType.APPLICATION_FORM_URLENCODED_TYPE, null, is);
+        MultivaluedMap<String, String> mvMap = form.getData();
+        assertEquals("Wrong entry for foo", "bar", mvMap.getFirst("foo"));
+        assertEquals("Wrong entry for boo", "far", mvMap.getFirst("boo"));
+
+    }
+    
+    @SuppressWarnings("unchecked")
+    @Test
     public void testDecoded() throws Exception {
         String values = "foo=1+2&bar=1+3";
         
@@ -169,6 +182,17 @@ public class FormEncodingProviderTest ex
         assertEquals("Wrong value", "a=a1&b=b1", result);  
     }
     
+    @Test
+    public void testWriteForm() throws Exception {
+        Form form = new Form();
+        ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
+        ferp.writeTo(form.set("a", "a1").set("b", "b1"), Form.class, Form.class, 
+                     new Annotation[0], MediaType.APPLICATION_FORM_URLENCODED_TYPE, 
+                     new MetadataMap<String, Object>(), bos);
+        String result = bos.toString();
+        assertEquals("Wrong value", "a=a1&b=b1", result);  
+    }
+    
     @SuppressWarnings("unchecked")
     @Test
     public void testValidation() throws Exception {
@@ -225,9 +249,14 @@ public class FormEncodingProviderTest ex
     }
 
     @Test
-    public void testReadable() {
+    public void testReadableMap() {
         assertTrue(ferp.isReadable(MultivaluedMap.class, null, null, null));
     }
+    
+    @Test
+    public void testReadableForm() {
+        assertTrue(ferp.isReadable(Form.class, null, null, null));
+    }
 
     @Test
     public void testAnnotations() {



Mime
View raw message