Skip to content

Commit 69478ce

Browse files
committed
[#373] rework other redirects
1 parent 4a6e7df commit 69478ce

File tree

2 files changed

+46
-33
lines changed
  • bundles
    • ilg.gnumcueclipse.packs.core/src/ilg/gnumcueclipse/packs/core/data
    • ilg.gnumcueclipse.packs.data/src/ilg/gnumcueclipse/packs/data

2 files changed

+46
-33
lines changed

bundles/ilg.gnumcueclipse.packs.core/src/ilg/gnumcueclipse/packs/core/data/PacksStorage.java

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import java.io.BufferedReader;
1515
import java.io.File;
16+
import java.io.FileNotFoundException;
1617
import java.io.FileOutputStream;
1718
import java.io.FileReader;
1819
import java.io.FileWriter;
@@ -42,6 +43,8 @@ public class PacksStorage {
4243

4344
private static IPath fgFolderPath = null;
4445

46+
private final static int TIME_OUT = 60 * 000;
47+
4548
// ------------------------------------------------------------------------
4649

4750
// Return a file object in Packages
@@ -175,26 +178,32 @@ public static long getRemoteFileSize(String packName, URL url, MessageConsoleStr
175178
while (true) {
176179
out.println("Getting size of \"" + url + "\"...");
177180
connection = url.openConnection();
178-
if ("file".equals(url.getProtocol())) { //$NON-NLS-1$
179-
break;
180-
}
181181
if (connection instanceof HttpURLConnection) {
182-
int responseCode = ((HttpURLConnection) connection).getResponseCode();
182+
connection.setConnectTimeout(TIME_OUT);
183+
connection.setReadTimeout(TIME_OUT);
184+
HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
185+
186+
int responseCode = httpURLConnection.getResponseCode();
183187
if (responseCode == HttpURLConnection.HTTP_OK) {
184188
break;
189+
} else if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP
190+
|| responseCode == HttpURLConnection.HTTP_MOVED_PERM
191+
|| responseCode == HttpURLConnection.HTTP_SEE_OTHER) {
192+
String newUrl = connection.getHeaderField("Location");
193+
url = new URL(newUrl);
194+
continue;
195+
// System.out.println("Redirect to URL : " + newUrl);
196+
} else if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
197+
httpURLConnection.disconnect();
198+
throw new FileNotFoundException(
199+
"File \"" + url + "\" not found (" + responseCode + ").");
185200
} else {
186-
if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP
187-
|| responseCode == HttpURLConnection.HTTP_MOVED_PERM
188-
|| responseCode == HttpURLConnection.HTTP_SEE_OTHER) {
189-
String newUrl = connection.getHeaderField("Location");
190-
url = new URL(newUrl);
191-
192-
// System.out.println("Redirect to URL : " + newUrl);
193-
} else {
194-
throw new FileNotFoundException("Failed to open connection, response code " + responseCode);
195-
}
201+
httpURLConnection.disconnect();
202+
throw new FileNotFoundException("Failed to open connection, response code " + responseCode);
196203
}
204+
197205
}
206+
break; // When non http protocol, for example.
198207
}
199208

200209
long length = connection.getContentLength();

bundles/ilg.gnumcueclipse.packs.data/src/ilg/gnumcueclipse/packs/data/Utils.java

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737

3838
public class Utils {
3939

40+
private final static int TIME_OUT = 60 * 000;
41+
4042
public static InputStream checkForUtf8BOM(InputStream inputStream) throws IOException {
4143

4244
PushbackInputStream pushbackInputStream = new PushbackInputStream(new BufferedInputStream(inputStream), 3);
@@ -108,30 +110,32 @@ public static void copyFile(URL sourceUrl, File destinationFile, MessageConsoleS
108110
URLConnection connection;
109111
while (true) {
110112
connection = url.openConnection();
111-
if ("file".equals(url.getProtocol())) { //$NON-NLS-1$
112-
break;
113-
}
114113
if (connection instanceof HttpURLConnection) {
115-
connection.setConnectTimeout(60 * 1000);
116-
connection.setReadTimeout(60 * 1000);
117-
int responseCode = ((HttpURLConnection) connection).getResponseCode();
114+
connection.setConnectTimeout(TIME_OUT);
115+
connection.setReadTimeout(TIME_OUT);
116+
HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
117+
118+
int responseCode = httpURLConnection.getResponseCode();
118119
if (responseCode == HttpURLConnection.HTTP_OK) {
119120
break;
121+
} else if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP
122+
|| responseCode == HttpURLConnection.HTTP_MOVED_PERM
123+
|| responseCode == HttpURLConnection.HTTP_SEE_OTHER) {
124+
String newUrl = connection.getHeaderField("Location");
125+
url = new URL(newUrl);
126+
continue;
127+
// System.out.println("Redirect to URL : " + newUrl);
128+
} else if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
129+
httpURLConnection.disconnect();
130+
throw new FileNotFoundException(
131+
"File \"" + url + "\" not found (" + responseCode + "), pack not installed.");
120132
} else {
121-
if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP
122-
|| responseCode == HttpURLConnection.HTTP_MOVED_PERM
123-
|| responseCode == HttpURLConnection.HTTP_SEE_OTHER) {
124-
String newUrl = connection.getHeaderField("Location");
125-
url = new URL(newUrl);
126-
127-
// System.out.println("Redirect to URL : " + newUrl);
128-
} else if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
129-
throw new FileNotFoundException("File \"" + url + "\" not found (" + responseCode + "), pack not installed.");
130-
} else {
131-
throw new IOException("Failed to open connection, response code " + responseCode);
132-
}
133+
httpURLConnection.disconnect();
134+
throw new IOException("Failed to open connection, response code " + responseCode);
133135
}
136+
134137
}
138+
break; // When non http protocol, for example.
135139
}
136140

137141
destinationFile.getParentFile().mkdirs();
@@ -142,7 +146,7 @@ public static void copyFile(URL sourceUrl, File destinationFile, MessageConsoleS
142146
// conn.getContentLength() returns -1 when the file is sent in
143147
// chunks, so it cannot be used; instead it is computed.
144148
int totalBytes = 0;
145-
149+
146150
byte[] buf = new byte[1024];
147151
int bytesRead;
148152
while ((bytesRead = input.read(buf)) > 0) {

0 commit comments

Comments
 (0)