|
|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||

java.lang.Objectgr.abiss.mvn.plugins.jstools.web.JavascriptDependencyFilter
public class JavascriptDependencyFilter
A Servlet Filter that loads Javascript files or other static resources from
the runtime classpath and uses them to build an HTTP response. Web
applications can, for example, include JS dependencies in their Maven POM and
the filter will resolve those at runtime, cache them and produce the
appropriate HTTP response.
The filter will only serve classpath resource types with file extentions you
configure it to, (see the allowedExtentions init-param) and
with the Cache-Control HTTP header you specify (see the
cacheControl init-param).
Note that the filter will only handle HTTP GET and HTTP HEAD requests. All
other HTTP request methods will be server with a "Non Implemented" (501) HTTP
response error.
To add the filter in your application, add the folowing in your web.xml or,
in case you use XDoclet (webdoclet), your filters.xml merge file:
<filter>
<filter-name>JavascriptDependencyFilter</filter-name>
<filter-class>gr.abiss.mvn.plugins.jstools.web.JavascriptDependencyFilter</filter-class>
<init-param>
<!--
Optional, tells the filter which file extentions are allowed.
The default are the sample values below (if you just want those,
you don't have to configure the filter).
Note that the Filter will NOT load resources from META-INF.
-->
<param-name>allowedExtentions</param-name>
<param-value>js png jpg gif txt html htm xml xsl xslt svg svgz swf</param-value>
</init-param>
<init-param>
<!--
Optional, used to configure the Cache-Control HTTP header.
Default is "max-age=86400"
-->
<param-name>cacheControl</param-name>
<param-value>max-age=86400</param-value>
</init-param>
<init-param>
<!--
Optional, enable gzip compression for browsers that support it
(based on HTTP request headers). Default is true.
-->
<param-name>enableGzip</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<!--
Optional, control whether resources are cached to
avoid I/O overhead every time they are requested. Default is true.
-->
<param-name>enableCache</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<!--
Optional, control whether gzipped resources are cached to
avoid GZIPing overhead every time they are requested. Default is true.
-->
<param-name>enableGzipedCache</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<!--
Optional, tells the filter whether to send an HTTP 404 ("not found") code
or let go of the filter chain to the application if no matching resource
is found in the classpath. Possible values are {true, false}.
The default is to send a 404 (true).
-->
<param-name>send404</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<!--
Optional (mandatory for arbitary url-pattern in filter-mapping).
Used to tell the plugin what base URL fragment to ignore when looking
for Javascript resources. Must match the url-pattern in filter-mapping,
see below. Default is /lib/js
-->
<param-name>basePath</param-name>
<param-value>/lib/js/</param-value>
</init-param>
</filter>
The servlet container also needs a mapping for the filter. You really don't
want the filter intercepting all HTTP requests! The URL pattern in the filter
mapping must be exactly the same with the value of the
basePath inialization parameter:
<filter-mapping>
<filter-name>JavascriptDependencyFilter</filter-name>
<!-- matches the default value of the basePath
init - param
<url-pattern>/lib/js/</url-pattern>
</filter-mapping>
This helps in inly invoking the filter for resources that are actually in the
classpath. To clarify, suppose an HTML response has a script tag like this:
<script type="text/javascript"
src="/lib/js/gr/abiss/js/sarissa/sarissa.js">
<script>
The browser will try to load the script making an HTTP GET for that URL. The
server will have the filter intercept the request since it matches the URL
pattern of the filter mapping. The filter will then do the following:
basePath initialization parametersend404 initialization parameter.
| Field Summary | |
|---|---|
protected Set<String> |
allowedExtentions
init-param accepting a whitespace-seperated list of
allowed file extentions. |
protected String |
basePath
init-param for the filter's base mapping path. |
protected static Map<String,byte[]> |
cache
The Javascript resources cache, used for binrary resources or when client does not support a GZIPed response |
protected boolean |
enableGzip
init-param to control whether to send textual GZIPed
textual content to increase HTTP performance. |
protected static Map<String,byte[]> |
gzipedCache
The Javascript GZIPed resources cache, used for non-binary resources when client supports it |
protected static Map<String,String> |
MIME_TYPE_MAPPINGS
The knwon MIME types map for this filter (file extention, MIME type) |
protected boolean |
send404
init-param to control whether to send an HTTP 404 ("not
found") code or let go of the filter chain to the application in case the
requested resource cannot be found. |
| Constructor Summary | |
|---|---|
JavascriptDependencyFilter()
|
|
| Method Summary | |
|---|---|
void |
destroy()
Clean up by doing nothing :-) |
void |
doFilter(javax.servlet.ServletRequest request,
javax.servlet.ServletResponse response,
javax.servlet.FilterChain chain)
|
protected String |
getMimeTypeFromFileExtention(String extention)
Get the MIME type for the given file extention |
void |
init(javax.servlet.FilterConfig filterConfig)
Initialize and configure based on any init parameters available |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
protected static Map<String,String> MIME_TYPE_MAPPINGS
protected static Map<String,byte[]> cache
protected static Map<String,byte[]> gzipedCache
protected Set<String> allowedExtentions
init-param accepting a whitespace-seperated list of
allowed file extentions. The default value is js png jpg gif txt html htm
xml xsl xslt svg svgz swf initialization parameter.
protected String basePath
init-param for the filter's base mapping path.
Must be the same as the url-pattern
mapping for the filter, e.g. /lib/js
protected boolean send404
init-param to control whether to send an HTTP 404 ("not
found") code or let go of the filter chain to the application in case the
requested resource cannot be found. Possible values are {true, false} The
default behaviour is to send a 404 (true)
protected boolean enableGzip
init-param to control whether to send textual GZIPed
textual content to increase HTTP performance. Possible values are {true,
false} The default true
| Constructor Detail |
|---|
public JavascriptDependencyFilter()
| Method Detail |
|---|
protected String getMimeTypeFromFileExtention(String extention)
extention - The file extention
public void doFilter(javax.servlet.ServletRequest request,
javax.servlet.ServletResponse response,
javax.servlet.FilterChain chain)
throws IOException,
javax.servlet.ServletException
doFilter in interface javax.servlet.FilterIOException
javax.servlet.ServletExceptionFilter.doFilter(javax.servlet.ServletRequest,
javax.servlet.ServletResponse, javax.servlet.FilterChain)
public void init(javax.servlet.FilterConfig filterConfig)
throws javax.servlet.ServletException
init in interface javax.servlet.Filterjavax.servlet.ServletExceptionFilter.init(javax.servlet.FilterConfig)public void destroy()
destroy in interface javax.servlet.FilterFilter.destroy()
|
© 2007-2007 Abiss.gr | ||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||