Revision 47840

View differences:

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/IOExceptionWithStatus.java
1
package org.gvsig.downloader.lib.impl;
2

  
3
import java.io.IOException;
4

  
5
/**
6
 *
7
 * @author jjdelcerro
8
 */
9
public class IOExceptionWithStatus extends IOException {
10

  
11
    private final int status;
12

  
13
    public IOExceptionWithStatus(int status, String message, Throwable cause) {
14
        super(message, cause);
15
        this.status = status;
16
    }
17

  
18
    public IOExceptionWithStatus(int status, String message) {
19
        this(status, message, null);
20
    }
21

  
22
    public int getStatus() {
23
        return status;
24
    }
25

  
26
    @Override
27
    public String getMessage() {
28
        return super.getMessage() + ". status = " + this.status;
29
    }
30

  
31
}
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/DownloaderManagerImpl.java
32 32
import java.util.List;
33 33
import java.util.Map;
34 34
import org.apache.commons.lang3.StringUtils;
35
import org.apache.http.entity.ContentType;
35 36
import org.gvsig.compat.se.net.downloader.se.SEDownloader;
36 37
import org.gvsig.downloader.DownloaderAuthenticationConfig;
37 38
import org.gvsig.downloader.DownloaderAuthenticationFactory;
......
74 75
    }
75 76

  
76 77
    @Override
78
    protected Runnable createDownloaderTask(SEDownloader downloader, URL url, String method, ContentType contenttype, String data, File target, Object groupID, int maxbytes) {
79
        SEAuthDownloaderTask t = new SEAuthDownloaderTask(
80
            downloader, 
81
            url, 
82
            method,
83
            contenttype,
84
            data, 
85
            target, 
86
            groupID);
87
        t.setMaxbytes(maxbytes);
88
        return t;
89
    }
90

  
91
    @Override
77 92
    public void addOrReplaceCredentials(DownloaderCredentials credentials) {
78 93
        if( credentials == null ) {
79 94
            LOG.warn("Invalid credentials (null).");
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
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()  {
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/types/httpbasic/DownloaderHTTPBasicAuthenticationRequester.java
52 52
            try {
53 53
                MutableBoolean r = new MutableBoolean();
54 54
//                SwingUtilities.invokeAndWait(() -> {
55
                executorUI.execute(() -> {
56
                    r.setValue(requestAuthorization(executorUI));
57
                });
55
//                executorUI.execute(() -> {
56
//                    r.setValue(requestAuthorization(executorUI));
57
//                });
58 58
                executorUI.execute(new Runnable() {
59 59
                    @Override
60 60
                    public void run() {
trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.downloader/org.gvsig.downloader.lib/org.gvsig.downloader.lib.api/src/main/java/org/gvsig/downloader/spi/AbstractDownloaderAuthenticationRequester.java
5 5
 */
6 6
package org.gvsig.downloader.spi;
7 7

  
8
import java.util.concurrent.Executor;
8 9
import org.gvsig.downloader.DownloaderAuthenticationConfig;
9 10
import org.gvsig.downloader.DownloaderAuthenticationRequester;
10 11

  
......
36 37
    public String toString() {
37 38
        return "(BaseURL = '"+this.config.getServiceUrl()+"')";
38 39
    }
39
    
40
    
40

  
41
    @Override
42
    public boolean requestAuthorization() {
43
        return this.requestAuthorization((Runnable command) -> { command.run(); });
44
    }
45

  
41 46
   
42 47
}
trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.downloader/org.gvsig.downloader.lib/org.gvsig.downloader.lib.api/src/main/java/org/gvsig/downloader/DownloaderAuthenticationRequester.java
12 12
    
13 13
    public String getProviderName();
14 14

  
15
    public boolean requestAuthorization();
16
    
15 17
    public boolean requestAuthorization(Executor executorUI);
16 18

  
17 19
    public DownloaderCredentials getCredentials();
trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.downloader/org.gvsig.downloader.lib/org.gvsig.downloader.lib.api/src/main/java/org/gvsig/downloader/IOExceptionWithStatus.java
1
package org.gvsig.downloader;
2

  
3
import java.io.IOException;
4

  
5
/**
6
 *
7
 * @author jjdelcerro
8
 */
9
public class IOExceptionWithStatus extends IOException {
10

  
11
    private final int status;
12

  
13
    public IOExceptionWithStatus(int status, String message, Throwable cause) {
14
        super(message, cause);
15
        this.status = status;
16
    }
17

  
18
    public IOExceptionWithStatus(int status, String message) {
19
        this(status, message, null);
20
    }
21

  
22
    public int getStatus() {
23
        return status;
24
    }
25

  
26
    @Override
27
    public String getMessage() {
28
        return super.getMessage() + ". status = " + this.status;
29
    }
30

  
31
}
trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.downloader/org.gvsig.downloader.lib/org.gvsig.downloader.lib.api/src/main/java/org/gvsig/downloader/DownloaderManager.java
35 35
 * 
36 36
 */
37 37
public interface DownloaderManager extends Downloader {
38
    
38

  
39
    public static final String METHOD_PUT = "PUT";
40
    public static final String METHOD_POST = "POST";
41
    public static final String METHOD_GET = "GET";
42
    public static final String METHOD_DELETE = "DELETE";
43

  
39 44
    /**
40 45
     * Comprueba si la URL url_s comienza por la URL prefix ignorando el protocolo y la query
41 46
     * 

Also available in: Unified diff