Java Code Examples for org.apache.catalina.WebResource#getLastModified()

The following examples show how to use org.apache.catalina.WebResource#getLastModified() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultServlet.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Check if the if-modified-since condition is satisfied.
 *
 * @param request   The servlet request we are processing
 * @param response  The servlet response we are creating
 * @param resource  The resource
 * @return <code>true</code> if the resource meets the specified condition,
 *  and <code>false</code> if the condition is not satisfied, in which case
 *  request processing is stopped
 */
protected boolean checkIfModifiedSince(HttpServletRequest request,
        HttpServletResponse response, WebResource resource) {
    try {
        long headerValue = request.getDateHeader("If-Modified-Since");
        long lastModified = resource.getLastModified();
        if (headerValue != -1) {

            // If an If-None-Match header has been specified, if modified since
            // is ignored.
            if ((request.getHeader("If-None-Match") == null)
                && (lastModified < headerValue + 1000)) {
                // The entity has not been modified since the date
                // specified by the client. This is not an error case.
                response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                response.setHeader("ETag", resource.getETag());

                return false;
            }
        }
    } catch (IllegalArgumentException illegalArgument) {
        return true;
    }
    return true;
}
 
Example 2
Source File: DefaultServlet.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Check if the if-unmodified-since condition is satisfied.
 *
 * @param request   The servlet request we are processing
 * @param response  The servlet response we are creating
 * @param resource  The resource
 * @return <code>true</code> if the resource meets the specified condition,
 *  and <code>false</code> if the condition is not satisfied, in which case
 *  request processing is stopped
 * @throws IOException an IO error occurred
 */
protected boolean checkIfUnmodifiedSince(HttpServletRequest request,
        HttpServletResponse response, WebResource resource)
        throws IOException {
    try {
        long lastModified = resource.getLastModified();
        long headerValue = request.getDateHeader("If-Unmodified-Since");
        if (headerValue != -1) {
            if ( lastModified >= (headerValue + 1000)) {
                // The entity has not been modified since the date
                // specified by the client. This is not an error case.
                response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
                return false;
            }
        }
    } catch(IllegalArgumentException illegalArgument) {
        return true;
    }
    return true;
}
 
Example 3
Source File: WebappClassLoaderBase.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private void trackLastModified(String path, WebResource resource) {
    if (resourceEntries.containsKey(path)) {
        return;
    }
    ResourceEntry entry = new ResourceEntry();
    entry.lastModified = resource.getLastModified();
    synchronized(resourceEntries) {
        if (!resourceEntries.containsKey(path)) {
            resourceEntries.put(path, entry);
        }
    }
}
 
Example 4
Source File: CachedResource.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
protected boolean validateResource(boolean useClassLoaderResources) {
    // It is possible that some resources will only be visible for a given
    // value of useClassLoaderResources. Therefore, if the lookup is made
    // with a different value of useClassLoaderResources than was used when
    // creating the cache entry, invalidate the entry. This should have
    // minimal performance impact as it would be unusual for a resource to
    // be looked up both as a static resource and as a class loader
    // resource.
    if (usesClassLoaderResources != useClassLoaderResources) {
        return false;
    }

    long now = System.currentTimeMillis();

    if (webResource == null) {
        synchronized (this) {
            if (webResource == null) {
                webResource = root.getResourceInternal(
                        webAppPath, useClassLoaderResources);
                getLastModified();
                getContentLength();
                nextCheck = ttl + now;
                // exists() is a relatively expensive check for a file so
                // use the fact that we know if it exists at this point
                if (webResource instanceof EmptyResource) {
                    cachedExists = Boolean.FALSE;
                } else {
                    cachedExists = Boolean.TRUE;
                }
                return true;
            }
        }
    }

    if (now < nextCheck) {
        return true;
    }

    // Assume resources inside WARs will not change
    if (!root.isPackedWarFile()) {
        WebResource webResourceInternal = root.getResourceInternal(
                webAppPath, useClassLoaderResources);
        if (!webResource.exists() && webResourceInternal.exists()) {
            return false;
        }

        // If modified date or length change - resource has changed / been
        // removed etc.
        if (webResource.getLastModified() != getLastModified() ||
                webResource.getContentLength() != getContentLength()) {
            return false;
        }

        // Has a resource been inserted / removed in a different resource set
        if (webResource.getLastModified() != webResourceInternal.getLastModified() ||
                webResource.getContentLength() != webResourceInternal.getContentLength()) {
            return false;
        }
    }

    nextCheck = ttl + now;
    return true;
}