I did some refactoring to AbstractHTTPServlet to get static resource
extensions configurable at 2 levels (custom resources or overriding) so
that the default extensions map is not modified in the CXF code any
longer, and I had to introduce getBus() abstract method into it.
I could've pushed all of CXFNonSpringServlet bus initialization into
AbstractHttpservlet but thought that the actual task (supporting the
more flexible extensions mapping) and given that CXFServlets loading
static resources themselves is not really part of CXFServlet
functionality, just a little utility (redirection to the default servlet
or other mechanisms can scale better), I thought that it all did not
warrant a big refactoring at this moment of time, comments are welcome
either way
Sergey
On 22/05/13 17:58, [email protected] wrote:
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