You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by se...@apache.org on 2013/05/22 23:33:20 UTC

svn commit: r1485437 - in /cxf/branches/2.7.x-fixes: ./ 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/resources/jaxrs_non_spring/ systests/jaxrs/src...

Author: sergeyb
Date: Wed May 22 21:33:20 2013
New Revision: 1485437

URL: http://svn.apache.org/r1485437
Log:
Merged revisions 1485284 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r1485284 | sergeyb | 2013-05-22 17:58:24 +0100 (Wed, 22 May 2013) | 1 line
  
  [CXF-5021] Allowing to customize static media types, using ResourceManager to load the resources
........

Added:
    cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/resources/jaxrs_non_spring/WEB-INF/cxfServletStaticResourcesMap.txt
      - copied unchanged from r1485284, cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_non_spring/WEB-INF/cxfServletStaticResourcesMap.txt
    cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/resources/jaxrs_non_spring/staticmodel.xml
      - copied unchanged from r1485284, cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_non_spring/staticmodel.xml
Modified:
    cxf/branches/2.7.x-fixes/   (props changed)
    cxf/branches/2.7.x-fixes/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java
    cxf/branches/2.7.x-fixes/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFNonSpringServlet.java
    cxf/branches/2.7.x-fixes/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/Messages.properties
    cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerNonSpringBookTest.java
    cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/resources/jaxrs_non_spring/WEB-INF/web.xml

Propchange: cxf/branches/2.7.x-fixes/
------------------------------------------------------------------------------
    svn:mergeinfo = /cxf/trunk:1485284

Propchange: cxf/branches/2.7.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.7.x-fixes/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java?rev=1485437&r1=1485436&r2=1485437&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java (original)
+++ cxf/branches/2.7.x-fixes/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java Wed May 22 21:33:20 2013
@@ -27,6 +27,9 @@ import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+import java.util.Properties;
+import java.util.ResourceBundle;
+import java.util.logging.Logger;
 import java.util.regex.Pattern;
 
 import javax.servlet.Filter;
@@ -43,15 +46,21 @@ import javax.servlet.http.HttpServletReq
 import javax.servlet.http.HttpServletRequestWrapper;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.cxf.Bus;
+import org.apache.cxf.common.classloader.ClassLoaderUtils;
+import org.apache.cxf.common.i18n.BundleUtils;
+import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.helpers.IOUtils;
+import org.apache.cxf.resource.ResourceManager;
 import org.apache.cxf.transport.http.AbstractHTTPDestination;
 
 
 public abstract class AbstractHTTPServlet extends HttpServlet implements Filter {
     
     private static final long serialVersionUID = -8357252743467075117L;
-
+    private static final Logger LOG = LogUtils.getL7dLogger(AbstractHTTPServlet.class);
+    private static final ResourceBundle BUNDLE = BundleUtils.getBundle(AbstractHTTPServlet.class);
     /**
      * List of well-known HTTP 1.1 verbs, with POST and GET being the most used verbs at the top 
      */
@@ -60,6 +69,7 @@ public abstract class AbstractHTTPServle
     
     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 STATIC_RESOURCES_MAP_RESOURCE = "/WEB-INF/cxfServletStaticResourcesMap.txt";    
     
     private static final String REDIRECTS_PARAMETER = "redirects-list";
     private static final String REDIRECT_SERVLET_NAME_PARAMETER = "redirect-servlet-name";
@@ -67,16 +77,16 @@ public abstract class AbstractHTTPServle
     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;
+    private static final Map<String, String> DEFAULT_STATIC_CONTENT_TYPES;
     
     static {
-        STATIC_CONTENT_TYPES = new HashMap<String, String>();
-        STATIC_CONTENT_TYPES.put("html", "text/html");
-        STATIC_CONTENT_TYPES.put("txt", "text/plain");
-        STATIC_CONTENT_TYPES.put("css", "text/css");
-        STATIC_CONTENT_TYPES.put("pdf", "application/pdf");
-        STATIC_CONTENT_TYPES.put("xsd", "application/xml");
-        // TODO : add more types if needed
+        DEFAULT_STATIC_CONTENT_TYPES = new HashMap<String, String>();
+        DEFAULT_STATIC_CONTENT_TYPES.put("html", "text/html");
+        DEFAULT_STATIC_CONTENT_TYPES.put("txt", "text/plain");
+        DEFAULT_STATIC_CONTENT_TYPES.put("css", "text/css");
+        DEFAULT_STATIC_CONTENT_TYPES.put("pdf", "application/pdf");
+        DEFAULT_STATIC_CONTENT_TYPES.put("xsd", "application/xml");
+        DEFAULT_STATIC_CONTENT_TYPES.put("js", "application/javascript");
     }
     
     private List<Pattern> staticResourcesList;
@@ -85,6 +95,8 @@ public abstract class AbstractHTTPServle
     private String dispatcherServletPath;
     private String dispatcherServletName;
     private Map<String, String> redirectAttributes;
+    private Map<String, String> staticContentTypes = 
+        new HashMap<String, String>(DEFAULT_STATIC_CONTENT_TYPES);
     private boolean redirectQueryCheck;
     
     public void init(ServletConfig servletConfig) throws ServletException {
@@ -99,9 +111,40 @@ public abstract class AbstractHTTPServle
         dispatcherServletPath = servletConfig.getInitParameter(REDIRECT_SERVLET_PATH_PARAMETER);
         
         redirectAttributes = parseMapSequence(servletConfig.getInitParameter(REDIRECT_ATTRIBUTES_PARAMETER));
-            
+    }
+    
+    protected void finalizeServletInit(ServletConfig servletConfig) {
+        InputStream is = getResourceAsStream("/WEB-INF" + STATIC_RESOURCES_MAP_RESOURCE);
+        if (is == null) {
+            is = getResourceAsStream(STATIC_RESOURCES_MAP_RESOURCE);
+        }
+        if (is != null) {
+            try {
+                Properties props = new Properties();
+                props.load(is);
+                for (String name : props.stringPropertyNames()) {
+                    staticContentTypes.put(name, props.getProperty(name));
+                }
+            } catch (IOException ex) {
+                String message = new org.apache.cxf.common.i18n.Message("STATIC_RESOURCES_MAP_LOAD_FAILURE",
+                                                                        BUNDLE).toString();
+                LOG.warning(message);
+            }    
+        }    
+    }
+    
+    protected InputStream getResourceAsStream(String path) {
         
+        InputStream is = ClassLoaderUtils.getResourceAsStream(path, AbstractHTTPServlet.class);
+        if (is == null && getBus() != null) {
+            ResourceManager rm = getBus().getExtension(ResourceManager.class);
+            if (rm != null) {
+                is = rm.resolveResource(path, InputStream.class);
+            }
+        }
+        return is;
     }
+    
     public final void init(final FilterConfig filterConfig) throws ServletException {
         init(new ServletConfig() {
             public String getServletName() {
@@ -262,17 +305,19 @@ public abstract class AbstractHTTPServle
         return false;
     }
     
+    protected abstract Bus getBus();
+    
     protected void serveStaticContent(HttpServletRequest request, 
                                       HttpServletResponse response,
                                       String pathInfo) throws ServletException {
-        InputStream is = super.getServletContext().getResourceAsStream(pathInfo);
+        InputStream is = getResourceAsStream(pathInfo);
         if (is == null) {
             throw new ServletException("Static resource " + pathInfo + " is not available");
         }
         try {
             int ind = pathInfo.lastIndexOf(".");
             if (ind != -1 && ind < pathInfo.length()) {
-                String type = STATIC_CONTENT_TYPES.get(pathInfo.substring(ind + 1));
+                String type = getStaticResourceContentType(pathInfo.substring(ind + 1));
                 if (type != null) {
                     response.setContentType(type);
                 }
@@ -288,6 +333,10 @@ public abstract class AbstractHTTPServle
         
     }
     
+    protected String getStaticResourceContentType(String extension) {
+        return staticContentTypes.get(extension);
+    }
+    
     protected void redirect(HttpServletRequest request, HttpServletResponse response, String pathInfo) 
         throws ServletException {
         

Modified: cxf/branches/2.7.x-fixes/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFNonSpringServlet.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFNonSpringServlet.java?rev=1485437&r1=1485436&r2=1485437&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFNonSpringServlet.java (original)
+++ cxf/branches/2.7.x-fixes/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFNonSpringServlet.java Wed May 22 21:33:20 2013
@@ -80,6 +80,7 @@ public class CXFNonSpringServlet extends
         }
 
         this.controller = createServletController(sc);
+        finalizeServletInit(sc);
     }
 
     private static DestinationRegistry getDestinationRegistryFromBus(Bus bus) {

Modified: cxf/branches/2.7.x-fixes/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/Messages.properties
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/Messages.properties?rev=1485437&r1=1485436&r2=1485437&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/Messages.properties (original)
+++ cxf/branches/2.7.x-fixes/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/Messages.properties Wed May 22 21:33:20 2013
@@ -22,6 +22,7 @@ LOAD_BUS_WITHOUT_APPLICATION_CONTEXT = L
 LOAD_BUS_WITH_APPLICATION_CONTEXT = Load the bus with application context
 REPLACED_HTTP_DESTIONFACTORY = Replaced the http destination factory with servlet transport factory
 BUILD_ENDPOINTS_FROM_CONFIG_LOCATION = Build endpoints from config-location: {0}
+STATIC_RESOURCES_MAP_LOAD_FAILURE = Static resources map resource has been found but can not be loaded
 DESTIONFACTORY_ALREADY_REGISTERED = Servlet transport factory already registered 
 UNEXPECTED_RESPONSE_TYPE_MSG = Unexpected response type {0}
 DECOUPLED_RESPONSE_FAILED_MSG = Decouple response failed

Modified: cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerNonSpringBookTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerNonSpringBookTest.java?rev=1485437&r1=1485436&r2=1485437&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerNonSpringBookTest.java (original)
+++ cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerNonSpringBookTest.java Wed May 22 21:33:20 2013
@@ -61,6 +61,16 @@ public class JAXRSClientServerNonSpringB
     }
     
     @Test
+    public void testGetStaticResource() throws Exception {
+        String address = "http://localhost:" + PORT + "/singleton/staticmodel.xml";
+        WebClient wc = WebClient.create(address);
+        String response = wc.get(String.class);
+        assertTrue(response.startsWith("<model"));
+        assertEquals("application/xml+model", wc.getResponse().getMetadata().getFirst("Content-Type"));
+        
+    }
+    
+    @Test
     public void testGetBook123UserModel() throws Exception {
         getAndCompareAsStrings("http://localhost:" + PORT + "/usermodel/bookstore/books/123",
                                "resources/expected_get_book123.txt",

Modified: cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/resources/jaxrs_non_spring/WEB-INF/web.xml
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/resources/jaxrs_non_spring/WEB-INF/web.xml?rev=1485437&r1=1485436&r2=1485437&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/resources/jaxrs_non_spring/WEB-INF/web.xml (original)
+++ cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/resources/jaxrs_non_spring/WEB-INF/web.xml Wed May 22 21:33:20 2013
@@ -52,7 +52,12 @@
 		           org.apache.cxf.jaxrs.provider.JAXBElementProvider
 		      </param-value>    
 		</init-param>
-		
+		<init-param>
+		      <param-name>static-resources-list</param-name>
+		      <param-value>
+		           /staticmodel.xml
+		      </param-value>    
+		</init-param>
 		<load-on-startup>1</load-on-startup>
 	</servlet>