Revision 9515 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
OnlineDownloaderImpl.java | ||
---|---|---|
7 | 7 |
import java.io.InputStream; |
8 | 8 |
import java.net.URI; |
9 | 9 |
import java.net.URL; |
10 |
import java.nio.charset.StandardCharsets; |
|
11 | 10 |
import java.util.ArrayList; |
12 | 11 |
import java.util.List; |
13 | 12 |
import java.util.Map; |
14 | 13 |
import java.util.Objects; |
15 |
import org.apache.commons.io.FileUtils; |
|
16 | 14 |
import org.apache.commons.io.FilenameUtils; |
17 | 15 |
import org.apache.commons.io.IOUtils; |
18 | 16 |
import org.apache.commons.lang3.StringUtils; |
... | ... | |
32 | 30 |
import org.apache.http.impl.client.CloseableHttpClient; |
33 | 31 |
import org.apache.http.impl.client.HttpClientBuilder; |
34 | 32 |
import org.apache.http.message.BasicNameValuePair; |
35 |
import org.gvsig.json.Json; |
|
36 |
import org.gvsig.online.lib.api.OnlineLocator; |
|
37 |
import org.gvsig.online.lib.api.OnlineManager; |
|
33 |
import org.gvsig.online.lib.api.OnlineSite; |
|
38 | 34 |
import org.gvsig.online.lib.api.OnlineUserIdentificationRequester; |
39 | 35 |
import org.gvsig.tools.ToolsLocator; |
40 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
|
41 |
import org.gvsig.tools.observer.Observer; |
|
42 | 36 |
|
43 | 37 |
/** |
44 | 38 |
* |
... | ... | |
47 | 41 |
@SuppressWarnings("UseSpecificCatch") |
48 | 42 |
public class OnlineDownloaderImpl implements OnlineDownloader { |
49 | 43 |
|
44 |
// private OnlineAuthorizationRequester authorizationRequester; |
|
45 |
private final OnlineSite site; |
|
50 | 46 |
|
51 |
|
|
52 |
private String authorization; |
|
53 |
private OnlineAuthorizationRequester authorizationRequester; |
|
54 |
|
|
55 | 47 |
private static class DownloaderResponseHandler implements ResponseHandler<Object> { |
56 | 48 |
|
57 | 49 |
private int status; |
... | ... | |
96 | 88 |
} |
97 | 89 |
|
98 | 90 |
|
99 |
public OnlineDownloaderImpl() { |
|
100 |
|
|
91 |
public OnlineDownloaderImpl(OnlineSite site) {
|
|
92 |
this.site = site; |
|
101 | 93 |
} |
102 | 94 |
|
103 |
public void setAuthorizationRequester(OnlineAuthorizationRequester authorizationRequester) { |
|
104 |
this.authorizationRequester = authorizationRequester; |
|
105 |
} |
|
106 |
|
|
107 |
@Override |
|
108 |
public String getAuthorization() { |
|
109 |
return this.authorization; |
|
110 |
} |
|
111 |
|
|
112 | 95 |
private DownloaderResponseHandler executeRequest(HttpUriRequest request) throws IOException { |
113 | 96 |
|
114 | 97 |
CloseableHttpClient httpClient = HttpClientBuilder.create() |
115 | 98 |
.build(); |
116 |
if( StringUtils.isNotBlank(this.authorization) ) { |
|
117 |
request.addHeader("Authorization", this.authorization); |
|
99 |
String authorization = this.site.getCurrentAuthorizationToken(); |
|
100 |
if( StringUtils.isNotBlank(authorization) ) { |
|
101 |
request.addHeader("Authorization", authorization); |
|
118 | 102 |
} |
119 | 103 |
DownloaderResponseHandler responseHandler = new DownloaderResponseHandler(request.getURI().toString()); |
120 | 104 |
|
... | ... | |
204 | 188 |
int numretries = 3; |
205 | 189 |
for (int retries = 0; retries < numretries; retries++) { |
206 | 190 |
HttpPost request = new HttpPost(uri); |
207 |
final List<NameValuePair> params = new ArrayList<NameValuePair>();
|
|
191 |
final List<NameValuePair> params = new ArrayList<>(); |
|
208 | 192 |
for (Map.Entry<String, String> entry : data.entrySet()) { |
209 | 193 |
String key = entry.getKey(); |
210 | 194 |
String value = entry.getValue(); |
... | ... | |
236 | 220 |
} |
237 | 221 |
|
238 | 222 |
private void authorize() throws Exception { |
239 |
System.out.println("AUTHORIZATION REQUIRED"); |
|
240 |
if( this.authorizationRequester == null ) { |
|
241 |
throw new AuthenticationException("Not authorization requester configured"); |
|
242 |
} |
|
243 |
if( !this.authorizationRequester.requestAuthorization() ) { |
|
244 |
return; |
|
245 |
} |
|
246 |
this.authorization = this.authorizationRequester.getAuthorization(); |
|
247 |
System.out.println("AUTH TOKEN: "+this.authorization); |
|
248 |
} |
|
249 |
|
|
250 |
public static void main0(String[] args) throws Exception { |
|
251 |
new DefaultLibrariesInitializer().fullInitialize(); |
|
252 |
|
|
253 |
OnlineDownloaderImpl downloader = new OnlineDownloaderImpl(); |
|
254 |
File f = downloader.get(new URL("https://devel.gvsigonline.com/gvsigonline/api/v1/projects")); |
|
255 |
System.out.println(f); |
|
256 |
} |
|
257 |
|
|
258 |
public static void main1(String[] args) throws Exception { |
|
259 |
new DefaultLibrariesInitializer().fullInitialize(); |
|
260 |
|
|
261 |
OnlineManager manager = OnlineLocator.getOnlineManager(); |
|
262 |
manager.setUserIdentificationRequester(new OnlineUserIdentificationRequester() { |
|
263 |
@Override |
|
264 |
public boolean requestIdentification() { |
|
265 |
return true; |
|
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"); |
|
266 | 228 |
} |
267 |
|
|
268 |
@Override |
|
269 |
public String getUserId() { |
|
270 |
// return "TheUserName"; |
|
271 |
return "jjdelcerro"; |
|
229 |
if( !identificationRequester.requestIdentification() ) { |
|
230 |
return; |
|
272 | 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); |
|
273 | 238 |
|
274 |
@Override |
|
275 |
public String getPassword() { |
|
276 |
// return "ThePassword"; |
|
277 |
return "Iefo0She"; |
|
278 |
} |
|
279 |
|
|
280 |
@Override |
|
281 |
public void addObserver(Observer o) { |
|
282 |
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody |
|
283 |
} |
|
284 |
|
|
285 |
@Override |
|
286 |
public void deleteObserver(Observer o) { |
|
287 |
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody |
|
288 |
} |
|
289 |
|
|
290 |
@Override |
|
291 |
public void deleteObservers() { |
|
292 |
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody |
|
293 |
} |
|
294 |
}); |
|
295 |
File f; |
|
296 |
String s; |
|
297 |
OnlineSiteImpl site = (OnlineSiteImpl) manager.connectSite(new URL("https://devel.gvsigonline.com/gvsigonline")); |
|
298 |
OnlineDownloader downloader = site.getDownloader(); |
|
299 |
// f = downloader.get(new URL("https://devel.gvsigonline.com/gvsigonline/api/v1/projects")); |
|
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")); |
|
300 | 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); |
|
301 | 276 |
// s = FileUtils.readFileToString(f, StandardCharsets.UTF_8); |
302 | 277 |
// System.out.println(Json.createObject(s).toString()); |
303 |
f = downloader.get(new URL("https://devel.gvsigonline.com/gvsigonline/api/v1/projects/83/layers/")); |
|
304 |
System.out.println(f); |
|
305 |
s = FileUtils.readFileToString(f, StandardCharsets.UTF_8); |
|
306 |
System.out.println(Json.createObject(s).toString()); |
|
307 |
System.out.println("Fin"); |
|
308 |
} |
|
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 |
// } |
|
309 | 288 |
|
310 |
public static void main(String[] args) throws Exception { |
|
311 |
new DefaultLibrariesInitializer().fullInitialize(); |
|
312 |
|
|
313 |
OnlineDownloaderImpl downloader = new OnlineDownloaderImpl(); |
|
314 |
File f = downloader.get(new URL("https://tile.openstreetmap.org/4/8/8.png")); |
|
315 |
System.out.println(f); |
|
316 |
} |
|
317 |
|
|
318 | 289 |
} |
Also available in: Unified diff