|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjavax.servlet.GenericServlet
javax.servlet.http.HttpServlet
org.clazzes.util.http.ResourceServlet
public class ResourceServlet
A servlet, which exports parts of the ServletContext
's resources 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> </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="org.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> </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.
Constructor Summary | |
---|---|
ResourceServlet()
|
Method Summary | |
---|---|
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 |
addRedirect(String pathInfo,
String url)
Add a path-to-URL redirect. |
protected void |
doGet(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse resp)
|
Map<String,String> |
getAliases()
|
int |
getBufferSize()
|
Pattern |
getCacheRegex()
|
Set<String> |
getExcludeMimeTypes()
|
int |
getMaxAgeSeconds()
|
int |
getMinCompressionSize()
|
Pattern |
getNocacheRegex()
|
Map<String,String> |
getRedirects()
|
String |
getResourcePath()
|
String |
getServletInfo()
|
void |
init()
|
void |
setAliases(Map<String,String> aliases)
|
void |
setBufferSize(int bufferSize)
|
void |
setCacheRegex(Pattern cacheRegex)
|
void |
setExcludeMimeTypes(Set<String> excludeMimeTypes)
|
void |
setMaxAgeSeconds(int seconds)
|
void |
setMinCompressionSize(int minCompressionSize)
|
void |
setNocacheRegex(Pattern nocacheRegex)
|
void |
setRedirects(Map<String,String> redirects)
The keys of this map are path infos like returned by HttpServletRequest.getPathInfo() , which start with a '/' sign. |
void |
setResourcePath(String resourcePath)
|
Methods inherited from class javax.servlet.http.HttpServlet |
---|
doDelete, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service, service |
Methods inherited from class javax.servlet.GenericServlet |
---|
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletName, init, log, log |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public ResourceServlet()
Method Detail |
---|
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
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 ClassLoader to set.
All request exports resources acquired through
ServletContext.getResourceAsStream(String)
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 javax.servlet.GenericServlet
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 start 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 paremeter 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 Map<String,String> getAliases()
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.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |