Example usage for java.net URLConnection setIfModifiedSince

List of usage examples for java.net URLConnection setIfModifiedSince

Introduction

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

Prototype

public void setIfModifiedSince(long ifmodifiedsince) 

Source Link

Document

Sets the value of the ifModifiedSince field of this URLConnection to the specified value.

Usage

From source file:Main.java

public static void main(String args[]) throws Exception {

    URL u = new URL("http://www.java2s.com");
    URLConnection uc = u.openConnection();

    uc.setIfModifiedSince(1000);
    System.out.println(uc.getIfModifiedSince());
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Date today = new Date();
    long millisecondsPerDay = 24 * 60 * 60 * 1000;

    URL u = new URL("http://www.java2s.com");
    URLConnection uc = u.openConnection();
    uc.setIfModifiedSince((new Date(today.getTime() - millisecondsPerDay)).getTime());
    InputStream in = new BufferedInputStream(uc.getInputStream());
    Reader r = new InputStreamReader(in);
    int c;/*from   w  w  w .ja  v a2s .  c  om*/
    while ((c = r.read()) != -1) {
        System.out.print((char) c);
    }
}

From source file:MainClass.java

public static void main(String[] args) {
    try {/*w  ww. j  a  v  a 2s.  co  m*/
        URLConnection uc = new URL("http://www.demo2s.com").openConnection();
        System.out
                .println("Will retrieve file if it's been modified since " + new Date(uc.getIfModifiedSince()));
        uc.setIfModifiedSince(System.currentTimeMillis());
        System.out
                .println("Will retrieve file if it's been modified since " + new Date(uc.getIfModifiedSince()));
    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:eionet.cr.util.URLUtil.java

/**
 * Connect to the URL and check if it is modified since the timestamp argument. Returns null, if it cannot be determined for
 * sure./* ww  w  .  j a va  2  s .c  o  m*/
 *
 * @param urlString
 * @param timestamp
 * @return true if it is modified.
 */
public static Boolean isModifiedSince(String urlString, long timestamp) {

    if (!URLUtil.isURL(urlString)) {
        return Boolean.FALSE;
    }

    if (timestamp == 0) {
        return Boolean.TRUE;
    }

    URLConnection urlConnection = null;
    try {
        URL url = new URL(StringUtils.substringBefore(urlString, "#"));
        urlConnection = escapeIRI(url).openConnection();
        urlConnection.setRequestProperty("Connection", "close");
        urlConnection.setRequestProperty("User-Agent", userAgentHeader());
        urlConnection.setIfModifiedSince(timestamp);

        int responseCode = ((HttpURLConnection) urlConnection).getResponseCode();
        System.out.println(responseCode);
        if (responseCode == HttpURLConnection.HTTP_NOT_MODIFIED) {
            return Boolean.FALSE;
        } else if (responseCode == HttpURLConnection.HTTP_OK) {
            return Boolean.TRUE;
        } else {
            // Return null to indicate if it's unclear whether the source has
            // been modified or not.
            return null;
        }
    } catch (IOException ioe) {
        return null;
    } finally {
        URLUtil.disconnect(urlConnection);
    }
}

From source file:com.comcast.cdn.traffic_control.traffic_router.core.util.Fetcher.java

protected HttpURLConnection getConnection(final String url, final String data, final String requestMethod,
        final long lastFetchTime) throws IOException {
    String method = GET_STR;//  w ww. j av  a2  s  . c  o m

    if (requestMethod != null) {
        method = requestMethod;
    }

    LOGGER.info(method + "ing: " + url + "; timeout is " + timeout);

    final URLConnection connection = new URL(url).openConnection();

    connection.setIfModifiedSince(lastFetchTime);

    if (timeout != 0) {
        connection.setConnectTimeout(timeout);
        connection.setReadTimeout(timeout);
    }

    final HttpURLConnection http = (HttpURLConnection) connection;

    if (connection instanceof HttpsURLConnection) {
        final HttpsURLConnection https = (HttpsURLConnection) connection;
        https.setHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(final String arg0, final SSLSession arg1) {
                return true;
            }
        });
    }

    http.setInstanceFollowRedirects(false);
    http.setRequestMethod(method);
    http.setAllowUserInteraction(true);

    for (final String key : requestProps.keySet()) {
        http.addRequestProperty(key, requestProps.get(key));
    }

    if (method.equals(POST_STR) && data != null) {
        http.setDoOutput(true); // Triggers POST.

        try (final OutputStream output = http.getOutputStream()) {
            output.write(data.getBytes(UTF8_STR));
        }
    }

    connection.connect();

    return http;
}

From source file:org.shredzone.commons.gravatar.impl.GravatarServiceImpl.java

/**
 * Fetches a Gravatar icon from the server and stores it in the given {@link File}.
 *
 * @param url// ww  w.  j a va 2  s  .  c o  m
 *            Gravatar URL to fetch
 * @param file
 *            {@link File} to store the icon to
 */
private void fetchGravatar(URL url, File file) throws IOException {
    limitUpstreamRequests();

    URLConnection conn = url.openConnection();
    conn.setConnectTimeout(TIMEOUT);
    conn.setReadTimeout(TIMEOUT);

    if (file.exists()) {
        conn.setIfModifiedSince(file.lastModified());
    }

    conn.connect();

    long lastModified = conn.getLastModified();
    if (lastModified > 0L && lastModified <= file.lastModified()) {
        // Cache file exists and is unchanged
        if (log.isDebugEnabled()) {
            log.debug("Cached Gravatar is still good: {}", url);
        }

        file.setLastModified(System.currentTimeMillis()); // touch
        return;
    }

    try (InputStream in = conn.getInputStream(); OutputStream out = new FileOutputStream(file)) {
        byte[] buffer = new byte[8192];
        int total = 0;
        int len;

        while ((len = in.read(buffer)) >= 0) {
            out.write(buffer, 0, len);
            total += len;
            if (total > MAX_GRAVATAR_SIZE) {
                log.warn("Gravatar exceeded maximum size: {}", url);
                break;
            }
        }

        out.flush();

        if (log.isDebugEnabled()) {
            log.debug("Downloaded Gravatar: {}", url);
        }
    }
}

From source file:com.blackducksoftware.integration.hub.cli.CLIDownloadService.java

public void customInstall(HubProxyInfo hubProxyInfo, CLILocation cliLocation,
        CIEnvironmentVariables ciEnvironmentVariables, final URL archive, String hubVersion,
        final String localHostName) throws IOException, InterruptedException, HubIntegrationException,
        IllegalArgumentException, EncryptionException {
    boolean cliMismatch = true;
    try {//from   w  w  w.j a  v a  2s  .  co  m
        final File hubVersionFile = cliLocation.createHubVersionFile();
        if (hubVersionFile.exists()) {
            final String storedHubVersion = IOUtils.toString(new FileReader(hubVersionFile));
            if (hubVersion.equals(storedHubVersion)) {
                cliMismatch = false;
            } else {
                hubVersionFile.delete();
                hubVersionFile.createNewFile();
            }
        }
        final File cliInstallDirectory = cliLocation.getCLIInstallDir();
        if (!cliInstallDirectory.exists()) {
            cliMismatch = true;
        }

        if (cliMismatch) {
            logger.debug("Attempting to download the Hub CLI.");
            final FileWriter writer = new FileWriter(hubVersionFile);
            writer.write(hubVersion);
            writer.close();
            hubVersionFile.setLastModified(0L);
        }
        final long cliTimestamp = hubVersionFile.lastModified();

        URLConnection connection = null;
        try {
            Proxy proxy = null;
            if (hubProxyInfo != null) {
                String proxyHost = hubProxyInfo.getHost();
                int proxyPort = hubProxyInfo.getPort();
                String proxyUsername = hubProxyInfo.getUsername();
                String proxyPassword = hubProxyInfo.getDecryptedPassword();

                if (StringUtils.isNotBlank(proxyHost) && proxyPort > 0) {
                    proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
                }
                if (proxy != null) {
                    if (StringUtils.isNotBlank(proxyUsername) && StringUtils.isNotBlank(proxyPassword)) {
                        AuthenticatorUtil.setAuthenticator(proxyUsername, proxyPassword);
                    } else {
                        AuthenticatorUtil.resetAuthenticator();
                    }
                }
            }
            if (proxy != null) {
                connection = archive.openConnection(proxy);
            } else {
                connection = archive.openConnection();
            }
            connection.setIfModifiedSince(cliTimestamp);
            connection.connect();
        } catch (final IOException ioe) {
            logger.error("Skipping installation of " + archive + " to " + cliLocation.getCanonicalPath() + ": "
                    + ioe.toString());
            return;
        }

        if (connection instanceof HttpURLConnection
                && ((HttpURLConnection) connection).getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
            // CLI has not been modified
            return;
        }

        final long sourceTimestamp = connection.getLastModified();

        if (cliInstallDirectory.exists() && cliInstallDirectory.listFiles().length > 0) {
            if (!cliMismatch && sourceTimestamp == cliTimestamp) {
                logger.debug("The current Hub CLI is up to date.");
                return;
            }
            for (final File file : cliInstallDirectory.listFiles()) {
                FileUtils.deleteDirectory(file);
            }
        } else {
            cliInstallDirectory.mkdir();
        }
        logger.debug("Updating the Hub CLI.");
        hubVersionFile.setLastModified(sourceTimestamp);

        logger.info("Unpacking " + archive.toString() + " to " + cliInstallDirectory.getCanonicalPath() + " on "
                + localHostName);

        final CountingInputStream cis = new CountingInputStream(connection.getInputStream());
        try {
            unzip(cliInstallDirectory, cis, logger);
            updateJreSecurity(logger, cliLocation, ciEnvironmentVariables);
        } catch (final IOException e) {
            throw new IOException(String.format("Failed to unpack %s (%d bytes read of total %d)", archive,
                    cis.getByteCount(), connection.getContentLength()), e);
        }
    } catch (final IOException e) {
        throw new IOException("Failed to install " + archive + " to " + cliLocation.getCanonicalPath(), e);
    }
}

From source file:opendap.metacat.DDXRetriever.java

/**
 * Given a URL to a DDX, get the DDX document. If the DDXRetriever was
 * built with caching turned on, this uses a poor man's HTTP/1.1 cache
 * based on Last Modified Times. /*from  w w w.ja v  a  2  s .  c o  m*/
 * 
 * If caching is on, then calling this on a series of DDX URLs will fill
 * the cache. If the cache is saved and later used again it is possible
 * to re-read the URLs straight from the cache.
 *  
 * @see getCache()
 * @param DDXURL Get the DDX referenced by this URL
 * @return The DDX document, in a String
 * @throws Exception 
 */
public String getDDXDoc(String DDXURL) throws Exception {
    String ddx = null;

    URL url = new URL(DDXURL);
    URLConnection connection = url.openConnection();

    if (DDXCache.getLastVisited(DDXURL) != 0 && DDXCache.getCachedResponse(DDXURL) != null)
        connection.setIfModifiedSince(DDXCache.getLastVisited(DDXURL));

    // Here's where we'd poke in a header to ask for the DAP3.2 DDX

    connection.connect();

    // Cast to a HttpURLConnection
    if (connection instanceof HttpURLConnection) {
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
        int code = httpConnection.getResponseCode();

        // If we have something, process. Since a conditional get was
        // used, the response might be empty (code == 304) and nothing
        // should be done in that case
        switch (code) {
        case 200:
            ddx = convertStreamToString(httpConnection.getInputStream());
            // Update the last visited and document caches
            if (!readOnly) {
                Date date = new Date();
                DDXCache.setLastVisited(DDXURL, date.getTime());
                DDXCache.setCachedResponse(DDXURL, ddx);
            }
            break;

        case 304:
            ddx = DDXCache.getCachedResponse(DDXURL);
            if (!readOnly) {
                // Update the last visited cache to now
                Date date = new Date();
                DDXCache.setLastVisited(DDXURL, date.getTime());
            }
            break;

        default:
            log.error("Expected a 200 or 304 HTTP return code. Got: " + new Integer(code).toString());
        }
    } else {
        throw new MalformedURLException("Expected a HTTP URL (" + DDXURL + ").");
    }

    return ddx;
}

From source file:org.chililog.server.workbench.WorkbenchServiceTest.java

/**
 * Check if our 304 Not Modified is working when getting a static file.
 * /*from  w ww  .j a  v a2  s .  c om*/
 * @throws IOException
 * @throws ParseException
 */
@Test()
public void testStaticFileCache() throws IOException, ParseException {
    String TEXT = "abc\n123";

    String fileName = UUID.randomUUID().toString() + ".txt";
    File file = new File(_workbenchStaticFilesDirectory, fileName);

    FileOutputStream fos = new FileOutputStream(file);
    OutputStreamWriter out = new OutputStreamWriter(fos, "UTF-8");
    out.write(TEXT);
    out.close();

    // Refresh
    file = new File(file.getPath());

    // ******************************************************
    // Initial request
    // ******************************************************
    // Create a URL for the desired page
    URL url = new URL("http://localhost:8989/static/testdata/" + fileName);
    URLConnection conn = url.openConnection();

    // Read all the text returned by the server
    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    StringBuffer sb = new StringBuffer();
    String str;
    while ((str = in.readLine()) != null) {
        sb.append(str + "\n");
    }
    in.close();

    assertEquals(TEXT, sb.toString().trim());

    // Get headers
    HashMap<String, String> headers = new HashMap<String, String>();
    for (int i = 0;; i++) {
        String name = conn.getHeaderFieldKey(i);
        String value = conn.getHeaderField(i);
        if (name == null && value == null) {
            break;
        }
        if (name == null) {
            _logger.debug("*** Intial Call, Response code: %s", value);
        } else {
            headers.put(name, value);
            _logger.debug("%s = %s", name, value);
        }
    }

    assertEquals("7", headers.get("Content-Length"));
    assertEquals("text/plain", headers.get("Content-Type"));

    // Check last modified should be the same as the file's last modified date
    SimpleDateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
    fmt.setTimeZone(TimeZone.getTimeZone("GMT"));
    assertEquals(fmt.format(new Date(file.lastModified())), headers.get("Last-Modified"));

    // Check Expiry
    Date expires = fmt.parse(headers.get("Expires"));
    Date serverDate = fmt.parse(headers.get("Date"));
    Calendar cal = new GregorianCalendar();
    cal.setTime(serverDate);
    cal.add(Calendar.SECOND, AppProperties.getInstance().getWorkbenchStaticFilesCacheSeconds());
    assertEquals(cal.getTimeInMillis(), expires.getTime());

    // ******************************************************
    // Cache Validation
    // ******************************************************
    url = new URL("http://localhost:8989/static/testdata/" + fileName);
    conn = url.openConnection();
    conn.setIfModifiedSince(fmt.parse(headers.get("Last-Modified")).getTime());

    in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    sb = new StringBuffer();
    while ((str = in.readLine()) != null) {
        sb.append(str + "\n");
    }
    in.close();

    // No content should be returned
    assertEquals("", sb.toString().trim());

    HashMap<String, String> headers2 = new HashMap<String, String>();
    String responseCode = "";
    for (int i = 0;; i++) {
        String name = conn.getHeaderFieldKey(i);
        String value = conn.getHeaderField(i);
        if (name == null && value == null) {
            break;
        }
        if (name == null) {
            responseCode = value;
            _logger.debug("*** Cache Call, Response code: %s", value);
        } else {
            headers2.put(name, value);
            _logger.debug("%s = %s", name, value);
        }
    }

    // Should get back a 304
    assertEquals("HTTP/1.1 304 Not Modified", responseCode);
    assertTrue(!StringUtils.isBlank(headers2.get("Date")));

    // ******************************************************
    // Finish
    // ******************************************************
    // Clean up
    file.delete();
}

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

/**
 * Conditionally set the header that "If-Modified-Since" header for a
 * feed. Must be called on a <tt>URLConnection</tt> before the
 * <tt>InputStream</tt> is retrieved. Uses the feed cache to set the
 * value.//from   w  w  w .  j  av a2s  . c o  m
 *
 * @param conn     the <tt>URLConnection</tt> on which to set the
 *                 header
 * @param feedInfo the information on the feed
 * @param cache    the cache
 */
private void setIfModifiedSinceHeader(final URLConnection conn, final FeedInfo feedInfo,
        final FeedCache cache) {
    long lastSeen = 0;
    URL feedURL = feedInfo.getURL();

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

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

            if (lastSeen > 0) {
                if (log.isDebugEnabled()) {
                    log.debug(
                            "Setting If-Modified-Since header for " + "feed \"" + feedURL.toString() + "\" to: "
                                    + String.valueOf(lastSeen) + " (" + new Date(lastSeen).toString() + ")");
                }

                conn.setIfModifiedSince(lastSeen);
            }
        }
    }
}