Class LastModifiedServlet
- All Implemented Interfaces:
- Serializable,- Servlet,- ServletConfig
When mapped to handle *.css files, parses the underlying file from the web resources and automatically adds lastModified=#### URL parameters. This allows the replacement of files to be immediately visible to browsers while still efficiently caching when nothing changed.
The current CSS parser is extremely simple and may not catch all URLs. Specifically, it only looks for URLs on a line-by-line basis and does not support backslash (\) escapes. (TODO: Are backslash escapes even part of CSS?)
TODO: Handle escapes, which might require a parser more complicated than this regular expression, including escaped quotes in quotes, or escaped parenthesis: https://developer.mozilla.org/en-US/docs/Web/CSS/url(), https://stackoverflow.com/questions/25613552/how-should-i-escape-image-urls-for-css.
All files must be in the StandardCharsets.UTF_8 encoding.
TODO: Add support for non-url imports TODO: Review recursive import url(...) works correctly (urls within the included urls should be applied)
TODO: Rewrite all URLs to be app-relative, with contextPath prefixed, much like how other URLs are rewritten. This is so paths will still be relative to the CSS file even when included into a page directly.
- See Also:
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionstatic classCreates theLastModifiedServlet.ParsedCssFilecache during application start-up.
- 
Field SummaryFields
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic StringaddLastModified(ServletContext servletContext, HttpServletRequest request, String servletPath, String url, AddLastModified when) Adds a last modified time (to the nearest second) to a URL if the resource is directly available as a local resource.protected voiddoGet(HttpServletRequest request, HttpServletResponse response) static StringencodeLastModified(long lastModified) Encodes a last modified value.protected longgetLastModified(HttpServletRequest request) static longgetLastModified(ServletContext servletContext, HttpServletRequest request, String path) Automatically determines extension from path.static longgetLastModified(ServletContext servletContext, HttpServletRequest request, String path, String extension) Gets a last modified time given a context-relative path starting with a slash (/).voidinit(ServletConfig config) Methods inherited from class javax.servlet.http.HttpServletdoDelete, doHead, doOptions, doPost, doPut, doTrace, service, serviceMethods inherited from class javax.servlet.GenericServletdestroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
- 
Field Details- 
DEFAULT_CACHE_CONTROLThe default, short-term,cache-controlheader value.Only used when the cache-controlheader has not already been set, which will normally already be set to a much more aggressive value byLastModifiedHeaderFilter.- See Also:
 
- 
LAST_MODIFIED_PARAMETER_NAMEThe name of the last modified parameter that is optionally added. The value is URL-safe and does not need to be encoded.- See Also:
 
- 
LAST_MODIFIED_HEADER_NAMEThe header that may be used to disable automatic lastModified parameters.- See Also:
 
 
- 
- 
Constructor Details- 
LastModifiedServletpublic LastModifiedServlet()
 
- 
- 
Method Details- 
encodeLastModifiedEncodes a last modified value. The value is URL-safe and does not need to be encoded.
- 
getLastModifiedpublic static long getLastModified(ServletContext servletContext, HttpServletRequest request, String path, String extension) Gets a last modified time given a context-relative path starting with a slash (/).Any file ending in ".css" (case-insensitive) will be parsed and will have a modified time that is equal to the greatest of itself or any referenced URL. - Returns:
- the modified time or 0when unknown.
 
- 
getLastModifiedpublic static long getLastModified(ServletContext servletContext, HttpServletRequest request, String path) Automatically determines extension from path.- See Also:
 
- 
addLastModifiedpublic static String addLastModified(ServletContext servletContext, HttpServletRequest request, String servletPath, String url, AddLastModified when) throws MalformedURLException Adds a last modified time (to the nearest second) to a URL if the resource is directly available as a local resource. Only applies to relative URLs (./, ../) or URLs that begin with a slash (/).Will not modify when the request has header LAST_MODIFIED_HEADER_NAMEequal to "false".Will not modify Canonical URLs. - Throws:
- MalformedURLException
 
- 
init- Specified by:
- initin interface- Servlet
- Overrides:
- initin class- GenericServlet
- Throws:
- ServletException
 
- 
getLastModified- Overrides:
- getLastModifiedin class- HttpServlet
 
- 
doGetprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException - Overrides:
- doGetin class- HttpServlet
- Throws:
- ServletException
- IOException
 
 
- 

