Changeset 4262 in josm


Ignore:
Timestamp:
2011-07-24T22:33:49+02:00 (13 years ago)
Author:
bastiK
Message:

fixed #6636 - JOSM isn't able to follow a "Moved permanently" (based on patch by Gubaer)

File:
1 edited

Legend:

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

    r4240 r4262  
    1111import java.io.IOException;
    1212import java.io.InputStream;
     13import java.net.HttpURLConnection;
     14import java.net.MalformedURLException;
    1315import java.net.URL;
    1416import java.net.URLConnection;
     
    2931    InputStream fs = null;
    3032    File file = null;
    31    
     33
    3234    public final static long DEFAULT_MAXTIME = -1l;
    3335
     
    186188        String prefKey = getPrefKey(url, destDir);
    187189        long age = 0L;
    188         File file = null;
    189         // FIXME: replace with normal getCollection after july 2011
    190         Collection<String> localPathEntry = Main.pref.getCollectionOld(prefKey, ";");
     190        File localFile = null;
     191        Collection<String> localPathEntry = Main.pref.getCollection(prefKey);
    191192        if(localPathEntry.size() == 2) {
    192193            String[] lp = (String[]) localPathEntry.toArray();
    193             file = new File(lp[1]);
    194             if(!file.exists())
    195                 file = null;
     194            localFile = new File(lp[1]);
     195            if(!localFile.exists())
     196                localFile = null;
    196197            else {
    197                 if ( maxTime == DEFAULT_MAXTIME 
     198                if ( maxTime == DEFAULT_MAXTIME
    198199                        || maxTime <= 0 // arbitrary value <= 0 is deprecated
    199200                ) {
     
    202203                age = System.currentTimeMillis() - Long.parseLong(lp[0]);
    203204                if (age < maxTime*1000) {
    204                     return file;
     205                    return localFile;
    205206                }
    206207            }
     
    221222        BufferedInputStream bis = null;
    222223        try {
    223             URLConnection conn = url.openConnection();
    224             conn.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15)*1000);
    225             conn.setReadTimeout(Main.pref.getInteger("socket.timeout.read",30)*1000);
    226             bis = new BufferedInputStream(conn.getInputStream());
     224            HttpURLConnection con = connectFollowingRedirect(url);
     225            bis = new BufferedInputStream(con.getInputStream());
    227226            FileOutputStream fos = new FileOutputStream(destDirFile);
    228227            bos = new BufferedOutputStream(fos);
     
    237236            fos.close();
    238237            fos = null;
    239             file = new File(destDir, localPath);
    240             if(Main.platform.rename(destDirFile, file)) {
     238            localFile = new File(destDir, localPath);
     239            if(Main.platform.rename(destDirFile, localFile)) {
    241240                Main.pref.putCollection(prefKey, Arrays.asList(new String[]
    242                 {Long.toString(System.currentTimeMillis()), file.toString()}));
     241                {Long.toString(System.currentTimeMillis()), localFile.toString()}));
    243242            } else {
    244243                System.out.println(tr("Failed to rename file {0} to {1}.",
    245                 destDirFile.getPath(), file.getPath()));
     244                destDirFile.getPath(), localFile.getPath()));
    246245            }
    247246        } catch (IOException e) {
     
    249248                System.out.println(tr("Failed to load {0}, use cached file and retry next time: {1}",
    250249                url, e));
    251                 return file;
     250                return localFile;
    252251            } else {
    253252                throw e;
     
    270269        }
    271270
    272         return file;
    273     }
     271        return localFile;
     272    }
     273
     274    /**
     275     * Opens a connection for downloading a resource.
     276     * <p>
     277     * Manually follows redirects because
     278     * {@link HttpURLConnection#setFollowRedirects(boolean)} fails if the redirect
     279     * is going from a http to a https URL, see <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4620571">bug report</a>.
     280     * <p>
     281     * This can causes problems when downloading from certain GitHub URLs.
     282     */
     283    protected HttpURLConnection connectFollowingRedirect(URL downloadUrl) throws MalformedURLException, IOException {
     284        HttpURLConnection con = null;
     285        int numRedirects = 0;
     286        while(true) {
     287            con = (HttpURLConnection)downloadUrl.openConnection();
     288            con.setInstanceFollowRedirects(false);
     289            con.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15)*1000);
     290            con.setReadTimeout(Main.pref.getInteger("socket.timeout.read",30)*1000);
     291            con.connect();
     292            switch(con.getResponseCode()) {
     293            case HttpURLConnection.HTTP_OK:
     294                return con;
     295            case HttpURLConnection.HTTP_MOVED_PERM:
     296            case HttpURLConnection.HTTP_MOVED_TEMP:
     297            case HttpURLConnection.HTTP_SEE_OTHER:
     298                String redirectLocation = con.getHeaderField("Location");
     299                if (downloadUrl == null) {
     300                    String msg = tr("Fatal: unexpected response from HTTP server. Got {0} response without 'Location' header. Can''t redirect. Aborting.", con.getResponseCode());
     301                    throw new IOException(msg);
     302                }
     303                downloadUrl = new URL(redirectLocation);
     304                // keep track of redirect attempts to break a redirect loops if it happens
     305                // to occur for whatever reason
     306                numRedirects++;
     307                if (numRedirects >= Main.pref.getInteger("socket.maxredirects", 5)) {
     308                    String msg = tr("Fatal: too many redirects to the download URL detected. Aborting.");
     309                    throw new IOException(msg);
     310                }
     311                System.out.println(tr("Download redirected to ''{0}''", downloadUrl));
     312                break;
     313            default:
     314                String msg = tr("Error: failed to read from ''{0}''. Server responded with status code {1}.", downloadUrl, con.getResponseCode());
     315                throw new IOException(msg);
     316            }
     317        }
     318    }
     319
    274320    @Override
    275321    public int available() throws IOException
Note: See TracChangeset for help on using the changeset viewer.