Java Code Examples for java.net.URLConnection#setIfModifiedSince()

The following examples show how to use java.net.URLConnection#setIfModifiedSince() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpURLFeedFetcher.java    From rome with Apache License 2.0 6 votes vote down vote up
/**
 * <p>
 * Set appropriate HTTP headers, including conditional get and gzip encoding headers
 * </p>
 *
 * @param connection A URLConnection
 * @param syndFeedInfo The SyndFeedInfo for the feed to be retrieved. May be null
 * @param userAgent the name of the user-agent to be placed in HTTP-header.
 */
protected void setRequestHeaders(final URLConnection connection, final SyndFeedInfo syndFeedInfo, final String userAgent) {
    if (syndFeedInfo != null) {
        // set the headers to get feed only if modified
        // we support the use of both last modified and eTag headers
        if (syndFeedInfo.getLastModified() != null) {
            final Object lastModified = syndFeedInfo.getLastModified();
            if (lastModified instanceof Long) {
                connection.setIfModifiedSince((Long) syndFeedInfo.getLastModified());
            }
        }
        if (syndFeedInfo.getETag() != null) {
            connection.setRequestProperty("If-None-Match", syndFeedInfo.getETag());
        }

    }
    // header to retrieve feed gzipped
    connection.setRequestProperty("Accept-Encoding", "gzip");
    connection.addRequestProperty("User-Agent", userAgent);

    if (isUsingDeltaEncoding()) {
        connection.addRequestProperty("A-IM", "feed");
    }
}
 
Example 2
Source File: UrlModuleSourceProvider.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
void applyConditionals(URLConnection urlConnection) {
    if(lastModified != 0L) {
        urlConnection.setIfModifiedSince(lastModified);
    }
    if(entityTags != null && entityTags.length() > 0) {
        urlConnection.addRequestProperty("If-None-Match", entityTags);
    }
}
 
Example 3
Source File: UniformResourceStorage.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
/**
 * isValid
 * 
 * @return boolean
 */
public boolean isValid() {
	if (timestamp == -1) {
		return false;
	}
	if (expires >= System.currentTimeMillis()) {
		return true;
	}
	try {
		URLConnection connection = getURI().toURL().openConnection();
		if (connection instanceof HttpURLConnection) {
			connection.setIfModifiedSince(timestamp);
			((HttpURLConnection) connection).setRequestMethod("HEAD"); //$NON-NLS-1$
		}
		connection.connect();
		if (connection instanceof HttpURLConnection) {
			HttpURLConnection httpConnection = (HttpURLConnection) connection;
			long lastModified = httpConnection.getLastModified();
			if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED || (lastModified != 0 && timestamp >= lastModified)) {
				expires = System.currentTimeMillis();
				long expiration = connection.getExpiration();
				long date = connection.getDate();
				if (expiration != 0 && date != 0 && expiration > date) {
					expires += (expiration - date);
				} else {
					expires += 10 * 1000; // 10 sec
				}
				return true;
			}
		}
	} catch (IOException e) {
		IdeLog.logError(CorePlugin.getDefault(), e);
	}
	return false;
}
 
Example 4
Source File: UrlModuleSourceProvider.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
void applyConditionals(URLConnection urlConnection) {
    if(lastModified != 0L) {
        urlConnection.setIfModifiedSince(lastModified);
    }
    if(entityTags != null && entityTags.length() > 0) {
        urlConnection.addRequestProperty("If-None-Match", entityTags);
    }
}
 
Example 5
Source File: Client.java    From android-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a if-modified-since header to the open {@link URLConnection} if this value is
 * stored in {@link OptlyStorage}.
 * @param urlConnection an open {@link URLConnection}
 */
public void setIfModifiedSince(@NonNull URLConnection urlConnection) {
    if (urlConnection == null || urlConnection.getURL() == null) {
        logger.error("Invalid connection");
        return;
    }

    long lastModified = optlyStorage.getLong(urlConnection.getURL().toString(), 0);
    if (lastModified > 0) {
        urlConnection.setIfModifiedSince(lastModified);
    }
}
 
Example 6
Source File: DatasetRepository.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Inspects if the dataset at the supplied URL location has been modified since the last load into this repository
 * and if so loads it into the supplied context.
 *
 * @param url     the location of the dataset
 * @param context the context in which to load the dataset
 * @param config  parser configuration to use for processing the dataset
 * @throws RepositoryException if an error occurred while loading the dataset.
 */
public void loadDataset(URL url, IRI context, ParserConfig config) throws RepositoryException {
	try {
		Long since = lastModified.get(url);
		URLConnection urlCon = url.openConnection();
		if (since != null) {
			urlCon.setIfModifiedSince(since);
		}
		if (since == null || since < urlCon.getLastModified()) {
			load(url, urlCon, context, config);
		}
	} catch (RDFParseException | IOException e) {
		throw new RepositoryException(e);
	}
}
 
Example 7
Source File: URICertStore.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a <code>Collection</code> of <code>X509Certificate</code>s that
 * match the specified selector. If no <code>X509Certificate</code>s
 * match the selector, an empty <code>Collection</code> will be returned.
 *
 * @param selector a <code>CertSelector</code> used to select which
 *  <code>X509Certificate</code>s should be returned. Specify
 *  <code>null</code> to return all <code>X509Certificate</code>s.
 * @return a <code>Collection</code> of <code>X509Certificate</code>s that
 *         match the specified selector
 * @throws CertStoreException if an exception occurs
 */
@Override
@SuppressWarnings("unchecked")
public synchronized Collection<X509Certificate> engineGetCertificates
    (CertSelector selector) throws CertStoreException {

    // if ldap URI we wrap the CertSelector in an LDAPCertSelector to
    // avoid LDAP DN matching issues (see LDAPCertSelector for more info)
    if (ldap) {
        X509CertSelector xsel = (X509CertSelector) selector;
        try {
            xsel = ldapHelper.wrap(xsel, xsel.getSubject(), ldapPath);
        } catch (IOException ioe) {
            throw new CertStoreException(ioe);
        }
        // Fetch the certificates via LDAP. LDAPCertStore has its own
        // caching mechanism, see the class description for more info.
        // Safe cast since xsel is an X509 certificate selector.
        return (Collection<X509Certificate>)
            ldapCertStore.getCertificates(xsel);
    }

    // Return the Certificates for this entry. It returns the cached value
    // if it is still current and fetches the Certificates otherwise.
    // For the caching details, see the top of this class.
    long time = System.currentTimeMillis();
    if (time - lastChecked < CHECK_INTERVAL) {
        if (debug != null) {
            debug.println("Returning certificates from cache");
        }
        return getMatchingCerts(certs, selector);
    }
    lastChecked = time;
    try {
        URLConnection connection = uri.toURL().openConnection();
        if (lastModified != 0) {
            connection.setIfModifiedSince(lastModified);
        }
        long oldLastModified = lastModified;
        try (InputStream in = connection.getInputStream()) {
            lastModified = connection.getLastModified();
            if (oldLastModified != 0) {
                if (oldLastModified == lastModified) {
                    if (debug != null) {
                        debug.println("Not modified, using cached copy");
                    }
                    return getMatchingCerts(certs, selector);
                } else if (connection instanceof HttpURLConnection) {
                    // some proxy servers omit last modified
                    HttpURLConnection hconn = (HttpURLConnection)connection;
                    if (hconn.getResponseCode()
                                == HttpURLConnection.HTTP_NOT_MODIFIED) {
                        if (debug != null) {
                            debug.println("Not modified, using cached copy");
                        }
                        return getMatchingCerts(certs, selector);
                    }
                }
            }
            if (debug != null) {
                debug.println("Downloading new certificates...");
            }
            // Safe cast since factory is an X.509 certificate factory
            certs = (Collection<X509Certificate>)
                factory.generateCertificates(in);
        }
        return getMatchingCerts(certs, selector);
    } catch (IOException | CertificateException e) {
        if (debug != null) {
            debug.println("Exception fetching certificates:");
            e.printStackTrace();
        }
    }
    // exception, forget previous values
    lastModified = 0;
    certs = Collections.emptySet();
    return certs;
}
 
Example 8
Source File: URICertStore.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a <code>Collection</code> of <code>X509Certificate</code>s that
 * match the specified selector. If no <code>X509Certificate</code>s
 * match the selector, an empty <code>Collection</code> will be returned.
 *
 * @param selector a <code>CertSelector</code> used to select which
 *  <code>X509Certificate</code>s should be returned. Specify
 *  <code>null</code> to return all <code>X509Certificate</code>s.
 * @return a <code>Collection</code> of <code>X509Certificate</code>s that
 *         match the specified selector
 * @throws CertStoreException if an exception occurs
 */
@Override
@SuppressWarnings("unchecked")
public synchronized Collection<X509Certificate> engineGetCertificates
    (CertSelector selector) throws CertStoreException {

    // if ldap URI we wrap the CertSelector in an LDAPCertSelector to
    // avoid LDAP DN matching issues (see LDAPCertSelector for more info)
    if (ldap) {
        X509CertSelector xsel = (X509CertSelector) selector;
        try {
            xsel = ldapHelper.wrap(xsel, xsel.getSubject(), ldapPath);
        } catch (IOException ioe) {
            throw new CertStoreException(ioe);
        }
        // Fetch the certificates via LDAP. LDAPCertStore has its own
        // caching mechanism, see the class description for more info.
        // Safe cast since xsel is an X509 certificate selector.
        return (Collection<X509Certificate>)
            ldapCertStore.getCertificates(xsel);
    }

    // Return the Certificates for this entry. It returns the cached value
    // if it is still current and fetches the Certificates otherwise.
    // For the caching details, see the top of this class.
    long time = System.currentTimeMillis();
    if (time - lastChecked < CHECK_INTERVAL) {
        if (debug != null) {
            debug.println("Returning certificates from cache");
        }
        return getMatchingCerts(certs, selector);
    }
    lastChecked = time;
    try {
        URLConnection connection = uri.toURL().openConnection();
        if (lastModified != 0) {
            connection.setIfModifiedSince(lastModified);
        }
        long oldLastModified = lastModified;
        try (InputStream in = connection.getInputStream()) {
            lastModified = connection.getLastModified();
            if (oldLastModified != 0) {
                if (oldLastModified == lastModified) {
                    if (debug != null) {
                        debug.println("Not modified, using cached copy");
                    }
                    return getMatchingCerts(certs, selector);
                } else if (connection instanceof HttpURLConnection) {
                    // some proxy servers omit last modified
                    HttpURLConnection hconn = (HttpURLConnection)connection;
                    if (hconn.getResponseCode()
                                == HttpURLConnection.HTTP_NOT_MODIFIED) {
                        if (debug != null) {
                            debug.println("Not modified, using cached copy");
                        }
                        return getMatchingCerts(certs, selector);
                    }
                }
            }
            if (debug != null) {
                debug.println("Downloading new certificates...");
            }
            // Safe cast since factory is an X.509 certificate factory
            certs = (Collection<X509Certificate>)
                factory.generateCertificates(in);
        }
        return getMatchingCerts(certs, selector);
    } catch (IOException | CertificateException e) {
        if (debug != null) {
            debug.println("Exception fetching certificates:");
            e.printStackTrace();
        }
    }
    // exception, forget previous values
    lastModified = 0;
    certs = Collections.emptySet();
    return certs;
}
 
Example 9
Source File: URICertStore.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a <code>Collection</code> of <code>X509Certificate</code>s that
 * match the specified selector. If no <code>X509Certificate</code>s
 * match the selector, an empty <code>Collection</code> will be returned.
 *
 * @param selector a <code>CertSelector</code> used to select which
 *  <code>X509Certificate</code>s should be returned. Specify
 *  <code>null</code> to return all <code>X509Certificate</code>s.
 * @return a <code>Collection</code> of <code>X509Certificate</code>s that
 *         match the specified selector
 * @throws CertStoreException if an exception occurs
 */
@Override
@SuppressWarnings("unchecked")
public synchronized Collection<X509Certificate> engineGetCertificates
    (CertSelector selector) throws CertStoreException {

    // if ldap URI we wrap the CertSelector in an LDAPCertSelector to
    // avoid LDAP DN matching issues (see LDAPCertSelector for more info)
    if (ldap) {
        X509CertSelector xsel = (X509CertSelector) selector;
        try {
            xsel = ldapHelper.wrap(xsel, xsel.getSubject(), ldapPath);
        } catch (IOException ioe) {
            throw new CertStoreException(ioe);
        }
        // Fetch the certificates via LDAP. LDAPCertStore has its own
        // caching mechanism, see the class description for more info.
        // Safe cast since xsel is an X509 certificate selector.
        return (Collection<X509Certificate>)
            ldapCertStore.getCertificates(xsel);
    }

    // Return the Certificates for this entry. It returns the cached value
    // if it is still current and fetches the Certificates otherwise.
    // For the caching details, see the top of this class.
    long time = System.currentTimeMillis();
    if (time - lastChecked < CHECK_INTERVAL) {
        if (debug != null) {
            debug.println("Returning certificates from cache");
        }
        return getMatchingCerts(certs, selector);
    }
    lastChecked = time;
    try {
        URLConnection connection = uri.toURL().openConnection();
        if (lastModified != 0) {
            connection.setIfModifiedSince(lastModified);
        }
        long oldLastModified = lastModified;
        try (InputStream in = connection.getInputStream()) {
            lastModified = connection.getLastModified();
            if (oldLastModified != 0) {
                if (oldLastModified == lastModified) {
                    if (debug != null) {
                        debug.println("Not modified, using cached copy");
                    }
                    return getMatchingCerts(certs, selector);
                } else if (connection instanceof HttpURLConnection) {
                    // some proxy servers omit last modified
                    HttpURLConnection hconn = (HttpURLConnection)connection;
                    if (hconn.getResponseCode()
                                == HttpURLConnection.HTTP_NOT_MODIFIED) {
                        if (debug != null) {
                            debug.println("Not modified, using cached copy");
                        }
                        return getMatchingCerts(certs, selector);
                    }
                }
            }
            if (debug != null) {
                debug.println("Downloading new certificates...");
            }
            // Safe cast since factory is an X.509 certificate factory
            certs = (Collection<X509Certificate>)
                factory.generateCertificates(in);
        }
        return getMatchingCerts(certs, selector);
    } catch (IOException | CertificateException e) {
        if (debug != null) {
            debug.println("Exception fetching certificates:");
            e.printStackTrace();
        }
    }
    // exception, forget previous values
    lastModified = 0;
    certs = Collections.emptySet();
    return certs;
}
 
Example 10
Source File: URICertStore.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a <code>Collection</code> of <code>X509Certificate</code>s that
 * match the specified selector. If no <code>X509Certificate</code>s
 * match the selector, an empty <code>Collection</code> will be returned.
 *
 * @param selector a <code>CertSelector</code> used to select which
 *  <code>X509Certificate</code>s should be returned. Specify
 *  <code>null</code> to return all <code>X509Certificate</code>s.
 * @return a <code>Collection</code> of <code>X509Certificate</code>s that
 *         match the specified selector
 * @throws CertStoreException if an exception occurs
 */
@Override
@SuppressWarnings("unchecked")
public synchronized Collection<X509Certificate> engineGetCertificates
    (CertSelector selector) throws CertStoreException {

    // if ldap URI we wrap the CertSelector in an LDAPCertSelector to
    // avoid LDAP DN matching issues (see LDAPCertSelector for more info)
    if (ldap) {
        X509CertSelector xsel = (X509CertSelector) selector;
        try {
            xsel = ldapHelper.wrap(xsel, xsel.getSubject(), ldapPath);
        } catch (IOException ioe) {
            throw new CertStoreException(ioe);
        }
        // Fetch the certificates via LDAP. LDAPCertStore has its own
        // caching mechanism, see the class description for more info.
        // Safe cast since xsel is an X509 certificate selector.
        return (Collection<X509Certificate>)
            ldapCertStore.getCertificates(xsel);
    }

    // Return the Certificates for this entry. It returns the cached value
    // if it is still current and fetches the Certificates otherwise.
    // For the caching details, see the top of this class.
    long time = System.currentTimeMillis();
    if (time - lastChecked < CHECK_INTERVAL) {
        if (debug != null) {
            debug.println("Returning certificates from cache");
        }
        return getMatchingCerts(certs, selector);
    }
    lastChecked = time;
    try {
        URLConnection connection = uri.toURL().openConnection();
        if (lastModified != 0) {
            connection.setIfModifiedSince(lastModified);
        }
        long oldLastModified = lastModified;
        try (InputStream in = connection.getInputStream()) {
            lastModified = connection.getLastModified();
            if (oldLastModified != 0) {
                if (oldLastModified == lastModified) {
                    if (debug != null) {
                        debug.println("Not modified, using cached copy");
                    }
                    return getMatchingCerts(certs, selector);
                } else if (connection instanceof HttpURLConnection) {
                    // some proxy servers omit last modified
                    HttpURLConnection hconn = (HttpURLConnection)connection;
                    if (hconn.getResponseCode()
                                == HttpURLConnection.HTTP_NOT_MODIFIED) {
                        if (debug != null) {
                            debug.println("Not modified, using cached copy");
                        }
                        return getMatchingCerts(certs, selector);
                    }
                }
            }
            if (debug != null) {
                debug.println("Downloading new certificates...");
            }
            // Safe cast since factory is an X.509 certificate factory
            certs = (Collection<X509Certificate>)
                factory.generateCertificates(in);
        }
        return getMatchingCerts(certs, selector);
    } catch (IOException | CertificateException e) {
        if (debug != null) {
            debug.println("Exception fetching certificates:");
            e.printStackTrace();
        }
    }
    // exception, forget previous values
    lastModified = 0;
    certs = Collections.emptySet();
    return certs;
}
 
Example 11
Source File: URICertStore.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a <code>Collection</code> of <code>X509Certificate</code>s that
 * match the specified selector. If no <code>X509Certificate</code>s
 * match the selector, an empty <code>Collection</code> will be returned.
 *
 * @param selector a <code>CertSelector</code> used to select which
 *  <code>X509Certificate</code>s should be returned. Specify
 *  <code>null</code> to return all <code>X509Certificate</code>s.
 * @return a <code>Collection</code> of <code>X509Certificate</code>s that
 *         match the specified selector
 * @throws CertStoreException if an exception occurs
 */
@Override
@SuppressWarnings("unchecked")
public synchronized Collection<X509Certificate> engineGetCertificates
    (CertSelector selector) throws CertStoreException {

    // if ldap URI we wrap the CertSelector in an LDAPCertSelector to
    // avoid LDAP DN matching issues (see LDAPCertSelector for more info)
    if (ldap) {
        X509CertSelector xsel = (X509CertSelector) selector;
        try {
            xsel = ldapHelper.wrap(xsel, xsel.getSubject(), ldapPath);
        } catch (IOException ioe) {
            throw new CertStoreException(ioe);
        }
        // Fetch the certificates via LDAP. LDAPCertStore has its own
        // caching mechanism, see the class description for more info.
        // Safe cast since xsel is an X509 certificate selector.
        return (Collection<X509Certificate>)
            ldapCertStore.getCertificates(xsel);
    }

    // Return the Certificates for this entry. It returns the cached value
    // if it is still current and fetches the Certificates otherwise.
    // For the caching details, see the top of this class.
    long time = System.currentTimeMillis();
    if (time - lastChecked < CHECK_INTERVAL) {
        if (debug != null) {
            debug.println("Returning certificates from cache");
        }
        return getMatchingCerts(certs, selector);
    }
    lastChecked = time;
    try {
        URLConnection connection = uri.toURL().openConnection();
        if (lastModified != 0) {
            connection.setIfModifiedSince(lastModified);
        }
        long oldLastModified = lastModified;
        try (InputStream in = connection.getInputStream()) {
            lastModified = connection.getLastModified();
            if (oldLastModified != 0) {
                if (oldLastModified == lastModified) {
                    if (debug != null) {
                        debug.println("Not modified, using cached copy");
                    }
                    return getMatchingCerts(certs, selector);
                } else if (connection instanceof HttpURLConnection) {
                    // some proxy servers omit last modified
                    HttpURLConnection hconn = (HttpURLConnection)connection;
                    if (hconn.getResponseCode()
                                == HttpURLConnection.HTTP_NOT_MODIFIED) {
                        if (debug != null) {
                            debug.println("Not modified, using cached copy");
                        }
                        return getMatchingCerts(certs, selector);
                    }
                }
            }
            if (debug != null) {
                debug.println("Downloading new certificates...");
            }
            // Safe cast since factory is an X.509 certificate factory
            certs = (Collection<X509Certificate>)
                factory.generateCertificates(in);
        }
        return getMatchingCerts(certs, selector);
    } catch (IOException | CertificateException e) {
        if (debug != null) {
            debug.println("Exception fetching certificates:");
            e.printStackTrace();
        }
    }
    // exception, forget previous values
    lastModified = 0;
    certs = Collections.emptySet();
    return certs;
}
 
Example 12
Source File: URICertStore.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a <code>Collection</code> of <code>X509Certificate</code>s that
 * match the specified selector. If no <code>X509Certificate</code>s
 * match the selector, an empty <code>Collection</code> will be returned.
 *
 * @param selector a <code>CertSelector</code> used to select which
 *  <code>X509Certificate</code>s should be returned. Specify
 *  <code>null</code> to return all <code>X509Certificate</code>s.
 * @return a <code>Collection</code> of <code>X509Certificate</code>s that
 *         match the specified selector
 * @throws CertStoreException if an exception occurs
 */
@Override
@SuppressWarnings("unchecked")
public synchronized Collection<X509Certificate> engineGetCertificates
    (CertSelector selector) throws CertStoreException {

    // if ldap URI we wrap the CertSelector in an LDAPCertSelector to
    // avoid LDAP DN matching issues (see LDAPCertSelector for more info)
    if (ldap) {
        X509CertSelector xsel = (X509CertSelector) selector;
        try {
            xsel = ldapHelper.wrap(xsel, xsel.getSubject(), ldapPath);
        } catch (IOException ioe) {
            throw new CertStoreException(ioe);
        }
        // Fetch the certificates via LDAP. LDAPCertStore has its own
        // caching mechanism, see the class description for more info.
        // Safe cast since xsel is an X509 certificate selector.
        return (Collection<X509Certificate>)
            ldapCertStore.getCertificates(xsel);
    }

    // Return the Certificates for this entry. It returns the cached value
    // if it is still current and fetches the Certificates otherwise.
    // For the caching details, see the top of this class.
    long time = System.currentTimeMillis();
    if (time - lastChecked < CHECK_INTERVAL) {
        if (debug != null) {
            debug.println("Returning certificates from cache");
        }
        return getMatchingCerts(certs, selector);
    }
    lastChecked = time;
    try {
        URLConnection connection = uri.toURL().openConnection();
        if (lastModified != 0) {
            connection.setIfModifiedSince(lastModified);
        }
        long oldLastModified = lastModified;
        try (InputStream in = connection.getInputStream()) {
            lastModified = connection.getLastModified();
            if (oldLastModified != 0) {
                if (oldLastModified == lastModified) {
                    if (debug != null) {
                        debug.println("Not modified, using cached copy");
                    }
                    return getMatchingCerts(certs, selector);
                } else if (connection instanceof HttpURLConnection) {
                    // some proxy servers omit last modified
                    HttpURLConnection hconn = (HttpURLConnection)connection;
                    if (hconn.getResponseCode()
                                == HttpURLConnection.HTTP_NOT_MODIFIED) {
                        if (debug != null) {
                            debug.println("Not modified, using cached copy");
                        }
                        return getMatchingCerts(certs, selector);
                    }
                }
            }
            if (debug != null) {
                debug.println("Downloading new certificates...");
            }
            // Safe cast since factory is an X.509 certificate factory
            certs = (Collection<X509Certificate>)
                factory.generateCertificates(in);
        }
        return getMatchingCerts(certs, selector);
    } catch (IOException | CertificateException e) {
        if (debug != null) {
            debug.println("Exception fetching certificates:");
            e.printStackTrace();
        }
    }
    // exception, forget previous values
    lastModified = 0;
    certs = Collections.emptySet();
    return certs;
}
 
Example 13
Source File: URICertStore.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a <code>Collection</code> of <code>X509Certificate</code>s that
 * match the specified selector. If no <code>X509Certificate</code>s
 * match the selector, an empty <code>Collection</code> will be returned.
 *
 * @param selector a <code>CertSelector</code> used to select which
 *  <code>X509Certificate</code>s should be returned. Specify
 *  <code>null</code> to return all <code>X509Certificate</code>s.
 * @return a <code>Collection</code> of <code>X509Certificate</code>s that
 *         match the specified selector
 * @throws CertStoreException if an exception occurs
 */
@Override
@SuppressWarnings("unchecked")
public synchronized Collection<X509Certificate> engineGetCertificates
    (CertSelector selector) throws CertStoreException {

    // if ldap URI we wrap the CertSelector in an LDAPCertSelector to
    // avoid LDAP DN matching issues (see LDAPCertSelector for more info)
    if (ldap) {
        X509CertSelector xsel = (X509CertSelector) selector;
        try {
            xsel = ldapHelper.wrap(xsel, xsel.getSubject(), ldapPath);
        } catch (IOException ioe) {
            throw new CertStoreException(ioe);
        }
        // Fetch the certificates via LDAP. LDAPCertStore has its own
        // caching mechanism, see the class description for more info.
        // Safe cast since xsel is an X509 certificate selector.
        return (Collection<X509Certificate>)
            ldapCertStore.getCertificates(xsel);
    }

    // Return the Certificates for this entry. It returns the cached value
    // if it is still current and fetches the Certificates otherwise.
    // For the caching details, see the top of this class.
    long time = System.currentTimeMillis();
    if (time - lastChecked < CHECK_INTERVAL) {
        if (debug != null) {
            debug.println("Returning certificates from cache");
        }
        return getMatchingCerts(certs, selector);
    }
    lastChecked = time;
    try {
        URLConnection connection = uri.toURL().openConnection();
        if (lastModified != 0) {
            connection.setIfModifiedSince(lastModified);
        }
        long oldLastModified = lastModified;
        try (InputStream in = connection.getInputStream()) {
            lastModified = connection.getLastModified();
            if (oldLastModified != 0) {
                if (oldLastModified == lastModified) {
                    if (debug != null) {
                        debug.println("Not modified, using cached copy");
                    }
                    return getMatchingCerts(certs, selector);
                } else if (connection instanceof HttpURLConnection) {
                    // some proxy servers omit last modified
                    HttpURLConnection hconn = (HttpURLConnection)connection;
                    if (hconn.getResponseCode()
                                == HttpURLConnection.HTTP_NOT_MODIFIED) {
                        if (debug != null) {
                            debug.println("Not modified, using cached copy");
                        }
                        return getMatchingCerts(certs, selector);
                    }
                }
            }
            if (debug != null) {
                debug.println("Downloading new certificates...");
            }
            // Safe cast since factory is an X.509 certificate factory
            certs = (Collection<X509Certificate>)
                factory.generateCertificates(in);
        }
        return getMatchingCerts(certs, selector);
    } catch (IOException | CertificateException e) {
        if (debug != null) {
            debug.println("Exception fetching certificates:");
            e.printStackTrace();
        }
    }
    // exception, forget previous values
    lastModified = 0;
    certs = Collections.emptySet();
    return certs;
}
 
Example 14
Source File: URICertStore.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a <code>Collection</code> of <code>X509Certificate</code>s that
 * match the specified selector. If no <code>X509Certificate</code>s
 * match the selector, an empty <code>Collection</code> will be returned.
 *
 * @param selector a <code>CertSelector</code> used to select which
 *  <code>X509Certificate</code>s should be returned. Specify
 *  <code>null</code> to return all <code>X509Certificate</code>s.
 * @return a <code>Collection</code> of <code>X509Certificate</code>s that
 *         match the specified selector
 * @throws CertStoreException if an exception occurs
 */
@Override
@SuppressWarnings("unchecked")
public synchronized Collection<X509Certificate> engineGetCertificates
    (CertSelector selector) throws CertStoreException {

    if (ldap) {
        // caching mechanism, see the class description for more info.
        return (Collection<X509Certificate>)
            ldapCertStore.getCertificates(selector);
    }

    // Return the Certificates for this entry. It returns the cached value
    // if it is still current and fetches the Certificates otherwise.
    // For the caching details, see the top of this class.
    long time = System.currentTimeMillis();
    if (time - lastChecked < CHECK_INTERVAL) {
        if (debug != null) {
            debug.println("Returning certificates from cache");
        }
        return getMatchingCerts(certs, selector);
    }
    lastChecked = time;
    try {
        URLConnection connection = uri.toURL().openConnection();
        if (lastModified != 0) {
            connection.setIfModifiedSince(lastModified);
        }
        long oldLastModified = lastModified;
        try (InputStream in = connection.getInputStream()) {
            lastModified = connection.getLastModified();
            if (oldLastModified != 0) {
                if (oldLastModified == lastModified) {
                    if (debug != null) {
                        debug.println("Not modified, using cached copy");
                    }
                    return getMatchingCerts(certs, selector);
                } else if (connection instanceof HttpURLConnection) {
                    // some proxy servers omit last modified
                    HttpURLConnection hconn = (HttpURLConnection)connection;
                    if (hconn.getResponseCode()
                                == HttpURLConnection.HTTP_NOT_MODIFIED) {
                        if (debug != null) {
                            debug.println("Not modified, using cached copy");
                        }
                        return getMatchingCerts(certs, selector);
                    }
                }
            }
            if (debug != null) {
                debug.println("Downloading new certificates...");
            }
            // Safe cast since factory is an X.509 certificate factory
            certs = (Collection<X509Certificate>)
                factory.generateCertificates(in);
        }
        return getMatchingCerts(certs, selector);
    } catch (IOException | CertificateException e) {
        if (debug != null) {
            debug.println("Exception fetching certificates:");
            e.printStackTrace();
        }
    }
    // exception, forget previous values
    lastModified = 0;
    certs = Collections.emptySet();
    return certs;
}
 
Example 15
Source File: HttpDownloadHelper.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
private URLConnection openConnection(URL aSource) throws IOException {

            // set up the URL connection
            URLConnection connection = aSource.openConnection();
            // modify the headers
            // NB: things like user authentication could go in here too.
            if (hasTimestamp) {
                connection.setIfModifiedSince(timestamp);
            }

            // in case the plugin manager is its own project, this can become an authenticator
            boolean isSecureProcotol = "https".equalsIgnoreCase(aSource.getProtocol());
            boolean isAuthInfoSet = !Strings.isNullOrEmpty(aSource.getUserInfo());
            if (isAuthInfoSet) {
                if (!isSecureProcotol) {
                    throw new IOException("Basic auth is only supported for HTTPS!");
                }
                String basicAuth = Base64.encodeBytes(aSource.getUserInfo().getBytes(StandardCharsets.UTF_8));
                connection.setRequestProperty("Authorization", "Basic " + basicAuth);
            }

            if (connection instanceof HttpURLConnection) {
                ((HttpURLConnection) connection).setInstanceFollowRedirects(false);
                connection.setUseCaches(true);
                connection.setConnectTimeout(5000);
            }
            connection.setRequestProperty("ES-Version", Version.CURRENT.toString());
            connection.setRequestProperty("ES-Build-Hash", Build.CURRENT.hashShort());
            connection.setRequestProperty("User-Agent", "elasticsearch-plugin-manager");

            // connect to the remote site (may take some time)
            connection.connect();

            // First check on a 301 / 302 (moved) response (HTTP only)
            if (connection instanceof HttpURLConnection) {
                HttpURLConnection httpConnection = (HttpURLConnection) connection;
                int responseCode = httpConnection.getResponseCode();
                if (responseCode == HttpURLConnection.HTTP_MOVED_PERM ||
                        responseCode == HttpURLConnection.HTTP_MOVED_TEMP ||
                        responseCode == HttpURLConnection.HTTP_SEE_OTHER) {
                    String newLocation = httpConnection.getHeaderField("Location");
                    URL newURL = new URL(newLocation);
                    if (!redirectionAllowed(aSource, newURL)) {
                        return null;
                    }
                    return openConnection(newURL);
                }
                // next test for a 304 result (HTTP only)
                long lastModified = httpConnection.getLastModified();
                if (responseCode == HttpURLConnection.HTTP_NOT_MODIFIED
                        || (lastModified != 0 && hasTimestamp && timestamp >= lastModified)) {
                    // not modified so no file download. just return
                    // instead and trace out something so the user
                    // doesn't think that the download happened when it
                    // didn't
                    return null;
                }
                // test for 401 result (HTTP only)
                if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
                    String message = "HTTP Authorization failure";
                    throw new IOException(message);
                }
            }

            //REVISIT: at this point even non HTTP connections may
            //support the if-modified-since behaviour -we just check
            //the date of the content and skip the write if it is not
            //newer. Some protocols (FTP) don't include dates, of
            //course.
            return connection;
        }
 
Example 16
Source File: URICertStore.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
     * Returns a <code>Collection</code> of <code>X509Certificate</code>s that
     * match the specified selector. If no <code>X509Certificate</code>s
     * match the selector, an empty <code>Collection</code> will be returned.
     *
     * @param selector a <code>CertSelector</code> used to select which
     *  <code>X509Certificate</code>s should be returned. Specify
     *  <code>null</code> to return all <code>X509Certificate</code>s.
     * @return a <code>Collection</code> of <code>X509Certificate</code>s that
     *         match the specified selector
     * @throws CertStoreException if an exception occurs
     */
    @Override
    @SuppressWarnings("unchecked")
    public synchronized Collection<X509Certificate> engineGetCertificates
        (CertSelector selector) throws CertStoreException {

        // if ldap URI we wrap the CertSelector in an LDAPCertSelector to
        // avoid LDAP DN matching issues (see LDAPCertSelector for more info)
//        if (ldap) {
//            X509CertSelector xsel = (X509CertSelector) selector;
//            try {
//                xsel = ldapHelper.wrap(xsel, xsel.getSubject(), ldapPath);
//            } catch (IOException ioe) {
//                throw new CertStoreException(ioe);
//            }
//            // Fetch the certificates via LDAP. LDAPCertStore has its own
//            // caching mechanism, see the class description for more info.
//            // Safe cast since xsel is an X509 certificate selector.
//            return (Collection<X509Certificate>)
//                ldapCertStore.getCertificates(xsel);
//        }

        // Return the Certificates for this entry. It returns the cached value
        // if it is still current and fetches the Certificates otherwise.
        // For the caching details, see the top of this class.
        long time = System.currentTimeMillis();
        if (time - lastChecked < CHECK_INTERVAL) {
            if (debug != null) {
                debug.println("Returning certificates from cache");
            }
            return getMatchingCerts(certs, selector);
        }
        lastChecked = time;
        try {
            URLConnection connection = uri.toURL().openConnection();
            if (lastModified != 0) {
                connection.setIfModifiedSince(lastModified);
            }
            long oldLastModified = lastModified;
            try (InputStream in = connection.getInputStream()) {
                lastModified = connection.getLastModified();
                if (oldLastModified != 0) {
                    if (oldLastModified == lastModified) {
                        if (debug != null) {
                            debug.println("Not modified, using cached copy");
                        }
                        return getMatchingCerts(certs, selector);
                    } else if (connection instanceof HttpURLConnection) {
                        // some proxy servers omit last modified
                        HttpURLConnection hconn = (HttpURLConnection)connection;
                        if (hconn.getResponseCode()
                                    == HttpURLConnection.HTTP_NOT_MODIFIED) {
                            if (debug != null) {
                                debug.println("Not modified, using cached copy");
                            }
                            return getMatchingCerts(certs, selector);
                        }
                    }
                }
                if (debug != null) {
                    debug.println("Downloading new certificates...");
                }
                // Safe cast since factory is an X.509 certificate factory
                certs = (Collection<X509Certificate>)
                    factory.generateCertificates(in);
            }
            return getMatchingCerts(certs, selector);
        } catch (IOException | CertificateException e) {
            if (debug != null) {
                debug.println("Exception fetching certificates:");
                e.printStackTrace();
            }
        }
        // exception, forget previous values
        lastModified = 0;
        certs = Collections.emptySet();
        return certs;
    }
 
Example 17
Source File: URICertStore.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a <code>Collection</code> of <code>X509Certificate</code>s that
 * match the specified selector. If no <code>X509Certificate</code>s
 * match the selector, an empty <code>Collection</code> will be returned.
 *
 * @param selector a <code>CertSelector</code> used to select which
 *  <code>X509Certificate</code>s should be returned. Specify
 *  <code>null</code> to return all <code>X509Certificate</code>s.
 * @return a <code>Collection</code> of <code>X509Certificate</code>s that
 *         match the specified selector
 * @throws CertStoreException if an exception occurs
 */
@Override
@SuppressWarnings("unchecked")
public synchronized Collection<X509Certificate> engineGetCertificates
    (CertSelector selector) throws CertStoreException {

    // if ldap URI we wrap the CertSelector in an LDAPCertSelector to
    // avoid LDAP DN matching issues (see LDAPCertSelector for more info)
    if (ldap) {
        X509CertSelector xsel = (X509CertSelector) selector;
        try {
            xsel = ldapHelper.wrap(xsel, xsel.getSubject(), ldapPath);
        } catch (IOException ioe) {
            throw new CertStoreException(ioe);
        }
        // Fetch the certificates via LDAP. LDAPCertStore has its own
        // caching mechanism, see the class description for more info.
        // Safe cast since xsel is an X509 certificate selector.
        return (Collection<X509Certificate>)
            ldapCertStore.getCertificates(xsel);
    }

    // Return the Certificates for this entry. It returns the cached value
    // if it is still current and fetches the Certificates otherwise.
    // For the caching details, see the top of this class.
    long time = System.currentTimeMillis();
    if (time - lastChecked < CHECK_INTERVAL) {
        if (debug != null) {
            debug.println("Returning certificates from cache");
        }
        return getMatchingCerts(certs, selector);
    }
    lastChecked = time;
    try {
        URLConnection connection = uri.toURL().openConnection();
        if (lastModified != 0) {
            connection.setIfModifiedSince(lastModified);
        }
        long oldLastModified = lastModified;
        try (InputStream in = connection.getInputStream()) {
            lastModified = connection.getLastModified();
            if (oldLastModified != 0) {
                if (oldLastModified == lastModified) {
                    if (debug != null) {
                        debug.println("Not modified, using cached copy");
                    }
                    return getMatchingCerts(certs, selector);
                } else if (connection instanceof HttpURLConnection) {
                    // some proxy servers omit last modified
                    HttpURLConnection hconn = (HttpURLConnection)connection;
                    if (hconn.getResponseCode()
                                == HttpURLConnection.HTTP_NOT_MODIFIED) {
                        if (debug != null) {
                            debug.println("Not modified, using cached copy");
                        }
                        return getMatchingCerts(certs, selector);
                    }
                }
            }
            if (debug != null) {
                debug.println("Downloading new certificates...");
            }
            // Safe cast since factory is an X.509 certificate factory
            certs = (Collection<X509Certificate>)
                factory.generateCertificates(in);
        }
        return getMatchingCerts(certs, selector);
    } catch (IOException | CertificateException e) {
        if (debug != null) {
            debug.println("Exception fetching certificates:");
            e.printStackTrace();
        }
    }
    // exception, forget previous values
    lastModified = 0;
    certs = Collections.emptySet();
    return certs;
}
 
Example 18
Source File: URICertStore.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a <code>Collection</code> of <code>X509Certificate</code>s that
 * match the specified selector. If no <code>X509Certificate</code>s
 * match the selector, an empty <code>Collection</code> will be returned.
 *
 * @param selector a <code>CertSelector</code> used to select which
 *  <code>X509Certificate</code>s should be returned. Specify
 *  <code>null</code> to return all <code>X509Certificate</code>s.
 * @return a <code>Collection</code> of <code>X509Certificate</code>s that
 *         match the specified selector
 * @throws CertStoreException if an exception occurs
 */
@Override
@SuppressWarnings("unchecked")
public synchronized Collection<X509Certificate> engineGetCertificates
    (CertSelector selector) throws CertStoreException {

    // if ldap URI we wrap the CertSelector in an LDAPCertSelector to
    // avoid LDAP DN matching issues (see LDAPCertSelector for more info)
    if (ldap) {
        X509CertSelector xsel = (X509CertSelector) selector;
        try {
            xsel = ldapHelper.wrap(xsel, xsel.getSubject(), ldapPath);
        } catch (IOException ioe) {
            throw new CertStoreException(ioe);
        }
        // Fetch the certificates via LDAP. LDAPCertStore has its own
        // caching mechanism, see the class description for more info.
        // Safe cast since xsel is an X509 certificate selector.
        return (Collection<X509Certificate>)
            ldapCertStore.getCertificates(xsel);
    }

    // Return the Certificates for this entry. It returns the cached value
    // if it is still current and fetches the Certificates otherwise.
    // For the caching details, see the top of this class.
    long time = System.currentTimeMillis();
    if (time - lastChecked < CHECK_INTERVAL) {
        if (debug != null) {
            debug.println("Returning certificates from cache");
        }
        return getMatchingCerts(certs, selector);
    }
    lastChecked = time;
    try {
        URLConnection connection = uri.toURL().openConnection();
        if (lastModified != 0) {
            connection.setIfModifiedSince(lastModified);
        }
        long oldLastModified = lastModified;
        try (InputStream in = connection.getInputStream()) {
            lastModified = connection.getLastModified();
            if (oldLastModified != 0) {
                if (oldLastModified == lastModified) {
                    if (debug != null) {
                        debug.println("Not modified, using cached copy");
                    }
                    return getMatchingCerts(certs, selector);
                } else if (connection instanceof HttpURLConnection) {
                    // some proxy servers omit last modified
                    HttpURLConnection hconn = (HttpURLConnection)connection;
                    if (hconn.getResponseCode()
                                == HttpURLConnection.HTTP_NOT_MODIFIED) {
                        if (debug != null) {
                            debug.println("Not modified, using cached copy");
                        }
                        return getMatchingCerts(certs, selector);
                    }
                }
            }
            if (debug != null) {
                debug.println("Downloading new certificates...");
            }
            // Safe cast since factory is an X.509 certificate factory
            certs = (Collection<X509Certificate>)
                factory.generateCertificates(in);
        }
        return getMatchingCerts(certs, selector);
    } catch (IOException | CertificateException e) {
        if (debug != null) {
            debug.println("Exception fetching certificates:");
            e.printStackTrace();
        }
    }
    // exception, forget previous values
    lastModified = 0;
    certs = Collections.emptySet();
    return certs;
}
 
Example 19
Source File: URICertStore.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a <code>Collection</code> of <code>X509Certificate</code>s that
 * match the specified selector. If no <code>X509Certificate</code>s
 * match the selector, an empty <code>Collection</code> will be returned.
 *
 * @param selector a <code>CertSelector</code> used to select which
 *  <code>X509Certificate</code>s should be returned. Specify
 *  <code>null</code> to return all <code>X509Certificate</code>s.
 * @return a <code>Collection</code> of <code>X509Certificate</code>s that
 *         match the specified selector
 * @throws CertStoreException if an exception occurs
 */
@Override
@SuppressWarnings("unchecked")
public synchronized Collection<X509Certificate> engineGetCertificates
    (CertSelector selector) throws CertStoreException {

    // if ldap URI we wrap the CertSelector in an LDAPCertSelector to
    // avoid LDAP DN matching issues (see LDAPCertSelector for more info)
    if (ldap) {
        X509CertSelector xsel = (X509CertSelector) selector;
        try {
            xsel = ldapHelper.wrap(xsel, xsel.getSubject(), ldapPath);
        } catch (IOException ioe) {
            throw new CertStoreException(ioe);
        }
        // Fetch the certificates via LDAP. LDAPCertStore has its own
        // caching mechanism, see the class description for more info.
        // Safe cast since xsel is an X509 certificate selector.
        return (Collection<X509Certificate>)
            ldapCertStore.getCertificates(xsel);
    }

    // Return the Certificates for this entry. It returns the cached value
    // if it is still current and fetches the Certificates otherwise.
    // For the caching details, see the top of this class.
    long time = System.currentTimeMillis();
    if (time - lastChecked < CHECK_INTERVAL) {
        if (debug != null) {
            debug.println("Returning certificates from cache");
        }
        return getMatchingCerts(certs, selector);
    }
    lastChecked = time;
    try {
        URLConnection connection = uri.toURL().openConnection();
        if (lastModified != 0) {
            connection.setIfModifiedSince(lastModified);
        }
        long oldLastModified = lastModified;
        try (InputStream in = connection.getInputStream()) {
            lastModified = connection.getLastModified();
            if (oldLastModified != 0) {
                if (oldLastModified == lastModified) {
                    if (debug != null) {
                        debug.println("Not modified, using cached copy");
                    }
                    return getMatchingCerts(certs, selector);
                } else if (connection instanceof HttpURLConnection) {
                    // some proxy servers omit last modified
                    HttpURLConnection hconn = (HttpURLConnection)connection;
                    if (hconn.getResponseCode()
                                == HttpURLConnection.HTTP_NOT_MODIFIED) {
                        if (debug != null) {
                            debug.println("Not modified, using cached copy");
                        }
                        return getMatchingCerts(certs, selector);
                    }
                }
            }
            if (debug != null) {
                debug.println("Downloading new certificates...");
            }
            // Safe cast since factory is an X.509 certificate factory
            certs = (Collection<X509Certificate>)
                factory.generateCertificates(in);
        }
        return getMatchingCerts(certs, selector);
    } catch (IOException | CertificateException e) {
        if (debug != null) {
            debug.println("Exception fetching certificates:");
            e.printStackTrace();
        }
    }
    // exception, forget previous values
    lastModified = 0;
    certs = Collections.emptySet();
    return certs;
}
 
Example 20
Source File: URICertStore.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a <code>Collection</code> of <code>X509Certificate</code>s that
 * match the specified selector. If no <code>X509Certificate</code>s
 * match the selector, an empty <code>Collection</code> will be returned.
 *
 * @param selector a <code>CertSelector</code> used to select which
 *  <code>X509Certificate</code>s should be returned. Specify
 *  <code>null</code> to return all <code>X509Certificate</code>s.
 * @return a <code>Collection</code> of <code>X509Certificate</code>s that
 *         match the specified selector
 * @throws CertStoreException if an exception occurs
 */
@Override
@SuppressWarnings("unchecked")
public synchronized Collection<X509Certificate> engineGetCertificates
    (CertSelector selector) throws CertStoreException {

    // if ldap URI we wrap the CertSelector in an LDAPCertSelector to
    // avoid LDAP DN matching issues (see LDAPCertSelector for more info)
    if (ldap) {
        X509CertSelector xsel = (X509CertSelector) selector;
        try {
            xsel = ldapHelper.wrap(xsel, xsel.getSubject(), ldapPath);
        } catch (IOException ioe) {
            throw new CertStoreException(ioe);
        }
        // Fetch the certificates via LDAP. LDAPCertStore has its own
        // caching mechanism, see the class description for more info.
        // Safe cast since xsel is an X509 certificate selector.
        return (Collection<X509Certificate>)
            ldapCertStore.getCertificates(xsel);
    }

    // Return the Certificates for this entry. It returns the cached value
    // if it is still current and fetches the Certificates otherwise.
    // For the caching details, see the top of this class.
    long time = System.currentTimeMillis();
    if (time - lastChecked < CHECK_INTERVAL) {
        if (debug != null) {
            debug.println("Returning certificates from cache");
        }
        return getMatchingCerts(certs, selector);
    }
    lastChecked = time;
    try {
        URLConnection connection = uri.toURL().openConnection();
        if (lastModified != 0) {
            connection.setIfModifiedSince(lastModified);
        }
        long oldLastModified = lastModified;
        try (InputStream in = connection.getInputStream()) {
            lastModified = connection.getLastModified();
            if (oldLastModified != 0) {
                if (oldLastModified == lastModified) {
                    if (debug != null) {
                        debug.println("Not modified, using cached copy");
                    }
                    return getMatchingCerts(certs, selector);
                } else if (connection instanceof HttpURLConnection) {
                    // some proxy servers omit last modified
                    HttpURLConnection hconn = (HttpURLConnection)connection;
                    if (hconn.getResponseCode()
                                == HttpURLConnection.HTTP_NOT_MODIFIED) {
                        if (debug != null) {
                            debug.println("Not modified, using cached copy");
                        }
                        return getMatchingCerts(certs, selector);
                    }
                }
            }
            if (debug != null) {
                debug.println("Downloading new certificates...");
            }
            // Safe cast since factory is an X.509 certificate factory
            certs = (Collection<X509Certificate>)
                factory.generateCertificates(in);
        }
        return getMatchingCerts(certs, selector);
    } catch (IOException | CertificateException e) {
        if (debug != null) {
            debug.println("Exception fetching certificates:");
            e.printStackTrace();
        }
    }
    // exception, forget previous values
    lastModified = 0;
    certs = Collections.emptySet();
    return certs;
}