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 Summary
Modifier and TypeClassDescriptionstatic class
Creates theLastModifiedServlet.ParsedCssFile
cache during application start-up. -
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
addLastModified
(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 void
doGet
(HttpServletRequest request, HttpServletResponse response) static String
encodeLastModified
(long lastModified) Encodes a last modified value.protected long
getLastModified
(HttpServletRequest request) static long
getLastModified
(ServletContext servletContext, HttpServletRequest request, String path) Automatically determines extension from path.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 (/).void
init
(ServletConfig config) Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPost, doPut, doTrace, service, service
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
-
Field Details
-
DEFAULT_CACHE_CONTROL
The default, short-term,cache-control
header value.Only used when the
cache-control
header has not already been set, which will normally already be set to a much more aggressive value byLastModifiedHeaderFilter
.- See Also:
-
LAST_MODIFIED_PARAMETER_NAME
The 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_NAME
The header that may be used to disable automatic lastModified parameters.- See Also:
-
-
Constructor Details
-
LastModifiedServlet
public LastModifiedServlet()
-
-
Method Details
-
encodeLastModified
Encodes a last modified value. The value is URL-safe and does not need to be encoded. -
getLastModified
public 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
0
when unknown.
-
getLastModified
public static long getLastModified(ServletContext servletContext, HttpServletRequest request, String path) Automatically determines extension from path.- See Also:
-
addLastModified
public 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_NAME
equal to "false".Will not modify Canonical URLs.
- Throws:
MalformedURLException
-
init
- Specified by:
init
in interfaceServlet
- Overrides:
init
in classGenericServlet
- Throws:
ServletException
-
getLastModified
- Overrides:
getLastModified
in classHttpServlet
-
doGet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException - Overrides:
doGet
in classHttpServlet
- Throws:
ServletException
IOException
-