Changeset 4262 in josm for trunk/src/org
- Timestamp:
- 2011-07-24T22:33:49+02:00 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/MirroredInputStream.java
r4240 r4262 11 11 import java.io.IOException; 12 12 import java.io.InputStream; 13 import java.net.HttpURLConnection; 14 import java.net.MalformedURLException; 13 15 import java.net.URL; 14 16 import java.net.URLConnection; … … 29 31 InputStream fs = null; 30 32 File file = null; 31 33 32 34 public final static long DEFAULT_MAXTIME = -1l; 33 35 … … 186 188 String prefKey = getPrefKey(url, destDir); 187 189 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); 191 192 if(localPathEntry.size() == 2) { 192 193 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; 196 197 else { 197 if ( maxTime == DEFAULT_MAXTIME 198 if ( maxTime == DEFAULT_MAXTIME 198 199 || maxTime <= 0 // arbitrary value <= 0 is deprecated 199 200 ) { … … 202 203 age = System.currentTimeMillis() - Long.parseLong(lp[0]); 203 204 if (age < maxTime*1000) { 204 return file;205 return localFile; 205 206 } 206 207 } … … 221 222 BufferedInputStream bis = null; 222 223 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()); 227 226 FileOutputStream fos = new FileOutputStream(destDirFile); 228 227 bos = new BufferedOutputStream(fos); … … 237 236 fos.close(); 238 237 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)) { 241 240 Main.pref.putCollection(prefKey, Arrays.asList(new String[] 242 {Long.toString(System.currentTimeMillis()), file.toString()}));241 {Long.toString(System.currentTimeMillis()), localFile.toString()})); 243 242 } else { 244 243 System.out.println(tr("Failed to rename file {0} to {1}.", 245 destDirFile.getPath(), file.getPath()));244 destDirFile.getPath(), localFile.getPath())); 246 245 } 247 246 } catch (IOException e) { … … 249 248 System.out.println(tr("Failed to load {0}, use cached file and retry next time: {1}", 250 249 url, e)); 251 return file;250 return localFile; 252 251 } else { 253 252 throw e; … … 270 269 } 271 270 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 274 320 @Override 275 321 public int available() throws IOException
Note:
See TracChangeset
for help on using the changeset viewer.