|
16 | 16 | import ilg.gnumcueclipse.packs.data.Repos;
|
17 | 17 |
|
18 | 18 | import java.io.BufferedReader;
|
| 19 | +import java.io.FileNotFoundException; |
19 | 20 | import java.io.IOException;
|
| 21 | +import java.io.InputStream; |
20 | 22 | import java.io.InputStreamReader;
|
21 | 23 | import java.io.StringReader;
|
| 24 | +import java.net.HttpURLConnection; |
22 | 25 | import java.net.URL;
|
| 26 | +import java.net.URLConnection; |
23 | 27 | import java.util.List;
|
24 | 28 |
|
25 | 29 | import javax.xml.parsers.DocumentBuilder;
|
@@ -72,13 +76,45 @@ public class Index {
|
72 | 76 | // Append string arrays to the given list
|
73 | 77 | // new String[] { url, name, version }
|
74 | 78 |
|
| 79 | + private final static int TIME_OUT = 60 * 000; |
| 80 | + |
75 | 81 | public static int readIndex(String indexUrl, List<String[]> pdscList)
|
76 | 82 | throws ParserConfigurationException, SAXException, IOException {
|
77 | 83 |
|
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)); |
79 | 117 |
|
80 |
| - // Read from url to local buffer |
81 |
| - BufferedReader in = new BufferedReader(new InputStreamReader(u.openStream())); |
82 | 118 | String line = null;
|
83 | 119 |
|
84 | 120 | // Insert missing root element
|
@@ -137,14 +173,14 @@ public static int readIndex(String indexUrl, List<String[]> pdscList)
|
137 | 173 | List<Element> pdscElements = Xml.getChildrenElementsList(pindex, "pdsc");
|
138 | 174 | for (Element pdscElement : pdscElements) {
|
139 | 175 |
|
140 |
| - String url = pdscElement.getAttribute("url").trim(); |
| 176 | + String aUrl = pdscElement.getAttribute("url").trim(); |
141 | 177 | String vendor = pdscElement.getAttribute("vendor").trim();
|
142 | 178 | String name = pdscElement.getAttribute("name").trim();
|
143 | 179 | String version = pdscElement.getAttribute("version").trim();
|
144 | 180 | String deprecated = pdscElement.getAttribute("deprecated").trim();
|
145 | 181 | String replacement = pdscElement.getAttribute("replacement").trim();
|
146 | 182 |
|
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, |
148 | 184 | replacement });
|
149 | 185 | ++count;
|
150 | 186 | }
|
|
0 commit comments