@@ -223,13 +223,13 @@ public synchronized long getLastModified(URI uri, HttpTransportFactory transport
223
223
224
224
public synchronized File fetchFile (URI uri , HttpTransportFactory transportFactory , Logger logger )
225
225
throws IOException {
226
- boolean exits = file .isFile ();
227
- if (exits && !mustValidate ()) {
226
+ boolean exists = file .isFile ();
227
+ if (exists && !mustValidate ()) {
228
228
return file ;
229
229
}
230
230
HttpTransport transport = transportFactory .createTransport (uri );
231
231
Properties lastHeader = getHeader ();
232
- if (exits ) {
232
+ if (exists ) {
233
233
if (lastHeader .containsKey (Headers .ETAG_HEADER .toLowerCase ())) {
234
234
transport .setHeader ("If-None-Match" , lastHeader .getProperty (Headers .ETAG_HEADER .toLowerCase ()));
235
235
}
@@ -242,7 +242,7 @@ public synchronized File fetchFile(URI uri, HttpTransportFactory transportFactor
242
242
return transport .get (response -> {
243
243
File tempFile ;
244
244
int code = response .statusCode ();
245
- if (exits && code == HttpURLConnection .HTTP_NOT_MODIFIED ) {
245
+ if (exists && code == HttpURLConnection .HTTP_NOT_MODIFIED ) {
246
246
updateHeader (response , getResponseCode ());
247
247
return file ;
248
248
}
@@ -251,19 +251,37 @@ public synchronized File fetchFile(URI uri, HttpTransportFactory transportFactor
251
251
}
252
252
updateHeader (response , code );
253
253
if (isRedirected (code )) {
254
- File cachedFile = SharedHttpCacheStorage .this .getCacheEntry (getRedirect (uri ), logger )
255
- .getCacheFile (transportFactory );
256
- // https://github.com/eclipse-tycho/tycho/issues/2938
257
- // Redirect may change extension. P2's SimpleMetadataRepositoryFactory relies on
258
- // accurate file extension to be cached.
259
- // Copying file to accommodate original request and its file extension.
260
- // Once https://github.com/eclipse-equinox/p2/issues/355 is fixed, cachedFile
261
- // may be returned directly without copying.
262
- response .close (); // early close before doing unrelated file I/O
263
- FileUtils .copyFile (cachedFile , file );
264
- return file ;
254
+ URI redirect = getRedirect (uri );
255
+ if (code == HttpURLConnection .HTTP_MOVED_TEMP ) {
256
+ // https://github.com/eclipse-tycho/tycho/issues/4459
257
+ // Don't save temporary redirects since they might change later, rendering the
258
+ // cache entry useless. Save them in the original request URI instead.
259
+ logger .warn (
260
+ String .format ("%s was temporarily redirected to %s but cached in the original location" ,
261
+ uri , redirect ));
262
+ HttpTransport redirectTransport = transportFactory .createTransport (redirect );
263
+ redirectTransport .get (r -> {
264
+ try (OutputStream os = new BufferedOutputStream (new FileOutputStream (file ))) {
265
+ r .transferTo (os );
266
+ }
267
+ return null ;
268
+ });
269
+ return file ;
270
+ } else {
271
+ File cachedFile = SharedHttpCacheStorage .this .getCacheEntry (redirect , logger )
272
+ .getCacheFile (transportFactory );
273
+ // https://github.com/eclipse-tycho/tycho/issues/2938
274
+ // Redirect may change extension. P2's SimpleMetadataRepositoryFactory relies on
275
+ // accurate file extension to be cached.
276
+ // Copying file to accommodate original request and its file extension.
277
+ // Once https://github.com/eclipse-equinox/p2/issues/355 is fixed, cachedFile
278
+ // may be returned directly without copying.
279
+ response .close (); // early close before doing unrelated file I/O
280
+ FileUtils .copyFile (cachedFile , file );
281
+ return file ;
282
+ }
265
283
}
266
- if (exits ) {
284
+ if (exists ) {
267
285
FileUtils .forceDelete (file );
268
286
}
269
287
response .checkResponseCode ();
0 commit comments