Statistics
| Revision:

gvsig-raster / org.gvsig.raster.wms / trunk / org.gvsig.raster.wms / org.gvsig.raster.wms.io / src / main / java / org / gvsig / raster / wms / io / WMSServerExplorer.java @ 22299

History | View | Annotate | Download (11 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
*
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
* MA  02110-1301, USA.
20
*
21
*/
22

    
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2009 IVER T.I   {{Task}}
26
*/
27

    
28
package org.gvsig.raster.wms.io;
29

    
30
import java.awt.geom.Rectangle2D;
31
import java.io.File;
32
import java.io.IOException;
33
import java.io.InputStream;
34
import java.net.MalformedURLException;
35
import java.net.URI;
36
import java.net.URISyntaxException;
37
import java.net.URL;
38
import java.net.URLConnection;
39
import java.util.ArrayList;
40
import java.util.Hashtable;
41
import java.util.List;
42
import java.util.Vector;
43
import java.util.logging.Level;
44
import org.apache.commons.lang3.StringUtils;
45

    
46
import org.gvsig.compat.net.ICancellable;
47
import org.gvsig.fmap.dal.DALLocator;
48
import org.gvsig.fmap.dal.DataManager;
49
import org.gvsig.fmap.dal.DataServerExplorerParameters;
50
import org.gvsig.fmap.dal.DataStore;
51
import org.gvsig.fmap.dal.DataStoreParameters;
52
import org.gvsig.fmap.dal.NewDataStoreParameters;
53
import org.gvsig.fmap.dal.coverage.exception.ConnectException;
54
import org.gvsig.fmap.dal.coverage.exception.RemoteServiceException;
55
import org.gvsig.fmap.dal.coverage.store.RasterDataServerExplorer;
56
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
57
import org.gvsig.fmap.dal.exception.DataException;
58
import org.gvsig.fmap.dal.exception.InitializeException;
59
import org.gvsig.fmap.dal.exception.OpenException;
60
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
61
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
62
import org.gvsig.fmap.dal.spi.AbstractDataServerExplorer;
63
import org.gvsig.fmap.dal.spi.DataServerExplorerProvider;
64
import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices;
65
import org.gvsig.i18n.Messages;
66
import org.gvsig.remoteclient.utils.Utilities;
67
import org.gvsig.tools.task.Cancellable;
68

    
69
import org.slf4j.Logger;
70
import org.slf4j.LoggerFactory;
71

    
72
/**
73
 * Explorer for a WMS server
74
 * @author Nacho Brodin (nachobrodin@gmail.com)
75
 */
76
public class WMSServerExplorer extends AbstractDataServerExplorer implements RasterDataServerExplorer, DataServerExplorerProvider {
77
        public static final String          NAME                     = WMSProvider.NAME;
78
        private WMSConnector                connector                = null;
79
//        private WMSServerExplorerParameters parameters               = null;
80
        private Logger                      log                      = LoggerFactory.getLogger(WMSServerExplorer.class);
81

    
82
        public WMSServerExplorer(
83
                        WMSServerExplorerParameters parameters,
84
                        DataServerExplorerProviderServices services)
85
                        throws InitializeException {
86
                super(parameters, services);
87
//                this.parameters = parameters;
88
        }
89

    
90
        /**
91
         * Gets the provider's name
92
         * @return
93
         */
94
        public String getDataStoreProviderName() {
95
                return WMSProvider.NAME;
96
        }
97

    
98
        public String getDescription() {
99
                return WMSProvider.DESCRIPTION;
100
        }
101

    
102
        public boolean add(String provider, NewDataStoreParameters parameters,
103
                        boolean overwrite) throws DataException {
104
                return false;
105
        }
106

    
107
        public boolean canAdd() {
108
                return false;
109
        }
110

    
111
        public boolean canAdd(String storeName) throws DataException {
112
                return false;
113
        }
114

    
115
        public NewDataStoreParameters getAddParameters(String storeName)
116
                        throws DataException {
117
                return null;
118
        }
119

    
120
        public List<String> getDataStoreProviderNames() {
121
                return null;
122
        }
123

    
124
        public WMSServerExplorerParameters getParameters() {
125
                return (WMSServerExplorerParameters) super.getParameters();
126
        }
127

    
128
        public List<DataStoreParameters> list() throws DataException {
129
                return null;
130
        }
131

    
132
        public List<DataStoreParameters> list(int mode) throws DataException {
133
                return null;
134
        }
135

    
136
        public void remove(DataStoreParameters parameters) throws DataException {
137

    
138
        }
139

    
140
        public String getProviderName() {
141
                return null;
142
        }
143

    
144
        //**********************************************
145
        //Connector
146
        //**********************************************
147

    
148
        @Override
149
        public DataStoreParameters get(String layerName) {
150
            WMSDataParameters params = (WMSDataParameters) this.getStoredParameters();
151
            params.setLayerQuery(layerName);
152
            return params;
153
        }
154
        
155
        public DataStoreParameters getStoredParameters() {
156
                DataManager manager = DALLocator.getDataManager();
157
                WMSDataParameters params = null;
158
                try {
159
                        params = (WMSDataParameters) manager.createStoreParameters(this.getDataStoreProviderName());
160
                        params.setURI(new URI(getParameters().getHost()));
161

    
162
                        /*if(WMSProvider.TILED) {
163
                                TileDataParameters tileParams = (TileDataParameters) manager.createStoreParameters("Tile Store");
164
                                tileParams.setDataParameters(params);
165
                                return tileParams;
166
                        } */
167
                } catch (InitializeException e) {
168
                        log.debug("Error initializating parameters", e);
169
                } catch (ProviderNotRegisteredException e) {
170
                        log.debug("Error getting parameters", e);
171
                } catch (URISyntaxException e) {
172
            log.debug("Can't create URI from "+getParameters().getHost(), e);
173
        }
174

    
175
                return params;
176
        }
177

    
178
        @Override
179
        public void open() throws OpenException {
180
            try {
181
                this.connect(null);
182
            } catch (ConnectException ex) {
183
                throw new OpenException(this.getHost(), ex);
184
            }
185
        }
186

    
187
        public void connect(ICancellable cancellable) throws ConnectException {
188
                connect(cancellable, false);
189
        }
190

    
191
        /**
192
         * Connects to the server and throws a getCapabilities. This loads
193
         * the basic information to make requests.
194
         * @throws RemoteServiceException
195
         */
196
        public void connect(ICancellable cancellable, boolean updateCache) throws ConnectException {
197
                URL url = null;
198

    
199
                try {
200
                        url = new URL(getParameters().getHost());
201
                } catch (Exception e) {
202
                        throw new ConnectException(Messages.getText("malformed_url"), e);
203
                }
204
        try {
205
                connector = WMSProvider.getConnectorFromURL(url, updateCache);
206
                if (!connector.connect(this.getParameters(), updateCache, cancellable))
207
                        throw new ConnectException(Messages.getText("error_connecting"));
208
        } catch (IOException e) {
209
                        throw new ConnectException(Messages.getText("error_connecting"), e);
210
                }
211

    
212
        }
213

    
214
        /**
215
         * Checks if the network and host are reachable
216
         * @param timeout for the host
217
         * @return true if both are reachable and false if they are not
218
         */
219
        public boolean isHostReachable(int timeout) {
220
                URL url = null;
221
                try {
222
                        url = new URL(getParameters().getHost());
223
                        File file = Utilities.downloadFile(url, "checkhost.dat", null);
224
                } catch (Exception e) {
225
                        return false;
226
                }
227

    
228
                return true;
229
        }
230

    
231
        /**
232
         * Checks if the network and host are reachable
233
         * @return true if both are reachable and false if they are not
234
         */
235
        public boolean isHostReachable() {
236
                int timeout = 10000;
237
                return isHostReachable(timeout);
238
        }
239

    
240
        /**
241
         * Returns true if this provider is connected to the server
242
         * @return
243
         */
244
        public boolean isConnected() {
245
                if(connector != null)
246
                        return true;
247
                return false;
248
        }
249

    
250
        /**
251
         * Gets the description of this service
252
         * @return
253
         */
254
        public String getAbstract() {
255
                if(connector != null)
256
                        return connector.getAbstract();
257
                return null;
258
        }
259

    
260
        /**
261
         * Gets the list of raster formats supported by the server
262
         * @return
263
         */
264
        @SuppressWarnings("unchecked")
265
        public String[] getFormats() {
266
                if(connector != null) {
267
                        Vector f = connector.getFormats();
268
                        ArrayList formatos = new ArrayList();
269
                        for (int i = 0; i < f.size(); i++) {
270
                                formatos.add(f.elementAt(i));
271
                        }
272
                        return (String[]) formatos.toArray(new String[0]);
273
                }
274
                return null;
275
        }
276

    
277
        /**
278
         * Gets the list of raster information formats supported by the server
279
         * @return
280
         */
281
        @SuppressWarnings("unchecked")
282
        public String[] getInfoFormats() {
283
                if(connector != null) {
284
                        Vector f = connector.getInfoFormats();
285
                        ArrayList formatos = new ArrayList();
286
                        for (int i = 0; i < f.size(); i++) {
287
                                formatos.add(f.elementAt(i));
288
                        }
289
                        return (String[]) formatos.toArray(new String[0]);
290
                }
291
                return null;
292
        }
293

    
294
        /**
295
         * Gets a tree of nodes which represents the server information
296
         * @return
297
         */
298
        public WMSLayerNode getLayerTree() {
299
                if(connector != null) {
300
                        return connector.getLayersTree();
301
                }
302
                return null;
303
        }
304

    
305
        /**
306
         * Gets the server title
307
         * @return
308
         */
309
        public String getServerType() {
310
                if (getVersion() == null)
311
                        return "WMS";
312
        return "WMS "+ getVersion();
313
        }
314

    
315
        /**
316
         * Gets the online resources
317
         * @return
318
         */
319
        public Hashtable getOnlineResources() {
320
                if(connector != null) {
321
                        return connector.getOnlineResources();
322
                }
323
                return null;
324
        }
325

    
326
        /**
327
         * Gets the protocol supported by the server
328
         * @return
329
         */
330
        public String getVersion() {
331
                if(connector != null) {
332
                        return (connector.getVersion() == null) ? "" : connector.getVersion();
333
                }
334
                return null;
335
        }
336

    
337
    public Rectangle2D getLayersExtent(String[] layerName, String srs) {
338
            return connector.getLayersExtent(layerName, srs);
339
    }
340

    
341
    /**
342
         * Gets a layer using its name
343
         * @param layerName
344
         * @return
345
         */
346
    public WMSLayerNode getLayer(String layerName) {
347
            return connector.getLayer(layerName);
348
    }
349

    
350
    /**
351
         * Gets the host URI
352
         * @return
353
         */
354
        public String getHost() {
355
                return getParameters().getHost();
356
        }
357

    
358
        public Rectangle2D getBoundingBox(String[] layerNames, String srs) {
359
                return connector.getLayersExtent(layerNames, srs);
360
    }
361

    
362
        public boolean isQueryable() {
363
            return connector.isQueryable();
364
    }
365

    
366
        /**
367
         * Gets the title
368
         * @return
369
         */
370
        public String getTitle() {
371
                return null;
372
        }
373

    
374
    /**
375
     * @return The title of the service offered by the WMS server.
376
     */
377
    public String getServiceTitle() {
378
                return connector.getServiceTitle();
379
    }
380

    
381
    public DataServerExplorerProviderServices getServerExplorerProviderServices() {
382
            return null;
383
    }
384

    
385
    @Override
386
    public DataStore open(DataStoreParameters par) throws DataException {
387
        String layerName = "unknown";
388
        try {
389
            layerName = (String) par.getDynValue(WMSDataParametersImpl.FIELD_LAYERQUERY);
390
            DataManager datamanager = DALLocator.getDataManager();
391
            RasterDataStore store = (RasterDataStore) datamanager.openStore(NAME, par);
392
            store.setExplorer(this);
393
            return store;
394
        } catch (Exception ex) {
395
            throw new OpenException(layerName,ex);
396
        }
397
    }
398

    
399
    @Override
400
    public Object getProperty(String name) {
401
        if( StringUtils.equalsIgnoreCase("OnlineResources", name) ) {
402
            return this.getOnlineResources();
403
        }
404
        return super.getProperty(name);
405
    }
406

    
407
}