Example usage for java.net URLConnection getLastModified

List of usage examples for java.net URLConnection getLastModified

Introduction

In this page you can find the example usage for java.net URLConnection getLastModified.

Prototype

public long getLastModified() 

Source Link

Document

Returns the value of the last-modified header field.

Usage

From source file:org.roosster.input.UrlFetcher.java

/**
 * URLs will be fetched a second time, if the entry's lastFetched
 * object is <code>null</code>, when processed the first time.
 *//*from  w w  w  .  j a va 2  s  .  c  om*/
private Entry[] fetch(URL url) throws IOException, Exception {
    LOG.debug("Opening connection to URL " + url);

    URLConnection con = url.openConnection();

    long modified = con.getLastModified();

    String embeddedContentEnc = null;

    String contentType = con.getContentType();
    if (contentType != null && contentType.indexOf(";") > -1) {
        LOG.debug("Content-type string (" + contentType + ") contains charset; strip it!");
        contentType = contentType.substring(0, contentType.indexOf(";")).trim();

        String cType = con.getContentType();
        if (cType.indexOf("=") > -1) {
            embeddedContentEnc = cType.substring(cType.indexOf("=") + 1).trim();
        }
    }

    String contentEnc = con.getContentEncoding();
    if (contentEnc == null) {
        if (embeddedContentEnc != null)
            contentEnc = embeddedContentEnc;
        else
            contentEnc = defaultEncoding;
    }

    ContentTypeProcessor proc = getProcessor(contentType);
    LOG.debug("ContentType: '" + contentType + "' - ContentEncoding: '" + contentEnc + "'");
    LOG.debug("Using Processor " + proc);

    Entry[] entries = proc.process(url, con.getInputStream(), contentEnc);

    Date modDate = new Date(modified);
    Date now = new Date();

    List returnArr = new ArrayList();
    for (int i = 0; i < entries.length; i++) {
        if (entries[i] == null)
            continue;

        URL entryUrl = entries[i].getUrl();

        String title = entries[i].getTitle();
        if (title == null || "".equals(title))
            entries[i].setTitle(entryUrl.toString());

        if (entries[i].getModified() == null)
            entries[i].setModified(modDate);

        if (entries[i].getIssued() == null)
            entries[i].setIssued(modDate);

        if (entries[i].getAdded() == null)
            entries[i].setAdded(now);

        String fileType = entries[i].getFileType();
        if (fileType == null || "".equals(fileType)) {

            int dotIndex = entryUrl.getPath().lastIndexOf(".");
            if (dotIndex != -1) {
                String type = entryUrl.getPath().substring(dotIndex + 1);
                entries[i].setFileType(type.toLowerCase());
                LOG.debug("Filetype is subsequently set to '" + type + "'");
            }

        }

        returnArr.add(entries[i]);
        entries[i] = null;
    }

    return (Entry[]) returnArr.toArray(new Entry[0]);
}

From source file:org.springframework.extensions.webscripts.servlet.mvc.ResourceController.java

/**
 * Commit the resource to the response stream.
 * Sets appropriate date, length and content type headers.
 * /*from w  w w .j a v  a2s.c om*/
 * @throws IOException
 */
public void commitResponse(final String path, final URL resourceUrl, final HttpServletResponse response)
        throws IOException {
    // determine properties of the resource being served back
    final URLConnection resourceConn = resourceUrl.openConnection();
    applyHeaders(path, response, resourceConn.getContentLength(), resourceConn.getLastModified());

    // stream back to response
    copyStream(resourceUrl.openStream(), response.getOutputStream());
}

From source file:org.apache.tiles.definition.dao.BaseLocaleUrlDefinitionDAO.java

/**
 * Loads definitions from an URL without loading from "parent" URLs.
 *
 * @param url The URL to read.//from w w w . j  a  v a 2s.  com
 * @return The definition map that has been read.
 */
protected Map<String, Definition> loadDefinitionsFromURL(URL url) {
    Map<String, Definition> defsMap = null;
    try {
        URLConnection connection = url.openConnection();
        connection.connect();
        lastModifiedDates.put(url.toExternalForm(), connection.getLastModified());

        // Definition must be collected, starting from the base
        // source up to the last localized file.
        defsMap = reader.read(connection.getInputStream());
    } catch (FileNotFoundException e) {
        // File not found. continue.
        if (log.isDebugEnabled()) {
            log.debug("File " + null + " not found, continue");
        }
    } catch (IOException e) {
        throw new DefinitionsFactoryException("I/O error processing configuration.", e);
    }

    return defsMap;
}

From source file:org.springframework.extensions.webscripts.servlet.mvc.ResourceController.java

public void commitResponse(final String path, final Resource resource, final HttpServletRequest request,
        final HttpServletResponse response) throws IOException, ServletException {
    // determine properties of the resource being served back
    final URLConnection resourceConn = resource.getURL().openConnection();
    applyHeaders(path, response, resourceConn.getContentLength(), resourceConn.getLastModified());

    // stream back to response
    RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/" + path);
    rd.include(request, response);//w ww .  jav a 2s .c  o  m
}

From source file:net.paissad.waqtsalat.utils.DownloadHelper.java

/**
 * <p>//from   ww w .j  a v  a 2  s .c o m
 * Download a resource from the specified url and save it to the specified
 * file.
 * </p>
 * <b>Note</b>: If you plan to cancel the download at any time, then this
 * method should be embed into it's own thread.
 * <p>
 * <b>Example</b>:
 * 
 * <pre>
 *  final DownloadHelper downloader = new DownloadHelper();
 *  Thread t = new Thread() {
 *      public void run() {
 *          try {
 *              downloader.download("http://domain.com/file.zip", new File("/tmp/output.zip"));
 *          } catch (Exception e) {
 *              ...
 *          }
 *      }
 *  };
 *  t.start();
 *  // Waits 3 seconds and then cancels the download.
 *  Thread.sleep(3 * 1000L);
 *  downloader.cancel();
 *  ...
 * </pre>
 * 
 * </p>
 * 
 * @param url
 *            - The url of the file to download.
 * @param file
 *            - The downloaded file (where it will be stored).
 * @throws IOException
 * @throws HttpException
 */
public void download(final String url, final File file) throws HttpException, IOException {

    GetMethod method = null;
    InputStream responseBody = null;
    OutputStream out = null;

    try {
        final boolean fileExisted = file.exists();

        HttpClient client = new HttpClient();
        method = new GetMethod(url);
        method.setFollowRedirects(true);
        method.setRequestHeader("User-Agent", WSConstants.WS_USER_AGENT);
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(3, false));
        method.getParams().setParameter(HttpMethodParams.WARN_EXTRA_INPUT, Boolean.TRUE);

        // Execute the method.
        int responseCode = client.executeMethod(method);
        if (responseCode != HttpStatus.SC_OK) {
            logger.error("Http method failed : {}.", method.getStatusLine().toString());
        }

        // Read the response body.
        responseBody = method.getResponseBodyAsStream();

        // Let's try to compute the amount of total bytes of the file to
        // download.
        URL u = new URL(url);
        URLConnection urlc = u.openConnection();
        this.totalBytes = urlc.getContentLength();
        long lastModified = urlc.getLastModified();

        // The OutputStream for the 'downloaded' file.
        out = new BufferedOutputStream(new FileOutputStream(file));

        this.updateState(DownloadState.IN_PROGRESS);

        byte[] data = new byte[BUFFER_SIZE];
        int length;
        while ((length = responseBody.read(data, 0, BUFFER_SIZE)) != -1 && !isCancelled) {
            out.write(data, 0, length);
            this.bytesDownloaded += length;
            setChanged();
            notifyObservers(getBytesDownloaded());
        }

        if (isCancelled) {
            this.updateState(DownloadState.CANCELED);
            logger.info("The download has been cancelled.");

        } else {
            // The download was not cancelled.
            out.flush();
            if (lastModified > 0) {
                file.setLastModified(lastModified);
            }

            if (bytesDownloaded != totalBytes) {
                logger.warn("The expected bytes to download is {}, but got {}.", getTotalBytes(),
                        getBytesDownloaded());
                this.updateState(DownloadState.ERROR);
            }

            this.updateState(DownloadState.FINISHED);
            logger.info("Download of '{}' finished successfully.", url);
        }

        // If the file did not exist before the download but does exit now,
        // we must remove it if an error occurred or if the download was
        // cancelled.
        if (getState() == DownloadState.CANCELED || getState() == DownloadState.ERROR) {
            if (!fileExisted && file.exists()) {
                FileUtils.forceDelete(file);
            }
        }

    } catch (HttpException he) {
        this.updateState(DownloadState.ERROR);
        logger.error("Fatal protocol violation : " + he);
        throw new HttpException("Error while downloading from the url '" + url + "'", he);

    } catch (IOException ioe) {
        this.updateState(DownloadState.ERROR);
        logger.error("Fatal transport error : " + ioe);
        throw new IOException(ioe);

    } finally {
        if (method != null)
            method.releaseConnection();
        if (responseBody != null)
            responseBody.close();
        if (out != null)
            out.close();
    }
}

From source file:gov.nih.nci.ncicb.tcga.dcc.common.web.StaticContentServlet.java

protected long getLastModified(HttpServletRequest request) {
    if (log.isDebugEnabled()) {
        log.debug("Checking last modified of resource: " + request.getServletPath() + request.getPathInfo());
    }//from   w ww.  j av a  2s  .  co m
    URL[] resources;
    try {
        resources = getRequestResourceURLs(request);
    } catch (MalformedURLException e) {
        return -1;
    }
    if (resources == null || resources.length == 0) {
        return -1;
    }
    long lastModified = -1;
    for (int i = 0; i < resources.length; i++) {
        URLConnection resourceConn;
        try {
            resourceConn = resources[i].openConnection();
        } catch (IOException e) {
            return -1;
        }
        if (resourceConn.getLastModified() > lastModified) {
            lastModified = resourceConn.getLastModified();
        }
    }
    return lastModified;
}

From source file:org.squale.welcom.struts.webServer.URLManager.java

/**
 * Retourn l'url a partie d'un site distant
 * /*from  w  w  w. j a  va 2  s  .c o  m*/
 * @param pUrl url
 * @throws MalformedURLException problem sur l'url
 * @throws IOException exception
 * @return webfile
 */
private WebFile getWebFileFromHTTP(String pUrl) throws MalformedURLException, IOException {
    WebFile webFile = new WebFile(WebFile.TYPE_DISTANT);
    webFile.setUrl(new URL(pUrl));
    final URLConnection urlcon = webFile.getUrl().openConnection();
    urlcon.setUseCaches(true);
    urlcon.connect();
    webFile.setLastDate(new Date(urlcon.getLastModified()));

    if (urlcon.getLastModified() == 0) {
        webFile.setLastDate(new Date(urlcon.getDate()));
    }

    return webFile;
}

From source file:org.theospi.portfolio.presentation.export.StreamedPage.java

public InputStream getStream() throws IOException {
    URLConnection conn = Access.getAccess().openConnection(link);

    // fetch and store final redirected URL and response headers
    InputStream returned = conn.getInputStream();

    this.setContentEncoding(conn.getContentEncoding());
    this.setContentType(conn.getContentType());
    this.setExpiration(conn.getExpiration());
    this.setLastModified(conn.getLastModified());

    return returned;
}

From source file:wicket.util.resource.UrlResourceStream.java

/**
 * @see wicket.util.watch.IModifiable#lastModifiedTime()
 * @return The last time this resource was modified
 */// w  ww .  jav a  2  s . co  m
public Time lastModifiedTime() {
    if (file != null) {
        long lastModified = file.lastModified();
        if (lastModified != this.lastModified) {
            this.lastModified = lastModified;
            this.contentLength = (int) file.length();
        }
    } else {
        URLConnection urlConnection = null;
        try {
            urlConnection = url.openConnection();

            // update the last modified time.
            long lastModified = urlConnection.getLastModified();
            if (lastModified != this.lastModified) {
                this.lastModified = lastModified;
                this.contentLength = urlConnection.getContentLength();
            }
        } catch (IOException e) {
            log.error("getLastModified for " + url + " failed: " + e.getMessage());
        } finally {
            // if applicable, disconnect
            if (urlConnection != null) {
                if (urlConnection instanceof HttpURLConnection) {
                    ((HttpURLConnection) urlConnection).disconnect();
                } else {
                    try {
                        urlConnection.getInputStream().close();
                    } catch (Exception ex) {
                        // ignore
                    }
                }
            }
        }
    }
    return Time.milliseconds(lastModified);
}

From source file:com.ibm.jaggr.core.impl.cache.GzipCacheImpl.java

@Override
public InputStream getInputStream(final String key, final URI source, final MutableInt retLength)
        throws IOException {
    final String sourceMethod = "getInputStream"; //$NON-NLS-1$
    final boolean isTraceLogging = log.isLoggable(Level.FINER);
    if (isTraceLogging) {
        log.entering(sourceClass, sourceMethod, new Object[] { key, source, retLength });
    }/*from w  ww  .jav  a 2 s. c om*/

    InputStream in = null, result = null;
    CacheEntry tryCacheEntry = (CacheEntry) super.get(key);
    URLConnection connection = source.toURL().openConnection();

    try {
        long lastModified = connection.getLastModified();
        if (tryCacheEntry != null) {
            // Make local copies of volatile CacheEntry fields
            byte[] bytes = tryCacheEntry.bytes;
            File file = tryCacheEntry.file;
            if (bytes != null) {
                // Important - CacheEntry.lastModified is set before CacheEntry.bytes so we can
                // safely
                // check CacheEntry.lastModified here even though we're not synchronized.
                if (lastModified != tryCacheEntry.lastModified) {
                    // stale cache entry. Remove it and create a new one below
                    cacheMap.remove(key, tryCacheEntry);
                } else {
                    retLength.setValue(tryCacheEntry.bytes.length);
                    result = new ByteArrayInputStream(tryCacheEntry.bytes);
                }
            } else if (file != null) {
                // Some platforms round file last modified times to nearest second.
                if (Math.abs(lastModified - file.lastModified()) > 1000) {
                    // Stale cache entry, remove it and create a new one below
                    cacheMap.remove(key, tryCacheEntry);
                    // also delete the associated cache file asynchronously.
                    cacheManager.deleteFileDelayed(file.getName());
                } else {
                    try {
                        retLength.setValue(file.length());
                        result = new FileInputStream(file);
                    } catch (FileNotFoundException ex) {
                        // File doesn't exist (was probably deleted outside this program)
                        // Not fatal, just fall through and create it again.
                        cacheMap.remove(key, tryCacheEntry);
                    }
                }
            }
            if (result != null) {
                // found result in cache. Return it.
                log.exiting(sourceClass, sourceMethod, result);
                return result;
            }
        }

        // Result not in cache (or we removed it). Try to create a new cache entry.
        CacheEntry newCacheEntry = new CacheEntry();
        CacheEntry oldCacheEntry = (CacheEntry) cacheMap.putIfAbsent(key, newCacheEntry);
        final CacheEntry cacheEntry = oldCacheEntry != null ? oldCacheEntry : newCacheEntry;

        // Synchronize on the cache entry so that more than one thread won't try to create the
        // zipped content.
        synchronized (cacheEntry) {
            if (cacheEntry.ex != null) {
                // An exception occurred trying to create the gzip response in another thread.
                // Re-throw the exception here.
                throw cacheEntry.ex;
            }
            // First, check to make sure that another thread didn't beat us to the punch.
            // Even though we're synchronized on the cacheEntry object, cacheEntry.bytes can be
            // cleared by the createCacheFileAsync callback, so we need to copy this volatile
            // field
            // to a local variable and access it from there.
            byte[] bytes = cacheEntry.bytes;
            if (bytes != null) {
                retLength.setValue(bytes.length);
                result = new ByteArrayInputStream(bytes);
            } else if (cacheEntry.file != null) { // once set, cacheEntry.file does not change
                // by convention
                retLength.setValue(cacheEntry.file.length());
                result = new FileInputStream(cacheEntry.file);
            } else {
                // Gzip encode the resource and save the result in the cache entry until the
                // cache
                // file is written asynchronously.
                try {
                    in = connection.getInputStream();
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    VariableGZIPOutputStream compress = new VariableGZIPOutputStream(bos, 10240);
                    compress.setLevel(Deflater.BEST_COMPRESSION);
                    CopyUtil.copy(in, compress);

                    // Important - CacheEntry.lastModified must be set before cacheEntry.bytes
                    cacheEntry.lastModified = lastModified;
                    cacheEntry.bytes = bos.toByteArray();
                    result = new ByteArrayInputStream(cacheEntry.bytes);
                    retLength.setValue(cacheEntry.bytes.length);

                    // Call the cache manager to asynchronously save the gzipped response to
                    // disk
                    // Include the filename part of the source URI in the cached filename
                    String path = source.getPath();
                    int idx = path.lastIndexOf("/"); //$NON-NLS-1$
                    String fname = (idx != -1) ? path.substring(idx + 1) : path;
                    cacheManager.createCacheFileAsync(fname + ".gzip.", //$NON-NLS-1$
                            new ByteArrayInputStream(cacheEntry.bytes),
                            new ICacheManager.CreateCompletionCallback() {
                                @Override
                                public void completed(String filename, Exception e) {
                                    if (e != null && log.isLoggable(Level.SEVERE)) {
                                        // Exception occurred saving file. Not much we can do
                                        // except log the error
                                        log.logp(Level.SEVERE, sourceClass, sourceMethod, e.getMessage(), e);
                                        return;
                                    }
                                    File cacheFile = new File(cacheManager.getCacheDir(), filename);
                                    cacheFile.setLastModified(cacheEntry.lastModified);
                                    // Important - cacheEntry.file must be set before clearing
                                    // cacheEntry.bytes
                                    cacheEntry.file = cacheFile;
                                    cacheEntry.bytes = null;
                                }
                            });
                } catch (Throwable t) {
                    cacheEntry.ex = (t instanceof IOException) ? (IOException) t : new IOException(t);
                    cacheMap.remove(key, cacheEntry);
                    throw cacheEntry.ex;
                }
            }
        }
    } finally {
        // URLConnection doesn't have a close method. The only way to make sure a connection is
        // closed is to close the input or output stream which is obtained from the connection.
        if (in != null) {
            in.close();
        } else {
            connection.getInputStream().close();
        }
    }
    if (isTraceLogging) {
        log.exiting(sourceClass, sourceClass, result);
    }
    return result;
}