Example usage for org.apache.wicket.util.resource IResourceStream lastModifiedTime

List of usage examples for org.apache.wicket.util.resource IResourceStream lastModifiedTime

Introduction

In this page you can find the example usage for org.apache.wicket.util.resource IResourceStream lastModifiedTime.

Prototype

Time lastModifiedTime();

Source Link

Document

Gets the last time this modifiable thing changed.

Usage

From source file:fiftyfive.wicket.resource.MergedResourceMapper.java

License:Apache License

/**
 * De-reference the resource and open its stream to determine the last modified time.
 *///from   w  ww  .  jav  a2  s  . com
protected Time getLastModifiedTime(ResourceReference ref) {
    Time modified = null;
    IResource res = ref.getResource();
    if (res instanceof IStaticCacheableResource) {
        IResourceStream stream = ((IStaticCacheableResource) res).getCacheableResourceStream();
        modified = stream.lastModifiedTime();
    }
    return modified;
}

From source file:org.cast.cwm.ResourceDirectory.java

License:Open Source License

/**
 * creates a new resource response based on the request attributes
 * /*from  w  w w  .  j  ava2 s . c  o  m*/
 * @param attributes
 *            current request attributes from client
 * @return resource response for answering request
 */
@Override
protected ResourceResponse newResourceResponse(Attributes attributes) {
    String relativePath = "";
    PageParameters parameters = attributes.getParameters();
    for (int i = 0; i < parameters.getIndexedCount(); i++) {
        relativePath += parameters.get(i);
        relativePath += '/';
    }
    if (relativePath.endsWith("/"))
        relativePath = relativePath.substring(0, relativePath.length() - 1);
    log.trace("relativePath is {}", relativePath);

    String absolutePath = new File(sourceDirectory, relativePath).getAbsolutePath();

    final ResourceResponse resourceResponse = new ResourceResponse();

    final IResourceStream resourceStream = getResourceStream(absolutePath);

    // bail out if resource stream could not be found
    if (resourceStream == null) {
        return sendResourceError(absolutePath, resourceResponse, HttpServletResponse.SC_NOT_FOUND,
                "Unable to find resource");
    }

    // allow caching
    resourceResponse.setCacheScope(WebResponse.CacheScope.PUBLIC);
    resourceResponse.setCacheDuration(cacheDuration);

    // add Last-Modified header (to support HEAD requests and If-Modified-Since)
    resourceResponse.setLastModified(resourceStream.lastModifiedTime());

    if (resourceResponse.dataNeedsToBeWritten(attributes)) {
        String contentType = resourceStream.getContentType();

        if (contentType == null && Application.exists())
            contentType = Application.get().getMimeType(absolutePath);

        // set Content-Type (may be null)
        resourceResponse.setContentType(contentType);

        try {
            // read resource data
            final byte[] bytes;

            bytes = IOUtils.toByteArray(resourceStream.getInputStream());

            // send Content-Length header
            resourceResponse.setContentLength(bytes.length);

            // send response body with resource data
            resourceResponse.setWriteCallback(new WriteCallback() {
                @Override
                public void writeData(Attributes attributes) {
                    attributes.getResponse().write(bytes);
                }
            });
        } catch (IOException e) {
            log.debug(e.getMessage(), e);
            return sendResourceError(absolutePath, resourceResponse, 500, "Unable to read resource stream");
        } catch (ResourceStreamNotFoundException e) {
            log.debug(e.getMessage(), e);
            return sendResourceError(absolutePath, resourceResponse, 500, "Unable to open resource stream");
        } finally {
            try {
                resourceStream.close();
            } catch (IOException e) {
                log.warn("Unable to close the resource stream", e);
            }
        }
    }

    return resourceResponse;
}