Java Code Examples for java.net.httpurlconnection#getHeaderFieldDate()

The following examples show how to use java.net.httpurlconnection#getHeaderFieldDate() . 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: Downloads.java    From openwebbeans-meecrowave with Apache License 2.0 6 votes vote down vote up
private static Download fillDownloadable(final Download download) {
    try {
        final URL url = new URL(download.mavenCentralUrl);
        final HttpURLConnection connection = HttpURLConnection.class.cast(url.openConnection());
        connection.setConnectTimeout((int) TimeUnit.SECONDS.toMillis(30));
        final int responseCode = connection.getResponseCode();
        if (responseCode != HttpURLConnection.HTTP_OK) {
            if (HttpURLConnection.HTTP_NOT_FOUND != responseCode) {
                System.err.println("Got " + responseCode + " for " + download.url);
            }
            return null;
        }

        long lastMod = connection.getHeaderFieldDate("Last-Modified", 0);
        download.date = Instant.ofEpochMilli(lastMod);

        download.size = toSize(ofNullable(connection.getHeaderField("Content-Length"))
                .map(Long::parseLong).orElse(0L), ofNullable(connection.getHeaderField("Accept-Ranges")).orElse("bytes"));

        connection.getInputStream().close();
    } catch (final IOException e) {
        e.printStackTrace();
        return null;
    }
    return download;
}
 
Example 2
Source File: NetStorageCMSv35Signer.java    From NetStorageKit-Java with Apache License 2.0 6 votes vote down vote up
/**
 * Attempt to validate the response and detect common causes of errors. The most common being time drift.
 * <p>
 * TODO: catch rate limitting errors. Should delay and retry.
 *
 * @param connection an open url connection
 * @return true if 200 OK response, false otherwise.
 * @throws NetStorageException wrapped exception if it is a recoverable exception
 * @throws IOException         shouldn't be called at this point, but could be triggered when interrogating the response
 */
public boolean validate(HttpURLConnection connection) throws NetStorageException, IOException {
    if (connection.getResponseCode() == HttpURLConnection.HTTP_OK)
        return true;

    // Validate Server-Time drift
    Date currentDate = new Date();
    long responseDate = connection.getHeaderFieldDate("Date", 0);
    if ((responseDate != 0 && currentDate.getTime() - responseDate > 30 * 1000)
        || (responseDate != 0 && (currentDate.getTime() - responseDate) * -1 > 30 * 1000))
        throw new NetStorageException("Local server Date is more than 30s out of sync with Remote server");

    // generic response
    throw new NetStorageException(String.format("Unexpected Response from Server: %d %s\n%s",
            connection.getResponseCode(), connection.getResponseMessage(), connection.getHeaderFields()), connection.getResponseCode());
}
 
Example 3
Source File: NetworkTimeProvider.java    From google-authenticator-android with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the system time by issuing a request over the network.
 *
 * @return time (milliseconds since epoch).
 *
 * @throws IOException if an I/O error occurs.
 */
public long getNetworkTime() throws IOException {
  HttpURLConnection urlConnection = mUrlConnectionFactory.openHttpUrl(TIME_SOURCE_URL);
  urlConnection.setRequestMethod("HEAD");
  int responseCode = urlConnection.getResponseCode();
  if (responseCode != HttpURLConnection.HTTP_OK) {
    Log.d(LOG_TAG, String.format("URL %s returned %d", TIME_SOURCE_URL, responseCode));
    throw new IOException(String.format("HTTP status code %d", responseCode));
  }
  long date = urlConnection.getHeaderFieldDate(DATE_HEADER, 0);
  if (date == 0) {
    String dateHeader = urlConnection.getHeaderField(DATE_HEADER);
    throw new IOException("Got missing or invalid date from header value " + dateHeader);
  }
  Log.d(LOG_TAG, String.format("Got date value %d", date));
  return date;
}
 
Example 4
Source File: DailyDumps.java    From NationStatesPlusPlus with MIT License 5 votes vote down vote up
/**
 * Checks to see if an update to the region daily dump, and if so, downloads the update.
 */
private void updateRegionsDump() {
	InputStream stream = null;
	try {
		HttpURLConnection conn = (HttpURLConnection)(new URL(REGIONS_URL)).openConnection();
		conn.setRequestProperty("User-Agent", userAgent);
		conn.connect();
		final long contentLength = conn.getContentLengthLong();
		final long time = conn.getHeaderFieldDate("Last-Modified", -1);
		final DateTime serverModified = new DateTime(time, DateTimeZone.forOffsetHours(0)); //set to UTC time
		Logger.info("Checking region dump for {}, length: {}, lastModified: {}", serverModified, contentLength, serverModified);
		
		File regionsDump = new File(regionsDir, serverModified.toString(FILE_DATE) + "-regions.xml.gz");
		latestRegionDump.set(regionsDump);
		
		stream = conn.getInputStream();
		if (!regionsDump.exists() || regionsDump.length() != contentLength) {
			Logger.info("Saving regions dump to " + regionsDump.getAbsolutePath());
			try (FileOutputStream fos = new FileOutputStream(regionsDump)) {
				IOUtils.copy(stream, fos);
				Logger.info("Saved regions dump, size: {}", regionsDump.length());
			}
		} else {
			Logger.debug("Regions dump is up to date");
		}
	} catch (IOException e) {
		Logger.error("Unable to process regions dump", e);
	} finally {
		IOUtils.closeQuietly(stream);
	}
}
 
Example 5
Source File: DailyDumps.java    From NationStatesPlusPlus with MIT License 5 votes vote down vote up
/**
 * Checks to see if an update to the nation daily dump, and if so, downloads the update.
 * 
 * Will create a DumpUpdateTask thread after the update is downloaded.
 */
private void updateNationsDump() {
	InputStream stream = null;
	try {
		HttpURLConnection conn = (HttpURLConnection)(new URL(NATIONS_URL)).openConnection();
		conn.setRequestProperty("User-Agent", userAgent);
		conn.connect();
		final long contentLength = conn.getContentLengthLong();
		final long time = conn.getHeaderFieldDate("Last-Modified", -1);
		final DateTime serverModified = new DateTime(time, DateTimeZone.forOffsetHours(0)); //set to UTC
		
		Logger.info("Checking nations dump, length: {}, lastModified: {}", contentLength, serverModified);
		File nationsDump = new File(nationsDir, serverModified.toString(FILE_DATE) + "-nations.xml.gz");
		latestNationDump.set(nationsDump);
		
		stream = conn.getInputStream();
		if (!nationsDump.exists() || nationsDump.length() != contentLength) {
			Logger.info("Saving nations dump to " + nationsDump.getAbsolutePath());
			try (FileOutputStream fos = new FileOutputStream(nationsDump)) {
				IOUtils.copy(stream, fos);
				Logger.info("Saved nations dump successfully, size: {}", nationsDump.length());
			}
			(new Thread(new DumpUpdateTask(access, getMostRecentRegionDump(), nationsDump), "Daily Dump Update Thread")).start();
		} else {
			Logger.debug("Nations dump is up to date");
		}
	} catch (IOException e) {
		Logger.error("Unable to process nations dump", e);
	} finally {
		IOUtils.closeQuietly(stream);
	}
}
 
Example 6
Source File: SimpleFileDownloader.java    From pf4j-update with Apache License 2.0 4 votes vote down vote up
/**
 * Downloads file from HTTP or FTP.
 *
 * @param fileUrl source file
 * @return path of downloaded file
 * @throws IOException if IO problems
 * @throws PluginRuntimeException if validation fails or any other problems
 */
protected Path downloadFileHttp(URL fileUrl) throws IOException {
    Path destination = Files.createTempDirectory("pf4j-update-downloader");
    destination.toFile().deleteOnExit();

    String path = fileUrl.getPath();
    String fileName = path.substring(path.lastIndexOf('/') + 1);
    Path file = destination.resolve(fileName);

    // set up the URL connection
    URLConnection connection = fileUrl.openConnection();

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

    // check for http authorization
    HttpURLConnection httpConnection = (HttpURLConnection) connection;
    if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) {
        throw new ConnectException("HTTP Authorization failure");
    }

    // try to get the server-specified last-modified date of this artifact
    long lastModified = httpConnection.getHeaderFieldDate("Last-Modified", System.currentTimeMillis());

    // try to get the input stream (three times)
    InputStream is = null;
    for (int i = 0; i < 3; i++) {
        try {
            is = connection.getInputStream();
            break;
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }
    if (is == null) {
        throw new ConnectException("Can't get '" + fileUrl + " to '" + file + "'");
    }

    // reade from remote resource and write to the local file
    FileOutputStream fos = new FileOutputStream(file.toFile());
    byte[] buffer = new byte[1024];
    int length;
    while ((length = is.read(buffer)) >= 0) {
        fos.write(buffer, 0, length);
    }
    fos.close();
    is.close();

    log.debug("Set last modified of '{}' to '{}'", file, lastModified);
    Files.setLastModifiedTime(file, FileTime.fromMillis(lastModified));

    return file;
}