Skip to content

Commit 4a6e7df

Browse files
committed
[#373] process redirects for index
1 parent 97dd134 commit 4a6e7df

File tree

1 file changed

+41
-5
lines changed
  • bundles/ilg.gnumcueclipse.packs.data/src/ilg/gnumcueclipse/packs/cmsis

1 file changed

+41
-5
lines changed

bundles/ilg.gnumcueclipse.packs.data/src/ilg/gnumcueclipse/packs/cmsis/Index.java

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616
import ilg.gnumcueclipse.packs.data.Repos;
1717

1818
import java.io.BufferedReader;
19+
import java.io.FileNotFoundException;
1920
import java.io.IOException;
21+
import java.io.InputStream;
2022
import java.io.InputStreamReader;
2123
import java.io.StringReader;
24+
import java.net.HttpURLConnection;
2225
import java.net.URL;
26+
import java.net.URLConnection;
2327
import java.util.List;
2428

2529
import javax.xml.parsers.DocumentBuilder;
@@ -72,13 +76,45 @@ public class Index {
7276
// Append string arrays to the given list
7377
// new String[] { url, name, version }
7478

79+
private final static int TIME_OUT = 60 * 000;
80+
7581
public static int readIndex(String indexUrl, List<String[]> pdscList)
7682
throws ParserConfigurationException, SAXException, IOException {
7783

78-
URL u = new URL(indexUrl);
84+
URL url = new URL(indexUrl);
85+
URLConnection connection;
86+
while (true) {
87+
// Read from url to local buffer
88+
connection = url.openConnection();
89+
if (connection instanceof HttpURLConnection) {
90+
connection.setConnectTimeout(TIME_OUT);
91+
connection.setReadTimeout(TIME_OUT);
92+
HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
93+
94+
int responseCode = httpURLConnection.getResponseCode();
95+
if (responseCode == HttpURLConnection.HTTP_OK) {
96+
break;
97+
} else if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP
98+
|| responseCode == HttpURLConnection.HTTP_MOVED_PERM
99+
|| responseCode == HttpURLConnection.HTTP_SEE_OTHER) {
100+
String newUrl = connection.getHeaderField("Location");
101+
url = new URL(newUrl);
102+
continue;
103+
} else if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
104+
httpURLConnection.disconnect();
105+
throw new FileNotFoundException(
106+
"File \"" + url + "\" not found (" + responseCode + ").");
107+
} else {
108+
httpURLConnection.disconnect();
109+
throw new FileNotFoundException("Failed to open connection, response code " + responseCode);
110+
}
111+
}
112+
break; // When non http protocol, for example.
113+
}
114+
115+
InputStream is = connection.getInputStream();
116+
BufferedReader in = new BufferedReader(new InputStreamReader(is));
79117

80-
// Read from url to local buffer
81-
BufferedReader in = new BufferedReader(new InputStreamReader(u.openStream()));
82118
String line = null;
83119

84120
// Insert missing root element
@@ -137,14 +173,14 @@ public static int readIndex(String indexUrl, List<String[]> pdscList)
137173
List<Element> pdscElements = Xml.getChildrenElementsList(pindex, "pdsc");
138174
for (Element pdscElement : pdscElements) {
139175

140-
String url = pdscElement.getAttribute("url").trim();
176+
String aUrl = pdscElement.getAttribute("url").trim();
141177
String vendor = pdscElement.getAttribute("vendor").trim();
142178
String name = pdscElement.getAttribute("name").trim();
143179
String version = pdscElement.getAttribute("version").trim();
144180
String deprecated = pdscElement.getAttribute("deprecated").trim();
145181
String replacement = pdscElement.getAttribute("replacement").trim();
146182

147-
pdscList.add(new String[] { url, vendor + "." + name + ".pdsc", version, vendor, name, deprecated,
183+
pdscList.add(new String[] { aUrl, vendor + "." + name + ".pdsc", version, vendor, name, deprecated,
148184
replacement });
149185
++count;
150186
}

0 commit comments

Comments
 (0)