Statistics
| Revision:

gvsig-projects-pool / org.gvsig.online / trunk / org.gvsig.online / org.gvsig.online.lib / org.gvsig.online.lib.impl / src / main / java / org / gvsig / online / lib / impl / OnlineDownloaderImpl.java @ 9524

History | View | Annotate | Download (12.9 KB)

1
package org.gvsig.online.lib.impl;
2

    
3
import org.gvsig.online.lib.api.OnlineDownloader;
4
import java.io.File;
5
import java.io.FileOutputStream;
6
import java.io.IOException;
7
import java.io.InputStream;
8
import java.net.URI;
9
import java.net.URL;
10
import java.util.ArrayList;
11
import java.util.List;
12
import java.util.Map;
13
import java.util.Objects;
14
import org.apache.commons.io.FilenameUtils;
15
import org.apache.commons.io.IOUtils;
16
import org.apache.commons.lang3.StringUtils;
17
import org.apache.http.Header;
18
import org.apache.http.HttpEntityEnclosingRequest;
19
import org.apache.http.HttpRequest;
20
import org.apache.http.HttpResponse;
21
import org.apache.http.NameValuePair;
22
import org.apache.http.auth.AuthenticationException;
23
import org.apache.http.client.ClientProtocolException;
24
import org.apache.http.client.ResponseHandler;
25
import org.apache.http.client.entity.UrlEncodedFormEntity;
26
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
27
import org.apache.http.client.methods.HttpGet;
28
import org.apache.http.client.methods.HttpPost;
29
import org.apache.http.client.methods.HttpPut;
30
import org.apache.http.client.methods.HttpUriRequest;
31
import org.apache.http.entity.ContentType;
32
import org.apache.http.entity.StringEntity;
33
import org.apache.http.impl.client.CloseableHttpClient;
34
import org.apache.http.impl.client.HttpClientBuilder;
35
import org.apache.http.message.BasicNameValuePair;
36
import org.gvsig.online.lib.api.OnlineSite;
37
import org.gvsig.online.lib.api.OnlineUserIdentificationRequester;
38
import org.gvsig.tools.ToolsLocator;
39

    
40
/**
41
 *
42
 * @author jjdelcerro
43
 */
44
@SuppressWarnings("UseSpecificCatch")
45
public class OnlineDownloaderImpl implements OnlineDownloader {
46
    
47
//    private OnlineAuthorizationRequester authorizationRequester;
48
    private final OnlineSite site;
49

    
50
    private static class DownloaderResponseHandler implements ResponseHandler<Object> {
51
        
52
        private int status;
53
        private File f;
54
        private HttpResponse response;
55

    
56
        public DownloaderResponseHandler(String urlpath) {
57
            this.status = 200;
58
            String fname = FilenameUtils.getBaseName(StringUtils.removeEnd(urlpath,"/"));
59
            this.f = ToolsLocator.getFoldersManager().getUniqueTemporaryFile(fname);
60
        }
61
        
62
        @Override
63
        public Object handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
64
            this.status=response.getStatusLine().getStatusCode();
65
            this.response = response;
66
            FileOutputStream fs = null;
67
            try {
68
                fs = new FileOutputStream(f);
69
                InputStream content = response.getEntity().getContent();
70
                IOUtils.copy(content, fs);
71
            } catch (IOException ex) {
72
                f = null;
73
                throw ex;
74
            } catch (Exception ex) {
75
                f = null;
76
                throw new IOException(ex);
77
            } finally {
78
                IOUtils.closeQuietly(fs);
79
            }
80
            return null;
81
        }
82

    
83
        private int getStatus() {
84
            return this.status;
85
        }
86

    
87
        private File getFile() {
88
            return this.f;
89
        }
90
        
91
    }
92

    
93
    
94
    public OnlineDownloaderImpl(OnlineSite site) {
95
        this.site = site;
96
    }
97

    
98
    private DownloaderResponseHandler executeRequest(HttpUriRequest request) throws IOException {
99

    
100
        CloseableHttpClient httpClient = HttpClientBuilder.create()
101
                .build();
102
        String authorization = this.site.getCurrentAuthorizationToken();
103
        if( StringUtils.isNotBlank(authorization) ) {
104
            request.addHeader("Authorization", authorization);
105
        }
106
        DownloaderResponseHandler responseHandler = new DownloaderResponseHandler(request.getURI().toString());
107

    
108
        System.out.println("REQUEST "+request.getMethod()+" "+request.getURI().toString());
109
        for (Header header : request.getAllHeaders()) {
110
            System.out.println(header.getName()+": "+header.getValue());
111
        }
112
        if( request instanceof HttpEntityEnclosingRequestBase ) {
113
            InputStream content = ((HttpEntityEnclosingRequestBase) request).getEntity().getContent();
114
            System.out.println("BODY data "+(StringUtils.join(IOUtils.readLines(content,"utf-8"),"\n")));                
115
        }
116
        httpClient.execute(request,responseHandler);
117
        System.out.println("RESPONSE CODE "+responseHandler.getStatus());
118
        
119
        return responseHandler;
120
    }
121
    
122
    @SuppressWarnings("Convert2Lambda")
123
    @Override
124
    public File get(URL url) throws IOException {
125
        try {
126
            int numretries = 3;
127
            for (int retries = 0; retries < numretries; retries++) {            
128
                HttpGet request = new HttpGet(url.toURI());
129
                request.setHeader("User-Agent","gvSIG-desktop");
130
                request.setHeader("Referer","http://www.gvsig.com");
131
                DownloaderResponseHandler responseHandler = executeRequest(request);
132
                switch(responseHandler.getStatus()) {
133
                    case 200: 
134
                        return responseHandler.getFile();
135
                    case 401: // 401 Authorization Required
136
                        if( retries < numretries-1 ) {
137
                            authorize();
138
                        }
139
                        break;
140
                    case 500:
141
                        throw new IOException("Can't download '"+Objects.toString(url)+"' 500 Server error");
142
                }
143
            }
144
            throw new IOException("Can't download '"+Objects.toString(url)+"'");
145
            
146
        } catch (IOException ex) {
147
            throw ex;
148
        } catch (Exception ex) {
149
            throw new IOException("Can't download '"+Objects.toString(url)+"'", ex);
150
        }
151
    }
152

    
153
    @Override
154
    public File post_json(URL url, String json) throws IOExceptionWithStatus {
155
        return post_json(url, json, "POST");
156
    }
157

    
158
    @Override
159
    public File put_json(URL url, String json) throws IOExceptionWithStatus {
160
        return post_json(url, json, "PUT");
161
    }
162

    
163
    @SuppressWarnings("Convert2Lambda")
164
    private File post_json(URL url, String json, String method) throws IOExceptionWithStatus {
165
        int status = 500;
166
        try {
167
            int numretries = 3;
168
            for (int retries = 0; retries < numretries; retries++) {            
169
                HttpEntityEnclosingRequestBase request;
170
                switch (method.toUpperCase()) {
171
                    case "PUT":
172
                        request = new HttpPut(url.toURI());
173
                        break;
174
                    case "POST":
175
                    default:
176
                        request = new HttpPost(url.toURI());
177
                        break;
178
                }
179
                request.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON));
180
                request.setHeader("Content-type","application/json");                
181
                DownloaderResponseHandler responseHandler = executeRequest(request);
182
                switch(status = responseHandler.getStatus()) {
183
                    case 200: 
184
                        return responseHandler.getFile();
185
                    case 401: // 401 Authorization Required
186
                        if( retries < numretries-1 ) {
187
                            authorize();
188
                        }
189
                        break;
190
                    case 409: // 409 Version Conflict
191
                        retries = numretries;
192
                        break;
193
                    case 500:
194
                        throw new IOExceptionWithStatus(500, "Can't download '"+Objects.toString(url)+"' 500 Server error");
195
                }
196
            }
197
            throw new IOExceptionWithStatus(status, "Can't download '"+Objects.toString(url)+"' too many retries");
198
            
199
        } catch (IOExceptionWithStatus ex) {
200
            throw ex;
201
        } catch (Exception ex) {
202
            throw new IOExceptionWithStatus(status,"Can't download '"+Objects.toString(url)+"'", ex);
203
        }
204
    }
205

    
206
    @Override
207
    public File post_form(URL url, Map<String,String>data) throws IOException {
208
        try {
209
            URI uri = url.toURI();
210
            if( !uri.toString().endsWith("/") ) {
211
                uri = new URI(url.toString()+"/");
212
            }
213
            int numretries = 3;
214
            for (int retries = 0; retries < numretries; retries++) {            
215
                HttpPost request = new HttpPost(uri);
216
                final List<NameValuePair> params = new ArrayList<>();
217
                for (Map.Entry<String, String> entry : data.entrySet()) {
218
                    String key = entry.getKey();
219
                    String value = entry.getValue();
220
                    params.add(new BasicNameValuePair(key, value));
221
                }
222
                UrlEncodedFormEntity formdata = new UrlEncodedFormEntity(params);
223
                request.setEntity(formdata);
224
                request.setHeader("Content-type","application/x-www-form-urlencoded");
225
                DownloaderResponseHandler responseHandler = executeRequest(request);
226
                switch(responseHandler.getStatus()) {
227
                    case 200: 
228
                        return responseHandler.getFile();
229
                    case 401: // 401 Authorization Required
230
                        if( retries < numretries-1 ) {
231
                            authorize();
232
                        }
233
                        break;
234
                    case 500:
235
                        throw new IOException("Can't download '"+Objects.toString(url)+"' 500 Server error");
236
                }
237
            }
238
            throw new IOException("Can't download '"+Objects.toString(url)+"' too many retries");
239
            
240
        } catch (IOException ex) {
241
            throw ex;
242
        } catch (Exception ex) {
243
            throw new IOException("Can't download '"+Objects.toString(url)+"'", ex);
244
        }
245
    }
246

    
247
    public void authorize() throws Exception {
248
        try {
249
            System.out.println("AUTHORIZATION REQUIRED");
250
            OnlineUserIdentificationRequester identificationRequester = this.site.getUserIdentificationRequester();
251
            if( identificationRequester == null ) {
252
                throw new AuthenticationException("Not identification requester configured");
253
            }
254
            if( !identificationRequester.requestIdentification() ) {
255
                return;
256
            }
257
            String authorization = identificationRequester.getAuthorization();
258
            this.site.setCurrentUserCode(identificationRequester.getUserId());
259
            this.site.setCurrentAuthorizationToken(authorization);
260
            System.out.println("AUTH TOKEN: "+authorization);
261
        } catch (Exception ex) {
262
//            LOGGER.warn("Can't authorize", ex);
263

    
264
        }
265
    }
266
//    
267
//    public static void main0(String[] args) throws Exception {
268
//        new DefaultLibrariesInitializer().fullInitialize();
269
//        
270
//        OnlineDownloaderImpl downloader = new OnlineDownloaderImpl();
271
//        File f = downloader.get(new URL("https://devel.gvsigonline.com/gvsigonline/api/v1/projects"));
272
//        System.out.println(f);
273
//    }
274
//
275
//    public static void main1(String[] args) throws Exception {
276
//        new DefaultLibrariesInitializer().fullInitialize();
277
//        
278
//        Properties props = new Properties();
279
//        props.load(FileUtils.openInputStream(FileUtils.getFile(System.getProperty("user.home"),"onlineclient.properties")));
280
//        OnlineManager manager = OnlineLocator.getOnlineManager();
281
//        manager.setUserIdentificationRequester(new AbstractOnlineUserIdentificationRequester(
282
//                props.getProperty("online_user"),
283
//                props.getProperty("online_password"),
284
//                null
285
//            ) {
286
//            @Override
287
//            public boolean requestIdentification(String urlbase) {
288
//                return true;
289
//            }
290
//        });
291
//        File f;
292
//        String s;
293
//        OnlineSiteImpl site = (OnlineSiteImpl) manager.connectSite(new URL("https://devel.gvsigonline.com/gvsigonline"));
294
//        OnlineDownloader downloader = site.getDownloader();
295
////        f = downloader.get(new URL("https://devel.gvsigonline.com/gvsigonline/api/v1/projects"));
296
////        System.out.println(f);
297
////        s = FileUtils.readFileToString(f, StandardCharsets.UTF_8);
298
////        System.out.println(Json.createObject(s).toString());
299
//        f = downloader.get(new URL("https://devel.gvsigonline.com/gvsigonline/api/v1/projects/83/layers/"));
300
//        System.out.println(f);
301
//        s = FileUtils.readFileToString(f, StandardCharsets.UTF_8);
302
//        System.out.println(Json.createObject(s).toString());
303
//        System.out.println("Fin");
304
//    }
305
//    
306
//    public static void main(String[] args) throws Exception {
307
//        new DefaultLibrariesInitializer().fullInitialize();
308
//        
309
//        OnlineDownloaderImpl downloader = new OnlineDownloaderImpl();
310
//        File f = downloader.get(new URL("https://tile.openstreetmap.org/4/8/8.png"));
311
//        System.out.println(f);
312
//    }
313
    
314
}