cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1485284 - in /cxf/trunk: 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/test/resources/ja...
Date Wed, 22 May 2013 16:58:24 GMT
Author: sergeyb
Date: Wed May 22 16:58:24 2013
New Revision: 1485284

URL: http://svn.apache.org/r1485284
Log:
[CXF-5021] Allowing to customize static media types, using ResourceManager to load the resources

Added:
    cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_non_spring/WEB-INF/cxfServletStaticResourcesMap.txt
  (with props)
    cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_non_spring/staticmodel.xml   (with props)
Modified:
    cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java
    cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFNonSpringServlet.java
    cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/Messages.properties
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerNonSpringBookTest.java
    cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_non_spring/WEB-INF/web.xml

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=1485284&r1=1485283&r2=1485284&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
Wed May 22 16:58:24 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/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFNonSpringServlet.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFNonSpringServlet.java?rev=1485284&r1=1485283&r2=1485284&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFNonSpringServlet.java
(original)
+++ cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFNonSpringServlet.java
Wed May 22 16:58:24 2013
@@ -80,6 +80,7 @@ public class CXFNonSpringServlet extends
         }
 
         this.controller = createServletController(sc);
+        finalizeServletInit(sc);
     }
 
     private static DestinationRegistry getDestinationRegistryFromBus(Bus bus) {

Modified: cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/Messages.properties
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/Messages.properties?rev=1485284&r1=1485283&r2=1485284&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/Messages.properties
(original)
+++ cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/Messages.properties
Wed May 22 16:58:24 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/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerNonSpringBookTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerNonSpringBookTest.java?rev=1485284&r1=1485283&r2=1485284&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerNonSpringBookTest.java
(original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerNonSpringBookTest.java
Wed May 22 16:58:24 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",

Added: cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_non_spring/WEB-INF/cxfServletStaticResourcesMap.txt
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_non_spring/WEB-INF/cxfServletStaticResourcesMap.txt?rev=1485284&view=auto
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_non_spring/WEB-INF/cxfServletStaticResourcesMap.txt
(added)
+++ cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_non_spring/WEB-INF/cxfServletStaticResourcesMap.txt
Wed May 22 16:58:24 2013
@@ -0,0 +1 @@
+xml=application/xml+model
\ No newline at end of file

Propchange: cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_non_spring/WEB-INF/cxfServletStaticResourcesMap.txt
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_non_spring/WEB-INF/cxfServletStaticResourcesMap.txt
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_non_spring/WEB-INF/web.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_non_spring/WEB-INF/web.xml?rev=1485284&r1=1485283&r2=1485284&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_non_spring/WEB-INF/web.xml (original)
+++ cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_non_spring/WEB-INF/web.xml Wed May 22
16:58:24 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>
     

Added: cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_non_spring/staticmodel.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_non_spring/staticmodel.xml?rev=1485284&view=auto
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_non_spring/staticmodel.xml (added)
+++ cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_non_spring/staticmodel.xml Wed May 22
16:58:24 2013
@@ -0,0 +1,18 @@
+<model xmlns="http://cxf.apache.org/jaxrs">
+ <resource name="org.apache.cxf.systest.jaxrs.BookStoreNoAnnotations" 
+    path="bookstore" produces="application/xml" consumes="application/xml">
+    <operation name="getBook" verb="GET" path="/books/{id}">
+       <param name="id" type="PATH" encoded="false"/>
+    </operation>
+    <operation name="getBookChapter" path="/books/{id}/chapter">
+        <param name="id" type="PATH"/>
+    </operation> 
+    <operation name="getBookWithAuthorization" verb="GET" path="/books/{id}/authorize">
+        <param name="id" type="PATH"/>
+        <param name="authorization" type="HEADER"/>
+    </operation>
+ </resource>
+ <resource name="org.apache.cxf.systest.jaxrs.ChapterNoAnnotations">
+    <operation name="getItself" verb="GET"/>
+ </resource>
+</model>
\ No newline at end of file

Propchange: cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_non_spring/staticmodel.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_non_spring/staticmodel.xml
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_non_spring/staticmodel.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml



Mime
View raw message