@Override public long getLastModified() { return delegate.getLastModified(); }
@Override public long getLastModified() { return delegate.getLastModified(); }
@Override protected Optional<Instant> dataStamp() { try { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(HEAD_METHOD); if (connection.getLastModified() != 0) { return Optional.of(Instant.ofEpochMilli(connection.getLastModified())); } } catch (IOException ex) { LOGGER.log(Level.FINE, ex, () -> "Configuration at url '" + url + "' HEAD is not accessible."); } Optional<Instant> timestamp = Optional.of(Instant.MAX); LOGGER.finer("Missing HEAD '" + url + "' response header 'Last-Modified'. Used time '" + timestamp + "' as a content timestamp."); return timestamp; }
@Override protected Optional<Instant> dataStamp() { try { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(HEAD_METHOD); if (connection.getLastModified() != 0) { return Optional.of(Instant.ofEpochMilli(connection.getLastModified())); } } catch (IOException ex) { LOGGER.log(Level.FINE, ex, () -> "Configuration at url '" + url + "' HEAD is not accessible."); } Optional<Instant> timestamp = Optional.of(Instant.MAX); LOGGER.finer("Missing HEAD '" + url + "' response header 'Last-Modified'. Used time '" + timestamp + "' as a content timestamp."); return timestamp; }
@Override protected ConfigParser.Content content() throws ConfigException { try { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(GET_METHOD); final String mediaType = mediaType(connection.getContentType()); final Optional<Instant> timestamp; if (connection.getLastModified() != 0) { timestamp = Optional.of(Instant.ofEpochMilli(connection.getLastModified())); } else { timestamp = Optional.of(Instant.now()); LOGGER.fine("Missing GET '" + url + "' response header 'Last-Modified'. Used current time '" + timestamp + "' as a content timestamp."); } Reader reader = new InputStreamReader(connection.getInputStream(), ConfigUtils.getContentCharset(connection.getContentEncoding())); return ConfigParser.Content.create(reader, mediaType, timestamp); } catch (ConfigException ex) { throw ex; } catch (Exception ex) { throw new ConfigException("Configuration at url '" + url + "' GET is not accessible.", ex); } }
@Override protected Data<OverrideData, Instant> loadData() throws ConfigException { try { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(GET_METHOD); Instant timestamp; if (connection.getLastModified() != 0) { timestamp = Instant.ofEpochMilli(connection.getLastModified()); } else { timestamp = Instant.now(); LOGGER.fine("Missing GET '" + url + "' response header 'Last-Modified'. Used current time '" + timestamp + "' as a content timestamp."); } Reader reader = new InputStreamReader(connection.getInputStream(), ConfigUtils.getContentCharset(connection.getContentEncoding())); return new Data<>(Optional.of(OverrideData.create(reader)), Optional.of(timestamp)); } catch (ConfigException ex) { throw ex; } catch (Exception ex) { throw new ConfigException("Configuration at url '" + url + "' GET is not accessible.", ex); } }
String contentType = httpConn.getContentType(); long contentLengthLong = httpConn.getContentLengthLong(); long lastModified = httpConn.getLastModified(); LOGGER.log(Level.INFO, "Reading " + (contentLengthLong < 0L ? "?"
final int t = conn.getResponseCode(); if (t >= 200 && t < 300) { timestamp = conn.getLastModified(); } else { throw new DownloadFailedException(format("%s request returned a non-200 status code: %s", httpMethod, url));
final long lastModified = httpConnection.getLastModified(); if (responseCode == HttpURLConnection.HTTP_NOT_MODIFIED || (lastModified != 0 && hasTimestamp && timestamp >= lastModified)) {
/** * Checks if we should read from the URL. * * @param connection The connection that was established. * @return true if there was no problem. * @throws IOException If an I/O Exception occured. */ boolean validateResponse(HttpURLConnection connection) throws IOException { Log.d(TAG, "validateResponse: " + item.title + ": local = " + new Date(connection.getIfModifiedSince()) + " remote = " + new Date(connection.getLastModified())); if (connection.getResponseCode() != 200) { Log.d(TAG, "validateResponse: " + item.title + ": Skipping: Server responded with " + connection.getResponseCode() + " for " + item.location); if (connection.getResponseCode() == 404) { parentTask.addError(item, context.getString(R.string.file_not_found)); } else if (connection.getResponseCode() != 304) { context.getResources().getString(R.string.host_update_error_item); parentTask.addError(item, context.getResources().getString(R.string.host_update_error_item, connection.getResponseCode(), connection.getResponseMessage())); } return false; } return true; }
/** * Downloads a file from a connection to an singleWriterMultipleReaderFile. * * @param file The file to write to * @param singleWriterMultipleReaderFile The atomic file for the destination file * @param connection The connection to read from * @throws IOException I/O exceptions. */ void downloadFile(File file, SingleWriterMultipleReaderFile singleWriterMultipleReaderFile, HttpURLConnection connection) throws IOException { InputStream inStream = connection.getInputStream(); FileOutputStream outStream = singleWriterMultipleReaderFile.startWrite(); try { copyStream(inStream, outStream); singleWriterMultipleReaderFile.finishWrite(outStream); outStream = null; // Write has started, set modification time. if (connection.getLastModified() == 0 || !file.setLastModified(connection.getLastModified())) { Log.d(TAG, "downloadFile: Could not set last modified"); } } finally { if (outStream != null) singleWriterMultipleReaderFile.failWrite(outStream); } }
/** * Creates an input stream for the URI, assuming it's a URL, and returns it. * * @return an open input stream. * @exception IOException if there is a problem obtaining an open input stream. */ @Override public InputStream createInputStream(URI uri, Map<?, ?> options) throws IOException { try { final HttpURLConnection httpConnection = getConnection(uri); httpConnection.setConnectTimeout(connectionTimeout); httpConnection.setReadTimeout(readTimeout); InputStream result = httpConnection.getInputStream(); Map<Object, Object> response = getResponse(options); if (response != null) { response.put( URIConverter.RESPONSE_TIME_STAMP_PROPERTY, httpConnection.getLastModified()); } return result; } catch (RuntimeException exception) { throw new Resource.IOWrappedException(exception); } }
long lastModified = httpConnection.getLastModified(); if (responseCode == HttpURLConnection.HTTP_NOT_MODIFIED || (lastModified != 0 && hasTimestamp && timestamp >= lastModified)) {
long lastModified = httpConnection.getLastModified(); if (responseCode == HttpURLConnection.HTTP_NOT_MODIFIED || (lastModified != 0 && hasTimestamp && timestamp >= lastModified)) {
long lastModified = httpConnection.getLastModified(); if (responseCode == HttpURLConnection.HTTP_NOT_MODIFIED || (lastModified != 0 && hasTimestamp && timestamp >= lastModified)) {
/** * Get last modified header * * @return last modified */ long getLastModified() { return delegateUrlConnexion.getLastModified(); }
when(connection.getLastModified()).thenReturn(0L); when(file.setLastModified(anyLong())).then(setLastModifiedAnswerFalse); when(connection.getLastModified()).thenReturn(0L); when(file.setLastModified(anyLong())).then(setLastModifiedAnswerTrue); when(connection.getLastModified()).thenReturn(1L); when(file.setLastModified(anyLong())).then(setLastModifiedAnswerFalse); when(connection.getLastModified()).thenReturn(1L); when(file.setLastModified(anyLong())).then(setLastModifiedAnswerTrue);
private static void logIncoming(HttpURLConnection connection) throws Exception { LOG.info("< Content-Length: " + connection.getContentLength()); LOG.info("< HTTP/1.1 Response: " + String.valueOf(connection.getResponseCode()) + " " + connection.getResponseMessage()); LOG.info("< Transfer-Encoding: " + connection.getContentEncoding()); LOG.info("< Content-Type: " + connection.getContentType()); LOG.info("< Date: " + connection.getLastModified()); LOG.info("< Response Content: "); try (InputStream is = connection.getInputStream()) { LOG.debug(readFullyAsString(is)); } }
@Override public long getLastModified () { handshake(); return this.connection.getLastModified(); }
@Override public void visit(AppQueryRequest.Response rsp) throws Exception { if (responseCode == HttpURLConnection.HTTP_OK) { long timeStamp = conn.getLastModified(); rsp.setTimeStamp(new Date(timeStamp)); rsp.setAppQueryJson(extractText()); } else if (responseCode == HttpURLConnection.HTTP_NOT_MODIFIED) { rsp.setAppQueryJson(null); rsp.setTimeStamp(null); } }