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 / OnlineSiteImpl.java @ 9515

History | View | Annotate | Download (10.4 KB)

1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.online.lib.impl;
7

    
8
import java.io.File;
9
import java.io.FileInputStream;
10
import java.io.IOException;
11
import java.net.MalformedURLException;
12
import java.net.URL;
13
import java.util.HashMap;
14
import java.util.Iterator;
15
import java.util.Map;
16
import javax.json.JsonArray;
17
import javax.json.JsonObject;
18
import javax.json.JsonValue;
19
import org.apache.commons.io.IOUtils;
20
import org.apache.commons.lang3.StringUtils;
21
import org.gvsig.json.Json;
22
import org.gvsig.json.JsonObjectBuilder;
23
import org.gvsig.online.lib.api.OnlineDownloader;
24
import org.gvsig.online.lib.api.OnlineLayer;
25
import org.gvsig.online.lib.api.OnlineLocator;
26
import org.gvsig.online.lib.api.OnlineProject;
27
import org.gvsig.online.lib.api.OnlineSite;
28
import org.gvsig.online.lib.api.OnlineUserIdentificationRequester;
29
import org.gvsig.online.lib.api.OnlineUserIdentificationRequester.OnlineUserIdentificationRequesterConfig;
30
import org.slf4j.Logger;
31
import org.slf4j.LoggerFactory;
32

    
33
/**
34
 *
35
 * @author jjdelcerro
36
 */
37
@SuppressWarnings("UseSpecificCatch")
38
public class OnlineSiteImpl implements OnlineSite {
39

    
40
    private static Logger LOGGER = LoggerFactory.getLogger(OnlineSiteImpl.class);
41
    
42
    private JsonObject projects_json;
43
//    private JsonObject layers_json;
44
    private Map<String, OnlineLayer> publicLayers;
45
    private URL urlbase;
46
    private OnlineDownloaderImpl downloader;
47
    private String currentUserCode;
48
    private String currentAuthenticationToken;
49
    private OnlineUserIdentificationRequesterConfig userIdentificationRequesterConfig;
50
       
51
    public OnlineSiteImpl() {
52
        clear();
53
    }
54
    
55
    public OnlineSiteImpl(OnlineUserIdentificationRequesterConfig userIdentificationRequesterConfig, URL url) {
56
        this();
57
        this.userIdentificationRequesterConfig = userIdentificationRequesterConfig;
58
        this.urlbase = url;
59
    }
60
    
61
    public final void clear() {
62
        this.userIdentificationRequesterConfig = null;
63
        this.urlbase = null;
64
        this.projects_json = null;
65
        this.publicLayers = null;
66
        this.downloader = null;
67
    }
68
    
69
    public OnlineDownloader getDownloader() {
70
        if( this.downloader == null ) {
71
            this.downloader = new OnlineDownloaderImpl(this);
72
        }
73
        return this.downloader;
74
    }
75
    
76
    @Override
77
    public void connect() throws IOException {
78
        this.connect(this.userIdentificationRequesterConfig,this.urlbase);
79
    }
80
    
81
    @Override
82
    public void connect(OnlineUserIdentificationRequesterConfig userIdentificationRequesterConfig, URL url) throws IOException {
83
        FileInputStream fis = null;
84
        try {
85
            this.userIdentificationRequesterConfig = userIdentificationRequesterConfig;
86
            OnlineDownloader downloader = this.getDownloader();
87
            File f = downloader.get(new URL(url.toString()+"/api/v1/projects"));
88
            fis = new FileInputStream(f);
89
            JsonObject json = Json.createObject(fis);
90
            this.urlbase = url;
91
            this.projects_json = json;
92
            this.publicLayers = null;
93
        } finally {
94
            IOUtils.closeQuietly(fis);
95
        }
96
    }
97
    
98
    @Override
99
    public URL getBaseUrl() {
100
        return this.urlbase;
101
    }
102

    
103
    @SuppressWarnings("Convert2Lambda")
104
    @Override
105
    public Iterable<OnlineProject> projects() {
106
        OnlineSiteImpl site = this;
107
        return new Iterable<OnlineProject>() {
108
            @Override
109
            public Iterator<OnlineProject> iterator() {
110
                Iterator<OnlineProject> it = new Iterator<OnlineProject>() {
111
                    JsonArray projects = projects_json.getJsonArray("content");
112
                    Iterator<JsonValue> jsonit = projects.iterator();
113

    
114
                    @Override
115
                    public boolean hasNext() {
116
                        return jsonit.hasNext();
117
                    }
118

    
119
                    @Override
120
                    public OnlineProjectImpl next() {
121
                        JsonObject jsonproject = (JsonObject) jsonit.next();
122
                        OnlineProjectImpl project = new OnlineProjectImpl().setSite(site);
123
                        project.fromJson(jsonproject);
124
                        return project;
125
                    }
126
                };
127
                return it;
128
            }
129
        };
130
    }
131
    
132
    @Override
133
    public OnlineProjectImpl getProject(String name) {
134
        JsonArray projects = projects_json.getJsonArray("content");
135
        for (JsonValue project0 : projects) {
136
            JsonObject project = (JsonObject) project0;
137
            if( StringUtils.equals(project.getString("name", null),name) ) {
138
                OnlineProjectImpl p = new OnlineProjectImpl().setSite(this);
139
                p.fromJson(project);
140
                return p;
141
            }
142
        }
143
        return null;
144
    }
145

    
146
    @Override
147
    public OnlineProjectImpl getProject(int id) {
148
        JsonArray projects = projects_json.getJsonArray("content");
149
        for (JsonValue project0 : projects) {
150
            JsonObject project = (JsonObject) project0;
151
            if( project.getInt("id", -1)==id ) {
152
                OnlineProjectImpl p = new OnlineProjectImpl().setSite(this);
153
                p.fromJson(project);
154
                return p;
155
            }
156
        }
157
        return null;
158
    }
159

    
160
    @Override
161
    public Map<String, OnlineLayer> getPublicLayers() {
162
        if( this.publicLayers == null ) {
163
            FileInputStream fis = null;
164
            try {
165
                OnlineDownloader downloader = this.getDownloader();
166
                File f = downloader.get(new URL(urlbase.toString()+"/api/v1/layers"));
167
                fis = new FileInputStream(f);
168
                JsonObject json = Json.createObject(fis);
169
                this.publicLayers = new HashMap<>();
170
                for (JsonValue layer_json : json.getJsonArray("content")) {
171
                    OnlineLayerImpl layer = new OnlineLayerImpl(this, (JsonObject) layer_json);
172
                    this.publicLayers.put(String.valueOf(layer.getId()), layer);
173
                }
174
            } catch(Exception ex) {
175
                throw new RuntimeException("Can't download layers from "+urlbase, ex);
176
            } finally {
177
                IOUtils.closeQuietly(fis);
178
            }
179
        }
180
        return this.publicLayers;
181
    }
182
    
183
    public boolean isPublicLayer(OnlineLayer layer) {
184
        return this.getPublicLayers().containsKey(String.valueOf(layer.getId()));
185
    }
186
    
187
//    public static void main(String[] args) throws Exception {
188
//        new DefaultLibrariesInitializer().fullInitialize();
189
//        
190
//        URL url = new URL("https://devel.gvsigonline.com/gvsigonline");
191
//        OnlineSiteImpl site = new OnlineSiteImpl();
192
//        site.connect(url);
193
//        for (OnlineProject project : site.projects()) {
194
//            System.out.println(project.getId()+", "+project.getName()+", "+project.getEnvelope());
195
//        }
196
//        System.out.println("-----------");
197
//        OnlineProjectImpl project = site.getProject("cartagena");
198
//        System.out.println(project.getId()+", "+project.getName()+", "+project.getEnvelope());
199
//        project = site.getProject(27);
200
//        System.out.println(project.getId()+", "+project.getName()+", "+project.getEnvelope());
201
//        
202
//        System.out.println("Fin");
203
//    }
204

    
205
    @Override
206
    public void fromJson(JsonObject json) {
207
        try {
208
//            String s = json.getString("url", null);
209
//            if( s == null ) {
210
//                throw new IllegalArgumentException("url is required");
211
//            }
212
//            URL url = new URL(s);
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
            }
220
            this.clear();
221
            this.urlbase = url;
222
        } catch (Exception ex) {
223
            throw new IllegalArgumentException("url is required", ex);
224
        }
225
    }
226

    
227
    @Override
228
    public JsonObjectBuilder toJsonBuilder() {
229
        JsonObjectBuilder builder = Json.createObjectBuilder();
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
        }
236
        return builder;
237
    }
238

    
239
    public URL getURL(String s) {
240
        try {
241
            String base_s = StringUtils.removeEnd(this.urlbase.toString(), "/");
242
            if( StringUtils.startsWith(s, "/") ) {
243
                return new URL(base_s+s);
244
            }
245
            return new URL(base_s+"/"+s);
246
        } catch (MalformedURLException ex) {
247
            throw new RuntimeException("Can't get URL",ex);
248
        }
249
    }
250

    
251
    @Override
252
    public URL getGeoserverURL(String s) {
253
        try {
254
            String base_s = StringUtils.removeEnd(this.urlbase.toString(), "/");
255
            base_s = StringUtils.left(base_s, StringUtils.lastIndexOf(base_s, '/'));
256
            if( StringUtils.startsWith(s, "/") ) {
257
                return new URL(base_s+"/geoserver"+s);
258
            }
259
            return new URL(base_s+"/geoserver/"+s);
260
        } catch (Exception ex) {
261
            throw new RuntimeException("Can't get geoserver URL",ex);
262
        }
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

    
295
}