cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r633167 - in /incubator/cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ systests/src/test/java/org/apache/cxf/systest/jaxrs/ systests/src/test/java/org...
Date Mon, 03 Mar 2008 16:45:31 GMT
Author: dkulp
Date: Mon Mar  3 08:45:27 2008
New Revision: 633167

URL: http://svn.apache.org/viewvc?rev=633167&view=rev
Log:
[CXF-1455] Patch from Barry Fitzgerald applied.  Thanks!

Added:
    incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProvider.java
  (with props)
    incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProviderTest.java
  (with props)
    incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/multiValPostBody.txt
  (with props)
    incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/singleValPostBody.txt
  (with props)
    incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/singleValPostBody.txt
  (with props)
Modified:
    incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImpl.java
    incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerResourceCreatedSpringProviderTest.java
    incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/PetStore.java

Added: incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProvider.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProvider.java?rev=633167&view=auto
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProvider.java
(added)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProvider.java
Mon Mar  3 08:45:27 2008
@@ -0,0 +1,92 @@
+/**
+ * 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 java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Arrays;
+import java.util.List;
+
+import javax.ws.rs.ConsumeMime;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.ext.MessageBodyReader;
+
+import org.apache.cxf.common.util.StringUtils;
+import org.apache.cxf.jaxrs.MetadataMap;
+
+@ConsumeMime("application/x-www-form-urlencoded")
+public final class FormEncodingReaderProvider implements MessageBodyReader<Object>
{
+
+    public boolean isReadable(Class<?> type) {
+        return type.isAssignableFrom(MultivaluedMap.class);
+    }
+
+    public MultivaluedMap<String, String> readFrom(Class<Object> type, MediaType
m,
+                                                   MultivaluedMap<String, String> headers,
InputStream is) {
+        try {
+
+            String charset = "UTF-8";
+
+            ByteArrayOutputStream bos = new ByteArrayOutputStream();
+            copy(is, bos, 1024);
+            String postBody = new String(bos.toByteArray(), charset);
+
+            MultivaluedMap<String, String> params = getParams(postBody);
+
+            return params;
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+        return null;
+    }
+
+    public static void copy(final InputStream input, final OutputStream output, final int
bufferSize)
+        throws IOException {
+        final byte[] buffer = new byte[bufferSize];
+        int n = 0;
+        n = input.read(buffer);
+        while (-1 != n) {
+            output.write(buffer, 0, n);
+            n = input.read(buffer);
+        }
+    }
+
+    /**
+     * 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>();
+        if (!StringUtils.isEmpty(body)) {
+            List<String> parts = Arrays.asList(body.split("&"));
+            for (String part : parts) {
+                String[] keyValue = part.split("=");
+                params.add(keyValue[0], keyValue[1]);
+            }
+        }
+        return params;
+    }
+}

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

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

Modified: incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImpl.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImpl.java?rev=633167&r1=633166&r2=633167&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImpl.java
(original)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactoryImpl.java
Mon Mar  3 08:45:27 2008
@@ -54,7 +54,8 @@
                      new StringProvider(),
                      new DOMSourceProvider(),
                      new AtomFeedProvider(),
-                     new AtomEntryProvider());
+                     new AtomEntryProvider(),
+                     new FormEncodingReaderProvider());
         headerProviders.add(new MediaTypeHeaderProvider());
     }
     

Added: incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProviderTest.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProviderTest.java?rev=633167&view=auto
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProviderTest.java
(added)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProviderTest.java
Mon Mar  3 08:45:27 2008
@@ -0,0 +1,75 @@
+/**
+ * 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 java.io.InputStream;
+import java.util.List;
+
+import javax.ws.rs.ConsumeMime;
+import javax.ws.rs.core.MultivaluedMap;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+public class FormEncodingReaderProviderTest extends Assert {
+
+    private FormEncodingReaderProvider ferp;
+
+    @Before
+    public void setUp() {
+        ferp = new FormEncodingReaderProvider();
+    }
+
+    @Test
+    public void testReadFrom() throws Exception {
+        InputStream is = getClass().getResourceAsStream("singleValPostBody.txt");
+        MultivaluedMap<String, String> mvMap = ferp.readFrom(Object.class, null, null,
is);
+        assertEquals("Wrong entry for foo", "bar", mvMap.getFirst("foo"));
+        assertEquals("Wrong entry for boo", "far", mvMap.getFirst("boo"));
+
+    }
+
+    @Test
+    public void testReadFromMultiples() throws Exception {
+        InputStream is = getClass().getResourceAsStream("multiValPostBody.txt");
+        MultivaluedMap<String, String> mvMap = ferp.readFrom(Object.class, null, null,
is);
+        List<String> vals = mvMap.get("foo");
+
+        assertEquals("Wrong size for foo params", 2, vals.size());
+        assertEquals("Wrong size for foo params", 1, mvMap.get("boo").size());
+        assertEquals("Wrong entry for foo 0", "bar", vals.get(0));
+        assertEquals("Wrong entry for foo 1", "bar2", vals.get(1));
+        assertEquals("Wrong entry for boo", "far", mvMap.getFirst("boo"));
+
+    }
+
+    @Test
+    public void testReadable() {
+        assertTrue(ferp.isReadable(MultivaluedMap.class));
+    }
+
+    @Test
+    public void testAnnotations() {
+        assertEquals("application/x-www-form-urlencoded", ferp.getClass().getAnnotation(ConsumeMime.class)
+                     .value()[0]);
+    }
+
+}

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

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

Added: incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/multiValPostBody.txt
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/multiValPostBody.txt?rev=633167&view=auto
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/multiValPostBody.txt
(added)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/multiValPostBody.txt
Mon Mar  3 08:45:27 2008
@@ -0,0 +1 @@
+foo=bar&boo=far&foo=bar2
\ No newline at end of file

Propchange: incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/multiValPostBody.txt
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/multiValPostBody.txt
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/singleValPostBody.txt
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/singleValPostBody.txt?rev=633167&view=auto
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/singleValPostBody.txt
(added)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/singleValPostBody.txt
Mon Mar  3 08:45:27 2008
@@ -0,0 +1 @@
+foo=bar&boo=far
\ No newline at end of file

Propchange: incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/singleValPostBody.txt
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/singleValPostBody.txt
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerResourceCreatedSpringProviderTest.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerResourceCreatedSpringProviderTest.java?rev=633167&r1=633166&r2=633167&view=diff
==============================================================================
--- incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerResourceCreatedSpringProviderTest.java
(original)
+++ incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerResourceCreatedSpringProviderTest.java
Mon Mar  3 08:45:27 2008
@@ -19,7 +19,10 @@
 
 package org.apache.cxf.systest.jaxrs;
 
+import java.io.FileInputStream;
 import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
 import java.net.URL;
 import java.net.URLConnection;
 
@@ -54,6 +57,48 @@
         assertEquals("BadgerFish output not correct", 
                      getStringFromInputStream(expected).trim(),
                      getStringFromInputStream(in).trim());
+    }
+    
+    @Test
+    public void testPostPetStatus() throws Exception {
+        
+        String endpointAddress =
+            "http://localhost:9080/petstore/pets";
+
+        URL url = new URL(endpointAddress);   
+        HttpURLConnection httpUrlConnection = (HttpURLConnection)url.openConnection();  
+             
+        httpUrlConnection.setUseCaches(false);   
+        httpUrlConnection.setDefaultUseCaches(false);   
+        httpUrlConnection.setDoOutput(true);   
+        httpUrlConnection.setDoInput(true);   
+        httpUrlConnection.setRequestMethod("POST");   
+        httpUrlConnection.setRequestProperty("Accept",   "text/xml");   
+        httpUrlConnection.setRequestProperty("Content-type",   "application/x-www-form-urlencoded");
  
+        httpUrlConnection.setRequestProperty("Connection",   "close");   
+        //httpurlconnection.setRequestProperty("Content-Length",   String.valueOf(is.available()));
  
+
+        OutputStream outputstream = httpUrlConnection.getOutputStream();
+        String inputFile = getClass().getResource("resources/singleValPostBody.txt").getFile();
        
+         
+        byte[] tmp = new byte[4096];
+        int i = 0;
+        InputStream is = new FileInputStream(inputFile);
+        try {
+            while ((i = is.read(tmp)) >= 0) {
+                outputstream.write(tmp, 0, i);
+            }
+        } finally {
+            is.close();
+        }
+
+        outputstream.flush();
+
+        int responseCode = httpUrlConnection.getResponseCode();   
+        assertEquals(200, responseCode); 
+        assertEquals("Wrong status returned", "open", getStringFromInputStream(httpUrlConnection
+            .getInputStream()));  
+        httpUrlConnection.disconnect();
     }
     
     private String getStringFromInputStream(InputStream in) throws Exception {

Modified: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/PetStore.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/PetStore.java?rev=633167&r1=633166&r2=633167&view=diff
==============================================================================
--- incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/PetStore.java
(original)
+++ incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/PetStore.java
Mon Mar  3 08:45:27 2008
@@ -17,21 +17,21 @@
  * under the License.
  */
 
-
 package org.apache.cxf.systest.jaxrs;
 
-
+import javax.ws.rs.ConsumeMime;
 import javax.ws.rs.GET;
+import javax.ws.rs.POST;
 import javax.ws.rs.Path;
 import javax.ws.rs.ProduceMime;
 import javax.ws.rs.UriParam;
+import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Response;
 
-
 @Path("/petstore/")
 public class PetStore {
 
-    public static final  String CLOSED = "The Pet Store is closed"; 
+    public static final String CLOSED = "The Pet Store is closed";
 
     public PetStore() {
         System.out.println("Petstore constructed");
@@ -40,11 +40,21 @@
     @GET
     @Path("/pets/{petId}/")
     @ProduceMime("text/xml")
-    public Response getStatus(@UriParam("petId") String petId) throws Exception {
+    public Response getStatus(@UriParam("petId")
+                              String petId) throws Exception {
         System.out.println("----invoking getStatus on the petStore for id: " + petId);
-        
+
         return Response.ok(CLOSED).build();
     }
-}
 
+    @POST
+    @Path("/pets/")
+    @ConsumeMime("application/x-www-form-urlencoded")
+    @ProduceMime("text/xml")
+    public Response updateStatus(MultivaluedMap<String, String> params) throws Exception
{
+        System.out.println("----invoking updateStatus on the petStore with stauts post param
value of: "
+                           + params.getFirst("status"));
 
+        return Response.ok(params.getFirst("status")).build();
+    }
+}

Added: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/singleValPostBody.txt
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/singleValPostBody.txt?rev=633167&view=auto
==============================================================================
--- incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/singleValPostBody.txt
(added)
+++ incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/singleValPostBody.txt
Mon Mar  3 08:45:27 2008
@@ -0,0 +1 @@
+status=open
\ No newline at end of file

Propchange: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/singleValPostBody.txt
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/resources/singleValPostBody.txt
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message