cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1355380 - in /cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/servlet/ rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/ systests/jaxrs/src/test/re...
Date Fri, 29 Jun 2012 13:38:14 GMT
Author: sergeyb
Date: Fri Jun 29 13:38:13 2012
New Revision: 1355380

URL: http://svn.apache.org/viewvc?rev=1355380&view=rev
Log:
[CXF-4400] Better handling of redirect and static resource requests in AbstractHttpServlet

Added:
    cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_dispatch/welcomeBook.html   (with props)
Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/servlet/CXFNonSpringJaxrsServlet.java
    cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSRequestDispatcherTest.java
    cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_dispatch/WEB-INF/web.xml

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/servlet/CXFNonSpringJaxrsServlet.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/servlet/CXFNonSpringJaxrsServlet.java?rev=1355380&r1=1355379&r2=1355380&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/servlet/CXFNonSpringJaxrsServlet.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/servlet/CXFNonSpringJaxrsServlet.java
Fri Jun 29 13:38:13 2012
@@ -129,30 +129,15 @@ public class CXFNonSpringJaxrsServlet ex
     }
     
     protected void setExtensions(JAXRSServerFactoryBean bean, ServletConfig servletConfig)
{
-        bean.setExtensionMappings(handleMapSequence(servletConfig.getInitParameter(EXTENSIONS_PARAM)));
-        bean.setLanguageMappings(handleMapSequence(servletConfig.getInitParameter(LANGUAGES_PARAM)));
+        bean.setExtensionMappings(
+             CastUtils.cast((Map<?, ?>)parseMapSequence(servletConfig.getInitParameter(EXTENSIONS_PARAM))));
+        bean.setLanguageMappings(
+             CastUtils.cast((Map<?, ?>)parseMapSequence(servletConfig.getInitParameter(LANGUAGES_PARAM))));
         bean.setProperties(CastUtils.cast(
-                handleMapSequence(servletConfig.getInitParameter(PROPERTIES_PARAM)),
+                parseMapSequence(servletConfig.getInitParameter(PROPERTIES_PARAM)),
                 String.class, Object.class));
     }
     
-    protected Map<Object, Object> handleMapSequence(String sequence) {
-        if (sequence != null) {
-            sequence = sequence.trim();
-            Map<Object, Object> map = new HashMap<Object, Object>();
-            String[] pairs = sequence.split(" ");
-            for (String pair : pairs) {
-                String[] value = pair.split("=");
-                if (value.length == 2) {
-                    map.put(value[0].trim(), value[1].trim());
-                }
-            }
-            return map;
-        } else {
-            return Collections.emptyMap();    
-        }
-    }
-    
     protected void setAllInterceptors(JAXRSServerFactoryBean bean, ServletConfig servletConfig)
{
         setInterceptors(bean, servletConfig, OUT_INTERCEPTORS_PARAM);
         setInterceptors(bean, servletConfig, IN_INTERCEPTORS_PARAM);
@@ -270,8 +255,7 @@ public class CXFNonSpringJaxrsServlet ex
         String theName = cName.trim();
         int ind = theName.indexOf("(");
         if (ind != -1 && theName.endsWith(")")) {
-            props.putAll(CastUtils.cast(handleMapSequence(theName.substring(ind + 1, theName.length()
- 1)),
-                    String.class, String.class));
+            props.putAll(parseMapSequence(theName.substring(ind + 1, theName.length() - 1)));
             theName = theName.substring(0, ind).trim();
         }
         return theName;

Modified: cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java?rev=1355380&r1=1355379&r2=1355380&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java
(original)
+++ cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java
Fri Jun 29 13:38:13 2012
@@ -21,11 +21,13 @@ package org.apache.cxf.transport.servlet
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+import java.util.regex.Pattern;
 
 import javax.servlet.Filter;
 import javax.servlet.FilterConfig;
@@ -41,6 +43,7 @@ import javax.servlet.http.HttpServletReq
 import javax.servlet.http.HttpServletRequestWrapper;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.transport.http.AbstractHTTPDestination;
 
@@ -56,10 +59,12 @@ public abstract class AbstractHTTPServle
         Arrays.asList(new String[]{"POST", "GET", "PUT", "DELETE", "HEAD", "OPTIONS", "TRACE"});
     
     private static final String STATIC_RESOURCES_PARAMETER = "static-resources-list";
+    private static final String STATIC_WELCOME_FILE_PARAMETER = "static-welcome-file";
     
     private static final String REDIRECTS_PARAMETER = "redirects-list";
     private static final String REDIRECT_SERVLET_NAME_PARAMETER = "redirect-servlet-name";
     private static final String REDIRECT_SERVLET_PATH_PARAMETER = "redirect-servlet-path";
+    private static final String REDIRECT_ATTRIBUTES_PARAMETER = "redirect-attributes";
     private static final String REDIRECT_QUERY_CHECK_PARAMETER = "redirect-query-check";
     
     private static final Map<String, String> STATIC_CONTENT_TYPES;
@@ -74,21 +79,28 @@ public abstract class AbstractHTTPServle
         // TODO : add more types if needed
     }
     
-    private List<String> staticResourcesList;
-    private List<String> redirectList; 
+    private List<Pattern> staticResourcesList;
+    private String staticWelcomeFile;
+    private List<Pattern> redirectList; 
     private String dispatcherServletPath;
     private String dispatcherServletName;
+    private Map<String, String> redirectAttributes;
     private boolean redirectQueryCheck;
     
     public void init(ServletConfig servletConfig) throws ServletException {
         super.init(servletConfig);
 
         staticResourcesList = parseListSequence(servletConfig.getInitParameter(STATIC_RESOURCES_PARAMETER));
+        staticWelcomeFile = servletConfig.getInitParameter(STATIC_WELCOME_FILE_PARAMETER);
         
         redirectList = parseListSequence(servletConfig.getInitParameter(REDIRECTS_PARAMETER));
         redirectQueryCheck = Boolean.valueOf(servletConfig.getInitParameter(REDIRECT_QUERY_CHECK_PARAMETER));
         dispatcherServletName = servletConfig.getInitParameter(REDIRECT_SERVLET_NAME_PARAMETER);
         dispatcherServletPath = servletConfig.getInitParameter(REDIRECT_SERVLET_PATH_PARAMETER);
+        
+        redirectAttributes = parseMapSequence(servletConfig.getInitParameter(REDIRECT_ATTRIBUTES_PARAMETER));
+            
+        
     }
     public final void init(final FilterConfig filterConfig) throws ServletException {
         init(new ServletConfig() {
@@ -107,14 +119,14 @@ public abstract class AbstractHTTPServle
         });
     }
 
-    private static List<String> parseListSequence(String values) {
+    protected static List<Pattern> parseListSequence(String values) {
         if (values != null) {
-            List<String> list = new LinkedList<String>();
+            List<Pattern> list = new LinkedList<Pattern>();
             String[] pathValues = values.split(" ");
             for (String value : pathValues) {
                 String theValue = value.trim();
                 if (theValue.length() > 0) {
-                    list.add(theValue);
+                    list.add(Pattern.compile(theValue));
                 }
             }
             return list;
@@ -123,6 +135,25 @@ public abstract class AbstractHTTPServle
         }
     }
     
+    protected static Map<String, String> parseMapSequence(String sequence) {
+        if (sequence != null) {
+            sequence = sequence.trim();
+            Map<String, String> map = new HashMap<String, String>();
+            String[] pairs = sequence.split(" ");
+            for (String pair : pairs) {
+                String[] value = pair.split("=");
+                if (value.length == 2) {
+                    map.put(value[0].trim(), value[1].trim());
+                } else {
+                    map.put(pair.trim(), "");
+                }
+            }
+            return map;
+        } else {
+            return Collections.emptyMap();
+        }
+    }
+    
     protected void doPost(HttpServletRequest request, HttpServletResponse response) 
         throws ServletException {
         handleRequest(request, response);
@@ -198,14 +229,17 @@ public abstract class AbstractHTTPServle
         }
         
         if (staticResourcesList != null 
-            && matchPath(staticResourcesList, request)) {
-            serveStaticContent(request, response, request.getPathInfo());
+            && matchPath(staticResourcesList, request)
+            || staticWelcomeFile != null 
+                && (StringUtils.isEmpty(request.getPathInfo()) || request.getPathInfo().equals("/")))
{
+            serveStaticContent(request, response, 
+                               staticWelcomeFile != null ? staticWelcomeFile : request.getPathInfo());
             return;
         }
         invoke(request, response);
     }
     
-    private boolean matchPath(List<String> values, HttpServletRequest request) {
+    private boolean matchPath(List<Pattern> values, HttpServletRequest request) {
         String path = request.getPathInfo();
         if (path == null) {
             path = "/";
@@ -216,8 +250,8 @@ public abstract class AbstractHTTPServle
                 path += "?" + queryString; 
             }
         }
-        for (String value : values) {
-            if (path.matches(value)) {
+        for (Pattern pattern : values) {
+            if (pattern.matcher(path).matches()) {
                 return true;
             }
         }
@@ -267,6 +301,9 @@ public abstract class AbstractHTTPServle
             throw new ServletException(errorMessage);
         }
         try {
+            for (Map.Entry<String, String> entry : redirectAttributes.entrySet()) {
+                request.setAttribute(entry.getKey(), entry.getValue());
+            }
             HttpServletRequestFilter servletRequest = 
                 new HttpServletRequestFilter(request, pathInfo, theServletPath);
             rd.forward(servletRequest, response);

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java?rev=1355380&r1=1355379&r2=1355380&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java (original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java Fri
Jun 29 13:38:13 2012
@@ -554,6 +554,26 @@ public class BookStore {
     }
     
     @GET
+    @Path("/books/{fiql}/chapter/{i}")
+    @Produces("application/xml")
+    public Chapter getInChapterFromSelectedBook(@Context SearchContext searchContext,
+                                           @PathParam("fiql") PathSegment fiqlSegment,
+                                           @PathParam("i") int chapterIndex) throws BookNotFoundFault
{
+        
+        SearchCondition<Book> sc = searchContext.getCondition(Book.class);
+        if (sc == null) {
+            throw new BookNotFoundFault("Search exception");
+        }
+        List<Book> found = sc.findAll(books.values());
+        if (found.size() != 1) {
+            throw new BookNotFoundFault("Single book is expected");
+        }
+        
+        Book book = found.get(0);
+        return book.getChapter(chapterIndex);
+    }
+    
+    @GET
     @Path("/books/text/xml/{bookId}")
     @Produces("text/xml")
     public Book getBookTextXml(@PathParam("bookId") String id) throws BookNotFoundFault {

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSRequestDispatcherTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSRequestDispatcherTest.java?rev=1355380&r1=1355379&r2=1355380&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSRequestDispatcherTest.java
(original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSRequestDispatcherTest.java
Fri Jun 29 13:38:13 2012
@@ -97,4 +97,33 @@ public class JAXRSRequestDispatcherTest 
         String value = source.getValue("xhtml:html/xhtml:body/xhtml:ul/books:bookTag", namespaces);
         assertEquals("CXF Rocks", value);
     }
+    @Test
+    public void testGetBookHTMLFromWelcomeList() throws Exception {
+        String endpointAddress = "http://localhost:" + PORT + "/welcome";
+        doTestGetBookHTMLFromWelcomeList(endpointAddress);
+    }
+    
+    @Test
+    public void testGetBookHTMLFromWelcomeList2() throws Exception {
+        String endpointAddress = "http://localhost:" + PORT + "/the";
+        doTestGetBookHTMLFromWelcomeList(endpointAddress);
+    }
+    
+    @Test
+    public void testGetBookHTMLFromStaticWelcomeFile() throws Exception {
+        String endpointAddress = "http://localhost:" + PORT + "/welcome2";
+        doTestGetBookHTMLFromWelcomeList(endpointAddress);
+    }
+    
+    private void doTestGetBookHTMLFromWelcomeList(String address) throws Exception {
+        WebClient client = WebClient.create(address);
+        client.accept("text/html");
+        WebClient.getConfig(client).getHttpConduit().getClient().setReceiveTimeout(100000000);
+        XMLSource source = client.accept("text/html").get(XMLSource.class);
+        Map<String, String> namespaces = new HashMap<String, String>();
+        namespaces.put("xhtml", "http://www.w3.org/1999/xhtml");
+        namespaces.put("books", "http://www.w3.org/books");
+        String value = source.getValue("xhtml:html/xhtml:body/xhtml:ul/books:bookTag", namespaces);
+        assertEquals("Welcome to CXF", value);
+    }
 }

Modified: cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_dispatch/WEB-INF/web.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_dispatch/WEB-INF/web.xml?rev=1355380&r1=1355379&r2=1355380&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_dispatch/WEB-INF/web.xml (original)
+++ cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_dispatch/WEB-INF/web.xml Fri Jun 29
13:38:13 2012
@@ -28,7 +28,7 @@
 		<param-name>contextConfigLocation</param-name>
 		<param-value>WEB-INF/beans.xml</param-value>
 	</context-param>
-
+	
 	<listener>
 		<listener-class>
 			org.springframework.web.context.ContextLoaderListener
@@ -41,6 +41,22 @@
 		<servlet-class>
 			org.apache.cxf.transport.servlet.CXFServlet
 		</servlet-class>
+		<init-param>
+    		<param-name>redirects-list</param-name>
+	    	<param-value>
+	    	  /
+	    	</param-value>
+	    </init-param>
+	    <init-param>
+    		<param-name>redirect-attributes</param-name>
+	    	<param-value>
+	    	  javax.servlet.include.request_uri
+	    	</param-value>
+	    </init-param>
+	    <init-param>
+    		<param-name>redirect-servlet-name</param-name>
+	    	<param-value>default</param-value>
+	    </init-param>
 		<load-on-startup>1</load-on-startup>
 	</servlet>
 
@@ -48,7 +64,6 @@
 		<servlet-name>CXFServlet</servlet-name>
 		<url-pattern>/the/*</url-pattern>
 	</servlet-mapping>
-	
 	<servlet>
 		<servlet-name>RedirectCXFServlet</servlet-name>
 		<display-name>Redirect CXF Servlet</display-name>
@@ -57,14 +72,10 @@
 		</servlet-class>
 		<init-param>
     		<param-name>redirects-list</param-name>
-	    	<param-value>/(\w)+.html</param-value>
+	    	<param-value>
+	    	  /(\w)+.html
+	    	</param-value>
 	    </init-param>
-	    <!--
-	    <init-param>
-    		<param-name>reditect-servlet-name</param-name>
-	    	<param-value>DefaultCXFServlet</param-value>
-	    </init-param>
-	    -->
 	    <init-param>
     		<param-name>redirect-servlet-path</param-name>
 	    	<param-value>/static</param-value>
@@ -74,9 +85,8 @@
 
     <servlet-mapping>
 		<servlet-name>RedirectCXFServlet</servlet-name>
-		<url-pattern>/*</url-pattern>
+		<url-pattern>/book.html</url-pattern>
 	</servlet-mapping>
-	
 	<servlet>
 		<servlet-name>DefaultCXFServlet</servlet-name>
 		<display-name>Default CXF Servlet</display-name>
@@ -89,11 +99,63 @@
 	    </init-param>	
 		<load-on-startup>1</load-on-startup>
 	</servlet>
-
-    <servlet-mapping>
+	
+	<servlet-mapping>
 		<servlet-name>DefaultCXFServlet</servlet-name>
 		<url-pattern>/static/*</url-pattern>
 	</servlet-mapping>
 	
+	<servlet>
+		<servlet-name>WelcomeCXFServlet</servlet-name>
+		<display-name>Welcome CXF Servlet</display-name>
+		<servlet-class>
+			org.apache.cxf.transport.servlet.CXFServlet
+		</servlet-class>
+		<init-param>
+    		<param-name>redirects-list</param-name>
+	    	<param-value>
+	    	  /
+	    	</param-value>
+	    </init-param>
+	    <init-param>
+    		<param-name>redirect-attributes</param-name>
+	    	<param-value>
+	    	  javax.servlet.include.request_uri
+	    	</param-value>
+	    </init-param>
+	    <init-param>
+    		<param-name>redirect-servlet-name</param-name>
+	    	<param-value>default</param-value>
+	    </init-param>
+	    <load-on-startup>1</load-on-startup>
+	</servlet>
+	
+
+    <servlet-mapping>
+		<servlet-name>WelcomeCXFServlet</servlet-name>
+		<url-pattern>/welcome/*</url-pattern>
+	</servlet-mapping>
+	
+	<servlet>
+		<servlet-name>WelcomeCXFServlet2</servlet-name>
+		<display-name>Welcome CXF Servlet2</display-name>
+		<servlet-class>
+			org.apache.cxf.transport.servlet.CXFServlet
+		</servlet-class>
+		<init-param>
+    		<param-name>static-welcome-file</param-name>
+	    	<param-value>/welcomeBook.html</param-value>
+	    </init-param>
+		<load-on-startup>1</load-on-startup>
+	</servlet>
+	
+    <servlet-mapping>
+		<servlet-name>WelcomeCXFServlet2</servlet-name>
+		<url-pattern>/welcome2/*</url-pattern>
+	</servlet-mapping>
+	
+	<welcome-file-list>
+		<welcome-file>welcomeBook.html</welcome-file>
+	</welcome-file-list>
 </web-app>
 <!-- END SNIPPET: webxml -->
\ No newline at end of file

Added: cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_dispatch/welcomeBook.html
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_dispatch/welcomeBook.html?rev=1355380&view=auto
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_dispatch/welcomeBook.html (added)
+++ cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_dispatch/welcomeBook.html Fri Jun 29
13:38:13 2012
@@ -0,0 +1,28 @@
+<!--
+	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.
+-->
+<html xmlns="http://www.w3.org/1999/xhtml"
+      xmlns:books="http://www.w3.org/books">
+<head> <title>Testing XML Example</title> </head>
+<body>
+	<h1>Book</h1>
+	<ul>
+	   <books:bookTag>Welcome to CXF</books:bookTag>
+	</ul>
+</body>
+</html>

Propchange: cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_dispatch/welcomeBook.html
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_dispatch/welcomeBook.html
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_dispatch/welcomeBook.html
------------------------------------------------------------------------------
    svn:mime-type = text/html



Mime
View raw message