Example usage for java.util.zip ZipEntry getLastModifiedTime

List of usage examples for java.util.zip ZipEntry getLastModifiedTime

Introduction

In this page you can find the example usage for java.util.zip ZipEntry getLastModifiedTime.

Prototype

public FileTime getLastModifiedTime() 

Source Link

Document

Returns the last modification time of the entry.

Usage

From source file:com.ikanow.aleph2.analytics.storm.utils.TestStormControllerUtil_Cache.java

private long getFileModifiedTime(File input_jar) throws IOException {
    ZipInputStream inputZip = new ZipInputStream(new FileInputStream(input_jar));
    ZipEntry e = inputZip.getNextEntry();
    long time = e.getLastModifiedTime().toMillis();
    inputZip.close();//from  w w  w. j  a  v a 2s .  c  o  m
    return time;
}

From source file:org.sakaiproject.portal.charon.handlers.StaticHandler.java

/**
 * serve a registered static file/* w  w  w .  jav a  2  s.c  o m*/
 * 
 * @param req
 * @param res
 * @param parts
 * @throws IOException
 */
public void doStatic(HttpServletRequest req, HttpServletResponse res, String[] parts) throws IOException {
    try {
        StaticCache[] staticCache = staticCacheHolder.get();
        if (staticCache == null) {
            staticCache = new StaticCache[100];
            staticCacheHolder.set(staticCache);
        }
        String path = URLUtils.getSafePathInfo(req);
        if (path.indexOf("..") >= 0) {
            res.sendError(404);
            return;
        }
        InputStream inputStream;
        String filename = path.substring(path.lastIndexOf("/"));
        long lastModified = -1;
        long length = -1;
        String realPath = servletContext.getRealPath(path);
        if (realPath == null) {
            // We not uncompressing the webapps.
            URL url = servletContext.getResource(path);
            inputStream = url.openStream();
            if (url != null) {
                try {
                    ZipEntry zipEntry = ((JarURLConnection) url.openConnection()).getJarEntry();
                    lastModified = zipEntry.getLastModifiedTime().toMillis();
                    length = zipEntry.getSize();
                } catch (ClassCastException cce) {
                    // Can't get extra data, but should all work.
                    log.debug("We don't seem to be a JAR either.", cce);
                }
            } else {
                res.sendError(404);
                return;
            }
        } else {
            File f = new File(realPath);
            inputStream = new FileInputStream(f);
            lastModified = f.lastModified();
            length = f.length();
        }
        if (length >= 0 && length < MAX_SIZE_KB * 1024) {
            for (int i = 0; i < staticCache.length; i++) {
                StaticCache sc = staticCache[i];
                if (sc != null && path.equals(sc.path)) {
                    // If we don't have a good last modified time it's cached forever
                    if (lastModified > sc.lastModified) {
                        sc.buffer = loadFileBuffer(inputStream, (int) length);
                        sc.path = path;
                        sc.lastModified = lastModified;
                        sc.contenttype = getContentType(filename);
                        sc.added = System.currentTimeMillis();
                    }
                    // send the output
                    sendContent(res, sc);
                    return;
                }
            }
            // not found in cache, find the oldest or null and evict
            // this is thread Safe, since the cache is per thread.
            StaticCache sc = null;
            for (int i = 1; i < staticCache.length; i++) {
                StaticCache current = staticCache[i];
                if (sc == null) {
                    sc = current;
                }
                if (current == null) {
                    sc = new StaticCache();
                    staticCache[i] = sc;
                    break;
                }
                if (sc.added < current.added) {
                    sc = current;
                }
            }
            sc.buffer = loadFileBuffer(inputStream, (int) length);
            sc.path = path;
            sc.lastModified = lastModified;
            sc.contenttype = getContentType(filename);
            sc.added = System.currentTimeMillis();
            sendContent(res, sc);
            return;

        } else {
            res.setContentType(getContentType(filename));
            res.addDateHeader("Last-Modified", lastModified);
            res.setContentLength((int) length);
            sendContent(res, inputStream);
            return;
        }

    } catch (IOException ex) {
        log.info("Failed to send portal content");
        if (log.isDebugEnabled()) {
            log.debug("Full detail of exception is:", ex);
        }
        res.sendError(404, URLUtils.getSafePathInfo(req));
    }

}