Search in sources :

Example 1 with DateParseException

use of org.apache.commons.httpclient.util.DateParseException in project eclipse-integration-commons by spring-projects.

the class HttpClientTransportService method getLastModified.

/**
 * Verify availability of resources at the given web locations. Normally
 * this would be done using an HTTP HEAD.
 *
 * @param locations the locations of the resource to verify
 * @param one indicate if only one of the resources must exist
 * @param progressMonitor the monitor
 * @return true if the resource exists
 */
public long getLastModified(java.net.URI uri, IProgressMonitor progressMonitor) throws CoreException {
    WebLocation location = new WebLocation(uri.toString());
    SubMonitor monitor = SubMonitor.convert(progressMonitor);
    monitor.subTask(NLS.bind("Fetching {0}", location.getUrl()));
    try {
        HttpClient client = new HttpClient();
        // $NON-NLS-1$
        org.eclipse.mylyn.commons.net.WebUtil.configureHttpClient(client, "");
        HeadMethod method = new HeadMethod(location.getUrl());
        try {
            HostConfiguration hostConfiguration = org.eclipse.mylyn.commons.net.WebUtil.createHostConfiguration(client, location, monitor);
            int result = org.eclipse.mylyn.commons.net.WebUtil.execute(client, hostConfiguration, method, monitor);
            if (result == HttpStatus.SC_OK) {
                // $NON-NLS-1$
                Header lastModified = method.getResponseHeader("Last-Modified");
                if (lastModified != null) {
                    try {
                        return DateUtil.parseDate(lastModified.getValue()).getTime();
                    } catch (DateParseException e) {
                    // fall through
                    }
                }
                return 0;
            } else {
                throw toException(location, result);
            }
        } catch (IOException e) {
            throw toException(location, e);
        } finally {
            method.releaseConnection();
        }
    } finally {
        monitor.done();
    }
}
Also used : HeadMethod(org.apache.commons.httpclient.methods.HeadMethod) Header(org.apache.commons.httpclient.Header) DateParseException(org.apache.commons.httpclient.util.DateParseException) HostConfiguration(org.apache.commons.httpclient.HostConfiguration) WebLocation(org.eclipse.mylyn.commons.net.WebLocation) HttpClient(org.apache.commons.httpclient.HttpClient) SubMonitor(org.eclipse.core.runtime.SubMonitor) IOException(java.io.IOException)

Example 2 with DateParseException

use of org.apache.commons.httpclient.util.DateParseException in project onebusaway-application-modules by camsys.

the class CacheControlInterceptor method intercept.

@Override
public String intercept(ActionInvocation invocation) throws Exception {
    CacheControl cacheControl = getCacheControlAnnotation(invocation);
    if (cacheControl == null)
        return invocation.invoke();
    HttpServletRequest request = ServletActionContext.getRequest();
    HttpServletResponse response = ServletActionContext.getResponse();
    applyCacheControlHeader(invocation, cacheControl, request, response);
    applyExpiresHeader(invocation, cacheControl, request, response);
    applyETagHeader(invocation, cacheControl, request, response);
    Date lastModifiedTime = applyLastModifiedHeader(invocation, cacheControl, request, response);
    Map<String, String> requestCacheControl = parseRequestCacheControl(request);
    boolean bypassCache = bypassCache(requestCacheControl);
    if (lastModifiedTime != null && !bypassCache) {
        String modifiedSinceValue = request.getHeader("if-modified-since");
        if (modifiedSinceValue != null) {
            try {
                Date modifiedSince = DateUtil.parseDate(modifiedSinceValue);
                if (!lastModifiedTime.after(modifiedSince)) {
                    response.setStatus(304);
                    return null;
                }
            } catch (DateParseException ex) {
            }
        }
    }
    /**
     * If we have a HEAD request and the action has specified a short-circuit
     * response, skip the rest of the action chain
     */
    String method = request.getMethod();
    if (cacheControl.shortCircuit() && "HEAD".equals(method))
        return null;
    return invocation.invoke();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) DateParseException(org.apache.commons.httpclient.util.DateParseException) HttpServletResponse(javax.servlet.http.HttpServletResponse) Date(java.util.Date)

Aggregations

DateParseException (org.apache.commons.httpclient.util.DateParseException)2 IOException (java.io.IOException)1 Date (java.util.Date)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Header (org.apache.commons.httpclient.Header)1 HostConfiguration (org.apache.commons.httpclient.HostConfiguration)1 HttpClient (org.apache.commons.httpclient.HttpClient)1 HeadMethod (org.apache.commons.httpclient.methods.HeadMethod)1 SubMonitor (org.eclipse.core.runtime.SubMonitor)1 WebLocation (org.eclipse.mylyn.commons.net.WebLocation)1