cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r720238 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/provider/ test/java/org/apache/cxf/jaxrs/provider/
Date Mon, 24 Nov 2008 17:55:11 GMT
Author: sergeyb
Date: Mon Nov 24 09:55:10 2008
New Revision: 720238

URL: http://svn.apache.org/viewvc?rev=720238&view=rev
Log:
JAXRS : enhancing FormEncodingReader

Added:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormValidator.java
  (with props)
Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProviderTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProvider.java?rev=720238&r1=720237&r2=720238&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProvider.java
Mon Nov 24 09:55:10 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, MediaType mt) {
-        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);
+        }
     }
 }

Added: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormValidator.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormValidator.java?rev=720238&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormValidator.java
(added)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormValidator.java
Mon Nov 24 09:55:10 2008
@@ -0,0 +1,25 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cxf.jaxrs.provider;
+
+import javax.ws.rs.core.MultivaluedMap;
+
+public interface FormValidator {
+    void validate(MultivaluedMap<String, ? extends Object> params);
+}

Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormValidator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormValidator.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java?rev=720238&r1=720237&r2=720238&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
Mon Nov 24 09:55:10 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/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProviderTest.java?rev=720238&r1=720237&r2=720238&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProviderTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProviderTest.java
Mon Nov 24 09:55:10 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.Consumes;
+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