public class ResourceServlet extends AbstractResourceSerlvlet implements HttpResourceResolver
A servlet, which exports parts of the ServletContext
's resources or resources
from the classloader configured via setResourceClassLoader(ClassLoader)
or
setResourceClassLoaderHint(Object)
as http resources
with configuration options on which items should be delivered in compressed form.
This servlet may be configured through initialization parameters inside a classical
servlet container like in the following web.xml
example:
<servlet> <servlet-name>foobar-resources</servlet-name> <servlet-class>org.clazzes.util.http.ResourceServlet</servlet-class> <init-param> <param-name>resourcePath</param-name> <param-value>/foobar</param-value> </init-param> <init-param> <param-name>excludeMimeType</param-name> <param-value>image/png,image/jpeg,image/gif</param-value> </init-param> <init-param> <param-name>aliases</param-name> <param-value>/=/index.html,/index.htm=/index.html</param-value> </init-param> <init-param> <param-name>redirects</param-name> <param-value>/app1=appServlet,/app2=/ctx2/appServlet</param-value> </init-param> <init-param> <param-name>putOriginRegex</param-name> <param-value>chrome-extension://.*</param-value> </init-param> <init-param> <param-name>additionalHeader.0</param-name> <param-value>X-Frame-Options: DENY</param-value> </init-param> <init-param> <param-name>additionalHeader.0.mimeTypeRegex</param-name> <param-value>text/html</param-value> </init-param> <init-param> <param-name>additionalHeader.1</param-name> <param-value>X-Frame-Options: SAMEORIGIN</param-value> </init-param> <init-param> <param-name>additionalHeader.1.pathRegex</param-name> <param-value>.*\.cache\.html</param-value> </init-param> <init-param> <param-name>additionalHeader.2</param-name> <param-value>X-UA-Compatible: IE=edge</param-value> </init-param> <init-param> <param-name>additionalHeader.2.mimeTypeRegex</param-name> <param-value>text/html</param-value> </init-param> </servlet>
Additional, seldom used initialization parameters are minCompressionSize
and
bufferSize
, which are actually integer values.
In OSGi frameworks it is recommended to set up this servlet using blueprint or a similar technology using the supplied bean property setters like in the following example:
<bean id="resourceServlet" class="orgresources.clazzes.util.http.ResourceServlet"> <property name="resourcePath" value="/foobar-resources"></property> <property name="excludeMimeTypes"> <set> <value>image/png</value> <value>image/gif</value> <value>image/jpeg</value> <value>image/tiff</value> </set> </property> <property name="aliases"> <map> <entry key="/" value="/index.html"/> <entry key="/index.htm" value="/index.html"/> </map> </property> <property name="redirects"> <map> <entry key="/app1" value="appServlet"/> <entry key="/app2" value="/ctx2/appServlet"/> </map> </property> <property name="maxAgeSeconds" value="7200"></property> <property name="cacheRegex" value=".*\.cache\.\w+"></property> <property name="nocacheRegex" value=".*\.nocache\.\w+"></property> <bp:property name="additionalHeaders"> <bp:list> <bp:bean class="org.clazzes.util.http.AdditionalHeader"> <bp:property name="header" value="X-Frame-Options"/> <bp:property name="value" value="DENY"/> <bp:property name="mimeTypeRegex" value="text/html"/> </bp:bean> <bp:bean class="org.clazzes.util.http.AdditionalHeader"> <bp:property name="header" value="X-Frame-Options"/> <bp:property name="value" value="SAMEORIGIN"/> <bp:property name="pathRegex" value=".*\.cache\.html"/> </bp:bean> <bp:bean class="org.clazzes.util.http.AdditionalHeader"> <bp:property name="header" value="X-UA-Compatible"/> <bp:property name="value" value="IE=edge"/> <bp:property name="mimeTypeRegex" value="text/html"/> </bp:bean> </bp:list> </bp:property> </bean>
The servlet delivers Expires
and Cache-Control
headers
according to the following rules.
cacheRegex
, no Expires
and Cache-Control
headers are delivered.maxAgeSeconds
is set to 0
or the name of the resource
after applying aliases matches nocacheRegex
, Expires: 0
and
Cache-Control: no-cache
headers are delivered.
maxAgeSeconds
is set to a negative value, no Expires
and
Cache-Control
headers are delivered.
Expires
and
Cache-Control: max-age=<maxAgeSeconds>
are delivered according to the value of the maxAgeSeconds
property.
In the default configuration, this servlet delivers resources by calling
ServletContext.getResourceAsStream(String)
. If a resource class
loader is configured by setResourceClassLoader(ClassLoader)
or setResourceClassLoaderHint(Object)
, the delivered resources
are acquired by calling ClassLoader.getResourceAsStream(String)
instead. In traditional web.xml
setups, a resource class loader
may be configured by setting the init parameter
useClassLoaderResources
to true
.
If moreover a delegate resource resolver is given through
setDelegateResolver(HttpResourceResolver)
, all resources
are resolved through this resolver and the extension points
getResourceAsStream(String)
or getResourceAsURL(String)
are not called.
It is possible to registers delegate servlets as plugins using
setGetPlugins(Map)
, setPostPlugins(Map)
,
addGetPlugin(String, Servlet)
or
addPostPlugin(String, Servlet)
for given extensions, which are
applied after resolving the incoming path info using the configured aliases.
This serlvet has limited support for updating file resource by enabling
PUT operations from given origins matchin the regular expression provided
ba setPutOriginRegex(Pattern)
This feature is mainly intended
for development environments and should not be activated in prodcution
environments.
Constructor and Description |
---|
ResourceServlet() |
Modifier and Type | Method and Description |
---|---|
void |
addAlias(String pathInfo,
String alias)
Add an alias for a given path info.
|
void |
addExcludeMimeType(String excludeMimeType)
Add an element to the list of MIME types to be excluded from compression.
|
void |
addGetPlugin(String extension,
javax.servlet.Servlet plugin)
Add a plugin for GET requests for a given extension.
|
void |
addPostPlugin(String extension,
javax.servlet.Servlet plugin)
Add a plugin for POST requests for a given extension.
|
void |
addRedirect(String pathInfo,
String url)
Add a path-to-URL redirect.
|
protected void |
clearResourceInfos()
Clear all cached checksums for delivering ETags.
|
protected void |
doGet(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse resp) |
protected void |
doOptions(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse resp) |
protected void |
doPost(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse resp) |
protected void |
doPut(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse resp) |
Map<String,String> |
getAliases() |
int |
getBufferSize() |
Pattern |
getCacheRegex() |
HttpResourceResolver |
getDelegateResolver() |
Set<String> |
getExcludeMimeTypes() |
Map<String,javax.servlet.Servlet> |
getGetPlugins() |
int |
getMaxAgeSeconds() |
int |
getMinCompressionSize() |
Pattern |
getNocacheRegex() |
Map<String,javax.servlet.Servlet> |
getPostPlugins() |
Pattern |
getPutOriginRegex() |
Map<String,String> |
getRedirects() |
protected InputStream |
getResourceAsStream(String resPath)
This method returns the result of
ServletContext.getResourceAsStream(String) if the
resource class loader is null or otherwise
calls resourceClassLoader.getResourceAsStream() . |
protected URL |
getResourceAsURL(String resPath)
This method returns the result of
ServletContext.getResource(String) if the
resource class loader is null or otherwise
calls resourceClassLoader.getResource() . |
ClassLoader |
getResourceClassLoader() |
String |
getResourcePath() |
String |
getServletInfo() |
String |
getWriteReplacement() |
String |
getWriteReplaceRegex() |
void |
init() |
boolean |
isNoETags() |
boolean |
isRootContext() |
protected String |
makeCORStag(javax.servlet.http.HttpServletRequest req,
String origin,
String resolvedPath) |
protected static boolean |
maybeCallPlugin(String path,
String resolvedPath,
Map<String,javax.servlet.Servlet> plugins,
javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse resp)
Call a plugin based on the extension of the resolved path.
|
protected void |
removeResourceInfo(String resolvedPath)
Delete a cached checksum for delivering ETags.
|
protected String |
resolvePath(String path)
Resolve a path using the configured list of aliases.
|
HttpResource |
resolveResource(String finalPath,
int bufSize) |
void |
setAliases(Map<String,String> aliases) |
void |
setBufferSize(int bufferSize) |
void |
setCacheRegex(Pattern cacheRegex) |
void |
setDelegateResolver(HttpResourceResolver delegateResolver) |
void |
setExcludeMimeTypes(Set<String> excludeMimeTypes) |
void |
setGetPlugins(Map<String,javax.servlet.Servlet> getPlugins) |
void |
setMaxAgeSeconds(int seconds) |
void |
setMinCompressionSize(int minCompressionSize) |
void |
setNocacheRegex(Pattern nocacheRegex) |
void |
setNoETags(boolean noETags) |
void |
setPostPlugins(Map<String,javax.servlet.Servlet> postPlugins) |
void |
setPutOriginRegex(Pattern putOriginRegex) |
void |
setRedirects(Map<String,String> redirects)
The keys of this map are path infos like returned by
HttpServletRequest.getPathInfo() , which starts with a '/' sign. |
void |
setResourceClassLoader(ClassLoader resourceClassLoader) |
void |
setResourceClassLoaderHint(Object hint)
Set the resource class loader according to the given hint object.
|
void |
setResourcePath(String resourcePath) |
void |
setRootContext(boolean rootContext)
This configuration option is needed, because servlet container are
not consistently reporting requests to the root context.
|
void |
setWriteReplacement(String writeReplacement) |
void |
setWriteReplaceRegex(String writeReplaceRegex) |
addAdditionalHeader, getAdditionalHeaders, propagateAdditionalHeaders, setAdditionalHeaders
doDelete, doHead, doTrace, getLastModified, service, service
public HttpResource resolveResource(String finalPath, int bufSize) throws IOException
resolveResource
in interface HttpResourceResolver
finalPath
- A final resource path with all alias processing applied,
usually starting with a slash.bufSize
- A buffer size used for copy operations.null
, if the resource
could not be found.IOException
protected URL getResourceAsURL(String resPath) throws IOException
This method returns the result of
ServletContext.getResource(String)
if the
resource class loader is null
or otherwise
calls resourceClassLoader.getResource()
.
If a delegate resource resolve is provided through
setDelegateResolver(HttpResourceResolver)
, this method is
not called and the delegate resolver is used instead.
If the legacy method getResourceAsStream(String)
is
overwritten by the concrete implementation, this
method is not called either.
resPath
- The final resource path to open.IOException
protected InputStream getResourceAsStream(String resPath)
This method returns the result of
ServletContext.getResourceAsStream(String)
if the
resource class loader is null
or otherwise
calls resourceClassLoader.getResourceAsStream()
.
If a delegate resource resolve is provided through
setDelegateResolver(HttpResourceResolver)
, this method is
not called and the delegate resolver is used instead.
This method served as an extension point in http-util-1.7.x for resolving resources other than servlet context ro classpath resources. Reimplementing this method is discouraged in http-util-1.8.x in favor of providing a delegate resource resolver.
resPath
- The final resource path to open.protected void clearResourceInfos()
getResourceAsStream(String)
or
getResourceAsURL(String)
or providing a delegate resource
resolver by setDelegateResolver(HttpResourceResolver)
.protected void removeResourceInfo(String resolvedPath)
getResourceAsStream(String)
or
getResourceAsURL(String)
or providing a delegate resource
resolver by setDelegateResolver(HttpResourceResolver)
.resolvedPath
- The resolved resource path to delete
the checksum of.protected String resolvePath(String path)
path
- The path, which should be set to the empty string, if
HttpServletRequest.getPathInfo()
returned
null
.protected static boolean maybeCallPlugin(String path, String resolvedPath, Map<String,javax.servlet.Servlet> plugins, javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp) throws javax.servlet.ServletException, IOException
path
- The original path, which is the empty string ,if HttpServletRequest.getPathInfo()
returned null
.resolvedPath
- The resolved server-side path to be propagated via
RequestHelper.setResolvedPath(HttpServletRequest, String)
if
it differs from path
plugins
- A list of plugins by their extensions.req
- The servlet request.resp
- The serlvet response.javax.servlet.ServletException
- Upon failure calling the plugin.IOException
- Upon failure calling the plugin.protected void doGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp) throws javax.servlet.ServletException, IOException
doGet
in class javax.servlet.http.HttpServlet
javax.servlet.ServletException
IOException
protected void doPost(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp) throws javax.servlet.ServletException, IOException
doPost
in class javax.servlet.http.HttpServlet
javax.servlet.ServletException
IOException
protected String makeCORStag(javax.servlet.http.HttpServletRequest req, String origin, String resolvedPath)
protected void doPut(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp) throws javax.servlet.ServletException, IOException
doPut
in class javax.servlet.http.HttpServlet
javax.servlet.ServletException
IOException
protected void doOptions(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp) throws javax.servlet.ServletException, IOException
doOptions
in class javax.servlet.http.HttpServlet
javax.servlet.ServletException
IOException
public int getBufferSize()
public void setBufferSize(int bufferSize)
bufferSize
- the internal buffer size to set. The default value is 4096 and should be
suitable for almost any purpose.public String getResourcePath()
null
, only redirections are available.public void setResourcePath(String resourcePath)
resourcePath
- the resource path, which is exported from the given resource resolver.
All requests export resources acquired through
resolveResource(String, int)
using this resource path
appended by HttpServletRequest.getPathInfo()
.public int getMinCompressionSize()
public void setMinCompressionSize(int minCompressionSize)
minCompressionSize
- the minimal size of objects to be compressed to set.public Set<String> getExcludeMimeTypes()
public void setExcludeMimeTypes(Set<String> excludeMimeTypes)
excludeMimeTypes
- The list of MIME types to be excluded from compression to set.
Most image types are already compressed, so should consider to include
their MIME types in this list.public void addExcludeMimeType(String excludeMimeType)
excludeMimeType
- The MIME type to add.public String getServletInfo()
getServletInfo
in interface javax.servlet.Servlet
getServletInfo
in class javax.servlet.GenericServlet
public void init() throws javax.servlet.ServletException
init
in class AbstractResourceSerlvlet
javax.servlet.ServletException
public void setMaxAgeSeconds(int seconds)
seconds
- The value of the Cache-Control: max-age=<sec>
header delivered to all resources, which are not explicitly marked
as cacheable by matching against getCacheRegex()
.
If set to 0
all resources are marked as non-cacheable.
If set to -1
all resources are delivered without
a Cache-Control header.public int getMaxAgeSeconds()
Cache-Control: max-age=<sec>
header delivered to all resources, which are not explicitly marked
as cacheable by matching against getCacheRegex()
.public Pattern getCacheRegex()
Expires:
and
Cache-Control: max-age=<max-age> headers.
The default value is ".*\.cache\.\w+"
public void setCacheRegex(Pattern cacheRegex)
cacheRegex
- A regular expression, which is used to determine resources, which
are delivered with no Expires:
and
Cache-Control: max-age=<max-age> headers.
The default value is ".*\.cache\.\w+"
public Pattern getNocacheRegex()
Expires: 0
and
Cache-Control: no-cache
headers.
The default value is ".*\.nocache\.\w+"
public void setNocacheRegex(Pattern nocacheRegex)
nocacheRegex
- A regular expression, which is used to determine resource,
which are delivered with Expires: 0
and
Cache-Control: no-cache
headers.
The default value is ".*\.nocache\.\w+"
public Map<String,String> getRedirects()
public void setRedirects(Map<String,String> redirects)
The keys of this map are path infos like returned by
HttpServletRequest.getPathInfo()
, which starts with a '/' sign.
The values are relative or absolute URLs, which are passed to
HttpServletResponse.sendRedirect(String)
.
The key may be optionally postfixed by a list of locales in the format
/localized-url?locales=de,en
meaning that any request for
/localized-url
with an appropriate Accept-Language
HTTP header will be redirected to the target with an URL parameter named
locale
with a value of the selected domain as per
LocaleHelper.selectLocale(String, String[])
using the comma-separated
list of server-side locales de
or en
.
redirects
- The mapping of requests path infos to redirect URLs to set.public void addRedirect(String pathInfo, String url)
pathInfo
- A path info as returned by HttpServletRequest.getPathInfo()
.url
- A redirect URL passed to HttpServletResponse.sendRedirect(String)
.public void setAliases(Map<String,String> aliases)
aliases
- the resource aliases to set.
The keys of this map are path infos like returned by
HttpServletRequest.getPathInfo()
, which start with a '/' sign.
The values are translated path infos and should therefore start with
a '/' sign, too.public void addAlias(String pathInfo, String alias)
pathInfo
- A path info as returned by HttpServletRequest.getPathInfo()
.alias
- An alias path info, which should start with a '/' sign.public ClassLoader getResourceClassLoader()
ServletContext.getResourceAsStream(String)
public void setResourceClassLoader(ClassLoader resourceClassLoader)
resourceClassLoader
- a class loader to resolve resources
from instead of resolving from
ServletContext.getResourceAsStream(String)
to set.
If a delegate resource resolver is set through
setDelegateResolver(HttpResourceResolver)
,
this property is meaningless.public void setResourceClassLoaderHint(Object hint)
hint
is null
, the resource class
loader is set to null
. If hint is an instance of
ClassLoader
, the resource class loader is set to
hint
.If hint is an instance of
Class
, the resource class loader is set to
hint.getClassLoader()
. Otherwise, the resource
class loader is set to
hint.getClass().getClassLoader()
.hint
- An object to deduce the resource class loader from.
If a delegate resource resolver is set through
setDelegateResolver(HttpResourceResolver)
,
this property is meaningless.public HttpResourceResolver getDelegateResolver()
getResourceAsStream(String)
method.public void setDelegateResolver(HttpResourceResolver delegateResolver)
delegateResolver
- A delegate resolver, which resolves resources
instead of the built-in getResourceAsStream(String)
method.public boolean isNoETags()
true
, the Last-Modified
header will also be omitted.public void setNoETags(boolean noETags)
noETags
- Whether no ETag-specific headers should be sent.
If set to true
, the Last-Modified
header will also be omitted.public boolean isRootContext()
/.
public void setRootContext(boolean rootContext)
rootContext
- the root context flag to set, aka this servlet
is registered under the root path /.
public Pattern getPutOriginRegex()
public void setPutOriginRegex(Pattern putOriginRegex)
putOriginRegex
- The regular expression determining the origins, which
my update resources by an OPTIONS and PUT sequence
using CORS to set.public String getWriteReplaceRegex()
public void setWriteReplaceRegex(String writeReplaceRegex)
writeReplaceRegex
- The regular expression to map filenames
for PUT requests to the originating file.public String getWriteReplacement()
public void setWriteReplacement(String writeReplacement)
writeReplacement
- the replacement used to map filenames
for PUT requests to the originating file.public Map<String,javax.servlet.Servlet> getGetPlugins()
public void addGetPlugin(String extension, javax.servlet.Servlet plugin)
extension
- The extension, which will be matched against the resolved path.plugin
- The serlvet to call for requests on resources with the given extension.public void setGetPlugins(Map<String,javax.servlet.Servlet> getPlugins)
getPlugins
- The map of registered plugins for GET requests by
their extension to set.public Map<String,javax.servlet.Servlet> getPostPlugins()
public void addPostPlugin(String extension, javax.servlet.Servlet plugin)
extension
- The extension, which will be matched against the resolved path.plugin
- The serlvet to call for requests on resources with the given extension.Copyright © 2018 Clazzes.org. All rights reserved.