Revision 47845 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

View differences:

SEAuthDownloaderTask.java
4 4
import java.io.IOException;
5 5
import java.io.InputStream;
6 6
import java.net.SocketTimeoutException;
7
import java.net.URI;
8
import java.net.URISyntaxException;
7 9
import java.net.URL;
8 10
import java.util.Objects;
9 11
import java.util.concurrent.Executor;
......
35 37
import org.gvsig.downloader.DownloaderCredentials;
36 38
import org.gvsig.downloader.DownloaderManager;
37 39
import org.gvsig.downloader.IOExceptionWithStatus;
40
import org.slf4j.Logger;
41
import org.slf4j.LoggerFactory;
38 42

  
39 43
@SuppressWarnings("UseSpecificCatch")
40 44
final class SEAuthDownloaderTask
41 45
        extends AbstractSEDownloaderTask
42
        implements Runnable {
46
        implements Runnable 
47
    {
43 48

  
49
    private static Logger LOGGER = LoggerFactory.getLogger(SEAuthDownloaderTask.class);
50
        
44 51
    private class DownloaderResponseHandler implements ResponseHandler<Object> {
45 52
        
46 53
        private int status;
......
194 201
        return responseHandler;
195 202
    }
196 203

  
197
//    public File send(String method) throws IOException {
198
//        int status = 500;
199
//        URL theUrl = url;
200
//        try {
201
//            int numretries = 3;
202
//            for (int retries = 0; retries < numretries; retries++) {    
203
//                HttpRequestBase request;
204
//                switch (method.toUpperCase()) {
205
//                    case METHOD_DELETE:
206
//                        request = new HttpDelete(theUrl.toURI());
207
//                        break;
208
//                    case METHOD_GET:
209
//                    default:
210
//                        request = new HttpGet(theUrl.toURI());
211
//                        break;
212
//                }
213
////                request.setHeader("User-Agent","Mozilla/5.0 (gvSIG) like Gecko");
214
//                request.setHeader("User-Agent","gvSIG-desktop");
215
//                request.setHeader("Referer","http://www.gvsig.com");
216
//
217
//                LOGGER.info("RETRY "+retries);
218
//                DownloaderResponseHandler responseHandler = executeRequest(request);
219
//                status = responseHandler.getStatus();
220
//                if(status >= 200 && status < 300) {
221
//                    //2xx success
222
//                    return this.dstFile;
223
//                    
224
//                } else if(status == 307 || status == 308) {
225
//                    String redirection = responseHandler.getRedirectionLocation();
226
//                    if(StringUtils.isBlank(redirection)) {
227
//                        throw new IOExceptionWithStatus(status, "Can't call method "+method+" "+Objects.toString(theUrl)+"' HTTPStatus = "+status+" redirect to blank URL.");
228
//                    }
229
//                    try {
230
//                        theUrl = new URL(redirection);
231
//                    } catch (Exception ex) {
232
//                        throw new IOExceptionWithStatus(status, "Can't call method "+method+" "+Objects.toString(theUrl)+"' HTTPStatus = "+status+" redirect to invalid URL "+redirection);
233
//                    }
234
//                } else if(status >= 300 && status < 400) {
235
//                    // 3xx redirection
236
//                    throw new IOExceptionWithStatus(status, "Can't call method "+method+" "+Objects.toString(theUrl)+"' HTTPStatus = "+status);
237
//                } else if(status == 401 || status == 403) {
238
//                    // "401 Unauthorized" indicates that the request lacks valid authentication credentials
239
//                    // "403 Forbidden" the client doesn't have permission to access the requested resource
240
//                    // https://www.permit.io/blog/401-vs-403-error-whats-the-difference
241
//                    if( retries < numretries-1 ) {
242
//                        authorize(theUrl);
243
//                    }
244
//                } else if(status >= 400 && status < 500) {
245
//                    // 4xx client errors
246
//                    throw new IOExceptionWithStatus(status, "Can't call method "+method+" "+Objects.toString(theUrl)+"' HTTPStatus = "+status);
247
//                } else if(status >= 500 && status < 600) {
248
//                    // 5xx server errors
249
//                    throw new IOExceptionWithStatus(status, "Can't call method "+method+" "+Objects.toString(theUrl)+"' HTTPStatus = "+status);
250
//                } else {
251
//                    //Unknown
252
//                    throw new IOExceptionWithStatus(status, "Can't call method "+method+" "+Objects.toString(theUrl)+"' HTTPStatus = "+status);
253
//                }
254
//            }
255
//            throw new IOExceptionWithStatus(status, "Can't call method "+method+" "+Objects.toString(theUrl)+"' too many retries, last status " + status);
256
//            
257
//        } catch (IOExceptionWithStatus ex) {
258
//            throw ex;
259
//        } catch (Throwable ex) {
260
//            throw new IOExceptionWithStatus(status, "Can't call method "+method+" "+Objects.toString(theUrl)+"' last status " + status, ex);
261
//        }
262
//    }
204
    private URI urlToUri(URL url) throws URISyntaxException{
205
        try {
206
            URI uri = url.toURI();
207
            return uri;
208
        } catch (URISyntaxException ex) {
209
            String s = url.toExternalForm();
210
            //?apa hasta que se arregle la construcci?n de URL de tiles de WMTS
211
//            s = StringUtils.replace(s, "{", "%7B");
212
//            s = StringUtils.replace(s, "}", "%7D");
213
            return new URI(s);
214
        }
215
    }
263 216
        
264 217
    private File send(String method, ContentType contentType, String data) throws IOExceptionWithStatus {
265 218
        int status = 500;
266 219
        URL theUrl = url;
267 220
        try {
221
            URI theUri = urlToUri(url);
268 222
            int numretries = 3;
269 223
            for (int retries = 0; retries < numretries; retries++) {            
270 224
                HttpRequestBase request;
271 225
                switch (method.toUpperCase()) {
272 226
                    case DownloaderManager.METHOD_DELETE:
273
                        request = new HttpDelete(theUrl.toURI());
227
                        request = new HttpDelete(theUri);
274 228
                        break;
275 229
                    case DownloaderManager.METHOD_PUT:
276
                        request = new HttpPut(theUrl.toURI());
230
                        request = new HttpPut(theUri);
277 231
                        request.setHeader("Content-type", contentType.toString());
278 232
                        ((HttpPut)request).setEntity(new StringEntity(data, contentType));
279 233
                        break;
280 234
                    case DownloaderManager.METHOD_POST:
281
                        request = new HttpPost(theUrl.toURI());
235
                        request = new HttpPost(theUri);
282 236
                        request.setHeader("SOAPAction", "post");                
283 237
                        request.setHeader("Content-type", contentType.toString());
284 238
                        ((HttpPost)request).setEntity(new StringEntity(data, contentType));
285 239
                        break;
286 240
                    case DownloaderManager.METHOD_GET:
287 241
                    default:
288
                        request = new HttpGet(theUrl.toURI());
242
                        request = new HttpGet(theUri);
289 243
                        break;
290 244
                }
291 245
//                request.setHeader("User-Agent","Mozilla/5.0 (gvSIG) like Gecko");

Also available in: Unified diff