12
12
13
13
public class DownloadTask implements Runnable {
14
14
15
+ private static final int MAX_REDIRECTS = 5 ;
16
+
15
17
private final DownloadStateListener m_listener ;
16
18
private final String m_url ;
17
19
private final String m_downloadTo ;
@@ -29,43 +31,73 @@ public void run() {
29
31
m_listener .DownloadProgress (m_url , 0 );
30
32
31
33
try {
32
- URL url = new URL (m_url );
33
- File file = new File (m_downloadTo );
34
-
35
- HttpURLConnection ucon = HttpConnectionFactory .create (url );
36
- ucon .setReadTimeout (10000 );
37
- ucon .setInstanceFollowRedirects (true );
38
- ucon .connect ();
39
-
40
- int repCode = ucon .getResponseCode ();
41
-
42
- if (repCode == HttpURLConnection .HTTP_OK ) {
43
- m_listener .DownloadProgress (m_url , 0 );
44
- int bytesTotal = ucon .getContentLength ();
45
-
46
- GLog .debug ("bytes in file: " + bytesTotal );
47
-
48
- try (InputStream is = ucon .getInputStream ();
49
- FileOutputStream fos = new FileOutputStream (file )) {
50
- byte [] buffer = new byte [1024 * 128 ];
51
- int count ;
52
- int bytesDownloaded = 0 ;
53
-
54
- while ((count = is .read (buffer )) != -1 ) {
55
- fos .write (buffer , 0 , count );
56
- bytesDownloaded += count ;
57
- m_listener .DownloadProgress (m_url , bytesDownloaded );
58
- Thread .yield ();
59
- }
60
- }
34
+ result = downloadFile (m_url , m_downloadTo , 0 );
35
+ } catch (Exception e ) {
36
+ EventCollector .logException (new ModError ("Downloading" , e ));
37
+ }
61
38
62
- result = true ;
63
- }
39
+ m_listener . DownloadComplete ( m_url , result ) ;
40
+ }
64
41
65
- } catch (Exception e ) {
66
- EventCollector .logException (new ModError ("Downloading" ,e ));
42
+ private boolean downloadFile (String urlStr , String downloadTo , int redirectCount ) throws Exception {
43
+ if (redirectCount > MAX_REDIRECTS ) {
44
+ GLog .debug ("Too many redirects, aborting download" );
45
+ return false ;
46
+ }
47
+
48
+ URL url = new URL (urlStr );
49
+ File file = new File (downloadTo );
50
+
51
+ HttpURLConnection ucon = HttpConnectionFactory .create (url );
52
+ ucon .setReadTimeout (10000 );
53
+ ucon .setInstanceFollowRedirects (true );
54
+ ucon .setConnectTimeout (10000 );
55
+ ucon .connect ();
56
+
57
+ int repCode = ucon .getResponseCode ();
58
+ GLog .debug ("HTTP response code: " + repCode + " for URL: " + urlStr );
59
+
60
+ // Handle redirects manually if needed
61
+ if (repCode == HttpURLConnection .HTTP_MOVED_PERM ||
62
+ repCode == HttpURLConnection .HTTP_MOVED_TEMP ||
63
+ repCode == HttpURLConnection .HTTP_SEE_OTHER ||
64
+ repCode == 307 || // TEMPORARY_REDIRECT
65
+ repCode == 308 ) { // PERMANENT_REDIRECT
66
+
67
+ String redirectUrl = ucon .getHeaderField ("Location" );
68
+ if (redirectUrl != null && !redirectUrl .isEmpty ()) {
69
+ GLog .debug ("Following redirect to: " + redirectUrl );
70
+ ucon .disconnect ();
71
+ return downloadFile (redirectUrl , downloadTo , redirectCount + 1 );
72
+ }
67
73
}
68
74
69
- m_listener .DownloadComplete (m_url , result );
75
+ if (repCode == HttpURLConnection .HTTP_OK ) {
76
+ m_listener .DownloadProgress (m_url , 0 );
77
+ int bytesTotal = ucon .getContentLength ();
78
+
79
+ GLog .debug ("bytes in file: " + bytesTotal );
80
+
81
+ try (InputStream is = ucon .getInputStream ();
82
+ FileOutputStream fos = new FileOutputStream (file )) {
83
+ byte [] buffer = new byte [1024 * 128 ];
84
+ int count ;
85
+ int bytesDownloaded = 0 ;
86
+
87
+ while ((count = is .read (buffer )) != -1 ) {
88
+ fos .write (buffer , 0 , count );
89
+ bytesDownloaded += count ;
90
+ m_listener .DownloadProgress (m_url , bytesDownloaded );
91
+ Thread .yield ();
92
+ }
93
+ }
94
+
95
+ ucon .disconnect ();
96
+ return true ;
97
+ } else {
98
+ GLog .debug ("Download failed with HTTP response code: " + repCode );
99
+ ucon .disconnect ();
100
+ return false ;
101
+ }
70
102
}
71
103
}
0 commit comments