Revision 47826 trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.downloader/org.gvsig.downloader.lib/org.gvsig.downloader.lib.impl/src/main/java/org/gvsig/downloader/lib/impl/SEAuthDownloaderTask.java
SEAuthDownloaderTask.java | ||
---|---|---|
5 | 5 |
import java.io.InputStream; |
6 | 6 |
import java.io.OutputStreamWriter; |
7 | 7 |
import java.net.HttpURLConnection; |
8 |
import java.net.SocketTimeoutException; |
|
8 | 9 |
import java.net.URL; |
9 | 10 |
import java.util.Objects; |
10 | 11 |
import java.util.concurrent.Executor; |
... | ... | |
16 | 17 |
import org.apache.http.auth.AuthenticationException; |
17 | 18 |
import org.apache.http.client.ClientProtocolException; |
18 | 19 |
import org.apache.http.client.ResponseHandler; |
20 |
import org.apache.http.client.config.RequestConfig; |
|
19 | 21 |
import org.apache.http.client.methods.HttpDelete; |
20 | 22 |
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; |
21 | 23 |
import org.apache.http.client.methods.HttpGet; |
... | ... | |
23 | 25 |
import org.apache.http.client.methods.HttpPut; |
24 | 26 |
import org.apache.http.client.methods.HttpRequestBase; |
25 | 27 |
import org.apache.http.client.methods.HttpUriRequest; |
28 |
import org.apache.http.conn.ConnectTimeoutException; |
|
26 | 29 |
import org.apache.http.entity.ContentType; |
27 | 30 |
import org.apache.http.entity.StringEntity; |
28 | 31 |
import org.apache.http.impl.client.CloseableHttpClient; |
... | ... | |
44 | 47 |
private HttpResponse response; |
45 | 48 |
|
46 | 49 |
public DownloaderResponseHandler() { |
47 |
this.status = 200;
|
|
50 |
this.status = 0; |
|
48 | 51 |
} |
49 | 52 |
|
50 | 53 |
@Override |
... | ... | |
53 | 56 |
this.response = response; |
54 | 57 |
try { |
55 | 58 |
HttpEntity entity = response.getEntity(); |
56 |
if(entity != null) {
|
|
59 |
if(entity == null) {
|
|
57 | 60 |
download(null); |
58 | 61 |
} else { |
59 | 62 |
InputStream content = entity.getContent(); |
60 | 63 |
download(content); |
61 | 64 |
} |
62 | 65 |
|
63 |
} catch (IOException ex) { |
|
66 |
} catch (IOExceptionWithStatus ex) {
|
|
64 | 67 |
throw ex; |
65 | 68 |
} catch (Exception ex) { |
66 |
throw new IOException(ex);
|
|
69 |
throw new IOExceptionWithStatus(this.status, ex.getLocalizedMessage(), ex);
|
|
67 | 70 |
} |
68 | 71 |
return null; |
69 | 72 |
} |
... | ... | |
126 | 129 |
|
127 | 130 |
private DownloaderResponseHandler executeRequest(HttpUriRequest request) throws IOException { |
128 | 131 |
|
129 |
CloseableHttpClient httpClient = HttpClientBuilder.create().build(); |
|
130 |
DownloaderCredentials credentials = this.getCredentials(); |
|
131 |
if( credentials==null ) { |
|
132 |
int timeout = getTimeout(); |
|
133 |
RequestConfig requestConfig = RequestConfig.custom() |
|
134 |
.setConnectTimeout(timeout) |
|
135 |
.setConnectionRequestTimeout(timeout) |
|
136 |
.setSocketTimeout(timeout) |
|
137 |
.build(); |
|
138 |
|
|
139 |
CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build(); |
|
140 |
DownloaderCredentials credentials = this.getCredentials(); |
|
141 |
if (credentials==null ) { |
|
132 | 142 |
DownloaderAuthenticationConfig config = this.getServiceAuthorizationConfig(); |
133 | 143 |
if( config!=null ) { |
134 | 144 |
DownloaderAuthenticationRequester authorizationRequester = config.create(); |
... | ... | |
146 | 156 |
DownloaderResponseHandler responseHandler = new DownloaderResponseHandler(); |
147 | 157 |
|
148 | 158 |
LOGGER.info("REQUEST "+request.getMethod()+" "+request.getURI().toString()); |
159 |
LOGGER.info("TIMEOUT "+timeout); |
|
149 | 160 |
for (Header header : request.getAllHeaders()) { |
150 | 161 |
LOGGER.info(header.getName()+": "+header.getValue()); |
151 | 162 |
} |
... | ... | |
153 | 164 |
InputStream content = ((HttpEntityEnclosingRequestBase) request).getEntity().getContent(); |
154 | 165 |
LOGGER.info("BODY data "+(StringUtils.join(IOUtils.readLines(content,"utf-8"),"\n"))); |
155 | 166 |
} |
156 |
httpClient.execute(request,responseHandler); |
|
167 |
LOGGER.info("DOWNLOAD TO "+Objects.toString(dstFile)); |
|
168 |
try { |
|
169 |
httpClient.execute(request,responseHandler); |
|
170 |
} catch (SocketTimeoutException|ConnectTimeoutException ex) { |
|
171 |
LOGGER.info("RESPONSE TIMEOUT"); |
|
172 |
throw ex; |
|
173 |
} catch (Exception ex) { |
|
174 |
LOGGER.info("RESPONSE ERROR "+ex.getLocalizedMessage()); |
|
175 |
throw ex; |
|
176 |
} |
|
157 | 177 |
LOGGER.info("RESPONSE CODE "+responseHandler.getStatus()); |
158 | 178 |
|
159 | 179 |
return responseHandler; |
... | ... | |
290 | 310 |
} |
291 | 311 |
|
292 | 312 |
|
293 |
LOGGER.info("downloading '" + url.toString() + "' to: " + dstFile.getAbsolutePath());
|
|
294 |
if (data != null) { |
|
295 |
LOGGER.info("using POST, request = " + data);
|
|
296 |
} |
|
297 |
int timeout = this.getTimeout(); |
|
298 |
|
|
299 |
try { |
|
300 |
OutputStreamWriter os = null; |
|
301 |
HttpURLConnection connection = null; |
|
302 |
|
|
303 |
//If the used protocol is HTTPS |
|
304 |
if (url.getProtocol().equals("https")) { |
|
305 |
disableHttsValidation(); |
|
306 |
} |
|
307 |
connection = (HttpURLConnection) url.openConnection(); |
|
308 |
connection.setUseCaches(false); |
|
309 |
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (gvSIG) like Gecko"); |
|
310 |
connection.setConnectTimeout(timeout); |
|
311 |
|
|
312 |
//If it uses a HTTP POST |
|
313 |
if (data != null) { |
|
314 |
connection.setRequestProperty("SOAPAction", "post"); |
|
315 |
connection.setRequestMethod("POST"); |
|
316 |
connection.setDoOutput(true); |
|
317 |
connection.setRequestProperty("Content-Type", "text/xml; charset=UTF-8"); |
|
318 |
os = new OutputStreamWriter(connection.getOutputStream()); |
|
319 |
os.write(data); |
|
320 |
os.flush(); |
|
321 |
} |
|
322 |
download(connection.getInputStream()); |
|
323 |
|
|
324 |
if (os != null) { |
|
325 |
os.close(); |
|
326 |
} |
|
327 |
|
|
328 |
postdownload(); |
|
329 |
|
|
330 |
} catch (Exception e) { |
|
331 |
exception(e); |
|
332 |
} |
|
313 |
// LOGGER.debug("downloading '" + url.toString() + "' to: " + dstFile.getAbsolutePath());
|
|
314 |
// if (data != null) {
|
|
315 |
// LOGGER.debug("using POST, request = " + data);
|
|
316 |
// }
|
|
317 |
// int timeout = this.getTimeout();
|
|
318 |
// |
|
319 |
// try {
|
|
320 |
// OutputStreamWriter os = null;
|
|
321 |
// HttpURLConnection connection = null;
|
|
322 |
// |
|
323 |
// //If the used protocol is HTTPS
|
|
324 |
// if (url.getProtocol().equals("https")) {
|
|
325 |
// disableHttsValidation();
|
|
326 |
// }
|
|
327 |
// connection = (HttpURLConnection) url.openConnection();
|
|
328 |
// connection.setUseCaches(false);
|
|
329 |
// connection.setRequestProperty("User-Agent", "Mozilla/5.0 (gvSIG) like Gecko");
|
|
330 |
// connection.setConnectTimeout(timeout);
|
|
331 |
// |
|
332 |
// //If it uses a HTTP POST
|
|
333 |
// if (data != null) {
|
|
334 |
// connection.setRequestProperty("SOAPAction", "post");
|
|
335 |
// connection.setRequestMethod("POST");
|
|
336 |
// connection.setDoOutput(true);
|
|
337 |
// connection.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
|
|
338 |
// os = new OutputStreamWriter(connection.getOutputStream());
|
|
339 |
// os.write(data);
|
|
340 |
// os.flush();
|
|
341 |
// }
|
|
342 |
// download(connection.getInputStream());
|
|
343 |
// |
|
344 |
// if (os != null) {
|
|
345 |
// os.close();
|
|
346 |
// }
|
|
347 |
// |
|
348 |
// postdownload();
|
|
349 |
// |
|
350 |
// } catch (Exception e) {
|
|
351 |
// exception(e);
|
|
352 |
// }
|
|
333 | 353 |
} |
334 | 354 |
|
335 | 355 |
private Executor getExecutorUI() { |
Also available in: Unified diff