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 @ 9515
History | View | Annotate | Download (11.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.HttpResponse; |
19 |
import org.apache.http.NameValuePair; |
20 |
import org.apache.http.auth.AuthenticationException; |
21 |
import org.apache.http.client.ClientProtocolException; |
22 |
import org.apache.http.client.ResponseHandler; |
23 |
import org.apache.http.client.entity.UrlEncodedFormEntity; |
24 |
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; |
25 |
import org.apache.http.client.methods.HttpGet; |
26 |
import org.apache.http.client.methods.HttpPost; |
27 |
import org.apache.http.client.methods.HttpUriRequest; |
28 |
import org.apache.http.entity.ContentType; |
29 |
import org.apache.http.entity.StringEntity; |
30 |
import org.apache.http.impl.client.CloseableHttpClient; |
31 |
import org.apache.http.impl.client.HttpClientBuilder; |
32 |
import org.apache.http.message.BasicNameValuePair; |
33 |
import org.gvsig.online.lib.api.OnlineSite; |
34 |
import org.gvsig.online.lib.api.OnlineUserIdentificationRequester; |
35 |
import org.gvsig.tools.ToolsLocator; |
36 |
|
37 |
/**
|
38 |
*
|
39 |
* @author jjdelcerro
|
40 |
*/
|
41 |
@SuppressWarnings("UseSpecificCatch") |
42 |
public class OnlineDownloaderImpl implements OnlineDownloader { |
43 |
|
44 |
// private OnlineAuthorizationRequester authorizationRequester;
|
45 |
private final OnlineSite site; |
46 |
|
47 |
private static class DownloaderResponseHandler implements ResponseHandler<Object> { |
48 |
|
49 |
private int status; |
50 |
private File f; |
51 |
private HttpResponse response;
|
52 |
|
53 |
public DownloaderResponseHandler(String urlpath) { |
54 |
this.status = 200; |
55 |
String fname = FilenameUtils.getBaseName(StringUtils.removeEnd(urlpath,"/")); |
56 |
this.f = ToolsLocator.getFoldersManager().getUniqueTemporaryFile(fname);
|
57 |
} |
58 |
|
59 |
@Override
|
60 |
public Object handleResponse(HttpResponse response) throws ClientProtocolException, IOException { |
61 |
this.status=response.getStatusLine().getStatusCode();
|
62 |
this.response = response;
|
63 |
FileOutputStream fs = null; |
64 |
try {
|
65 |
fs = new FileOutputStream(f); |
66 |
InputStream content = response.getEntity().getContent();
|
67 |
IOUtils.copy(content, fs); |
68 |
} catch (IOException ex) { |
69 |
f = null;
|
70 |
throw ex;
|
71 |
} catch (Exception ex) { |
72 |
f = null;
|
73 |
throw new IOException(ex); |
74 |
} finally {
|
75 |
IOUtils.closeQuietly(fs); |
76 |
} |
77 |
return null; |
78 |
} |
79 |
|
80 |
private int getStatus() { |
81 |
return this.status; |
82 |
} |
83 |
|
84 |
private File getFile() { |
85 |
return this.f; |
86 |
} |
87 |
|
88 |
} |
89 |
|
90 |
|
91 |
public OnlineDownloaderImpl(OnlineSite site) {
|
92 |
this.site = site;
|
93 |
} |
94 |
|
95 |
private DownloaderResponseHandler executeRequest(HttpUriRequest request) throws IOException { |
96 |
|
97 |
CloseableHttpClient httpClient = HttpClientBuilder.create() |
98 |
.build(); |
99 |
String authorization = this.site.getCurrentAuthorizationToken(); |
100 |
if( StringUtils.isNotBlank(authorization) ) {
|
101 |
request.addHeader("Authorization", authorization);
|
102 |
} |
103 |
DownloaderResponseHandler responseHandler = new DownloaderResponseHandler(request.getURI().toString());
|
104 |
|
105 |
System.out.println("REQUEST "+request.getMethod()+" "+request.getURI().toString()); |
106 |
for (Header header : request.getAllHeaders()) {
|
107 |
System.out.println(header.getName()+": "+header.getValue()); |
108 |
} |
109 |
if( request instanceof HttpEntityEnclosingRequestBase ) { |
110 |
InputStream content = ((HttpEntityEnclosingRequestBase) request).getEntity().getContent();
|
111 |
System.out.println("BODY data "+(StringUtils.join(IOUtils.readLines(content,"utf-8"),"\n"))); |
112 |
} |
113 |
httpClient.execute(request,responseHandler); |
114 |
System.out.println("RESPONSE CODE "+responseHandler.getStatus()); |
115 |
|
116 |
return responseHandler;
|
117 |
} |
118 |
|
119 |
@SuppressWarnings("Convert2Lambda") |
120 |
@Override
|
121 |
public File get(URL url) throws IOException { |
122 |
try {
|
123 |
int numretries = 3; |
124 |
for (int retries = 0; retries < numretries; retries++) { |
125 |
HttpGet request = new HttpGet(url.toURI());
|
126 |
request.setHeader("User-Agent","gvSIG-desktop"); |
127 |
request.setHeader("Referer","http://www.gvsig.com"); |
128 |
DownloaderResponseHandler responseHandler = executeRequest(request); |
129 |
switch(responseHandler.getStatus()) {
|
130 |
case 200: |
131 |
return responseHandler.getFile();
|
132 |
case 401: // 401 Authorization Required |
133 |
if( retries < numretries-1 ) { |
134 |
authorize(); |
135 |
} |
136 |
break;
|
137 |
case 500: |
138 |
throw new IOException("Can't download '"+Objects.toString(url)+"' 500 Server error"); |
139 |
} |
140 |
} |
141 |
throw new IOException("Can't download '"+Objects.toString(url)+"'"); |
142 |
|
143 |
} catch (IOException ex) { |
144 |
throw ex;
|
145 |
} catch (Exception ex) { |
146 |
throw new IOException("Can't download '"+Objects.toString(url)+"'", ex); |
147 |
} |
148 |
} |
149 |
|
150 |
@Override
|
151 |
@SuppressWarnings("Convert2Lambda") |
152 |
public File post_json(URL url, String json) throws IOException { |
153 |
try {
|
154 |
int numretries = 3; |
155 |
for (int retries = 0; retries < numretries; retries++) { |
156 |
HttpPost request = new HttpPost(url.toURI());
|
157 |
request.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON));
|
158 |
request.setHeader("Content-type","application/json"); |
159 |
DownloaderResponseHandler responseHandler = executeRequest(request); |
160 |
switch(responseHandler.getStatus()) {
|
161 |
case 200: |
162 |
return responseHandler.getFile();
|
163 |
case 401: // 401 Authorization Required |
164 |
if( retries < numretries-1 ) { |
165 |
authorize(); |
166 |
} |
167 |
break;
|
168 |
case 500: |
169 |
throw new IOException("Can't download '"+Objects.toString(url)+"' 500 Server error"); |
170 |
} |
171 |
} |
172 |
throw new IOException("Can't download '"+Objects.toString(url)+"' too many retries"); |
173 |
|
174 |
} catch (IOException ex) { |
175 |
throw ex;
|
176 |
} catch (Exception ex) { |
177 |
throw new IOException("Can't download '"+Objects.toString(url)+"'", ex); |
178 |
} |
179 |
} |
180 |
|
181 |
@Override
|
182 |
public File post_form(URL url, Map<String,String>data) throws IOException { |
183 |
try {
|
184 |
URI uri = url.toURI();
|
185 |
if( !uri.toString().endsWith("/") ) { |
186 |
uri = new URI(url.toString()+"/"); |
187 |
} |
188 |
int numretries = 3; |
189 |
for (int retries = 0; retries < numretries; retries++) { |
190 |
HttpPost request = new HttpPost(uri);
|
191 |
final List<NameValuePair> params = new ArrayList<>(); |
192 |
for (Map.Entry<String, String> entry : data.entrySet()) { |
193 |
String key = entry.getKey();
|
194 |
String value = entry.getValue();
|
195 |
params.add(new BasicNameValuePair(key, value));
|
196 |
} |
197 |
UrlEncodedFormEntity formdata = new UrlEncodedFormEntity(params);
|
198 |
request.setEntity(formdata); |
199 |
request.setHeader("Content-type","application/x-www-form-urlencoded"); |
200 |
DownloaderResponseHandler responseHandler = executeRequest(request); |
201 |
switch(responseHandler.getStatus()) {
|
202 |
case 200: |
203 |
return responseHandler.getFile();
|
204 |
case 401: // 401 Authorization Required |
205 |
if( retries < numretries-1 ) { |
206 |
authorize(); |
207 |
} |
208 |
break;
|
209 |
case 500: |
210 |
throw new IOException("Can't download '"+Objects.toString(url)+"' 500 Server error"); |
211 |
} |
212 |
} |
213 |
throw new IOException("Can't download '"+Objects.toString(url)+"' too many retries"); |
214 |
|
215 |
} catch (IOException ex) { |
216 |
throw ex;
|
217 |
} catch (Exception ex) { |
218 |
throw new IOException("Can't download '"+Objects.toString(url)+"'", ex); |
219 |
} |
220 |
} |
221 |
|
222 |
private void authorize() throws Exception { |
223 |
try {
|
224 |
System.out.println("AUTHORIZATION REQUIRED"); |
225 |
OnlineUserIdentificationRequester identificationRequester = this.site.getUserIdentificationRequester();
|
226 |
if( identificationRequester == null ) { |
227 |
throw new AuthenticationException("Not identification requester configured"); |
228 |
} |
229 |
if( !identificationRequester.requestIdentification() ) {
|
230 |
return;
|
231 |
} |
232 |
String authorization = identificationRequester.getAuthorization();
|
233 |
this.site.setCurrentUserCode(identificationRequester.getUserId());
|
234 |
this.site.setCurrentAuthorizationToken(authorization);
|
235 |
System.out.println("AUTH TOKEN: "+authorization); |
236 |
} catch (Exception ex) { |
237 |
// LOGGER.warn("Can't authorize", ex);
|
238 |
|
239 |
} |
240 |
} |
241 |
//
|
242 |
// public static void main0(String[] args) throws Exception {
|
243 |
// new DefaultLibrariesInitializer().fullInitialize();
|
244 |
//
|
245 |
// OnlineDownloaderImpl downloader = new OnlineDownloaderImpl();
|
246 |
// File f = downloader.get(new URL("https://devel.gvsigonline.com/gvsigonline/api/v1/projects"));
|
247 |
// System.out.println(f);
|
248 |
// }
|
249 |
//
|
250 |
// public static void main1(String[] args) throws Exception {
|
251 |
// new DefaultLibrariesInitializer().fullInitialize();
|
252 |
//
|
253 |
// Properties props = new Properties();
|
254 |
// props.load(FileUtils.openInputStream(FileUtils.getFile(System.getProperty("user.home"),"onlineclient.properties")));
|
255 |
// OnlineManager manager = OnlineLocator.getOnlineManager();
|
256 |
// manager.setUserIdentificationRequester(new AbstractOnlineUserIdentificationRequester(
|
257 |
// props.getProperty("online_user"),
|
258 |
// props.getProperty("online_password"),
|
259 |
// null
|
260 |
// ) {
|
261 |
// @Override
|
262 |
// public boolean requestIdentification(String urlbase) {
|
263 |
// return true;
|
264 |
// }
|
265 |
// });
|
266 |
// File f;
|
267 |
// String s;
|
268 |
// OnlineSiteImpl site = (OnlineSiteImpl) manager.connectSite(new URL("https://devel.gvsigonline.com/gvsigonline"));
|
269 |
// OnlineDownloader downloader = site.getDownloader();
|
270 |
//// f = downloader.get(new URL("https://devel.gvsigonline.com/gvsigonline/api/v1/projects"));
|
271 |
//// System.out.println(f);
|
272 |
//// s = FileUtils.readFileToString(f, StandardCharsets.UTF_8);
|
273 |
//// System.out.println(Json.createObject(s).toString());
|
274 |
// f = downloader.get(new URL("https://devel.gvsigonline.com/gvsigonline/api/v1/projects/83/layers/"));
|
275 |
// System.out.println(f);
|
276 |
// s = FileUtils.readFileToString(f, StandardCharsets.UTF_8);
|
277 |
// System.out.println(Json.createObject(s).toString());
|
278 |
// System.out.println("Fin");
|
279 |
// }
|
280 |
//
|
281 |
// public static void main(String[] args) throws Exception {
|
282 |
// new DefaultLibrariesInitializer().fullInitialize();
|
283 |
//
|
284 |
// OnlineDownloaderImpl downloader = new OnlineDownloaderImpl();
|
285 |
// File f = downloader.get(new URL("https://tile.openstreetmap.org/4/8/8.png"));
|
286 |
// System.out.println(f);
|
287 |
// }
|
288 |
|
289 |
} |