private void updatePropertiesFromResponse(HttpURLConnection request) { // ETag this.getProperties().setEtag(request.getHeaderField(Constants.HeaderConstants.ETAG)); // Last Modified if (0 != request.getLastModified()) { final Calendar lastModifiedCalendar = Calendar.getInstance(Utility.LOCALE_US); lastModifiedCalendar.setTimeZone(Utility.UTC_ZONE); lastModifiedCalendar.setTime(new Date(request.getLastModified())); this.getProperties().setLastModified(lastModifiedCalendar.getTime()); } }
/** * Images proxy to be able to load images from the map * * @param encoding * @param cache * @param modified * @param match * @param url * @return */ @GET @Path("image-proxy") @Produces("image/*") public Response getThumbnail( @HeaderParam("Accept-Encoding") String encoding, @HeaderParam("If-Modified-Since") String cache, @HeaderParam("If-Modified-Since") String modified, @HeaderParam("If-None-Match") String match, @QueryParam("url") String url) { UrlHelper urlHelper = new UrlHelper(url); try { urlHelper.addHeader("Accept-Encoding", encoding); urlHelper.addHeader("If-Modified-Since", modified); urlHelper.addHeader("If-None-Match", match); urlHelper.addHeader("Cache-Control", cache); urlHelper.openConnections(); HttpURLConnection connection = (HttpURLConnection) urlHelper.getConnection(); String tag = connection.getHeaderField("Etag"); if (tag == null) tag = ""; if (tag.startsWith("\"")) tag = tag.substring(1); if (tag.endsWith("\"")) tag = tag.substring(0, tag.length() - 1); return Response.ok(urlHelper.getStream(), urlHelper.getContentType()) .lastModified(new Date(connection.getLastModified())) .tag(tag) .status(connection.getResponseCode()) .header("Content-Length", connection.getContentLength()) .build(); } catch (Exception e) { Response.status(Responses.NOT_FOUND); } return null; }
public Date getLastModified() throws IOException { HttpURLConnection connection = head(); if (connection == null) throw new IOException("missing resource"); long millis = connection.getLastModified(); return new Date(millis == 0L ? System.currentTimeMillis() : millis); }
public static HttpURLConnection download(final HttpURLConnection con, final File file) throws IOException { if (file.exists()) { con.setIfModifiedSince(file.lastModified()); con.connect(); if (con.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) { log.fine("Using " + file.getName() + " from cache"); con.disconnect(); return con; } } log.fine("Downloading new " + file.getName()); final byte[] buffer = downloadBinary(con); if (!file.exists()) { file.createNewFile(); } if (file.exists() && (!file.canRead() || file.canWrite())) { file.setReadable(true); file.setWritable(true); } if (file.exists() && file.canRead() && file.canWrite()) { final FileOutputStream fos = new FileOutputStream(file); fos.write(buffer); fos.flush(); fos.close(); } file.setLastModified(con.getLastModified()); con.disconnect(); return con; }
/** * Makes an HTTP Head request to retrieve the last modified date of the given URL. If the file:// * protocol is specified, then the lastTimestamp of the file is returned. * * @param url the URL to retrieve the timestamp from * @return an epoch timestamp * @throws DownloadFailedException is thrown if an exception occurs making the HTTP request */ public static long getLastModified(URL url) throws DownloadFailedException { long timestamp = 0; // TODO add the FTP protocol? if ("file".equalsIgnoreCase(url.getProtocol())) { File lastModifiedFile; try { lastModifiedFile = new File(url.toURI()); } catch (URISyntaxException ex) { final String msg = format("Unable to locate '%s'", url.toString()); throw new DownloadFailedException(msg); } timestamp = lastModifiedFile.lastModified(); } else { final String httpMethod = determineHttpMethod(); HttpURLConnection conn = null; try { conn = URLConnectionFactory.createHttpURLConnection(url); conn.setRequestMethod(httpMethod); conn.connect(); 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", httpMethod)); } } catch (URLConnectionFailureException ex) { throw new DownloadFailedException( format("Error creating URL Connection for HTTP %s request.", httpMethod), ex); } catch (IOException ex) { analyzeException(ex); try { // retry if (!Settings.getBoolean(Settings.KEYS.DOWNLOADER_QUICK_QUERY_TIMESTAMP)) { Settings.setBoolean(Settings.KEYS.DOWNLOADER_QUICK_QUERY_TIMESTAMP, true); return getLastModified(url); } } catch (InvalidSettingException ex1) { LOGGER.debug("invalid setting?", ex); } throw new DownloadFailedException(format("Error making HTTP %s request.", httpMethod), ex); } finally { if (conn != null) { try { conn.disconnect(); } finally { conn = null; } } } } return timestamp; }
public static void main(String args[]) { for (int i = 0; i < args.length; i++) { try { URL u = new URL(args[i]); HttpURLConnection http = (HttpURLConnection) u.openConnection(); http.setRequestMethod("HEAD"); System.out.println(u + "was last modified at " + new Date(http.getLastModified())); } // end try catch (MalformedURLException ex) { System.err.println(args[i] + " is not a URL I understand"); } catch (IOException ex) { System.err.println(ex); } System.out.println(); } // end for } // end main
/** * Performs a <code>HEAD</code> request for retrieving the <code>LastModified</code> header value. */ protected static boolean isTileNewer(TileStoreEntry tile, HttpMapSource mapSource) throws IOException { long oldLastModified = tile.getTimeLastModified(); if (oldLastModified <= 0) { log.warn( "Tile age comparison not possible: " + "tile in tilestore does not contain lastModified attribute"); return true; } HttpURLConnection conn = mapSource.getTileUrlConnection(tile.getZoom(), tile.getX(), tile.getY()); conn.setRequestMethod("HEAD"); conn.setRequestProperty("Accept", ACCEPT); long newLastModified = conn.getLastModified(); if (newLastModified == 0) return true; return (newLastModified > oldLastModified); }
public static byte[] downloadTileAndUpdateStore( int x, int y, int zoom, HttpMapSource mapSource, boolean useTileStore) throws UnrecoverableDownloadException, IOException, InterruptedException { if (zoom < 0) throw new UnrecoverableDownloadException("Negative zoom!"); HttpURLConnection conn = mapSource.getTileUrlConnection(zoom, x, y); if (conn == null) throw new UnrecoverableDownloadException( "Tile x=" + x + " y=" + y + " zoom=" + zoom + " is not a valid tile in map source " + mapSource); log.trace("Downloading " + conn.getURL()); prepareConnection(conn); conn.connect(); int code = conn.getResponseCode(); byte[] data = loadBodyDataInBuffer(conn); if (code != HttpURLConnection.HTTP_OK) throw new DownloadFailedException(conn, code); checkContentType(conn, data); checkContentLength(conn, data); String eTag = conn.getHeaderField("ETag"); long timeLastModified = conn.getLastModified(); long timeExpires = conn.getExpiration(); Utilities.checkForInterruption(); TileImageType imageType = Utilities.getImageType(data); if (imageType == null) throw new UnrecoverableDownloadException("The returned image is of unknown format"); if (useTileStore) { TileStore.getInstance() .putTileData(data, x, y, zoom, mapSource, timeLastModified, timeExpires, eTag); } Utilities.checkForInterruption(); return data; }
/** * Simple ImageProperty fetcher based on URL / ImageType * * @param url _ * @param imageType _ * @return _ * @throws NullPointerException _ * @throws IOException _ */ public static ImageElement getImageProperties(final URL url, final ImageType imageType) throws NullPointerException, IOException { if (verifyNotNull(url, imageType)) { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); final long lastModified = connection.getLastModified(); final BufferedImage bufferedImage = ImageIO.read(connection.getInputStream()); final String mimeType = connection.getHeaderField("Content-Type"); if (!verifyNotNull(bufferedImage)) { throw new IOException("Unable to load image from resource (URL)"); } connection.disconnect(); return fetchImageProperties( bufferedImage, imageType, getFileName(url.getFile(), true), new Date(lastModified), mimeType); } throw new NullPointerException("URL or ImageType was null"); }
protected void setResponseHeader(HttpURLConnection conn) { conn.getLastModified(); Map<String, List<String>> header = conn.getHeaderFields(); if (null == header) return; Iterator<Entry<String, List<String>>> iterator = header.entrySet().iterator(); while (iterator.hasNext()) { Entry<String, List<String>> entry = (Entry<String, List<String>>) iterator.next(); List<String> values = entry.getValue(); int i = 0; String value = ""; for (String s : values) { if (i > 0) { value += "<--->"; } value += s; i++; } mResponseHeader.put(String.valueOf(entry.getKey()).toLowerCase(Locale.getDefault()), value); } }
/** * Returns true if the resource at the given remote URI is newer than the resource cached locally. */ private static boolean isUpdateAvailable(URI remoteUri, File localFile) { // if (DataManager.isOffline()) { // // don't bother checking for updates when we're offline // return false; // } URLConnection conn; try { conn = remoteUri.toURL().openConnection(); } catch (MalformedURLException ex) { ex.printStackTrace(); return false; } catch (IOException ex) { ex.printStackTrace(); return false; } if (!(conn instanceof HttpURLConnection)) { // don't bother with non-http connections return false; } long localLastMod = localFile.lastModified(); long remoteLastMod = 0L; HttpURLConnection httpconn = (HttpURLConnection) conn; // disable caching so we don't get in feedback loop with ResponseCache httpconn.setUseCaches(false); try { httpconn.connect(); remoteLastMod = httpconn.getLastModified(); } catch (IOException ex) { // ex.printStackTrace(); return false; } finally { httpconn.disconnect(); } return (remoteLastMod > localLastMod); }
public static void main(String[] args) { try { // Se obtiene el objeto URL. URL url = new URL("http://getbootstrap.com"); // Se abre la conexión y se hace a HttpURLConnection, ya que es una // conexión HTTP. HttpURLConnection conexion = (HttpURLConnection) url.openConnection(); // Se obtienen los datos de la cabecera de la respuesta y se // muestran por pantalla. // Campos con método get propio. System.out.println("\nCABECERA DE LA RESPUESTA\n"); System.out.println("CAMPOS CON MÉTODO GET PROPIO\n"); System.out.println("Método de petición [getRequestMethod()]: " + conexion.getRequestMethod()); System.out.println("Fecha petición [getDate()]: " + new Date(conexion.getDate())); System.out.println("Código de respuesta [getResponseCode()]: " + conexion.getResponseCode()); System.out.println( "Mensaje de respuesta [getResponseMessage()]: " + conexion.getResponseMessage()); System.out.println( "Fecha última modificación [getLastModified()]: " + new Date(conexion.getLastModified())); System.out.println("Tipo de contenido [getContentType()]: " + conexion.getContentType()); System.out.println("Codificación [getContentEncoding()]: " + conexion.getContentEncoding()); System.out.println( "Tamaño del contenido [getContentLength()]: " + conexion.getContentLength()); System.out.println( "Fecha expiración [getExpiration()]: " + new Date(conexion.getExpiration())); // Todos los campos de la cabecera. System.out.println("\nTODOS LOS CAMPOS DE LA CABECERA DE LA RESPUESTA [getHeaderFields()]\n"); Map<String, List<String>> cabeceraRespuesta = conexion.getHeaderFields(); for (Map.Entry<String, List<String>> campo : cabeceraRespuesta.entrySet()) { System.out.println(campo.getKey() + " : " + campo.getValue()); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public static byte[] updateStoredTile(TileStoreEntry tile, HttpMapSource mapSource) throws UnrecoverableDownloadException, IOException, InterruptedException { final int x = tile.getX(); final int y = tile.getY(); final int zoom = tile.getZoom(); final HttpMapSource.TileUpdate tileUpdate = mapSource.getTileUpdate(); switch (tileUpdate) { case ETag: { boolean unchanged = hasTileETag(tile, mapSource); if (unchanged) { if (log.isTraceEnabled()) log.trace("Data unchanged on server (eTag): " + mapSource + " " + tile); return null; } break; } case LastModified: { boolean isNewer = isTileNewer(tile, mapSource); if (!isNewer) { if (log.isTraceEnabled()) log.trace("Data unchanged on server (LastModified): " + mapSource + " " + tile); return null; } break; } } HttpURLConnection conn = mapSource.getTileUrlConnection(zoom, x, y); if (conn == null) throw new UnrecoverableDownloadException( "Tile x=" + x + " y=" + y + " zoom=" + zoom + " is not a valid tile in map source " + mapSource); if (log.isTraceEnabled()) log.trace(String.format("Checking %s %s", mapSource.getName(), tile)); prepareConnection(conn); boolean conditionalRequest = false; switch (tileUpdate) { case IfNoneMatch: { if (tile.geteTag() != null) { conn.setRequestProperty("If-None-Match", tile.geteTag()); conditionalRequest = true; } break; } case IfModifiedSince: { if (tile.getTimeLastModified() > 0) { conn.setIfModifiedSince(tile.getTimeLastModified()); conditionalRequest = true; } break; } } conn.connect(); Settings s = Settings.getInstance(); int code = conn.getResponseCode(); if (conditionalRequest && code == HttpURLConnection.HTTP_NOT_MODIFIED) { // Data unchanged on server if (s.tileStoreEnabled) { tile.update(conn.getExpiration()); TileStore.getInstance().putTile(tile, mapSource); } if (log.isTraceEnabled()) log.trace("Data unchanged on server: " + mapSource + " " + tile); return null; } byte[] data = loadBodyDataInBuffer(conn); if (code != HttpURLConnection.HTTP_OK) throw new DownloadFailedException(conn, code); checkContentType(conn, data); checkContentLength(conn, data); String eTag = conn.getHeaderField("ETag"); long timeLastModified = conn.getLastModified(); long timeExpires = conn.getExpiration(); Utilities.checkForInterruption(); TileImageType imageType = Utilities.getImageType(data); if (imageType == null) throw new UnrecoverableDownloadException("The returned image is of unknown format"); if (s.tileStoreEnabled) { TileStore.getInstance() .putTileData(data, x, y, zoom, mapSource, timeLastModified, timeExpires, eTag); } Utilities.checkForInterruption(); return data; }
public long getLastModified() { return _flddelegate.getLastModified(); }