cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject git commit: [CXF-5592] Fixing RequestDispatcherProvider to correctly forward to servlets with non-empty URL patterns
Date Mon, 10 Mar 2014 13:54:27 GMT
Repository: cxf
Updated Branches:
  refs/heads/2.7.x-fixes f835f3e0f -> c4b1360e0


[CXF-5592] Fixing RequestDispatcherProvider to correctly forward to servlets with non-empty
URL patterns


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

Branch: refs/heads/2.7.x-fixes
Commit: c4b1360e01b689eb588b36e7cfaccb8b4ea86919
Parents: f835f3e
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Mon Mar 10 13:42:31 2014 +0000
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Mon Mar 10 13:54:03 2014 +0000

----------------------------------------------------------------------
 .../provider/RequestDispatcherProvider.java     | 35 +++++++++++++++-----
 .../cxf/systest/jaxrs/BookStoreSpring.java      |  2 +-
 .../jaxrs/JAXRSSimpleRequestDispatcherTest.java | 12 ++++++-
 .../jaxrs_dispatch_simple/WEB-INF/beans.xml     | 19 +++++++++++
 .../jaxrs_dispatch_simple/WEB-INF/web.xml       | 14 +++++++-
 5 files changed, 70 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/c4b1360e/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/RequestDispatcherProvider.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/RequestDispatcherProvider.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/RequestDispatcherProvider.java
index 0b362d1..dfcb3a8 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/RequestDispatcherProvider.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/RequestDispatcherProvider.java
@@ -85,6 +85,7 @@ public class RequestDispatcherProvider extends AbstractConfigurableProvider
     private String beanName;
     private String dispatcherName;
     private String servletPath;
+    private boolean useCurrentServlet;
     private boolean saveParametersAsAttributes;
     private boolean logRedirects;
     private boolean strictPathCheck;
@@ -172,25 +173,37 @@ public class RequestDispatcherProvider extends AbstractConfigurableProvider
         throws IOException {
         
         ServletContext sc = getServletContext();
+        HttpServletRequest servletRequest = mc.getHttpServletRequest();
+        
         String path = getResourcePath(clazz, o);
-        RequestDispatcher rd = getRequestDispatcher(sc, clazz, path);
+        
+        String theServletPath = servletPath != null ? servletPath 
+            : useCurrentServlet ? servletRequest.getServletPath() : "/";
+                
+        if (theServletPath.endsWith("/") && path != null && path.startsWith("/"))
{
+            theServletPath = theServletPath.length() == 1 ? "" 
+                : theServletPath.substring(0, theServletPath.length() - 1);
+        } else if (!theServletPath.endsWith("/") && path != null && !path.startsWith("/"))
{
+            path = "/" + path;
+        }
+        
+        
+        RequestDispatcher rd = getRequestDispatcher(sc, clazz, theServletPath + path);
         
         try {
             mc.put(AbstractHTTPDestination.REQUEST_REDIRECTED, Boolean.TRUE);
             
-            String theServletPath = servletPath == null ? "/" : servletPath;
-            HttpServletRequestFilter servletRequest = 
-                new HttpServletRequestFilter(mc.getHttpServletRequest(), path, 
-                                             theServletPath, saveParametersAsAttributes);
+            HttpServletRequestFilter requestFilter = new HttpServletRequestFilter(
+                servletRequest, path, theServletPath, saveParametersAsAttributes);
             String attributeName = getBeanName(o);
             if (REQUEST_SCOPE.equals(scope)) {
-                servletRequest.setAttribute(attributeName, o);
+                requestFilter.setAttribute(attributeName, o);
             } else if (SESSION_SCOPE.equals(scope)) {
-                servletRequest.getSession(true).setAttribute(attributeName, o);
+                requestFilter.getSession(true).setAttribute(attributeName, o);
             } 
-            setRequestParameters(servletRequest);
+            setRequestParameters(requestFilter);
             logRedirection(path, attributeName, o);
-            rd.forward(servletRequest, mc.getHttpServletResponse());
+            rd.forward(requestFilter, mc.getHttpServletResponse());
         } catch (Throwable ex) {
             mc.put(AbstractHTTPDestination.REQUEST_REDIRECTED, Boolean.FALSE);
             ex.printStackTrace();
@@ -378,6 +391,10 @@ public class RequestDispatcherProvider extends AbstractConfigurableProvider
         this.enumResources = enumResources;
     }
 
+    public void setUseCurrentServlet(boolean useCurrentServlet) {
+        this.useCurrentServlet = useCurrentServlet;
+    }
+
     protected static class HttpServletRequestFilter extends HttpServletRequestWrapper {
         
         private Map<String, String[]> params;

http://git-wip-us.apache.org/repos/asf/cxf/blob/c4b1360e/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 ecd701a..4821af1 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
@@ -95,7 +95,7 @@ public class BookStoreSpring {
     @Path("/books/redirectComplete")
     public Book getBookRedirectComplete(@Context HttpServletRequest request) {
         Book book = (Book)request.getAttribute(Book.class.getSimpleName().toLowerCase());
-        book.setName("Redirect complete");
+        book.setName("Redirect complete: " + request.getRequestURI());
         return book; 
     }
     

http://git-wip-us.apache.org/repos/asf/cxf/blob/c4b1360e/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSSimpleRequestDispatcherTest.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSSimpleRequestDispatcherTest.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSSimpleRequestDispatcherTest.java
index 936a864..e52acbb 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSSimpleRequestDispatcherTest.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSSimpleRequestDispatcherTest.java
@@ -62,8 +62,18 @@ public class JAXRSSimpleRequestDispatcherTest extends AbstractBusClientServerTes
         WebClient.getConfig(client).getHttpConduit().getClient().setReceiveTimeout(10000000L);
         client.accept("application/json");
         Book book = client.get(Book.class);
-        assertEquals("Redirect complete", book.getName());
+        assertEquals("Redirect complete: /dispatch/bookstore/books/redirectComplete", book.getName());
     }
     
+    @Test
+    public void testGetRedirectedBook2() throws Exception {
+        String address = "http://localhost:" + PORT + "/dispatch/redirect/bookstore3/books/redirectStart";
+        WebClient client = WebClient.create(address);
+        WebClient.getConfig(client).getHttpConduit().getClient().setReceiveTimeout(10000000L);
+        client.accept("application/json");
+        Book book = client.get(Book.class);
+        assertEquals("Redirect complete: /dispatch/redirect/bookstore/books/redirectComplete",

+                     book.getName());
+    }
     
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/c4b1360e/systests/jaxrs/src/test/resources/jaxrs_dispatch_simple/WEB-INF/beans.xml
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/resources/jaxrs_dispatch_simple/WEB-INF/beans.xml b/systests/jaxrs/src/test/resources/jaxrs_dispatch_simple/WEB-INF/beans.xml
index f43aacf..3c27e47 100644
--- a/systests/jaxrs/src/test/resources/jaxrs_dispatch_simple/WEB-INF/beans.xml
+++ b/systests/jaxrs/src/test/resources/jaxrs_dispatch_simple/WEB-INF/beans.xml
@@ -48,5 +48,24 @@
             </bean>
         </jaxrs:providers>
     </jaxrs:server>
+    <jaxrs:server id="bookservice3" address="/bookstore3">
+        <jaxrs:serviceBeans>
+            <ref bean="serviceBean"/>
+        </jaxrs:serviceBeans>
+        <jaxrs:providers>
+            <bean class="org.apache.cxf.jaxrs.provider.RequestDispatcherProvider">
+                <property name="produceMediaTypes">
+                    <list>
+                      <value>application/json</value>
+                    </list>
+                </property>
+                <property name="resourcePath" value="/bookstore/books/redirectComplete"/>
+                <property name="useCurrentServlet" value="true"/>
+                <!--
+                <property name="servletPath" value="/redirect"/>
+                -->
+            </bean>
+        </jaxrs:providers>
+    </jaxrs:server>
 </beans>
 <!-- END SNIPPET: beans -->

http://git-wip-us.apache.org/repos/asf/cxf/blob/c4b1360e/systests/jaxrs/src/test/resources/jaxrs_dispatch_simple/WEB-INF/web.xml
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/resources/jaxrs_dispatch_simple/WEB-INF/web.xml b/systests/jaxrs/src/test/resources/jaxrs_dispatch_simple/WEB-INF/web.xml
index ac100a1..b55ed6a 100644
--- a/systests/jaxrs/src/test/resources/jaxrs_dispatch_simple/WEB-INF/web.xml
+++ b/systests/jaxrs/src/test/resources/jaxrs_dispatch_simple/WEB-INF/web.xml
@@ -45,9 +45,21 @@
     </init-param>
     <load-on-startup>1</load-on-startup>
    </servlet>
+   <servlet>
+        <servlet-name>CXFServlet2</servlet-name>
+        <display-name>CXF Servlet2</display-name>
+        <servlet-class>
+          org.apache.cxf.transport.servlet.CXFServlet
+        </servlet-class>  
+        <load-on-startup>1</load-on-startup>
+   </servlet>
    <servlet-mapping>
      <servlet-name>CXFServlet</servlet-name>
      <url-pattern>/*</url-pattern>
-  </servlet-mapping>
+   </servlet-mapping>
+   <servlet-mapping>
+     <servlet-name>CXFServlet2</servlet-name>
+     <url-pattern>/redirect/*</url-pattern>
+   </servlet-mapping>
 </web-app>
 <!-- END SNIPPET: webxml -->


Mime
View raw message