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:com.apache.ivy.BasicURLHandler.java

public URLInfo getURLInfo(URL url, int timeout) {
    // Install the IvyAuthenticator
    if ("http".equals(url.getProtocol()) || "https".equals(url.getProtocol())) {
        IvyAuthenticator.install();// w w  w.  j  a  v a2  s .  c  om
    }

    URLConnection con = null;
    try {
        url = normalizeToURL(url);
        con = url.openConnection();
        con.setRequestProperty("User-Agent", "Apache Ivy/1.0");//+ Ivy.getIvyVersion());
        if (con instanceof HttpURLConnection) {
            HttpURLConnection httpCon = (HttpURLConnection) con;
            if (getRequestMethod() == URLHandler.REQUEST_METHOD_HEAD) {
                httpCon.setRequestMethod("HEAD");
            }
            if (checkStatusCode(url, httpCon)) {
                String bodyCharset = getCharSetFromContentType(con.getContentType());
                return new URLInfo(true, httpCon.getContentLength(), con.getLastModified(), bodyCharset);
            }
        } else {
            int contentLength = con.getContentLength();
            if (contentLength <= 0) {
                return UNAVAILABLE;
            } else {
                // TODO: not HTTP... maybe we *don't* want to default to ISO-8559-1 here?
                String bodyCharset = getCharSetFromContentType(con.getContentType());
                return new URLInfo(true, contentLength, con.getLastModified(), bodyCharset);
            }
        }
    } catch (UnknownHostException e) {
        // Message.warn("Host " + e.getMessage() + " not found. url=" + url);
        // Message.info("You probably access the destination server through "
        //    + "a proxy server that is not well configured.");
    } catch (IOException e) {
        //Message.error("Server access error at url " + url, e);
    } finally {
        disconnect(con);
    }
    return UNAVAILABLE;
}

From source file:com.google.gwt.dev.shell.GWTShellServlet.java

/**
 * Fetch a file and return it as the HTTP response, setting the cache-related
 * headers according to the name of the file (see
 * {@link #getCacheTime(String)}). This function honors If-Modified-Since to
 * minimize the impact of limiting caching of files for development.
 * /*from  w ww  .  j av  a  2 s . com*/
 * @param request the HTTP request
 * @param response the HTTP response
 * @param logger a TreeLogger to use for debug output
 * @param partialPath the path within the module
 * @param moduleName the name of the module
 * @throws IOException
 */
@SuppressWarnings("deprecation")
private void doGetPublicFile(HttpServletRequest request, HttpServletResponse response, TreeLogger logger,
        String partialPath, String moduleName) throws IOException {

    // Create a logger branch for this request.
    logger = logger.branch(TreeLogger.TRACE, "The development shell servlet received a request for '"
            + partialPath + "' in module '" + moduleName + ".gwt.xml' ", null);

    // Handle auto-generation of resources.
    if (shouldAutoGenerateResources()) {
        if (autoGenerateResources(request, response, logger, partialPath, moduleName)) {
            return;
        }
    }

    URL foundResource = null;
    try {
        // Look for the requested file on the public path.
        //
        ModuleDef moduleDef = getModuleDef(logger, moduleName);
        if (shouldAutoGenerateResources()) {
            Resource publicResource = moduleDef.findPublicFile(partialPath);
            if (publicResource != null) {
                foundResource = publicResource.getURL();
            }

            if (foundResource == null) {
                // Look for public generated files
                File shellDir = getShellWorkDirs().getShellPublicGenDir(moduleDef);
                File requestedFile = new File(shellDir, partialPath);
                if (requestedFile.exists()) {
                    try {
                        foundResource = requestedFile.toURI().toURL();
                    } catch (MalformedURLException e) {
                        // ignore since it was speculative anyway
                    }
                }
            }
        }

        /*
         * If the user is coming from compiled web-mode, check the linker output
         * directory for the real bootstrap file.
         */
        if (foundResource == null) {
            File moduleDir = getShellWorkDirs().getCompilerOutputDir(moduleDef);
            File requestedFile = new File(moduleDir, partialPath);
            if (requestedFile.exists()) {
                try {
                    foundResource = requestedFile.toURI().toURL();
                } catch (MalformedURLException e) {
                    // ignore since it was speculative anyway
                }
            }
        }

        if (foundResource == null) {
            String msg;
            if ("gwt.js".equals(partialPath)) {
                msg = "Loading the old 'gwt.js' bootstrap script is no longer supported; please load '"
                        + moduleName + ".nocache.js' directly";
            } else {
                msg = "Resource not found: " + partialPath + "; "
                        + "(could a file be missing from the public path or a <servlet> "
                        + "tag misconfigured in module " + moduleName + ".gwt.xml ?)";
            }
            logger.log(TreeLogger.WARN, msg, null);
            throw new UnableToCompleteException();
        }
    } catch (UnableToCompleteException e) {
        sendErrorResponse(response, HttpServletResponse.SC_NOT_FOUND,
                "Cannot find resource '" + partialPath + "' in the public path of module '" + moduleName + "'");
        return;
    }

    // Get the MIME type.
    String path = foundResource.toExternalForm();
    String mimeType = null;
    try {
        mimeType = getServletContext().getMimeType(path);
    } catch (UnsupportedOperationException e) {
        // Certain minimalist servlet containers throw this.
        // Fall through to guess the type.
    }

    if (mimeType == null) {
        mimeType = guessMimeType(path);
        if (logger.isLoggable(TreeLogger.TRACE)) {
            logger.log(TreeLogger.TRACE, "Guessed MIME type '" + mimeType + "'", null);
        }
    }

    maybeIssueXhtmlWarning(logger, mimeType, partialPath);

    long cacheSeconds = getCacheTime(path);

    InputStream is = null;
    try {
        // Check for up-to-datedness.
        URLConnection conn = foundResource.openConnection();
        long lastModified = conn.getLastModified();
        if (isNotModified(request, lastModified)) {
            response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
            setResponseCacheHeaders(response, cacheSeconds);
            return;
        }

        // Set up headers to really send it.
        response.setStatus(HttpServletResponse.SC_OK);
        long now = new Date().getTime();
        response.setHeader(HttpHeaders.DATE, HttpHeaders.toInternetDateFormat(now));
        response.setContentType(mimeType);
        String lastModifiedStr = HttpHeaders.toInternetDateFormat(lastModified);
        response.setHeader(HttpHeaders.LAST_MODIFIED, lastModifiedStr);

        // Expiration header. Either immediately stale (requiring an
        // "If-Modified-Since") or infinitely cacheable (not requiring even a
        // freshness check).
        setResponseCacheHeaders(response, cacheSeconds);

        // Content length.
        int contentLength = conn.getContentLength();
        if (contentLength >= 0) {
            response.setHeader(HttpHeaders.CONTENT_LENGTH, Integer.toString(contentLength));
        }

        // Send the bytes.
        is = conn.getInputStream();
        streamOut(is, response.getOutputStream(), 1024 * 8);
    } finally {
        Utility.close(is);
    }
}

From source file:org.wso2.carbon.mediation.registry.ESBRegistry.java

public RegistryEntry getRegistryEntry(String key) {

    // get information from the actual resource
    MediationRegistryEntryImpl entryEmbedded = new MediationRegistryEntryImpl();

    try {/*w ww .j a v a  2  s.co  m*/
        URL url = new URL(getRoot() + key);
        if ("file".equals(url.getProtocol())) {
            try {
                url.openStream();
            } catch (IOException ignored) {
                if (!localRegistry.endsWith(URL_SEPARATOR)) {
                    localRegistry = localRegistry + URL_SEPARATOR;
                }
                url = new URL(url.getProtocol() + ":" + localRegistry + key);
                try {
                    url.openStream();
                } catch (IOException e) {
                    return null;
                }
            }
        }
        URLConnection urlc = url.openConnection();

        entryEmbedded.setKey(key);
        entryEmbedded.setName(url.getFile());
        entryEmbedded.setType(ESBRegistryConstants.FILE);

        entryEmbedded.setDescription("Resource at : " + url.toString());
        entryEmbedded.setLastModified(urlc.getLastModified());
        entryEmbedded.setVersion(urlc.getLastModified());
        if (urlc.getExpiration() > 0) {
            entryEmbedded.setCachableDuration(urlc.getExpiration() - System.currentTimeMillis());
        } else {
            entryEmbedded.setCachableDuration(getCachableDuration());
        }

    } catch (MalformedURLException e) {
        handleException("Invalid URL reference " + getRoot() + key, e);
    } catch (IOException e) {
        handleException("IO Error reading from URL " + getRoot() + key, e);
    }

    // get information from the database
    PersistenceManager persistenceManager = PersistenceManager.getInstance();
    RegistryEntryDO registryEntryDO = persistenceManager.getRegistryEntry(key);

    if (registryEntryDO != null) {

        if (registryEntryDO.getExpiryTime() != null) {
            entryEmbedded.setCachableDuration(registryEntryDO.getExpiryTime());
        } else {
            entryEmbedded.setCachableDuration(0);
        }
    }

    return entryEmbedded;
}

From source file:org.wso2.carbon.mediation.registry.MicroIntegratorRegistry.java

@Override
public RegistryEntry getRegistryEntry(String key) {

    // get information from the actual resource
    MediationRegistryEntryImpl entryEmbedded = new MediationRegistryEntryImpl();

    try {/*from  w w w  .jav a 2  s .co m*/
        URL url = new URL(resolveRegistryPath(key));
        if ("file".equals(url.getProtocol())) {
            try {
                url.openStream();
            } catch (IOException e) {
                log.error("Error occurred while accessing registry resource: " + key, e);
                return null;
            }
        }
        URLConnection urlc = url.openConnection();

        entryEmbedded.setKey(key);
        entryEmbedded.setName(url.getFile());
        entryEmbedded.setType(ESBRegistryConstants.FILE);
        entryEmbedded.setDescription("Resource at : " + url.toString());
        entryEmbedded.setLastModified(urlc.getLastModified());
        entryEmbedded.setVersion(urlc.getLastModified());

        if (urlc.getExpiration() > 0) {
            entryEmbedded.setCachableDuration(urlc.getExpiration() - System.currentTimeMillis());
        } else {
            entryEmbedded.setCachableDuration(getCachableDuration());
        }

    } catch (MalformedURLException e) {
        handleException("Invalid URL reference " + resolveRegistryPath(key), e);
    } catch (IOException e) {
        handleException("IO Error reading from URL " + resolveRegistryPath(key), e);
    }

    return entryEmbedded;
}

From source file:org.apache.felix.webconsole.AbstractWebConsolePlugin.java

/**
 * If the request addresses a resource which may be served by the
 * <code>getResource</code> method of the
 * {@link #getResourceProvider() resource provider}, this method serves it
 * and returns <code>true</code>. Otherwise <code>false</code> is returned.
 * <code>false</code> is also returned if the resource provider has no
 * <code>getResource</code> method.
 * <p>/*  w w  w  . j av  a 2 s  .  com*/
 * If <code>true</code> is returned, the request is considered complete and
 * request processing terminates. Otherwise request processing continues
 * with normal plugin rendering.
 *
 * @param request The request object
 * @param response The response object
 * @return <code>true</code> if the request causes a resource to be sent back.
 *
 * @throws IOException If an error occurs accessing or spooling the resource.
 */
private final boolean spoolResource(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    // no resource if no resource accessor
    Method getResourceMethod = getGetResourceMethod();
    if (getResourceMethod == null) {
        return false;
    }

    String pi = request.getPathInfo();
    InputStream ins = null;
    try {

        // check for a resource, fail if none
        URL url = (URL) getResourceMethod.invoke(getResourceProvider(), new Object[] { pi });
        if (url == null) {
            return false;
        }

        // open the connection and the stream (we use the stream to be able
        // to at least hint to close the connection because there is no
        // method to explicitly close the conneciton, unfortunately)
        URLConnection connection = url.openConnection();
        ins = connection.getInputStream();

        // FELIX-2017 Equinox may return an URL for a non-existing
        // resource but then (instead of throwing) return null on
        // getInputStream. We should account for this situation and
        // just assume a non-existing resource in this case.
        if (ins == null) {
            return false;
        }

        // check whether we may return 304/UNMODIFIED
        long lastModified = connection.getLastModified();
        if (lastModified > 0) {
            long ifModifiedSince = request.getDateHeader("If-Modified-Since");
            if (ifModifiedSince >= (lastModified / 1000 * 1000)) {
                // Round down to the nearest second for a proper compare
                // A ifModifiedSince of -1 will always be less
                response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);

                return true;
            }

            // have to send, so set the last modified header now
            response.setDateHeader("Last-Modified", lastModified);
        }

        // describe the contents
        response.setContentType(getServletContext().getMimeType(pi));
        response.setIntHeader("Content-Length", connection.getContentLength());

        // spool the actual contents
        OutputStream out = response.getOutputStream();
        byte[] buf = new byte[2048];
        int rd;
        while ((rd = ins.read(buf)) >= 0) {
            out.write(buf, 0, rd);
        }

        // over and out ...
        return true;
    } catch (IllegalAccessException iae) {
        // log or throw ???
    } catch (InvocationTargetException ite) {
        // log or throw ???
        // Throwable cause = ite.getTargetException();
    } finally {
        IOUtils.closeQuietly(ins);
    }

    return false;
}

From source file:org.clapper.curn.FeedDownloadThread.java

/**
 * Query the appropriate URL connection headers to determine whether
 * the remote server thinks feed data has changed since the last time
 * the feed was downloaded. Must be called on a <tt>URLConnection</tt>
 * after the <tt>InputStream</tt> is retrieved. Uses the feed cache to
 * set the value./*from   www  . jav  a2s. com*/
 *
 * @param conn     the <tt>URLConnection</tt> whose headers are to be
 *                 checked
 * @param feedInfo the information on the feed
 * @param cache    the cache
 *
 * @throws IOException I/O error
 */
private boolean feedHasChanged(final URLConnection conn, final FeedInfo feedInfo, final FeedCache cache)
        throws IOException {
    long lastSeen = 0;
    long lastModified = 0;
    boolean hasChanged = false;
    URL feedURL = feedInfo.getURL();

    if (cache != null) {
        FeedCacheEntry entry = cache.getEntryByURL(feedURL);

        if (entry != null)
            lastSeen = entry.getTimestamp();
    }

    if (lastSeen == 0) {
        log.debug("Feed \"" + feedURL.toString() + "\" has no recorded last-seen time.");
        hasChanged = true;
    }

    else if ((lastModified = conn.getLastModified()) == 0) {
        log.debug("Feed \"" + feedURL.toString() + "\" provides no last-modified time.");
        hasChanged = true;
    }

    else if (lastSeen >= lastModified) {
        log.debug("Feed \"" + feedURL.toString() + "\" has Last-Modified time of "
                + new Date(lastModified).toString() + ", which is not newer than last-seen time of "
                + new Date(lastSeen).toString() + ". Feed has no new data.");
    }

    else {
        log.debug("Feed \"" + feedURL.toString() + "\" has Last-Modified time of "
                + new Date(lastModified).toString() + ", which is newer than last-seen time of "
                + new Date(lastSeen).toString() + ". Feed might have new data.");
        hasChanged = true;
    }

    return hasChanged;
}

From source file:com.opoopress.maven.plugins.plugin.downloader.ProgressURLDownloader.java

private void downloadInternal(URL url, File destination) throws IOException {
    OutputStream out = null;/*  w ww .  j av a  2  s  .c o  m*/
    URLConnection conn;
    InputStream in = null;
    try {
        //URL url = address.toURL();
        conn = url.openConnection();

        //user agent
        final String userAgentValue = calculateUserAgent();
        conn.setRequestProperty("User-Agent", userAgentValue);

        //do not set gzip header if download zip file
        if (useGzip) {
            conn.setRequestProperty("Accept-Encoding", "gzip");
        }

        if (!useCache) {
            conn.setRequestProperty("Pragma", "no-cache");
        }

        in = conn.getInputStream();
        out = new BufferedOutputStream(new FileOutputStream(destination));

        copy(in, out);

        if (checkContentLength) {
            long contentLength = conn.getContentLengthLong();
            if (contentLength > 0 && contentLength != destination.length()) {
                throw new IllegalArgumentException("File length mismatch. expected: " + contentLength
                        + ", actual: " + destination.length());
            }
        }

        if (keepLastModified) {
            long lastModified = conn.getLastModified();
            if (lastModified > 0) {
                destination.setLastModified(lastModified);
            }
        }
    } finally {
        logMessage("");
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }
}

From source file:org.getobjects.appserver.publisher.GoResource.java

@Override
public void appendToResponse(final WOResponse _r, final WOContext _ctx) {
    URLConnection con = null;
    try {//from   w  ww.ja va 2 s  .com
        con = this.url.openConnection();
    } catch (IOException coe) {
        log.warn("could not open connection to url: " + this.url);
        _r.setStatus(WOMessage.HTTP_STATUS_NOT_FOUND);
        return;
    }

    /* open stream */

    InputStream is = null;
    try {
        is = con.getInputStream();
    } catch (IOException ioe) {
        log.warn("could not open stream to url: " + this.url);
        _r.setStatus(WOMessage.HTTP_STATUS_NOT_FOUND);
        return;
    }

    /* transfer */

    try {
        String mimeType = con.getContentType();
        if (mimeType == null || "content/unknown".equals(mimeType))
            mimeType = mimeTypeForPath(this.url.getPath());

        if (mimeType == null)
            mimeType = "application/octet-stream";

        _r.setHeaderForKey(mimeType, "content-type");
        _r.setHeaderForKey("" + con.getContentLength(), "content-length");

        /* setup caching headers */

        Date now = new Date();
        GregorianCalendar cal = new GregorianCalendar();

        cal.setTime(new Date(con.getLastModified()));
        _r.setHeaderForKey(WOMessage.httpFormatDate(cal), "last-modified");

        cal.setTime(now);
        _r.setHeaderForKey(WOMessage.httpFormatDate(cal), "date");

        cal.add(Calendar.SECOND, this.expirationIntervalForMimeType(mimeType));
        _r.setHeaderForKey(WOMessage.httpFormatDate(cal), "expires");

        /* start streaming */

        _r.enableStreaming();

        byte[] buffer = new byte[0xFFFF];
        for (int len; (len = is.read(buffer)) != -1;)
            _r.appendContentData(buffer, len);
    } catch (IOException e) {
        log.error("IO error trying to deliver resource: " + this.url, e);
        _r.setStatus(WOMessage.HTTP_STATUS_INTERNAL_ERROR);
    } finally {
        try {
            if (is != null)
                is.close();
        } catch (IOException e) {
            log.warn("could not close URL input stream: " + this.url, e);
        }
    }
}

From source file:org.getobjects.appserver.publisher.JoResource.java

@Override
public void appendToResponse(WOResponse _r, WOContext _ctx) {
    URLConnection con = null;
    try {/*from w w  w. j av  a 2  s.c  o  m*/
        con = this.url.openConnection();
    } catch (IOException coe) {
        log.warn("could not open connection to url: " + this.url);
        _r.setStatus(WOMessage.HTTP_STATUS_NOT_FOUND);
        return;
    }

    /* open stream */

    InputStream is = null;
    try {
        is = con.getInputStream();
    } catch (IOException ioe) {
        log.warn("could not open stream to url: " + this.url);
        _r.setStatus(WOMessage.HTTP_STATUS_NOT_FOUND);
        return;
    }

    /* transfer */

    try {
        String mimeType = con.getContentType();
        if (mimeType == null || "content/unknown".equals(mimeType))
            mimeType = mimeTypeForPath(this.url.getPath());

        if (mimeType == null)
            mimeType = "application/octet-stream";

        _r.setHeaderForKey(mimeType, "content-type");
        _r.setHeaderForKey("" + con.getContentLength(), "content-length");

        /* setup caching headers */

        Date now = new Date();
        GregorianCalendar cal = new GregorianCalendar();

        cal.setTime(new Date(con.getLastModified()));
        _r.setHeaderForKey(WOMessage.httpFormatDate(cal), "last-modified");

        cal.setTime(now);
        _r.setHeaderForKey(WOMessage.httpFormatDate(cal), "date");

        cal.add(Calendar.SECOND, this.expirationIntervalForMimeType(mimeType));
        _r.setHeaderForKey(WOMessage.httpFormatDate(cal), "expires");

        /* start streaming */

        _r.enableStreaming();

        byte[] buffer = new byte[0xFFFF];
        for (int len; (len = is.read(buffer)) != -1;)
            _r.appendContentData(buffer, len);
    } catch (IOException e) {
        log.error("IO error trying to deliver resource: " + this.url, e);
        _r.setStatus(WOMessage.HTTP_STATUS_INTERNAL_ERROR);
    } finally {
        try {
            if (is != null)
                is.close();
        } catch (IOException e) {
            log.warn("could not close URL input stream: " + this.url, e);
        }
    }
}

From source file:org.apache.myfaces.config.FacesConfigurator.java

private long getResourceLastModified(URLConnection connection) throws IOException {
    long modified;
    if (connection instanceof JarURLConnection) {
        // The following hack is required to work-around a JDK bug.
        // getLastModified() on a JAR entry URL delegates to the actual JAR file
        // rather than the JAR entry.
        // This opens internally, and does not close, an input stream to the JAR
        // file./* ww  w  . j ava  2 s.c o  m*/
        // In turn, you cannot close it by yourself, because it's internal.
        // The work-around is to get the modification date of the JAR file
        // manually,
        // and then close that connection again.

        URL jarFileUrl = ((JarURLConnection) connection).getJarFileURL();
        URLConnection jarFileConnection = jarFileUrl.openConnection();

        try {
            modified = jarFileConnection.getLastModified();
        } finally {
            try {
                jarFileConnection.getInputStream().close();
            } catch (Exception exception) {
                // Ignored
            }
        }
    } else {
        modified = connection.getLastModified();
    }

    return modified;
}