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.jab.docsearch.utils.NetUtils.java

/**
 * Get SpiderUrl status//from w w  w  .  ja v  a 2s .  c  o m
 *
 * Content type, content length, last modified and md5 will be set in SpiderUrl if url is changed
 *
 * @param spiderUrl  URL to check
 * @param file       URL content downloads to to this file
 * @return           -1 if link is broken, 0 if the file is unchanged or 1 if the file
 *                   is different... part of caching algoritm.
 */
public int getURLStatus(final SpiderUrl spiderUrl, final String file) {
    // -1 means broken link
    //  0 means same file
    //  1 means changed

    int status;

    try {
        // attempt to obtain status from date
        URL url = new URL(spiderUrl.getUrl());
        URLConnection conn = url.openConnection();

        // set connection parameter
        conn.setDoInput(true);
        conn.setDoOutput(false);
        conn.setUseCaches(false);
        conn.setRequestProperty("User-Agent", USER_AGENT);

        // connect
        conn.connect();

        // content type
        spiderUrl.setContentType(getContentType(conn));

        // check date
        long spiDate = spiderUrl.getLastModified();
        long urlDate = conn.getLastModified();

        // same if date is equal and not zero
        if (spiDate == urlDate && urlDate != 0) {
            // the file is not changed
            status = 0;
        } else {
            // download the URL and compare hashes
            boolean downloaded = downloadURLToFile(conn, file);
            if (downloaded) {
                // download ok

                // compare file hashes
                String fileHash = FileUtils.getMD5Sum(file);

                if (fileHash.equals(spiderUrl.getMd5())) {
                    // same
                    status = 0;
                } else {
                    // changed
                    status = 1;

                    // set changed values
                    spiderUrl.setSize(FileUtils.getFileSize(file));
                    spiderUrl.setLastModified(urlDate);
                    spiderUrl.setMd5(fileHash);
                }
            } else {
                // download failed

                // broken link
                status = -1;
            }
        }
    } catch (IOException ioe) {
        logger.error("getURLStatus() failed for URL='" + spiderUrl.getUrl() + "'", ioe);
        status = -1;
    }

    return status;
}

From source file:org.apache.sling.jcr.contentloader.internal.BundleContentLoader.java

/**
 * Create a file from the given url.//from   w ww  . j a v a  2 s  .  c o  m
 *
 * @param configuration
 * @param parent
 * @param source
 * @param createdNodes
 * @param contentCreator
 * @throws IOException
 * @throws RepositoryException
 */
private void createFile(PathEntry configuration, Node parent, URL source, List<String> createdNodes,
        final DefaultContentCreator contentCreator) throws IOException, RepositoryException {

    final String srcPath = source.getPath();
    int pos = srcPath.lastIndexOf("/");
    final String name = getName(source.getPath());
    final String path;
    if (pos == -1) {
        path = name;
    } else {
        path = srcPath.substring(0, pos + 1) + name;
    }

    contentCreator.init(configuration, getContentReaders(), createdNodes, null);
    contentCreator.prepareParsing(parent, name);
    final URLConnection conn = source.openConnection();
    final long lastModified = Math.min(conn.getLastModified(), configuration.getLastModified());
    final String type = conn.getContentType();
    final InputStream data = conn.getInputStream();
    contentCreator.createFileAndResourceNode(path, data, type, lastModified);
    contentCreator.finishNode();
    contentCreator.finishNode();
}

From source file:org.apache.synapse.registry.url.SimpleURLRegistry.java

public RegistryEntry getRegistryEntry(String key) {

    if (log.isDebugEnabled()) {
        log.debug("Perform RegistryEntry lookup for key : " + key);
    }/*from  w ww . jav a 2s. c  om*/
    URL url = SynapseConfigUtils.getURLFromPath(root + key,
            properties.get(SynapseConstants.SYNAPSE_HOME) != null
                    ? properties.get(SynapseConstants.SYNAPSE_HOME).toString()
                    : "");
    if (url == null) {
        return null;
    }
    URLConnection connection = SynapseConfigUtils.getURLConnection(url);
    if (connection == null) {
        if (log.isDebugEnabled()) {
            log.debug("Cannot create a URLConnection for given URL : " + url);
        }
        return null;
    }

    RegistryEntryImpl wre = new RegistryEntryImpl();
    wre.setKey(key);
    wre.setName(url.getFile());
    wre.setType(connection.getContentType());
    wre.setDescription("Resource at : " + url.toString());
    wre.setLastModified(connection.getLastModified());
    wre.setVersion(connection.getLastModified());
    if (connection.getExpiration() > 0) {
        wre.setCachableDuration(connection.getExpiration() - System.currentTimeMillis());
    } else {
        wre.setCachableDuration(getCachableDuration());
    }
    return wre;
}

From source file:net.sf.eclipsecs.core.config.configtypes.RemoteConfigurationType.java

/**
 * {@inheritDoc}/* w ww  .  ja va 2s .c o m*/
 */
public CheckstyleConfigurationFile getCheckstyleConfiguration(ICheckConfiguration checkConfiguration)
        throws CheckstylePluginException {

    boolean useCacheFile = Boolean.valueOf(checkConfiguration.getAdditionalData().get(KEY_CACHE_CONFIG))
            .booleanValue();

    CheckstyleConfigurationFile data = new CheckstyleConfigurationFile();

    synchronized (Authenticator.class) {

        String currentRedirects = System.getProperty("http.maxRedirects"); //$NON-NLS-1$

        Authenticator oldAuthenticator = RemoteConfigAuthenticator.getDefault();
        try {

            // resolve the true configuration file URL
            data.setResolvedConfigFileURL(resolveLocation(checkConfiguration));

            Authenticator.setDefault(new RemoteConfigAuthenticator(data.getResolvedConfigFileURL()));

            boolean originalFileSuccess = false;
            byte[] configurationFileData = null;

            try {

                System.setProperty("http.maxRedirects", "3"); //$NON-NLS-1$ //$NON-NLS-2$

                URLConnection connection = data.getResolvedConfigFileURL().openConnection();

                // get the configuration file data
                configurationFileData = getBytesFromURLConnection(connection);

                // get last modification timestamp
                data.setModificationStamp(connection.getLastModified());

                originalFileSuccess = true;
            } catch (IOException e) {
                if (useCacheFile) {
                    configurationFileData = getBytesFromCacheFile(checkConfiguration);
                } else {
                    throw e;
                }
            }

            data.setCheckConfigFileBytes(configurationFileData);

            // get the properties bundle
            byte[] additionalPropertiesBytes = null;
            if (originalFileSuccess) {
                additionalPropertiesBytes = getAdditionPropertiesBundleBytes(data.getResolvedConfigFileURL());
            } else if (useCacheFile) {
                additionalPropertiesBytes = getBytesFromCacheBundleFile(checkConfiguration);
            }

            data.setAdditionalPropertyBundleBytes(additionalPropertiesBytes);

            // get the property resolver
            PropertyResolver resolver = getPropertyResolver(checkConfiguration, data);
            data.setPropertyResolver(resolver);

            // write to cache file
            if (originalFileSuccess && useCacheFile) {
                writeToCacheFile(checkConfiguration, configurationFileData, additionalPropertiesBytes);
            }

        } catch (UnknownHostException e) {
            CheckstylePluginException.rethrow(e,
                    NLS.bind(Messages.RemoteConfigurationType_errorUnknownHost, e.getMessage()));
        } catch (FileNotFoundException e) {
            CheckstylePluginException.rethrow(e,
                    NLS.bind(Messages.RemoteConfigurationType_errorFileNotFound, e.getMessage()));
        } catch (IOException e) {
            CheckstylePluginException.rethrow(e);
        } finally {
            Authenticator.setDefault(oldAuthenticator);

            if (currentRedirects != null) {
                System.setProperty("http.maxRedirects", currentRedirects); //$NON-NLS-1$
            } else {
                System.getProperties().remove("http.maxRedirects"); //$NON-NLS-1$
            }
        }

    }
    return data;
}

From source file:org.tinygroup.jspengine.compiler.Compiler.java

/**
 * Determine if a compilation is necessary by checking the time stamp
 * of the JSP page with that of the corresponding .class or .java file.
 * If the page has dependencies, the check is also extended to its
 * dependeants, and so on./*from  w w  w .j  av  a  2  s.com*/
 * This method can by overidden by a subclasses of Compiler.
 * @param checkClass If true, check against .class file,
 *                   if false, check against .java file.
 */
public boolean isOutDated(boolean checkClass) {

    String jsp = ctxt.getJspFile();

    if (jsw != null && (ctxt.getOptions().getModificationTestInterval() > 0)) {

        if (jsw.getLastModificationTest() + (ctxt.getOptions().getModificationTestInterval() * 1000) > System
                .currentTimeMillis()) {
            return false;
        } else {
            jsw.setLastModificationTest(System.currentTimeMillis());
        }
    }

    long jspRealLastModified = 0;
    // START PWC 6468930
    File targetFile;

    if (checkClass) {
        targetFile = new File(ctxt.getClassFileName());
    } else {
        targetFile = new File(ctxt.getServletJavaFileName());
    }

    // Get the target file's last modified time. File.lastModified()
    // returns 0 if the file does not exist.
    long targetLastModified = targetFile.lastModified();

    // Check cached class file
    if (checkClass) {
        JspRuntimeContext rtctxt = ctxt.getRuntimeContext();
        String className = ctxt.getFullClassName();
        long cachedTime = rtctxt.getBytecodeBirthTime(className);
        if (cachedTime > targetLastModified) {
            targetLastModified = cachedTime;
        } else {
            // Remove from cache, since the bytecodes from the file is more
            // current, so that JasperLoader won't load the cached version
            rtctxt.setBytecode(className, null);
        }
    }

    if (targetLastModified == 0L) {
        return true;
    }

    // Check if the jsp exists in the filesystem (instead of a jar
    // or a remote location). If yes, then do a File.lastModified()
    // to determine its last modified time. This is more performant 
    // (fewer stat calls) than the ctxt.getResource() followed by 
    // openConnection(). However, it only works for file system jsps.
    // If the file has indeed changed, then need to call URL.OpenConnection() 
    // so that the cache loads the latest jsp file
    if (jsw != null) {
        File jspFile = jsw.getJspFile();
        if (jspFile != null) {
            jspRealLastModified = jspFile.lastModified();
        }
    }
    if (jspRealLastModified == 0 || targetLastModified < jspRealLastModified) {
        // END PWC 6468930
        try {
            URL jspUrl = ctxt.getResource(jsp);
            if (jspUrl == null) {
                ctxt.incrementRemoved();
                return false;
            }
            URLConnection uc = jspUrl.openConnection();
            jspRealLastModified = uc.getLastModified();
            uc.getInputStream().close();
        } catch (Exception e) {
            e.printStackTrace();
            return true;
        }
        // START PWC 6468930
    }
    // END PWC 6468930
    /* PWC 6468930
    long targetLastModified = 0;
    File targetFile;
            
    if( checkClass ) {
    targetFile = new File(ctxt.getClassFileName());
    } else {
    targetFile = new File(ctxt.getServletJavaFileName());
    }
            
    if (!targetFile.exists()) {
    return true;
    }
            
    targetLastModified = targetFile.lastModified();
    */
    if (checkClass && jsw != null) {
        jsw.setServletClassLastModifiedTime(targetLastModified);
    }

    if (targetLastModified < jspRealLastModified) {
        // Remember JSP mod time
        jspModTime = jspRealLastModified;
        if (log.isDebugEnabled()) {
            log.debug("Compiler: outdated: " + targetFile + " " + targetLastModified);
        }
        return true;
    }

    // determine if source dependent files (e.g. includes using include
    // directives) have been changed.
    if (jsw == null) {
        return false;
    }

    List depends = jsw.getDependants();
    if (depends == null) {
        return false;
    }

    Iterator it = depends.iterator();
    while (it.hasNext()) {
        String include = (String) it.next();
        try {
            URL includeUrl = ctxt.getResource(include);
            if (includeUrl == null) {
                return true;
            }

            URLConnection includeUconn = includeUrl.openConnection();
            long includeLastModified = includeUconn.getLastModified();
            includeUconn.getInputStream().close();

            if (includeLastModified > targetLastModified) {
                // START GlassFish 750
                if (include.endsWith(".tld")) {
                    ctxt.clearTaglibs();
                    ctxt.clearTagFileJarUrls();
                }
                // END GlassFish 750
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return true;
        }
    }

    return false;

}

From source file:com.googlecode.CallerLookup.Main.java

public void doUpdate() {
    showDialog(DIALOG_PROGRESS);//www  .j a va 2  s .c  o  m
    savePreferences();
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                URL uri = new URL(UPDATE_URL);
                URLConnection urlc = uri.openConnection();
                long lastModified = urlc.getLastModified();
                if ((lastModified == 0) || (lastModified != mPrefs.getLong(PREFS_UPDATE, 0))) {
                    FileOutputStream file = getApplicationContext().openFileOutput(UPDATE_FILE, MODE_PRIVATE);
                    OutputStreamWriter content = new OutputStreamWriter(file);

                    String tmp;
                    InputStream is = urlc.getInputStream();
                    BufferedReader br = new BufferedReader(new InputStreamReader(is));
                    while ((tmp = br.readLine()) != null) {
                        content.write(tmp + "\n");
                    }

                    content.flush();
                    content.close();
                    file.close();

                    SharedPreferences.Editor prefsEditor = mPrefs.edit();
                    prefsEditor.putLong(PREFS_UPDATE, lastModified);
                    prefsEditor.commit();

                    Message message = mUpdateHandler.obtainMessage();
                    message.what = MESSAGE_UPDATE_FINISHED;
                    mUpdateHandler.sendMessage(message);
                } else {
                    Message message = mUpdateHandler.obtainMessage();
                    message.what = MESSAGE_UPDATE_UNNECESSARY;
                    mUpdateHandler.sendMessage(message);
                }
            } catch (MalformedURLException e) {
                System.out.println(e.getMessage());
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }).start();
}

From source file:org.codehaus.groovy.grails.web.pages.GroovyPageMetaInfo.java

/**
 * Attempts to establish what the last modified date of the given resource is. If the last modified date cannot
 * be etablished -1 is returned//from w  ww . j  av  a2 s  . c om
 *
 * @param resource The Resource to evaluate
 * @return The last modified date or -1
 */
private long establishLastModified(Resource resource) {
    if (resource == null)
        return -1;

    if (resource instanceof FileSystemResource) {
        return ((FileSystemResource) resource).getFile().lastModified();
    }

    long last;
    URLConnection urlc = null;

    try {
        URL url = resource.getURL();
        if ("file".equals(url.getProtocol())) {
            File file = new File(url.getFile());
            if (file.exists()) {
                return file.lastModified();
            }
        }
        urlc = url.openConnection();
        urlc.setDoInput(false);
        urlc.setDoOutput(false);
        last = urlc.getLastModified();
    } catch (FileNotFoundException fnfe) {
        last = -1;
    } catch (IOException e) {
        last = -1;
    } finally {
        if (urlc != null) {
            try {
                InputStream is = urlc.getInputStream();
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                // ignore
            }
        }
    }

    return last;
}

From source file:org.protorabbit.servlet.ProtoRabbitServlet.java

void updateLastModified(String uri) throws IOException {
    URL turl = ctx.getResource(uri);
    if (turl != null) {
        URLConnection uc = turl.openConnection();
        long lastMod = uc.getLastModified();
        lastUpdated.put(uri, lastMod);/*  w w  w .  j av a 2  s. c  o m*/
    } else {
        getLogger().warning("Error checking for last modified on:  " + uri);
    }
}

From source file:org.protorabbit.servlet.ProtoRabbitServlet.java

void updateConfig() throws IOException {
    boolean needsUpdate = false;
    String templateName = null;/*from w  w w .  j  a  v a2s  .c o  m*/
    for (int i = 0; i < templates.length; i++) {
        try {
            templateName = templates[i];
            URL turl = ctx.getResource(templateName);
            URLConnection uc = turl.openConnection();
            long lastMod = uc.getLastModified();
            Long lu = lastUpdated.get(templates[i]);
            long lastTime = 0;
            if (lu != null) {
                lastTime = lu.longValue();
            }
            if (lastMod > lastTime) {
                needsUpdate = true;
                break;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (NullPointerException npe) {
            getLogger().severe("Error reading configuration. Could not find " + templateName);
        }
    }

    if ((needsUpdate) && templates.length > 0) {
        jcfg.resetTemplates();
        for (int i = 0; i < templates.length; i++) {
            JSONObject base = null;
            InputStream is = this.ctx.getResourceAsStream(templates[i]);
            if (is != null) {
                base = JSONUtil.loadFromInputStream(is);
            } else {
                getLogger().log(Level.SEVERE, "Error  loading " + templates[i]);
                throw new IOException("Error  loading " + templates[i] + ": Please verify the file exists.");
            }
            if (base == null) {
                getLogger().log(Level.SEVERE, "Error  loading " + templates[i]);
                throw new IOException(
                        "Error  loading" + templates[i] + ": Please verify the file is correctly formatted.");
            }
            String baseURI = getTemplateDefDir(templates[i]);
            try {
                JSONArray templatesArray = base.getJSONArray("templates");
                if (templatesArray != null) {
                    jcfg.registerTemplates(templatesArray, baseURI);
                }
                updateLastModified(templates[i]);
                getLogger().info("Registered " + templates[i]);
            } catch (JSONException e1) {
                e1.printStackTrace();
            } catch (Exception ex) {
                getLogger().log(Level.SEVERE, "Error  loading" + templates[i], ex);
            }
        }
    }
}

From source file:org.drools.core.io.impl.UrlResource.java

private long grabLastMod() throws IOException {
    // use File if possible, as http rounds milliseconds on some machines, this fine level of granularity is only really an issue for testing
    // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4504473
    if ("file".equals(url.getProtocol())) {
        File file = getFile();/*from w ww  .ja va2  s  .  c om*/
        return file.lastModified();
    } else {
        URLConnection conn = openURLConnection(getURL());
        if (conn instanceof HttpURLConnection) {
            ((HttpURLConnection) conn).setRequestMethod("HEAD");
            if ("enabled".equalsIgnoreCase(basicAuthentication)) {
                String userpassword = username + ":" + password;
                byte[] authEncBytes = Base64.encodeBase64(userpassword.getBytes(IoUtils.UTF8_CHARSET));

                ((HttpURLConnection) conn).setRequestProperty("Authorization",
                        "Basic " + new String(authEncBytes, IoUtils.UTF8_CHARSET));
            }
        }
        long date = conn.getLastModified();
        if (date == 0) {
            try {
                date = Long.parseLong(conn.getHeaderField("lastModified"));
            } catch (Exception e) { /* well, we tried ... */
            }
        }
        return date;
    }
}