cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: [CXF-7075] Supporting HttpServletRequest getParameter expectations
Date Thu, 06 Oct 2016 07:24:15 GMT
Repository: cxf
Updated Branches:
  refs/heads/3.1.x-fixes ec8fb26c4 -> f8a384f5a


[CXF-7075] Supporting HttpServletRequest getParameter expectations


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/f8a384f5
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/f8a384f5
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/f8a384f5

Branch: refs/heads/3.1.x-fixes
Commit: f8a384f5a5441072365462e6b0bbc7bb4c19ed48
Parents: ec8fb26
Author: Sergey Beryozkin <sberyozkin@gmail.com>
Authored: Thu Oct 6 08:22:02 2016 +0100
Committer: Sergey Beryozkin <sberyozkin@gmail.com>
Committed: Thu Oct 6 08:23:52 2016 +0100

----------------------------------------------------------------------
 .../jaxrs/impl/HttpServletRequestFilter.java    | 73 ++++++++++++++++++++
 .../org/apache/cxf/jaxrs/utils/FormUtils.java   |  6 ++
 .../transport/http/AbstractHTTPDestination.java |  6 --
 .../cxf/systest/jaxrs/BookStoreSpring.java      | 28 ++++++++
 .../jaxrs/JAXRSClientServerSpringBookTest.java  | 21 +++++-
 .../src/test/resources/jaxrs/WEB-INF/beans.xml  |  1 -
 6 files changed, 127 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/f8a384f5/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/HttpServletRequestFilter.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/HttpServletRequestFilter.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/HttpServletRequestFilter.java
index 9d393d4..1790d36 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/HttpServletRequestFilter.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/HttpServletRequestFilter.java
@@ -20,20 +20,35 @@ package org.apache.cxf.jaxrs.impl;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
 
 import javax.servlet.ServletInputStream;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletRequestWrapper;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
 
 import org.apache.cxf.io.DelegatingInputStream;
+import org.apache.cxf.jaxrs.utils.FormUtils;
+import org.apache.cxf.jaxrs.utils.HttpUtils;
+import org.apache.cxf.jaxrs.utils.JAXRSUtils;
 import org.apache.cxf.message.Message;
 
 public class HttpServletRequestFilter extends HttpServletRequestWrapper {
 
     private Message m;
+    private boolean isPostFormRequest;
+    private MultivaluedMap<String, String> formParams;
     public HttpServletRequestFilter(HttpServletRequest request, Message message) {
         super(request);
         m = message;
+        isPostFormRequest = FormUtils.isFormPostRequest(m);
     }
     @Override
     public ServletInputStream getInputStream() throws IOException {
@@ -47,5 +62,63 @@ public class HttpServletRequestFilter extends HttpServletRequestWrapper
{
             return super.getInputStream();
         }
     }
+    @Override
+    public String getParameter(String name) {
+        String[] values = this.getParameterValues(name);
+        return values == null ? null : values[0];
+    }
+    @Override
+    public String[] getParameterValues(String name) {
+        String[] value = super.getParameterValues(name);
+        if (value == null && isPostFormRequest) {
+            readFromParamsIfNeeded();
+            value = formParams.get(name).toArray(new String[]{});
+        }
+        return value;
+    }
+    @Override
+    public Map<String, String[]> getParameterMap() {
+        Map<String, String[]> map1 = super.getParameterMap();
+        if (isPostFormRequest) {
+            readFromParamsIfNeeded();
+            Map<String, String[]> map2 = new LinkedHashMap<String, String[]>();
+            map2.putAll(map1);
+            for (Map.Entry<String, List<String>> e : formParams.entrySet()) {
+                map2.put(e.getKey(), e.getValue().toArray(new String[]{}));
+            }
+            return Collections.unmodifiableMap(map2);
+        } else {
+            return map1;
+        }
+    }
+    @Override
+    public Enumeration<String> getParameterNames() {
+        Map<String, String[]> map = this.getParameterMap();
+        final Iterator<String> it = map.keySet().iterator();
+        return new Enumeration<String>() {
+
+            @Override
+            public boolean hasMoreElements() {
+                return it.hasNext();
+            }
+
+            @Override
+            public String nextElement() {
+                return it.next();
+            }
+            
+        };
+    }
+    
+    private void readFromParamsIfNeeded() {
+        if (formParams == null) {
+            formParams = new MetadataMap<String, String>();
+            MediaType mt = JAXRSUtils.toMediaType((String)m.get(Message.CONTENT_TYPE));
+            String enc = HttpUtils.getEncoding(mt, StandardCharsets.UTF_8.name());
+            String body = FormUtils.readBody(m.getContent(InputStream.class), enc);
+            FormUtils.populateMapFromString(formParams, m, body, enc, true);
+        }
+        
+    }
 }
 

http://git-wip-us.apache.org/repos/asf/cxf/blob/f8a384f5/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java
index ac01098..d38255b 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java
@@ -32,6 +32,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.logging.Logger;
 
+import javax.ws.rs.HttpMethod;
 import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.Form;
 import javax.ws.rs.core.MediaType;
@@ -271,4 +272,9 @@ public final class FormUtils {
             throw ExceptionUtils.toInternalServerErrorException(ex, null);
         }
     }
+
+    public static boolean isFormPostRequest(Message m) {
+        return MediaType.APPLICATION_FORM_URLENCODED.equals(m.get(Message.CONTENT_TYPE))
+            && HttpMethod.POST.equals(m.get(Message.HTTP_REQUEST_METHOD));
+    }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/f8a384f5/rt/transports/http/src/main/java/org/apache/cxf/transport/http/AbstractHTTPDestination.java
----------------------------------------------------------------------
diff --git a/rt/transports/http/src/main/java/org/apache/cxf/transport/http/AbstractHTTPDestination.java
b/rt/transports/http/src/main/java/org/apache/cxf/transport/http/AbstractHTTPDestination.java
index 538270f..d1e956f 100644
--- a/rt/transports/http/src/main/java/org/apache/cxf/transport/http/AbstractHTTPDestination.java
+++ b/rt/transports/http/src/main/java/org/apache/cxf/transport/http/AbstractHTTPDestination.java
@@ -45,7 +45,6 @@ import org.apache.cxf.attachment.AttachmentDataSource;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.common.util.Base64Exception;
 import org.apache.cxf.common.util.Base64Utility;
-import org.apache.cxf.common.util.PropertyUtils;
 import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.configuration.Configurable;
 import org.apache.cxf.configuration.security.AuthorizationPolicy;
@@ -107,7 +106,6 @@ public abstract class AbstractHTTPDestination
     private static final String SSL_PEER_CERT_CHAIN_ATTRIBUTE = "javax.servlet.request.X509Certificate";
 
     private static final Logger LOG = LogUtils.getL7dLogger(AbstractHTTPDestination.class);
-    private static final String CACHE_HTTP_REQUEST_PARAMETERS = "cache.http.request.parameters";

     
     protected final Bus bus;
     protected DestinationRegistry registry;
@@ -298,10 +296,6 @@ public abstract class AbstractHTTPDestination
                           resp);
         
         final Exchange exchange = inMessage.getExchange();
-        if (bus != null && PropertyUtils.isTrue(
-            bus.getProperty(CACHE_HTTP_REQUEST_PARAMETERS))) {
-            req.getParameterNames();
-        }
         DelegatingInputStream in = new DelegatingInputStream(req.getInputStream()) {
             public void cacheInput() {
                 if (!cached && (exchange.isOneWay() || isWSAddressingReplyToSpecified(exchange)))
{

http://git-wip-us.apache.org/repos/asf/cxf/blob/f8a384f5/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreSpring.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreSpring.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreSpring.java
index f47068a..caa97c3 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreSpring.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreSpring.java
@@ -93,6 +93,34 @@ public class BookStoreSpring {
         return new Book(name, id);
     }
     @POST
+    @Path("/bookform2")
+    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
+    @Produces("application/xml")
+    public Book echoBookForm2(@Context HttpServletRequest req) {
+        String name = req.getParameterValues("name")[0];
+        long id = Long.valueOf(req.getParameter("id"));
+        return new Book(name, id);
+    }
+    @POST
+    @Path("/bookform3")
+    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
+    @Produces("application/xml")
+    public Book echoBookForm3(@Context HttpServletRequest req) {
+        String name = req.getParameterMap().get("name")[0];
+        long id = Long.valueOf(req.getParameter("id"));
+        return new Book(name, id);
+    }
+    @POST
+    @Path("/bookform4")
+    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
+    @Produces("application/xml")
+    public Book echoBookForm4(@Context HttpServletRequest req) {
+        String key = req.getParameterNames().nextElement();
+        String name = req.getParameter(key);
+        long id = Long.valueOf(req.getParameter("id"));
+        return new Book(name, id);
+    }
+    @POST
     @Path("/bookform")
     @Consumes("application/xml")
     @Produces("application/xml")

http://git-wip-us.apache.org/repos/asf/cxf/blob/f8a384f5/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java
index 5e274ab..9597911 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java
@@ -106,7 +106,27 @@ public class JAXRSClientServerSpringBookTest extends AbstractBusClientServerTest
     @Test
     public void testEchoBookForm() throws Exception {
         String address = "http://localhost:" + PORT + "/bus/thebooksform/bookform";
+        doTestEchoBookForm(address);
+    }
+    @Test
+    public void testEchoBookForm2() throws Exception {
+        String address = "http://localhost:" + PORT + "/bus/thebooksform/bookform2";
+        doTestEchoBookForm(address);
+    }
+    @Test
+    public void testEchoBookForm3() throws Exception {
+        String address = "http://localhost:" + PORT + "/bus/thebooksform/bookform3";
+        doTestEchoBookForm(address);
+    }
+    @Test
+    public void testEchoBookForm4() throws Exception {
+        String address = "http://localhost:" + PORT + "/bus/thebooksform/bookform4";
+        doTestEchoBookForm(address);
+    }
+    private void doTestEchoBookForm(String address) throws Exception {
         WebClient wc = WebClient.create(address);
+        WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(10000000L);
+        
         Book b = 
             wc.form(new Form().param("name", "CXFForm").param("id", "125"))
                 .readEntity(Book.class);
@@ -117,7 +137,6 @@ public class JAXRSClientServerSpringBookTest extends AbstractBusClientServerTest
     public void testEchoBookFormXml() throws Exception {
         String address = "http://localhost:" + PORT + "/bus/thebooksform/bookform";
         WebClient wc = WebClient.create(address);
-        WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(10000000L);
         Book b = 
             wc.type("application/xml").post(new Book("CXFFormXml", 125L))
                 .readEntity(Book.class);

http://git-wip-us.apache.org/repos/asf/cxf/blob/f8a384f5/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml b/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml
index f259744..b4f5d5b 100644
--- a/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml
+++ b/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml
@@ -140,7 +140,6 @@
     <cxf:bus name="cxf2" id="cxf2">
        <cxf:properties>
            <entry key="skip.default.json.provider.registration" value="true"/>
-           <entry key="cache.http.request.parameters" value="true"/>
        </cxf:properties>
     </cxf:bus>
     <jaxrs:server id="bookJsonp2" address="/jsonp2" bus="cxf2">


Mime
View raw message