Using the Resource Server

The Resource Server provides a utility jar for web applications to use. It contains a JSP tag for easy fail-safe use of Resource Server content, a servlet Filter that can set caching related HTTP headers, and a version of EHCache's SimplePageCachingFilter that allows the CacheManager intance to be injected.

JSP Tag Library

This is the primary use of the utility library for the Resource Server Util library. It allows a web application to use static content from the Resource Server if it is deployed in the same servlet container and gracefully fail back to a local version of the content if the Resource Server is not available.

For example if I wanted to use jQuery 1.3.1 in my webapp I would add the file to the path /rs/jquery/1.3.1/jquery-1.3.1.js within my web application. I would then use the Resource Server JSP tag library as shown in the example below to get the correct URL to the resource (it will always start with a /) and use that in my <script> tag.

<%@ taglib prefix="rs" uri="http://www.jasig.org/resource-server" %>

<rs:resourceURL var="jQueryPath" value="/rs/jquery/1.3.1/jquery-1.3.1.js"/>
<script type="text/javascript" language="javascript" src="${jQueryPath}"></script>

If the Resource Server webapp is available the resulting URL would look like:

  • /ResourceServingWebapp/rs/jquery/1.3.1/jquery-1.3.1.js

If the Resource Server webapp is not available the resulting URL would look like:

  • /MyWebappContextPath/rs/jquery/1.3.1/jquery-1.3.1.js

The advantage of this approach is if there are multiple portlets in the portal all using jQuery 1.3.1 they would all render the same URL to the script. As a result of the HTTP cache headers set by the Resource Server the user's browser will only ever download the script once.

Configuration

The context init-param resourceContextPath can be set in your web application's web.xml file to set the name of the Resource Serving Webapp. It defaults to ResourceServingWebapp.

HTTP Caching Filter

The org.jasig.resourceserver.utils.filter.CacheExpirationFilter provides a way to set the Expires and Cache-Control HTTP headers for resources. The function of these headers is to tell the user's browser to not even check for new versions of the file for the specified amount of time.

This filter should NEVER be used on dynamic resources.

It can be used on static resources that do not have a version string in the URL path but the cacheMaxAge should be set to a low value. Once a resource has been retrieved and had these headers set there is no way for the server to tell the browser to re-retrieve the resource until the specified cache time passes.

Configuration

The filter can either be configured via context init-params in web.xml or via setters in a Spring application context when used in conjunction with Spring's DelegatingFilterProxy

  • cacheMaxAge - Sets the max age in seconds to be used in the cache headers for cached resources defaults to 1 year (31536000).
  • regenerateHeadersInterval - The interval in milliseconds to regenerate the cache headers defaults to 1 second (1000)

GZIP/Memory Caching Filter

The org.jasig.resourceserver.utils.cache.ConfigurablePageCachingFilter extends EHCache's SimplePageCachingFilter. This filter provides GZip compression of the response if the browser supports it and caches the GZip'd content in a backing EHCache instance. The extension is to allow an injected CacheManager to be used instead of creating a new one. This is useful for environments where a CacheManager already exists or additional configuration is needed.

This filter should NEVER be used on dynamic resources.

It can be used on static resources that do not have a version string in the URL path but the expiration time of the backing cache should be set to a low value so changes to the static file on the file system are propagated to the end user without too much delay.

Configuration

The filter must be configured in a Spring application context in conjunction with Spring's DelegatingFilterProxy. Configuration is done via constructor injection.

  • cacheManager - The CacheManager instance to use.
  • cacheName - The name of the backing Cache to store the gziped content in.