cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r720249 - in /cxf/branches/2.1.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/
Date Mon, 24 Nov 2008 18:30:45 GMT
Author: sergeyb
Date: Mon Nov 24 10:30:44 2008
New Revision: 720249

URL: http://svn.apache.org/viewvc?rev=720249&view=rev
Log:
Merged revisions 720238 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r720238 | sergeyb | 2008-11-24 17:55:10 +0000 (Mon, 24 Nov 2008) | 1 line
  
  JAXRS : enhancing FormEncodingReader
........

Added:
    cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormValidator.java
      - copied unchanged from r720238, cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormValidator.java
Modified:
    cxf/branches/2.1.x-fixes/   (props changed)
    cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProvider.java
    cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
    cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProviderTest.java

Propchange: cxf/branches/2.1.x-fixes/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Mon Nov 24 10:30:44 2008
@@ -1 +1 @@
-/cxf/trunk:686333-686363,686764,686820,687096,687194,687363,687387,687463,687543,687722,687798,687814,687817,687891,687910,687914,688086,688102,688133,688596,688735,688870,689572,689596,689855,689924,690067,690289,691246,691271,691295,691338,691355,691488,691602,691646,691706,691728,692116,692157,692310,692466,692499,693653,693819,694179,694263,694417,694716,694744,694747,694795,694869,694981,694987,694993,695041,695096,695396,695484,695537,695552,695561,695619,695684,695835,695840,695868,695935,695977,696016,696094,696433,696720,697085,697868,698128,699289,700261,700507,700602,700981,701316,701783,701830,701862,702187,702205-702248,702267,702547,702561,702580,702602,702609,702616,702653,702656,702957,703191,703239,703309,703501,703513,703548,704584,704937,704997,705150,705235,705274,705340,705446,705548,705614,705692,705708,706482,706631,706675,706900,706909,707034,707089,707100,707902,708035,708044,708074,708410,708417,708550,708554,709353-709354,709425,710076,710150,71015
 4,711193,711388,711410,711490,711635,711975,712194,712198,712238,712272,712299,712312,712670,712893,713082,713095-713096,713099,713584,713597,713737,713804,713899,714167-714168,714245,714255,717937-717961,718281,718439,718448,718565,718620,718640,718665,718970,719017,719210,719215-719218,719222,719273,719305,719327,719354,719362,719368,719382,719649,719680,720053,720119
+/cxf/trunk:686333-686363,686764,686820,687096,687194,687363,687387,687463,687543,687722,687798,687814,687817,687891,687910,687914,688086,688102,688133,688596,688735,688870,689572,689596,689855,689924,690067,690289,691246,691271,691295,691338,691355,691488,691602,691646,691706,691728,692116,692157,692310,692466,692499,693653,693819,694179,694263,694417,694716,694744,694747,694795,694869,694981,694987,694993,695041,695096,695396,695484,695537,695552,695561,695619,695684,695835,695840,695868,695935,695977,696016,696094,696433,696720,697085,697868,698128,699289,700261,700507,700602,700981,701316,701783,701830,701862,702187,702205-702248,702267,702547,702561,702580,702602,702609,702616,702653,702656,702957,703191,703239,703309,703501,703513,703548,704584,704937,704997,705150,705235,705274,705340,705446,705548,705614,705692,705708,706482,706631,706675,706900,706909,707034,707089,707100,707902,708035,708044,708074,708410,708417,708550,708554,709353-709354,709425,710076,710150,71015
 4,711193,711388,711410,711490,711635,711975,712194,712198,712238,712272,712299,712312,712670,712893,713082,713095-713096,713099,713584,713597,713737,713804,713899,714167-714168,714245,714255,717937-717961,718281,718439,718448,718565,718620,718640,718665,718970,719017,719210,719215-719218,719222,719273,719305,719327,719354,719362,719368,719382,719649,719680,720053,720119,720238

Propchange: cxf/branches/2.1.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProvider.java?rev=720249&r1=720248&r2=720249&view=diff
==============================================================================
--- cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProvider.java
(original)
+++ cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProvider.java
Mon Nov 24 10:30:44 2008
@@ -42,9 +42,24 @@
 @Provider
 public final class FormEncodingReaderProvider implements MessageBodyReader<Object>
{
 
+    private static final char SPACE_CHAR = '+';
+    private static final String NEXT_LINE = "%0D%0A";
+    
+    
+    private boolean decode;
+    private FormValidator validator;
+    
+    public void setDecode(boolean formDecoding) {
+        decode = formDecoding;
+    }
+    
+    public void setValidator(FormValidator formValidator) {
+        validator = formValidator;
+    }
+    
     public boolean isReadable(Class<?> type, Type genericType, 
                               Annotation[] annotations) {
-        return type.isAssignableFrom(MultivaluedMap.class);
+        return MultivaluedMap.class.isAssignableFrom(type);
     }
 
     public MultivaluedMap<String, String> readFrom(
@@ -59,9 +74,12 @@
             copy(is, bos, 1024);
             String postBody = new String(bos.toByteArray(), charset);
 
-            MultivaluedMap<String, String> params = getParams(postBody);
-
+            MultivaluedMap<String, String> params = createMap(clazz);
+            populateMap(params, postBody);
+            validateMap(params);
             return params;
+        } catch (WebApplicationException e) {
+            throw e;
         } catch (Exception e) {
             throw new WebApplicationException(e);
         }
@@ -78,26 +96,47 @@
         }
     }
 
+    @SuppressWarnings("unchecked")
+    protected MultivaluedMap<String, String> createMap(Class<?> clazz) throws
Exception {
+        if (clazz == MultivaluedMap.class) {
+            return new MetadataMap<String, String>();
+        }
+        return (MultivaluedMap<String, String>)clazz.newInstance();
+    }
+    
     /**
      * Retrieve map of parameters from the passed in message
      *
      * @param message
      * @return a Map of parameters.
      */
-    protected static MultivaluedMap<String, String> getParams(String body) {
-        MultivaluedMap<String, String> params = new MetadataMap<String, String>();
+    protected void populateMap(MultivaluedMap<String, String> params, 
+                               String body) {
         if (!StringUtils.isEmpty(body)) {
             List<String> parts = Arrays.asList(body.split("&"));
             for (String part : parts) {
                 String[] keyValue = part.split("=");
                 // Change to add blank string if key but not value is specified
                 if (keyValue.length == 2) {
-                    params.add(keyValue[0], keyValue[1]);
+                    if (decode) {
+                        String[] values = keyValue[1].split(NEXT_LINE);
+                        for (String value : values) {
+                            params.add(keyValue[0], 
+                                       value.replace(SPACE_CHAR, ' '));
+                        }
+                    } else {
+                        params.add(keyValue[0], keyValue[1]);
+                    }
                 } else {
                     params.add(keyValue[0], "");
                 }
             }
         }
-        return params;
+    }
+    
+    protected void validateMap(MultivaluedMap<String, String> params) {
+        if (validator != null) {
+            validator.validate(params);
+        }
     }
 }

Modified: cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java?rev=720249&r1=720248&r2=720249&view=diff
==============================================================================
--- cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
(original)
+++ cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
Mon Nov 24 10:30:44 2008
@@ -74,14 +74,13 @@
             }
             
         } catch (JAXBException e) {
-            // TODO : refactor it such that thsi caode can be used by across the board
+            Throwable t = e.getLinkedException() != null 
+                ? e.getLinkedException() : e.getCause() != null ? e.getCause() : e;
             String message = new org.apache.cxf.common.i18n.Message("JAXB_EXCEPTION", 
-                                 BUNDLE,
-                                 e.getLinkedException() != null 
-                                 ? e.getLinkedException().getMessage() : e.getMessage()).toString();
+                                 BUNDLE, t.getMessage()).toString();
             Response r = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
                 .type(MediaType.TEXT_PLAIN).entity(message).build();
-            throw new WebApplicationException(r);
+            throw new WebApplicationException(t, r);
         } catch (Exception e) {
             throw new WebApplicationException(e);        
         }

Modified: cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProviderTest.java?rev=720249&r1=720248&r2=720249&view=diff
==============================================================================
--- cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProviderTest.java
(original)
+++ cxf/branches/2.1.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProviderTest.java
Mon Nov 24 10:30:44 2008
@@ -19,12 +19,15 @@
 
 package org.apache.cxf.jaxrs.provider;
 
+import java.io.ByteArrayInputStream;
 import java.io.InputStream;
 import java.util.List;
 
 import javax.ws.rs.ConsumeMime;
+import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.MultivaluedMap;
 
+import org.apache.cxf.jaxrs.impl.MetadataMap;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
@@ -38,21 +41,94 @@
         ferp = new FormEncodingReaderProvider();
     }
 
+    @SuppressWarnings("unchecked")
     @Test
     public void testReadFrom() throws Exception {
         InputStream is = getClass().getResourceAsStream("singleValPostBody.txt");
         MultivaluedMap<String, String> mvMap = 
-            ferp.readFrom(Object.class, null, null, null, null, is);
+            ferp.readFrom((Class)MultivaluedMap.class, null, null, null, null, is);
         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 {
+        ferp.setDecode(true);
+        String values = "foo=1+2&bar=1+3";
+        
+        MultivaluedMap<String, String> mvMap = 
+            ferp.readFrom((Class)MultivaluedMap.class, null, null, null, null, 
+                          new ByteArrayInputStream(values.getBytes()));
+        assertEquals("Wrong entry for foo", "1 2", mvMap.getFirst("foo"));
+        assertEquals("Wrong entry for boo", "1 3", mvMap.getFirst("bar"));
+
+    }
+    
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testCustomMapImpl() throws Exception {
+        ferp.setDecode(true);
+        String values = "foo=1+2&bar=1+3&baz=4";
+        
+        MultivaluedMap<String, String> mvMap = 
+            ferp.readFrom((Class)CustomMap.class, null, null, null, null, 
+                          new ByteArrayInputStream(values.getBytes()));
+        assertEquals(3, mvMap.size());
+        assertEquals(1,  mvMap.get("foo").size());
+        assertEquals(1,  mvMap.get("bar").size());
+        assertEquals(1,  mvMap.get("baz").size());
+        assertEquals("Wrong entry for foo", "1 2", mvMap.getFirst("foo"));
+        assertEquals("Wrong entry for boo", "1 3", mvMap.getFirst("bar"));
+        assertEquals("Wrong entry for baz", "4", mvMap.getFirst("baz"));
+
+    }
+    
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testMultiLines() throws Exception {
+        ferp.setDecode(true);
+        String values = "foo=1+2&bar=line1%0D%0Aline+2&baz=4";
+        
+        MultivaluedMap<String, String> mvMap = 
+            ferp.readFrom((Class)CustomMap.class, null, null, null, null, 
+                          new ByteArrayInputStream(values.getBytes()));
+        assertEquals(3, mvMap.size());
+        assertEquals(1,  mvMap.get("foo").size());
+        assertEquals(2,  mvMap.get("bar").size());
+        assertEquals(1,  mvMap.get("baz").size());
+        assertEquals("Wrong entry for foo", "1 2", mvMap.getFirst("foo"));
+        assertEquals("Wrong first line for boo", "line1", mvMap.get("bar").get(0));
+        assertEquals("Wrong first line for boo", "line 2", mvMap.get("bar").get(1));
+        assertEquals("Wrong entry for baz", "4", mvMap.getFirst("baz"));
 
+    }
+    
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testvalidation() throws Exception {
+        ferp.setValidator(new CustomFormValidator());
+        String values = "foo=1+2&bar=1+3";
+        
+        try {
+            ferp.readFrom((Class)CustomMap.class, null, null, null, null, 
+                new ByteArrayInputStream(values.getBytes()));
+            fail();
+        } catch (WebApplicationException ex) {
+            // ignore
+        }
+
+    }
+    
+
+    @SuppressWarnings("unchecked")
     @Test
     public void testReadFromMultiples() throws Exception {
         InputStream is = getClass().getResourceAsStream("multiValPostBody.txt");
+        
         MultivaluedMap<String, String> mvMap = 
-            ferp.readFrom(Object.class, null, null, null, null, is);
+            ferp.readFrom((Class)MultivaluedMap.class, null, null, null, null, is);
         List<String> vals = mvMap.get("foo");
 
         assertEquals("Wrong size for foo params", 2, vals.size());
@@ -74,4 +150,15 @@
                      .value()[0]);
     }
 
+    public static class CustomMap extends MetadataMap<String, String> {
+        
+    }
+    
+    private static class CustomFormValidator implements FormValidator {
+
+        public void validate(MultivaluedMap<String, ? extends Object> params) {
+            throw new WebApplicationException();
+        }
+        
+    }
 }



Mime
View raw message