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:jmupen.JMupenUpdater.java

public static void checkForUpdates() {
    try {//w  w  w  .  jav a  2  s  .co  m
        jarFile = getJarFile();
    } catch (URISyntaxException ex) {
        System.err.println("Error getting jarfile path. " + ex.getLocalizedMessage());
        return;
    }
    try {
        if (jarFile != null) {
            // Get URL connection and lastModified time
            URL url = new URL(JarURL);
            URLConnection connection = url.openConnection();
            long localMod = jarFile.lastModified(), onlineMod = connection.getLastModified();
            updatePackage = new File(
                    JMupenUtils.getConfigDir().concat(JMupenUtils.getBar()).concat("jmupen-update.jar"));

            if (updatePackage.exists()) {
                JMupenUpdater.setUpdateAvailable(true);
                JMupenGUI.getInstance().showUpdateDialog();
                return;
            }
            if (localMod >= onlineMod - tenmin) {
                System.out.println("No update available at " + JarURL + '(' + localMod + '>' + onlineMod + ')');
                JMupenUpdater.setUpdateAvailable(false);
                return;
            } else {
                JMupenUpdater.setUpdateAvailable(true);
            }

            System.out.println("Loading update from " + JarURL);
            byte bytes[] = getBytes(connection);
            System.out.println("Update loaded");
            writeBytes(updatePackage, bytes);
            System.out.println("Update saved: " + updatePackage.getAbsolutePath());

            jarFile.setLastModified(onlineMod);
            JMupenGUI.getInstance().showUpdateDialog();
        }
    } catch (Exception e) {
        System.err.println("Error updating JMupen. " + e.getLocalizedMessage());
    }
}

From source file:GetURLInfo.java

/** Use the URLConnection class to get info about the URL */
public static void printinfo(URL url) throws IOException {
    URLConnection c = url.openConnection(); // Get URLConnection from URL
    c.connect(); // Open a connection to URL

    // Display some information about the URL contents
    System.out.println("  Content Type: " + c.getContentType());
    System.out.println("  Content Encoding: " + c.getContentEncoding());
    System.out.println("  Content Length: " + c.getContentLength());
    System.out.println("  Date: " + new Date(c.getDate()));
    System.out.println("  Last Modified: " + new Date(c.getLastModified()));
    System.out.println("  Expiration: " + new Date(c.getExpiration()));

    // If it is an HTTP connection, display some additional information.
    if (c instanceof HttpURLConnection) {
        HttpURLConnection h = (HttpURLConnection) c;
        System.out.println("  Request Method: " + h.getRequestMethod());
        System.out.println("  Response Message: " + h.getResponseMessage());
        System.out.println("  Response Code: " + h.getResponseCode());
    }/*from  w  w w.  j a  va2s  .  c  o  m*/
}

From source file:org.docx4j.fonts.fop.fonts.FontCache.java

/**
 * Retrieve the last modified date/time of a URL.
 * @param url the URL//from   w w  w  .  jav a 2s. c om
 * @return the last modified date/time
 */
public static long getLastModified(URL url) {
    try {
        URLConnection conn = url.openConnection();
        try {
            return conn.getLastModified();
        } finally {
            //An InputStream is created even if it's not accessed, but we need to close it.
            IOUtils.closeQuietly(conn.getInputStream());
        }
    } catch (IOException e) {
        // Should never happen, because URL must be local
        log.debug("IOError: " + e.getMessage());
        return 0;
    }
}

From source file:org.apache.fop.fonts.FontCache.java

/**
 * Retrieve the last modified date/time of a URL.
 *
 * @param url//w w w .j  av a  2 s.c  o  m
 *            the URL
 * @return the last modified date/time
 */
public static long getLastModified(URL url) {
    try {
        URLConnection conn = url.openConnection();
        try {
            return conn.getLastModified();
        } finally {
            // An InputStream is created even if it's not accessed, but we
            // need to close it.
            IOUtils.closeQuietly(conn.getInputStream());
        }
    } catch (IOException e) {
        // Should never happen, because URL must be local
        log.debug("IOError: " + e.getMessage());
        return 0;
    }
}

From source file:org.jenkins_ci.update_center.repo.NexusRepositoryImpl.java

/**
 * Loads a remote repository index (.zip or .gz), convert it to Lucene index and return it.
 *//* www  . j a v a2  s.co m*/
private static File loadIndex(String id, URL url) throws IOException, UnsupportedExistingLuceneIndexException {
    File dir = new File(new File(System.getProperty("java.io.tmpdir")), "maven-index/" + id);
    File local = new File(dir, "index" + getExtension(url));
    File expanded = new File(dir, "expanded");

    URLConnection con = url.openConnection();
    if (url.getUserInfo() != null) {
        con.setRequestProperty("Authorization",
                "Basic " + new sun.misc.BASE64Encoder().encode(url.getUserInfo().getBytes()));
    }

    if (!expanded.exists() || !local.exists() || local.lastModified() != con.getLastModified()) {
        System.out.println("Downloading " + url);
        // if the download fail in the middle, only leave a broken tmp file
        dir.mkdirs();
        File tmp = new File(dir, "index_" + getExtension(url));
        FileOutputStream o = new FileOutputStream(tmp);
        try {
            IOUtils.copy(con.getInputStream(), o);
        } finally {
            o.close();
        }

        if (expanded.exists()) {
            FileUtils.deleteDirectory(expanded);
        }
        expanded.mkdirs();

        if (url.toExternalForm().endsWith(".gz")) {
            FSDirectory directory = FSDirectory.getDirectory(expanded);
            NexusIndexWriter w = new NexusIndexWriter(directory, new NexusAnalyzer(), true);
            FileInputStream in = new FileInputStream(tmp);
            try {
                IndexDataReader dr = new IndexDataReader(in);
                dr.readIndex(w, new DefaultIndexingContext(id, id, null, expanded, null, null,
                        NexusIndexer.DEFAULT_INDEX, true));
            } finally {
                IndexUtils.close(w);
                IOUtils.closeQuietly(in);
                directory.close();
            }
        } else if (url.toExternalForm().endsWith(".zip")) {
            Expand e = new Expand();
            e.setSrc(tmp);
            e.setDest(expanded);
            e.execute();
        } else {
            throw new UnsupportedOperationException("Unsupported index format: " + url);
        }

        // as a proof that the expansion was properly completed
        tmp.renameTo(local);
        local.setLastModified(con.getLastModified());
    } else {
        System.out.println("Reusing the locally cached " + url + " at " + local);
    }

    return expanded;
}

From source file:org.jahia.bin.Jahia.java

public static String getBuildDate() {
    if (buildDate == null) {
        synchronized (Jahia.class) {
            if (buildDate == null) {
                try {
                    URL urlToVersionMarker = Jahia.class.getResource("/META-INF/jahia-impl-marker.txt");
                    if (urlToVersionMarker != null) {
                        URLConnection conn = urlToVersionMarker.openConnection();
                        long lastModified = (conn instanceof JarURLConnection)
                                ? ((JarURLConnection) conn).getJarEntry().getTime()
                                : conn.getLastModified();
                        buildDate = DateFormat
                                .getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.ENGLISH)
                                .format(new Date(lastModified));
                    } else {
                        buildDate = "";
                    }//  w w w.  j a  va  2  s  .  c o m
                } catch (IOException ioe) {
                    logger.error(ioe.getMessage(), ioe);
                    buildDate = "";
                }
            }
        }
    }
    return buildDate;
}

From source file:org.orbeon.oxf.util.NetUtils.java

/**
 * Get the last modification date of an open URLConnection.
 *
 * This handles the (broken at some point in the Java libraries) case of the file: protocol.
 *
 * @return last modified timestamp "as is"
 *//*  ww w. j  a  v a 2 s  . c  o  m*/
public static long getLastModified(URLConnection urlConnection) {
    try {
        long lastModified = urlConnection.getLastModified();
        if (lastModified == 0 && "file".equals(urlConnection.getURL().getProtocol()))
            lastModified = new File(
                    URLDecoder.decode(urlConnection.getURL().getFile(), STANDARD_PARAMETER_ENCODING))
                            .lastModified();
        return lastModified;
    } catch (UnsupportedEncodingException e) {
        // Should not happen as we are using a required encoding
        throw new OXFException(e);
    }
}

From source file:org.geoserver.wfs.notification.AbstractFileWatcher.java

protected boolean shouldUpdate(URLConnection conn) {
    return conn.getLastModified() != lastModTime || conn.getLastModified() == 0;
}

From source file:org.eclipse.ecr.core.event.script.URLScript.java

@Override
public Reader getReaderIfModified() throws IOException {
    URLConnection conn = url.openConnection();
    long tm = conn.getLastModified();
    if (tm > lastModified) {
        synchronized (this) {
            if (tm > lastModified) {
                lastModified = tm;/*  www .  ja v  a  2 s  . c om*/
                return new InputStreamReader(conn.getInputStream());
            }
        }
    }
    return null;
}

From source file:org.java.plugin.standard.ShadingPathResolver.java

static Date getLastModified(final URL url) throws IOException {
    long result = 0;
    if ("jar".equalsIgnoreCase(url.getProtocol())) { //$NON-NLS-1$
        String urlStr = url.toExternalForm();
        int p = urlStr.indexOf("!/"); //$NON-NLS-1$
        if (p != -1) {
            //sourceFile = IoUtil.url2file(new URL(urlStr.substring(4, p)));
            return getLastModified(new URL(urlStr.substring(4, p)));
        }//w  w  w .  j ava  2  s . c  om
    }
    File sourceFile = IoUtil.url2file(url);
    if (sourceFile != null) {
        result = sourceFile.lastModified();
    } else {
        URLConnection cnn = url.openConnection();
        try {
            cnn.setUseCaches(false);
            cnn.setDoInput(false); // this should force using HTTP HEAD method
            result = cnn.getLastModified();
        } finally {
            try {
                cnn.getInputStream().close();
            } catch (IOException ioe) {
                // ignore
            }
        }
    }
    if (result == 0) {
        throw new IOException("can't retrieve modification date for resource " //$NON-NLS-1$
                + url);
    }
    // for some reason modification milliseconds for some files are unstable
    Calendar cldr = Calendar.getInstance(Locale.ENGLISH);
    cldr.setTime(new Date(result));
    cldr.set(Calendar.MILLISECOND, 0);
    return cldr.getTime();
}