1 |
1 |
package org.gvsig.downloader.lib.impl;
|
2 |
2 |
|
|
3 |
import org.gvsig.downloader.IOExceptionWithStatus;
|
3 |
4 |
import java.io.File;
|
4 |
5 |
import java.io.IOException;
|
5 |
6 |
import java.io.InputStream;
|
... | ... | |
35 |
36 |
import org.gvsig.downloader.DownloaderAuthenticationConfig;
|
36 |
37 |
import org.gvsig.downloader.DownloaderAuthenticationRequester;
|
37 |
38 |
import org.gvsig.downloader.DownloaderCredentials;
|
|
39 |
import org.gvsig.downloader.DownloaderManager;
|
38 |
40 |
|
39 |
41 |
@SuppressWarnings("UseSpecificCatch")
|
40 |
42 |
final class SEAuthDownloaderTask
|
... | ... | |
88 |
90 |
}
|
89 |
91 |
}
|
90 |
92 |
|
91 |
|
private static final String METHOD_PUT = "PUT";
|
92 |
|
private static final String METHOD_POST = "POST";
|
93 |
|
private static final String METHOD_GET = "GET";
|
94 |
|
private static final String METHOD_DELETE = "DELETE";
|
95 |
|
|
96 |
93 |
public SEAuthDownloaderTask(SEDownloader downloader, URL url, String data, File dstFile, Object groupID) {
|
97 |
94 |
super(downloader, url, data, dstFile, groupID);
|
98 |
95 |
}
|
99 |
96 |
|
|
97 |
public SEAuthDownloaderTask(SEDownloader downloader, URL url, String method, ContentType contenttype, String data, File dstFile, Object groupID) {
|
|
98 |
super(downloader, url, method, contenttype, data, dstFile, groupID);
|
|
99 |
}
|
|
100 |
|
100 |
101 |
private DownloaderManagerImpl getDownloader() {
|
101 |
102 |
return (DownloaderManagerImpl) this.downloader;
|
102 |
103 |
}
|
... | ... | |
195 |
196 |
return responseHandler;
|
196 |
197 |
}
|
197 |
198 |
|
198 |
|
public File send(String method) throws IOException {
|
199 |
|
int status = 500;
|
200 |
|
URL theUrl = url;
|
201 |
|
try {
|
202 |
|
int numretries = 3;
|
203 |
|
for (int retries = 0; retries < numretries; retries++) {
|
204 |
|
HttpRequestBase request;
|
205 |
|
switch (method.toUpperCase()) {
|
206 |
|
case METHOD_DELETE:
|
207 |
|
request = new HttpDelete(theUrl.toURI());
|
208 |
|
break;
|
209 |
|
case METHOD_GET:
|
210 |
|
default:
|
211 |
|
request = new HttpGet(theUrl.toURI());
|
212 |
|
break;
|
213 |
|
}
|
214 |
|
// request.setHeader("User-Agent","Mozilla/5.0 (gvSIG) like Gecko");
|
215 |
|
request.setHeader("User-Agent","gvSIG-desktop");
|
216 |
|
request.setHeader("Referer","http://www.gvsig.com");
|
217 |
|
|
218 |
|
LOGGER.info("RETRY "+retries);
|
219 |
|
DownloaderResponseHandler responseHandler = executeRequest(request);
|
220 |
|
status = responseHandler.getStatus();
|
221 |
|
if(status >= 200 && status < 300) {
|
222 |
|
//2xx success
|
223 |
|
return this.dstFile;
|
224 |
|
|
225 |
|
} else if(status == 307 || status == 308) {
|
226 |
|
String redirection = responseHandler.getRedirectionLocation();
|
227 |
|
if(StringUtils.isBlank(redirection)) {
|
228 |
|
throw new IOExceptionWithStatus(status, "Can't call method "+method+" "+Objects.toString(theUrl)+"' HTTPStatus = "+status+" redirect to blank URL.");
|
229 |
|
}
|
230 |
|
try {
|
231 |
|
theUrl = new URL(redirection);
|
232 |
|
} catch (Exception ex) {
|
233 |
|
throw new IOExceptionWithStatus(status, "Can't call method "+method+" "+Objects.toString(theUrl)+"' HTTPStatus = "+status+" redirect to invalid URL "+redirection);
|
234 |
|
}
|
235 |
|
} else if(status >= 300 && status < 400) {
|
236 |
|
// 3xx redirection
|
237 |
|
throw new IOExceptionWithStatus(status, "Can't call method "+method+" "+Objects.toString(theUrl)+"' HTTPStatus = "+status);
|
238 |
|
} else if(status == 401 || status == 403) {
|
239 |
|
// "401 Unauthorized" indicates that the request lacks valid authentication credentials
|
240 |
|
// "403 Forbidden" the client doesn't have permission to access the requested resource
|
241 |
|
// https://www.permit.io/blog/401-vs-403-error-whats-the-difference
|
242 |
|
if( retries < numretries-1 ) {
|
243 |
|
authorize(theUrl);
|
244 |
|
}
|
245 |
|
} else if(status >= 400 && status < 500) {
|
246 |
|
// 4xx client errors
|
247 |
|
throw new IOExceptionWithStatus(status, "Can't call method "+method+" "+Objects.toString(theUrl)+"' HTTPStatus = "+status);
|
248 |
|
} else if(status >= 500 && status < 600) {
|
249 |
|
// 5xx server errors
|
250 |
|
throw new IOExceptionWithStatus(status, "Can't call method "+method+" "+Objects.toString(theUrl)+"' HTTPStatus = "+status);
|
251 |
|
} else {
|
252 |
|
//Unknown
|
253 |
|
throw new IOExceptionWithStatus(status, "Can't call method "+method+" "+Objects.toString(theUrl)+"' HTTPStatus = "+status);
|
254 |
|
}
|
255 |
|
}
|
256 |
|
throw new IOExceptionWithStatus(status, "Can't call method "+method+" "+Objects.toString(theUrl)+"' too many retries, last status " + status);
|
257 |
|
|
258 |
|
} catch (IOExceptionWithStatus ex) {
|
259 |
|
throw ex;
|
260 |
|
} catch (Throwable ex) {
|
261 |
|
throw new IOExceptionWithStatus(status, "Can't call method "+method+" "+Objects.toString(theUrl)+"' last status " + status, ex);
|
262 |
|
}
|
263 |
|
}
|
|
199 |
// public File send(String method) throws IOException {
|
|
200 |
// int status = 500;
|
|
201 |
// URL theUrl = url;
|
|
202 |
// try {
|
|
203 |
// int numretries = 3;
|
|
204 |
// for (int retries = 0; retries < numretries; retries++) {
|
|
205 |
// HttpRequestBase request;
|
|
206 |
// switch (method.toUpperCase()) {
|
|
207 |
// case METHOD_DELETE:
|
|
208 |
// request = new HttpDelete(theUrl.toURI());
|
|
209 |
// break;
|
|
210 |
// case METHOD_GET:
|
|
211 |
// default:
|
|
212 |
// request = new HttpGet(theUrl.toURI());
|
|
213 |
// break;
|
|
214 |
// }
|
|
215 |
//// request.setHeader("User-Agent","Mozilla/5.0 (gvSIG) like Gecko");
|
|
216 |
// request.setHeader("User-Agent","gvSIG-desktop");
|
|
217 |
// request.setHeader("Referer","http://www.gvsig.com");
|
|
218 |
//
|
|
219 |
// LOGGER.info("RETRY "+retries);
|
|
220 |
// DownloaderResponseHandler responseHandler = executeRequest(request);
|
|
221 |
// status = responseHandler.getStatus();
|
|
222 |
// if(status >= 200 && status < 300) {
|
|
223 |
// //2xx success
|
|
224 |
// return this.dstFile;
|
|
225 |
//
|
|
226 |
// } else if(status == 307 || status == 308) {
|
|
227 |
// String redirection = responseHandler.getRedirectionLocation();
|
|
228 |
// if(StringUtils.isBlank(redirection)) {
|
|
229 |
// throw new IOExceptionWithStatus(status, "Can't call method "+method+" "+Objects.toString(theUrl)+"' HTTPStatus = "+status+" redirect to blank URL.");
|
|
230 |
// }
|
|
231 |
// try {
|
|
232 |
// theUrl = new URL(redirection);
|
|
233 |
// } catch (Exception ex) {
|
|
234 |
// throw new IOExceptionWithStatus(status, "Can't call method "+method+" "+Objects.toString(theUrl)+"' HTTPStatus = "+status+" redirect to invalid URL "+redirection);
|
|
235 |
// }
|
|
236 |
// } else if(status >= 300 && status < 400) {
|
|
237 |
// // 3xx redirection
|
|
238 |
// throw new IOExceptionWithStatus(status, "Can't call method "+method+" "+Objects.toString(theUrl)+"' HTTPStatus = "+status);
|
|
239 |
// } else if(status == 401 || status == 403) {
|
|
240 |
// // "401 Unauthorized" indicates that the request lacks valid authentication credentials
|
|
241 |
// // "403 Forbidden" the client doesn't have permission to access the requested resource
|
|
242 |
// // https://www.permit.io/blog/401-vs-403-error-whats-the-difference
|
|
243 |
// if( retries < numretries-1 ) {
|
|
244 |
// authorize(theUrl);
|
|
245 |
// }
|
|
246 |
// } else if(status >= 400 && status < 500) {
|
|
247 |
// // 4xx client errors
|
|
248 |
// throw new IOExceptionWithStatus(status, "Can't call method "+method+" "+Objects.toString(theUrl)+"' HTTPStatus = "+status);
|
|
249 |
// } else if(status >= 500 && status < 600) {
|
|
250 |
// // 5xx server errors
|
|
251 |
// throw new IOExceptionWithStatus(status, "Can't call method "+method+" "+Objects.toString(theUrl)+"' HTTPStatus = "+status);
|
|
252 |
// } else {
|
|
253 |
// //Unknown
|
|
254 |
// throw new IOExceptionWithStatus(status, "Can't call method "+method+" "+Objects.toString(theUrl)+"' HTTPStatus = "+status);
|
|
255 |
// }
|
|
256 |
// }
|
|
257 |
// throw new IOExceptionWithStatus(status, "Can't call method "+method+" "+Objects.toString(theUrl)+"' too many retries, last status " + status);
|
|
258 |
//
|
|
259 |
// } catch (IOExceptionWithStatus ex) {
|
|
260 |
// throw ex;
|
|
261 |
// } catch (Throwable ex) {
|
|
262 |
// throw new IOExceptionWithStatus(status, "Can't call method "+method+" "+Objects.toString(theUrl)+"' last status " + status, ex);
|
|
263 |
// }
|
|
264 |
// }
|
264 |
265 |
|
265 |
266 |
private File send(String method, ContentType contentType, String data) throws IOExceptionWithStatus {
|
266 |
267 |
int status = 500;
|
... | ... | |
268 |
269 |
try {
|
269 |
270 |
int numretries = 3;
|
270 |
271 |
for (int retries = 0; retries < numretries; retries++) {
|
271 |
|
HttpEntityEnclosingRequestBase request;
|
|
272 |
HttpRequestBase request;
|
272 |
273 |
switch (method.toUpperCase()) {
|
273 |
|
case METHOD_PUT:
|
|
274 |
case DownloaderManager.METHOD_DELETE:
|
|
275 |
request = new HttpDelete(theUrl.toURI());
|
|
276 |
break;
|
|
277 |
case DownloaderManager.METHOD_PUT:
|
274 |
278 |
request = new HttpPut(theUrl.toURI());
|
|
279 |
((HttpPut)request).setEntity(new StringEntity(data, contentType));
|
275 |
280 |
break;
|
276 |
|
case METHOD_POST:
|
277 |
|
default:
|
|
281 |
case DownloaderManager.METHOD_POST:
|
278 |
282 |
request = new HttpPost(theUrl.toURI());
|
279 |
283 |
request.setHeader("SOAPAction", "post");
|
|
284 |
((HttpPost)request).setEntity(new StringEntity(data, contentType));
|
280 |
285 |
break;
|
|
286 |
case DownloaderManager.METHOD_GET:
|
|
287 |
default:
|
|
288 |
request = new HttpGet(theUrl.toURI());
|
|
289 |
break;
|
281 |
290 |
}
|
282 |
|
request.setEntity(new StringEntity(data, contentType));
|
283 |
291 |
// request.setHeader("User-Agent","Mozilla/5.0 (gvSIG) like Gecko");
|
284 |
292 |
request.setHeader("User-Agent","gvSIG-desktop");
|
285 |
293 |
request.setHeader("Referer","http://www.gvsig.com");
|
286 |
|
request.setHeader("Content-type", contentType.getMimeType());
|
|
294 |
request.setHeader("Content-type", contentType.toString());
|
287 |
295 |
DownloaderResponseHandler responseHandler = executeRequest(request);
|
288 |
296 |
status = responseHandler.getStatus();
|
289 |
297 |
if(status >= 200 && status < 300) {
|
... | ... | |
334 |
342 |
|
335 |
343 |
@Override
|
336 |
344 |
public void run() {
|
337 |
|
|
338 |
345 |
try {
|
339 |
|
if (data != null) {
|
340 |
|
send(METHOD_POST, ContentType.TEXT_XML, this.data);
|
341 |
|
} else {
|
342 |
|
send(METHOD_GET);
|
343 |
|
}
|
|
346 |
send(this.method, this.contenttype, this.data);
|
344 |
347 |
postdownload();
|
345 |
|
|
346 |
348 |
} catch (Exception e) {
|
347 |
349 |
exception(e);
|
348 |
350 |
}
|
349 |
|
|
350 |
|
|
351 |
|
// LOGGER.debug("downloading '" + url.toString() + "' to: " + dstFile.getAbsolutePath());
|
352 |
|
// if (data != null) {
|
353 |
|
// LOGGER.debug("using POST, request = " + data);
|
354 |
|
// }
|
355 |
|
// int timeout = this.getTimeout();
|
356 |
|
//
|
357 |
|
// try {
|
358 |
|
// OutputStreamWriter os = null;
|
359 |
|
// HttpURLConnection connection = null;
|
360 |
|
//
|
361 |
|
// //If the used protocol is HTTPS
|
362 |
|
// if (url.getProtocol().equals("https")) {
|
363 |
|
// disableHttsValidation();
|
364 |
|
// }
|
365 |
|
// connection = (HttpURLConnection) url.openConnection();
|
366 |
|
// connection.setUseCaches(false);
|
367 |
|
// connection.setRequestProperty("User-Agent", "Mozilla/5.0 (gvSIG) like Gecko");
|
368 |
|
// connection.setConnectTimeout(timeout);
|
369 |
|
//
|
370 |
|
// //If it uses a HTTP POST
|
371 |
|
// if (data != null) {
|
372 |
|
// connection.setRequestProperty("SOAPAction", "post");
|
373 |
|
// connection.setRequestMethod("POST");
|
374 |
|
// connection.setDoOutput(true);
|
375 |
|
// connection.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
|
376 |
|
// os = new OutputStreamWriter(connection.getOutputStream());
|
377 |
|
// os.write(data);
|
378 |
|
// os.flush();
|
379 |
|
// }
|
380 |
|
// download(connection.getInputStream());
|
381 |
|
//
|
382 |
|
// if (os != null) {
|
383 |
|
// os.close();
|
384 |
|
// }
|
385 |
|
//
|
386 |
|
// postdownload();
|
387 |
|
//
|
388 |
|
// } catch (Exception e) {
|
389 |
|
// exception(e);
|
390 |
|
// }
|
391 |
351 |
}
|
392 |
352 |
|
393 |
353 |
private Executor getExecutorUI() {
|