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/OnlineSiteImpl.java
OnlineSiteImpl.java | ||
---|---|---|
12 | 12 |
import java.net.URL; |
13 | 13 |
import java.util.HashMap; |
14 | 14 |
import java.util.Iterator; |
15 |
import java.util.LinkedHashMap; |
|
16 | 15 |
import java.util.Map; |
17 | 16 |
import javax.json.JsonArray; |
18 | 17 |
import javax.json.JsonObject; |
... | ... | |
24 | 23 |
import org.gvsig.online.lib.api.OnlineDownloader; |
25 | 24 |
import org.gvsig.online.lib.api.OnlineLayer; |
26 | 25 |
import org.gvsig.online.lib.api.OnlineLocator; |
27 |
import org.gvsig.online.lib.api.OnlineManager; |
|
28 | 26 |
import org.gvsig.online.lib.api.OnlineProject; |
29 | 27 |
import org.gvsig.online.lib.api.OnlineSite; |
30 | 28 |
import org.gvsig.online.lib.api.OnlineUserIdentificationRequester; |
29 |
import org.gvsig.online.lib.api.OnlineUserIdentificationRequester.OnlineUserIdentificationRequesterConfig; |
|
31 | 30 |
import org.slf4j.Logger; |
32 | 31 |
import org.slf4j.LoggerFactory; |
33 | 32 |
|
... | ... | |
45 | 44 |
private Map<String, OnlineLayer> publicLayers; |
46 | 45 |
private URL urlbase; |
47 | 46 |
private OnlineDownloaderImpl downloader; |
48 |
|
|
49 |
private class AuthorizationRequester implements OnlineDownloader.OnlineAuthorizationRequester { |
|
50 |
|
|
51 |
private String authorization; |
|
52 |
|
|
53 |
@Override |
|
54 |
public boolean requestAuthorization() { |
|
55 |
FileInputStream fis = null; |
|
56 |
try { |
|
57 |
OnlineManager manager = OnlineLocator.getOnlineManager(); |
|
58 |
OnlineUserIdentificationRequester identificationRequester = manager.getUserIdentificationRequester(); |
|
59 |
if( identificationRequester == null ) { |
|
60 |
return false; |
|
61 |
} |
|
62 |
if( !identificationRequester.requestIdentification() ) { |
|
63 |
return false; |
|
64 |
} |
|
65 |
URL url = new URL(urlbase+"/auth/api-token-auth"); |
|
66 |
OnlineDownloader downloader = getDownloader(); |
|
67 |
|
|
68 |
// JsonObjectBuilder data = Json.createObjectBuilder(); |
|
69 |
// data.add("username", identificationRequester.getUserId()); |
|
70 |
// data.add("password", identificationRequester.getPassword()); |
|
71 |
// File f = downloader.post_form(url, data.toString()); |
|
72 |
Map<String,String> data = new LinkedHashMap<>(); |
|
73 |
data.put("username", identificationRequester.getUserId()); |
|
74 |
data.put("password", identificationRequester.getPassword()); |
|
75 |
File f = downloader.post_form(url, data); |
|
76 |
|
|
77 |
fis = new FileInputStream(f); |
|
78 |
JsonObject resp = Json.createObject(fis); |
|
79 |
String token = resp.getString("token", null); |
|
80 |
if( StringUtils.isBlank(token) ) { |
|
81 |
return false; |
|
82 |
} |
|
83 |
this.authorization = "JWT " + token; |
|
84 |
return true; |
|
85 |
} catch (Exception ex) { |
|
86 |
LOGGER.warn("Can't authorize", ex); |
|
87 |
return false; |
|
88 |
} finally { |
|
89 |
IOUtils.closeQuietly(fis); |
|
90 |
} |
|
91 |
} |
|
92 |
|
|
93 |
@Override |
|
94 |
public String getAuthorization() { |
|
95 |
return this.authorization; |
|
96 |
} |
|
97 |
|
|
98 |
} |
|
99 |
|
|
100 |
|
|
47 |
private String currentUserCode; |
|
48 |
private String currentAuthenticationToken; |
|
49 |
private OnlineUserIdentificationRequesterConfig userIdentificationRequesterConfig; |
|
50 |
|
|
101 | 51 |
public OnlineSiteImpl() { |
102 | 52 |
clear(); |
103 | 53 |
} |
104 | 54 |
|
105 |
public OnlineSiteImpl(URL url) { |
|
55 |
public OnlineSiteImpl(OnlineUserIdentificationRequesterConfig userIdentificationRequesterConfig, URL url) {
|
|
106 | 56 |
this(); |
57 |
this.userIdentificationRequesterConfig = userIdentificationRequesterConfig; |
|
107 | 58 |
this.urlbase = url; |
108 | 59 |
} |
109 | 60 |
|
110 | 61 |
public final void clear() { |
62 |
this.userIdentificationRequesterConfig = null; |
|
111 | 63 |
this.urlbase = null; |
112 | 64 |
this.projects_json = null; |
113 | 65 |
this.publicLayers = null; |
... | ... | |
116 | 68 |
|
117 | 69 |
public OnlineDownloader getDownloader() { |
118 | 70 |
if( this.downloader == null ) { |
119 |
this.downloader = new OnlineDownloaderImpl(); |
|
120 |
this.downloader.setAuthorizationRequester(new AuthorizationRequester()); |
|
71 |
this.downloader = new OnlineDownloaderImpl(this); |
|
121 | 72 |
} |
122 | 73 |
return this.downloader; |
123 | 74 |
} |
124 | 75 |
|
125 | 76 |
@Override |
126 | 77 |
public void connect() throws IOException { |
127 |
this.connect(this.urlbase); |
|
78 |
this.connect(this.userIdentificationRequesterConfig,this.urlbase);
|
|
128 | 79 |
} |
129 | 80 |
|
130 | 81 |
@Override |
131 |
public void connect(URL url) throws IOException { |
|
82 |
public void connect(OnlineUserIdentificationRequesterConfig userIdentificationRequesterConfig, URL url) throws IOException {
|
|
132 | 83 |
FileInputStream fis = null; |
133 | 84 |
try { |
85 |
this.userIdentificationRequesterConfig = userIdentificationRequesterConfig; |
|
134 | 86 |
OnlineDownloader downloader = this.getDownloader(); |
135 | 87 |
File f = downloader.get(new URL(url.toString()+"/api/v1/projects")); |
136 | 88 |
fis = new FileInputStream(f); |
... | ... | |
205 | 157 |
return null; |
206 | 158 |
} |
207 | 159 |
|
160 |
@Override |
|
208 | 161 |
public Map<String, OnlineLayer> getPublicLayers() { |
209 | 162 |
if( this.publicLayers == null ) { |
210 | 163 |
FileInputStream fis = null; |
211 | 164 |
try { |
212 |
OnlineDownloader downloader = new OnlineDownloaderImpl();;
|
|
165 |
OnlineDownloader downloader = this.getDownloader();
|
|
213 | 166 |
File f = downloader.get(new URL(urlbase.toString()+"/api/v1/layers")); |
214 | 167 |
fis = new FileInputStream(f); |
215 | 168 |
JsonObject json = Json.createObject(fis); |
... | ... | |
231 | 184 |
return this.getPublicLayers().containsKey(String.valueOf(layer.getId())); |
232 | 185 |
} |
233 | 186 |
|
234 |
// private JsonObject getLayers() { |
|
235 |
// if( this.layers_json == null ) { |
|
236 |
// FileInputStream fis = null; |
|
237 |
// try { |
|
238 |
// OnlineDownloader downloader = this.getDownloader(); |
|
239 |
// File f = downloader.get(new URL(urlbase.toString()+"/api/v1/layers")); |
|
240 |
// fis = new FileInputStream(f); |
|
241 |
// JsonObject json = Json.createObject(fis); |
|
242 |
// this.layers_json = json; |
|
243 |
// } catch(Exception ex) { |
|
244 |
// throw new RuntimeException("Can't download layers from "+urlbase, ex); |
|
245 |
// } finally { |
|
246 |
// IOUtils.closeQuietly(fis); |
|
247 |
// } |
|
248 |
// } |
|
249 |
// return this.layers_json; |
|
250 |
// } |
|
251 |
|
|
252 |
// public OnlineEntityImpl getLayer(int id) { |
|
253 |
// JsonArray layers = this.getLayers().getJsonArray("content"); |
|
254 |
// for (JsonValue layer0 : layers) { |
|
255 |
// JsonObject layer = (JsonObject) layer0; |
|
256 |
// if( layer.getInt("id", -1)==id ) { |
|
257 |
// OnlineEntityImpl l = new OnlineEntityImpl().setSite(this); |
|
258 |
// l.fromJson(layer); |
|
259 |
// return l; |
|
260 |
// } |
|
261 |
// } |
|
262 |
// return null; |
|
263 |
// } |
|
264 |
// |
|
265 |
// public OnlineEntityImpl getLayer(String name) { |
|
266 |
// JsonArray layers = this.getLayers().getJsonArray("content"); |
|
267 |
// for (JsonValue layer0 : layers) { |
|
268 |
// JsonObject layer = (JsonObject) layer0; |
|
269 |
// if( StringUtils.equals(layer.getString("name", null),name) ) { |
|
270 |
// OnlineEntityImpl l = new OnlineEntityImpl().setSite(this); |
|
271 |
// l.fromJson(layer); |
|
272 |
// return l; |
|
273 |
// } |
|
274 |
// } |
|
275 |
// return null; |
|
276 |
// } |
|
277 |
|
|
278 | 187 |
// public static void main(String[] args) throws Exception { |
279 | 188 |
// new DefaultLibrariesInitializer().fullInitialize(); |
280 | 189 |
// |
... | ... | |
302 | 211 |
// } |
303 | 212 |
// URL url = new URL(s); |
304 | 213 |
URL url = (URL) Json.toObject(json, "url"); |
214 |
JsonObject uirc = json.getJsonObject("userIdentificationRequesterConfig"); |
|
215 |
if( uirc == null ) { |
|
216 |
this.userIdentificationRequesterConfig = null; |
|
217 |
} else { |
|
218 |
this.userIdentificationRequesterConfig = OnlineLocator.getOnlineManager().getUserIdentificationRequester(uirc); |
|
219 |
} |
|
305 | 220 |
this.clear(); |
306 | 221 |
this.urlbase = url; |
307 | 222 |
} catch (Exception ex) { |
... | ... | |
313 | 228 |
public JsonObjectBuilder toJsonBuilder() { |
314 | 229 |
JsonObjectBuilder builder = Json.createObjectBuilder(); |
315 | 230 |
builder.add("url", this.urlbase); |
231 |
if( this.userIdentificationRequesterConfig == null ) { |
|
232 |
builder.addNull("userIdentificationRequesterConfig"); |
|
233 |
} else { |
|
234 |
builder.add("userIdentificationRequesterConfig",this.userIdentificationRequesterConfig.toJsonBuilder()); |
|
235 |
} |
|
316 | 236 |
return builder; |
317 | 237 |
} |
318 | 238 |
|
... | ... | |
328 | 248 |
} |
329 | 249 |
} |
330 | 250 |
|
251 |
@Override |
|
331 | 252 |
public URL getGeoserverURL(String s) { |
332 | 253 |
try { |
333 | 254 |
String base_s = StringUtils.removeEnd(this.urlbase.toString(), "/"); |
... | ... | |
340 | 261 |
throw new RuntimeException("Can't get geoserver URL",ex); |
341 | 262 |
} |
342 | 263 |
} |
264 |
|
|
265 |
@Override |
|
266 |
public void setCurrentUserCode(String userCode) { |
|
267 |
this.currentUserCode = userCode; |
|
268 |
} |
|
269 |
|
|
270 |
@Override |
|
271 |
public String getCurrentUserCode() { |
|
272 |
return currentUserCode; |
|
273 |
} |
|
274 |
|
|
275 |
@Override |
|
276 |
public String getCurrentAuthorizationToken() { |
|
277 |
return this.currentAuthenticationToken; |
|
278 |
} |
|
279 |
|
|
280 |
@Override |
|
281 |
public void setCurrentAuthorizationToken(String authenticationToken) { |
|
282 |
this.currentAuthenticationToken = authenticationToken; |
|
283 |
} |
|
284 |
|
|
285 |
public OnlineUserIdentificationRequester getUserIdentificationRequester() { |
|
286 |
OnlineUserIdentificationRequester identificationRequester = this.userIdentificationRequesterConfig.createUserIdentificationRequester(); |
|
287 |
return identificationRequester; |
|
288 |
} |
|
289 |
|
|
290 |
@Override |
|
291 |
public void logout() { |
|
292 |
this.currentAuthenticationToken = null; |
|
293 |
} |
|
294 |
|
|
343 | 295 |
} |
Also available in: Unified diff