Changeset 7242 in josm


Ignore:
Timestamp:
2014-06-12T00:50:05+02:00 (10 years ago)
Author:
bastiK
Message:

add support for If-Modified-Since header in MirroredInputStream (see #10139)

use this for /maps request (sever support has been added) and change update
intervall from 7 days to 1 day

Location:
trunk/src/org/openstreetmap/josm
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/MirroredInputStream.java

    r7089 r7242  
    3333 */
    3434public class MirroredInputStream extends InputStream {
     35   
     36    /**
     37     * Caching strategy.
     38     */
     39    public enum CachingStrategy {
     40        /**
     41         * If cached file on disk is older than a certain time (7 days by default),
     42         * consider the cache stale and try to download the file again.
     43         */
     44        MaxAge,
     45        /**
     46         * Similar to MaxAge, considers the cache stale when a certain age is
     47         * exceeded. In addition, a If-Modified-Since HTTP header is added.
     48         * When the server replies "304 Not Modified", this is considered the same
     49         * as a full download.
     50         */
     51        IfModifiedSince
     52    }
     53   
    3554    InputStream fs = null;
    3655    File file = null;
    3756
    3857    public static final long DEFAULT_MAXTIME = -1L;
     58    public static final long DAYS = 24*60*60; // factor to get caching time in days
    3959
    4060    /**
     
    136156     */
    137157    public MirroredInputStream(String name, String destDir, long maxTime, String httpAccept) throws IOException {
     158        this(name, destDir, maxTime, httpAccept, CachingStrategy.MaxAge);
     159    }
     160
     161    /**
     162     * Constructs an input stream from a given filename, URL or internal resource.
     163     *
     164     * @param name can be:<ul>
     165     *  <li>relative or absolute file name</li>
     166     *  <li>{@code file:///SOME/FILE} the same as above</li>
     167     *  <li>{@code resource://SOME/FILE} file from the classpath (usually in the current *.jar)</li>
     168     *  <li>{@code josmdir://SOME/FILE} file inside josm config directory (since r7058)</li>
     169     *  <li>{@code http://...} a URL. It will be cached on disk.</li></ul>
     170     * @param destDir the destination directory for the cache file. Only applies for URLs.
     171     * @param maxTime the maximum age of the cache file (in seconds)
     172     * @param httpAccept The accepted MIME types sent in the HTTP Accept header. Only applies for URLs.
     173     * @param caching the caching strategy
     174     * @throws IOException when the resource with the given name could not be retrieved
     175     * @since 6867
     176     */
     177    public MirroredInputStream(String name, String destDir, long maxTime, String httpAccept, CachingStrategy caching) throws IOException {
    138178        URL url;
    139179        try {
     
    145185                }
    146186            } else {
    147                 file = checkLocal(url, destDir, maxTime, httpAccept);
     187                file = checkLocal(url, destDir, maxTime, httpAccept, caching);
    148188            }
    149189        } catch (java.net.MalformedURLException e) {
     
    276316    }
    277317
    278     private File checkLocal(URL url, String destDir, long maxTime, String httpAccept) throws IOException {
     318    private File checkLocal(URL url, String destDir, long maxTime, String httpAccept, CachingStrategy caching) throws IOException {
    279319        String prefKey = getPrefKey(url, destDir);
    280320        long age = 0L;
     321        Long ifModifiedSince = null;
    281322        File localFile = null;
    282323        List<String> localPathEntry = new ArrayList<>(Main.pref.getCollection(prefKey));
     
    295336                    return localFile;
    296337                }
     338                if (caching == CachingStrategy.IfModifiedSince) {
     339                    ifModifiedSince = Long.parseLong(localPathEntry.get(0));
     340                }
    297341            }
    298342        }
     
    305349            destDirFile.mkdirs();
    306350        }
    307 
     351       
    308352        String a = url.toString().replaceAll("[^A-Za-z0-9_.-]", "_");
    309353        String localPath = "mirror_" + a;
    310354        destDirFile = new File(destDir, localPath + ".tmp");
    311355        try {
    312             HttpURLConnection con = connectFollowingRedirect(url, httpAccept);
     356            HttpURLConnection con = connectFollowingRedirect(url, httpAccept, ifModifiedSince);
     357            if (ifModifiedSince != null && con.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
     358                Main.debug("304 Not Modified ("+url+")");
     359                if (localFile == null) throw new AssertionError();
     360                Main.pref.putCollection(prefKey,
     361                        Arrays.asList(Long.toString(System.currentTimeMillis()), localPathEntry.get(1)));
     362                return localFile;
     363            }
    313364            try (
    314365                InputStream bis = new BufferedInputStream(con.getInputStream());
     
    324375            localFile = new File(destDir, localPath);
    325376            if(Main.platform.rename(destDirFile, localFile)) {
    326                 Main.pref.putCollection(prefKey, Arrays.asList(new String[]
    327                 {Long.toString(System.currentTimeMillis()), localFile.toString()}));
     377                Main.pref.putCollection(prefKey,
     378                        Arrays.asList(Long.toString(System.currentTimeMillis()), localFile.toString()));
    328379            } else {
    329380                Main.warn(tr("Failed to rename file {0} to {1}.",
     
    353404     * @param downloadUrl The resource URL to download
    354405     * @param httpAccept The accepted MIME types sent in the HTTP Accept header. Can be {@code null}
     406     * @param ifModifiedSince The download time of the cache file, optional
    355407     * @return The HTTP connection effectively linked to the resource, after all potential redirections
    356408     * @throws MalformedURLException If a redirected URL is wrong
     
    358410     * @since 6867
    359411     */
    360     public static HttpURLConnection connectFollowingRedirect(URL downloadUrl, String httpAccept) throws MalformedURLException, IOException {
     412    public static HttpURLConnection connectFollowingRedirect(URL downloadUrl, String httpAccept, Long ifModifiedSince) throws MalformedURLException, IOException {
    361413        HttpURLConnection con = null;
    362414        int numRedirects = 0;
    363415        while(true) {
    364416            con = Utils.openHttpConnection(downloadUrl);
     417            if (ifModifiedSince != null) {
     418                con.setIfModifiedSince(ifModifiedSince);
     419            }
    365420            con.setInstanceFollowRedirects(false);
    366421            con.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15)*1000);
     
    380435            case HttpURLConnection.HTTP_OK:
    381436                return con;
     437            case HttpURLConnection.HTTP_NOT_MODIFIED:
     438                if (ifModifiedSince != null)
     439                    return con;
    382440            case HttpURLConnection.HTTP_MOVED_PERM:
    383441            case HttpURLConnection.HTTP_MOVED_TEMP:
  • trunk/src/org/openstreetmap/josm/io/imagery/ImageryReader.java

    r7186 r7242  
    5050            SAXParserFactory factory = SAXParserFactory.newInstance();
    5151            factory.setNamespaceAware(true);
    52             try (InputStream in = new MirroredInputStream(source)) {
     52            try (InputStream in = new MirroredInputStream(source, null, 1*MirroredInputStream.DAYS, null,
     53                    MirroredInputStream.CachingStrategy.IfModifiedSince)) {
    5354                InputSource is = new InputSource(UTFInputStreamReader.create(in));
    5455                factory.newSAXParser().parse(is, parser);
  • trunk/src/org/openstreetmap/josm/plugins/PluginDownloadTask.java

    r7033 r7242  
    125125            URL url = new URL(pi.downloadlink);
    126126            synchronized(this) {
    127                 downloadConnection = MirroredInputStream.connectFollowingRedirect(url, PLUGIN_MIME_TYPES);
     127                downloadConnection = MirroredInputStream.connectFollowingRedirect(url, PLUGIN_MIME_TYPES, null);
    128128            }
    129129            try (
Note: See TracChangeset for help on using the changeset viewer.