Lines 71-76
Link Here
|
71 |
|
71 |
|
72 |
private static final RequestProcessor NETWORK_ACCESS = new RequestProcessor("autoupdate-network-access", 10, false); |
72 |
private static final RequestProcessor NETWORK_ACCESS = new RequestProcessor("autoupdate-network-access", 10, false); |
73 |
|
73 |
|
|
|
74 |
private static final int MAX_REDIRECTS = 10; |
75 |
|
74 |
private NetworkAccess () {} |
76 |
private NetworkAccess () {} |
75 |
|
77 |
|
76 |
public static Task createNetworkAcessTask (URL url, int timeout, NetworkListener networkAcesssListener) { |
78 |
public static Task createNetworkAcessTask (URL url, int timeout, NetworkListener networkAcesssListener) { |
Lines 161-192
Link Here
|
161 |
if(conn instanceof HttpsURLConnection){ |
163 |
if(conn instanceof HttpsURLConnection){ |
162 |
NetworkAccess.initSSL((HttpsURLConnection) conn); |
164 |
NetworkAccess.initSSL((HttpsURLConnection) conn); |
163 |
} |
165 |
} |
164 |
//for HTTP or HTTPS: conenct and read response - redirection or not? |
166 |
|
165 |
if (conn instanceof HttpURLConnection) { |
167 |
// handle redirection here |
166 |
conn.connect(); |
168 |
int redirCount = 0; |
167 |
if (((HttpURLConnection) conn).getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP) { |
169 |
URLConnection redir = conn; |
168 |
// in case of redirection, try to obtain new URL |
170 |
do { |
169 |
String redirUrl = conn.getHeaderField("Location"); //NOI18N |
171 |
conn = redir; |
170 |
if (null != redirUrl && !redirUrl.isEmpty()) { |
172 |
redir = checkRedirect(conn, timeout); |
171 |
//create connection to redirected url and substitute original conn |
173 |
redirCount++; |
172 |
URL redirectedUrl = new URL(redirUrl); |
174 |
} while (conn != redir && redirCount <= MAX_REDIRECTS); |
173 |
URLConnection connRedir = redirectedUrl.openConnection(); |
175 |
|
174 |
connRedir.setRequestProperty("User-Agent", "NetBeans"); |
176 |
if (conn != redir) { |
175 |
connRedir.setConnectTimeout(timeout); |
177 |
throw new IOException("Too many redirects for " + url); |
176 |
conn = (HttpURLConnection) connRedir; |
|
|
177 |
} |
178 |
} |
179 |
} |
178 |
} |
|
|
179 |
|
180 |
InputStream is = conn.getInputStream (); |
180 |
InputStream is = conn.getInputStream (); |
181 |
contentLength = conn.getContentLength(); |
181 |
contentLength = conn.getContentLength(); |
182 |
Map <String, List <String>> map = conn.getHeaderFields(); |
182 |
if (err.isLoggable(Level.FINE)) { |
183 |
StringBuilder sb = new StringBuilder("Connection opened for:\n"); |
183 |
Map <String, List <String>> map = conn.getHeaderFields(); |
184 |
sb.append(" Url: ").append(conn.getURL()).append("\n"); |
184 |
StringBuilder sb = new StringBuilder("Connection opened for:\n"); |
185 |
for(String field : map.keySet()) { |
185 |
sb.append(" Url: ").append(conn.getURL()).append("\n"); |
186 |
sb.append(" ").append(field==null ? "Status" : field).append(": ").append(map.get(field)).append("\n"); |
186 |
for(String field : map.keySet()) { |
|
|
187 |
sb.append(" ").append(field==null ? "Status" : field).append(": ").append(map.get(field)).append("\n"); |
188 |
} |
189 |
sb.append("\n"); |
190 |
err.log(Level.FINE, sb.toString()); |
187 |
} |
191 |
} |
188 |
sb.append("\n"); |
|
|
189 |
err.log(Level.FINE, sb.toString()); |
190 |
return new BufferedInputStream (is); |
192 |
return new BufferedInputStream (is); |
191 |
} |
193 |
} |
192 |
}; |
194 |
}; |
Lines 244-247
Link Here
|
244 |
} |
246 |
} |
245 |
} |
247 |
} |
246 |
} |
248 |
} |
|
|
249 |
|
250 |
private static URLConnection checkRedirect(URLConnection conn, int timeout) throws IOException { |
251 |
if (conn instanceof HttpURLConnection) { |
252 |
conn.connect(); |
253 |
int code = ((HttpURLConnection) conn).getResponseCode(); |
254 |
if (code == HttpURLConnection.HTTP_MOVED_TEMP |
255 |
|| code == HttpURLConnection.HTTP_MOVED_PERM) { |
256 |
// in case of redirection, try to obtain new URL |
257 |
String redirUrl = conn.getHeaderField("Location"); //NOI18N |
258 |
if (null != redirUrl && !redirUrl.isEmpty()) { |
259 |
//create connection to redirected url and substitute original conn |
260 |
URL redirectedUrl = new URL(redirUrl); |
261 |
URLConnection connRedir = redirectedUrl.openConnection(); |
262 |
// XXX is this neede |
263 |
connRedir.setRequestProperty("User-Agent", "NetBeans"); // NOI18N |
264 |
connRedir.setConnectTimeout(timeout); |
265 |
connRedir.setReadTimeout(timeout); |
266 |
if (connRedir instanceof HttpsURLConnection) { |
267 |
NetworkAccess.initSSL((HttpsURLConnection) connRedir); |
268 |
} |
269 |
return connRedir; |
270 |
} |
271 |
} |
272 |
} |
273 |
return conn; |
274 |
} |
247 |
} |
275 |
} |