public static String addLastModified(ServletContext servletContext, HttpServletRequest request, String servletPath, String url, AddLastModifiedWhen when) throws MalformedURLException {
		// Never try to add if when==falsee
		if(when != AddLastModifiedWhen.FALSE) {
			// Get the context-relative path (resolves relative paths)
			String resourcePath = ServletUtil.getAbsolutePath(
				servletPath,
				url
			);
			if(resourcePath.startsWith("/")) {
				// Strip parameters from resourcePath
				{
					int questionPos = resourcePath.lastIndexOf('?');
					resourcePath = questionPos==-1 ? resourcePath : resourcePath.substring(0, questionPos);
				}
				String extension = FileUtils.getExtension(resourcePath).toLowerCase(Locale.ROOT);
				final boolean doAdd;
				if(when == AddLastModifiedWhen.TRUE) {
					// Always try to add
					doAdd = true;
				} else {
					// Check for header disabling auto last modified
					if("false".equalsIgnoreCase(request.getHeader(LAST_MODIFIED_HEADER_NAME))) {
						doAdd = false;
					} else {
						// Conditionally try to add based on file extension
						doAdd = staticExtensions.contains(
							extension
						);
					}
				}
				if(doAdd) {
					long lastModified = getLastModified(servletContext, request, resourcePath, extension);
					if(lastModified != 0) {
						int questionPos = url.lastIndexOf('?');
						int anchorStart = url.lastIndexOf('#');
						if(anchorStart == -1) {
							// No anchor
							url =
								url
								+ (questionPos==-1 ? '?' : '&')
								+ LAST_MODIFIED_PARAMETER_NAME + "="
								+ encodeLastModified(lastModified)
							;
						} else {
							// With anchor
							url =
								url.substring(0, anchorStart)
								+ (questionPos==-1 ? '?' : '&')
								+ LAST_MODIFIED_PARAMETER_NAME + "="
								+ encodeLastModified(lastModified)
								+ url.substring(anchorStart)
							;
						}
					}
				}
			}
		}
		return url;
	}
public static String addLastModified ( ServletContext servletContext , HttpServletRequest request , String servletPath , String url , AddLastModifiedWhen when ) throws MalformedURLException { if ( when != AddLastModifiedWhen . FALSE ) { String resourcePath = ServletUtil . getAbsolutePath ( servletPath , url ) ; if ( resourcePath . startsWith ( "/" ) ) { { int questionPos = resourcePath . lastIndexOf ( '?' ) ; resourcePath = questionPos == - 1 ? resourcePath : resourcePath . substring ( 0 , questionPos ) ; } String extension = FileUtils . getExtension ( resourcePath ) . toLowerCase ( Locale . ROOT ) ; final boolean doAdd ; if ( when == AddLastModifiedWhen . TRUE ) { doAdd = true ; } else { if ( "false" . equalsIgnoreCase ( request . getHeader ( LAST_MODIFIED_HEADER_NAME ) ) ) { doAdd = false ; } else { doAdd = staticExtensions . contains ( extension ) ; } } if ( doAdd ) { long lastModified = getLastModified ( servletContext , request , resourcePath , extension ) ; if ( lastModified != 0 ) { int questionPos = url . lastIndexOf ( '?' ) ; int anchorStart = url . lastIndexOf ( '#' ) ; if ( anchorStart == - 1 ) { url = url + ( questionPos == - 1 ? '?' : '&' ) + LAST_MODIFIED_PARAMETER_NAME + "=" + encodeLastModified ( lastModified ) ; } else { url = url . substring ( 0 , anchorStart ) + ( questionPos == - 1 ? '?' : '&' ) + LAST_MODIFIED_PARAMETER_NAME + "=" + encodeLastModified ( lastModified ) + url . substring ( anchorStart ) ; } } } } } return url ; }
<p>
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 (/).
</p>
<p>
This implementation assume anchors (#) are always after the last question mark (?).
</p>