cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1063042 - in /cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ rt/frontend/jaxrs/src/test/java/org/a...
Date Mon, 24 Jan 2011 22:40:54 GMT
Author: sergeyb
Date: Mon Jan 24 22:40:53 2011
New Revision: 1063042

URL: http://svn.apache.org/viewvc?rev=1063042&view=rev
Log:
[CXF-3274] Better support for Form requests

Modified:
    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/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/Customer.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/security/SecureBookStoreNoInterface.java

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=1063042&r1=1063041&r2=1063042&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
Mon Jan 24 22:40:53 2011
@@ -51,6 +51,8 @@ import org.apache.cxf.jaxrs.utils.Annota
 import org.apache.cxf.jaxrs.utils.FormUtils;
 import org.apache.cxf.jaxrs.utils.HttpUtils;
 import org.apache.cxf.jaxrs.utils.multipart.AttachmentUtils;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.phase.PhaseInterceptorChain;
 
 @Produces({"application/x-www-form-urlencoded", "multipart/form-data" })
 @Consumes({"application/x-www-form-urlencoded", "multipart/form-data" })
@@ -98,6 +100,9 @@ public class FormEncodingProvider implem
             populateMap(params, is, mt,
                         AnnotationUtils.getAnnotation(annotations, Encoded.class) == null);
             validateMap(params);
+            
+            persistParamsOnMessage(params);
+            
             return params;
         } catch (WebApplicationException e) {
             throw e;
@@ -106,6 +111,13 @@ public class FormEncodingProvider implem
         }
     }
 
+    protected void persistParamsOnMessage(MultivaluedMap<String, String> params) {
+        Message message = PhaseInterceptorChain.getCurrentMessage();
+        if (message != null) {
+            message.put(FormUtils.FORM_PARAM_MAP, params);
+        }
+    }
+    
     @SuppressWarnings("unchecked")
     protected MultivaluedMap<String, String> createMap(Class<?> clazz) throws
Exception {
         if (clazz == MultivaluedMap.class) {

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=1063042&r1=1063041&r2=1063042&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 Mon
Jan 24 22:40:53 2011
@@ -39,8 +39,9 @@ import org.apache.cxf.jaxrs.ext.multipar
 import org.apache.cxf.jaxrs.ext.multipart.MultipartBody;
 
 public final class FormUtils {
-    
-    private static final String FORM_DATA_TYPE = "form-data";  
+
+    public static final String FORM_PARAM_MAP = "org.apache.cxf.form_data";
+    private static final String MULTIPART_FORM_DATA_TYPE = "form-data";  
         
     private FormUtils() {
         
@@ -109,7 +110,7 @@ public final class FormUtils {
         List<Attachment> atts = body.getAllAttachments();
         for (Attachment a : atts) {
             ContentDisposition cd = a.getContentDisposition();
-            if (cd == null || !FORM_DATA_TYPE.equalsIgnoreCase(cd.getType())
+            if (cd == null || !MULTIPART_FORM_DATA_TYPE.equalsIgnoreCase(cd.getType())
                 || cd.getParameter("name") == null) {
                 throw new WebApplicationException(415);
             }

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=1063042&r1=1063041&r2=1063042&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 Mon
Jan 24 22:40:53 2011
@@ -110,8 +110,7 @@ public final class JAXRSUtils {
     private static final Logger LOG = LogUtils.getL7dLogger(JAXRSUtils.class);
     private static final ResourceBundle BUNDLE = BundleUtils.getBundle(JAXRSUtils.class);
     private static final String PROPOGATE_EXCEPTION = "org.apache.cxf.propagate.exception";
-    private static final String FORM_PARAM_MAP = JAXRSUtils.class.getName() + ".FORM_DATA";
-
+    
     private JAXRSUtils() {        
     }
     
@@ -689,18 +688,15 @@ public final class JAXRSUtils {
         MediaType mt = mc.getHttpHeaders().getMediaType();
         
         @SuppressWarnings("unchecked")
-        MultivaluedMap<String, String> params = (MultivaluedMap<String, String>)m.get(FORM_PARAM_MAP);

+        MultivaluedMap<String, String> params = 
+            (MultivaluedMap<String, String>)m.get(FormUtils.FORM_PARAM_MAP); 
         
         if (params == null) {
             params = new MetadataMap<String, String>();
-            m.put(FORM_PARAM_MAP, params);
+            m.put(FormUtils.FORM_PARAM_MAP, params);
         
             if (mt == null || mt.isCompatible(MediaType.APPLICATION_FORM_URLENCODED_TYPE))
{
-                String body = (String)m.get("org.apache.cxf.jaxrs.provider.form.body");
-                if (body == null) {
-                    body = FormUtils.readBody(m.getContent(InputStream.class), mt);
-                    m.put("org.apache.cxf.jaxrs.provider.form.body", body);
-                }
+                String body = FormUtils.readBody(m.getContent(InputStream.class), mt);
                 HttpServletRequest request = (HttpServletRequest)m.get(AbstractHTTPDestination.HTTP_REQUEST);
                 FormUtils.populateMapFromString(params, (String)body, decode, request);
             } else {

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/Customer.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/Customer.java?rev=1063042&r1=1063041&r2=1063042&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/Customer.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/Customer.java Mon Jan 24
22:40:53 2011
@@ -43,6 +43,8 @@ import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.Cookie;
 import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.PathSegment;
 import javax.ws.rs.core.Request;
 import javax.ws.rs.core.SecurityContext;
@@ -341,6 +343,13 @@ public class Customer extends AbstractCu
         // complete
     }
     
+    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
+    public void testMultivaluedMapAndFormParam(MultivaluedMap<String, String> params,
+                                               @FormParam("p1") String fp1, 
+                                               @FormParam("p2") List<String> fp2) {
+        // complete
+    }
+    
     public void testCookieParam(@CookieParam("c1") String c1,
                                 @CookieParam("c2") @DefaultValue("c2Value") String c2) {
         // complete

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java?rev=1063042&r1=1063041&r2=1063042&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java
Mon Jan 24 22:40:53 2011
@@ -73,6 +73,7 @@ import org.apache.cxf.jaxrs.model.ClassR
 import org.apache.cxf.jaxrs.model.MethodDispatcher;
 import org.apache.cxf.jaxrs.model.OperationResourceInfo;
 import org.apache.cxf.jaxrs.model.URITemplate;
+import org.apache.cxf.jaxrs.provider.FormEncodingProvider;
 import org.apache.cxf.jaxrs.provider.ProviderFactory;
 import org.apache.cxf.message.Exchange;
 import org.apache.cxf.message.ExchangeImpl;
@@ -1054,6 +1055,47 @@ public class JAXRSUtilsTest extends Asse
         assertEquals("3", list.get(1));
     }
     
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testFormParametersAndMap() throws Exception {
+        Class[] argType = {MultivaluedMap.class, String.class, List.class};
+        Method m = Customer.class.getMethod("testMultivaluedMapAndFormParam", argType);
+        final Message messageImpl = createMessage();
+        String body = "p1=1&p2=2&p2=3";
+        messageImpl.put(Message.REQUEST_URI, "/foo");
+        messageImpl.put("Content-Type", MediaType.APPLICATION_FORM_URLENCODED);
+        messageImpl.setContent(InputStream.class, new ByteArrayInputStream(body.getBytes()));
+        
+        ProviderFactory.getInstance(messageImpl).registerUserProvider(
+            new FormEncodingProvider() {
+                @Override
+                protected void persistParamsOnMessage(MultivaluedMap<String, String>
params) {
+                    messageImpl.put(FormUtils.FORM_PARAM_MAP, params);    
+                }
+            });
+        
+        List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m,
null), 
+                                                           new MetadataMap<String, String>(),
messageImpl);
+        assertEquals("3 params should've been identified", 3, params.size());
+        
+        MultivaluedMap<String, String> map = (MultivaluedMap<String, String>)params.get(0);
+        assertEquals(2, map.size());
+        assertEquals(1, map.get("p1").size());
+        assertEquals("First map parameter not matched correctly", 
+                     "1", map.getFirst("p1"));
+        assertEquals(2, map.get("p2").size());
+        
+        assertEquals("2", map.get("p2").get(0));
+        assertEquals("3", map.get("p2").get(1));
+        
+        assertEquals("First Form Parameter not matched correctly", 
+                     "1", params.get(1));
+        List<String> list = (List<String>)params.get(2);
+        assertEquals(2, list.size());
+        assertEquals("2", list.get(0));
+        assertEquals("3", list.get(1));
+    }
+    
     @Test
     public void testSelectResourceMethod() throws Exception {
         ClassResourceInfo cri = new ClassResourceInfo(Customer.class);

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/security/SecureBookStoreNoInterface.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/security/SecureBookStoreNoInterface.java?rev=1063042&r1=1063041&r2=1063042&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/security/SecureBookStoreNoInterface.java
(original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/security/SecureBookStoreNoInterface.java
Mon Jan 24 22:40:53 2011
@@ -33,6 +33,7 @@ import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
 
 import org.apache.cxf.systest.jaxrs.Book;
 import org.apache.cxf.systest.jaxrs.BookNotFoundFault;
@@ -51,8 +52,10 @@ public class SecureBookStoreNoInterface 
     @POST
     @Path("/bookforms")
     @RolesAllowed({"ROLE_USER", "ROLE_ADMIN" })
-    public Book getBookFromFormParams(@FormParam("name") String name, @FormParam("id") long
id) {
-        if (name == null || id == 0) {
+    public Book getBookFromFormParams(MultivaluedMap<String, String> map,
+        @FormParam("name") String name, @FormParam("id") long id) {
+        if (name == null || id == 0 || map.getFirst("name") == null
+            || Integer.valueOf(map.getFirst("id")) == 0) {
             throw new RuntimeException("FormParams are not set");
         }
         return new Book(name, id);
@@ -64,7 +67,12 @@ public class SecureBookStoreNoInterface 
     @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
     public Book getBookFromHttpRequestParams(@Context HttpServletRequest request) {
         Map<String, String[]> params = request.getParameterMap();
-        return getBookFromFormParams(params.get("name")[0], Long.valueOf(params.get("id")[0]));
+        String name = params.get("name")[0];
+        Long id = Long.valueOf(params.get("id")[0]);
+        if (name == null || id == 0) {
+            throw new RuntimeException("FormParams are not set");
+        }
+        return new Book(name, id);
     }
     
     @GET



Mime
View raw message