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 @ 4181

History | View | Annotate | Download (9.27 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.IOException;
32
import java.io.InputStream;
33
import java.net.MalformedURLException;
34
import java.net.URI;
35
import java.net.URISyntaxException;
36
import java.net.URL;
37
import java.net.URLConnection;
38
import java.util.ArrayList;
39
import java.util.Hashtable;
40
import java.util.List;
41
import java.util.Vector;
42

    
43
import org.gvsig.compat.net.ICancellable;
44
import org.gvsig.fmap.dal.DALLocator;
45
import org.gvsig.fmap.dal.DataManager;
46
import org.gvsig.fmap.dal.DataServerExplorerParameters;
47
import org.gvsig.fmap.dal.DataStoreParameters;
48
import org.gvsig.fmap.dal.NewDataStoreParameters;
49
import org.gvsig.fmap.dal.coverage.exception.ConnectException;
50
import org.gvsig.fmap.dal.coverage.exception.RemoteServiceException;
51
import org.gvsig.fmap.dal.coverage.store.RasterDataServerExplorer;
52
import org.gvsig.fmap.dal.exception.DataException;
53
import org.gvsig.fmap.dal.exception.InitializeException;
54
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
55
import org.gvsig.fmap.dal.spi.DataServerExplorerProvider;
56
import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices;
57
import org.gvsig.i18n.Messages;
58

    
59
import org.slf4j.Logger;
60
import org.slf4j.LoggerFactory;
61

    
62
/**
63
 * Explorer for a WMS server
64
 * @author Nacho Brodin (nachobrodin@gmail.com)
65
 */
66
public class WMSServerExplorer implements RasterDataServerExplorer, DataServerExplorerProvider {
67
        public static final String          NAME                     = WMSProvider.NAME;
68
        private WMSConnector                connector                = null;
69
        private WMSServerExplorerParameters parameters               = null;
70
        private Logger                      log                      = LoggerFactory.getLogger(WMSServerExplorer.class);
71

    
72
        public WMSServerExplorer(
73
                        WMSServerExplorerParameters parameters,
74
                        DataServerExplorerProviderServices services)
75
                        throws InitializeException {
76
                this.parameters = parameters;
77
        }
78

    
79
        /**
80
         * Gets the provider's name
81
         * @return
82
         */
83
        public String getDataStoreProviderName() {
84
                return WMSProvider.NAME;
85
        }
86

    
87
        public String getDescription() {
88
                return WMSProvider.DESCRIPTION;
89
        }
90

    
91
        public boolean add(String provider, NewDataStoreParameters parameters,
92
                        boolean overwrite) throws DataException {
93
                return false;
94
        }
95

    
96
        public boolean canAdd() {
97
                return false;
98
        }
99

    
100
        public boolean canAdd(String storeName) throws DataException {
101
                return false;
102
        }
103

    
104
        public NewDataStoreParameters getAddParameters(String storeName)
105
                        throws DataException {
106
                return null;
107
        }
108

    
109
        public List<?> getDataStoreProviderNames() {
110
                return null;
111
        }
112

    
113
        public DataServerExplorerParameters getParameters() {
114
                return parameters;
115
        }
116

    
117
        public List<?> list() throws DataException {
118
                return null;
119
        }
120

    
121
        public List<?> list(int mode) throws DataException {
122
                return null;
123
        }
124

    
125
        public void remove(DataStoreParameters parameters) throws DataException {
126

    
127
        }
128

    
129
        public void dispose() {
130

    
131
        }
132

    
133
        public String getProviderName() {
134
                return null;
135
        }
136

    
137
        //**********************************************
138
        //Connector
139
        //**********************************************
140

    
141
        public DataStoreParameters getStoredParameters() {
142
                DataManager manager = DALLocator.getDataManager();
143
                WMSDataParameters params = null;
144
                try {
145
                        params = (WMSDataParameters) manager.createStoreParameters(this.getDataStoreProviderName());
146
                        params.setURI(new URI(parameters.getHost()));
147

    
148
                        /*if(WMSProvider.TILED) {
149
                                TileDataParameters tileParams = (TileDataParameters) manager.createStoreParameters("Tile Store");
150
                                tileParams.setDataParameters(params);
151
                                return tileParams;
152
                        } */
153
                } catch (InitializeException e) {
154
                        log.debug("Error initializating parameters", e);
155
                } catch (ProviderNotRegisteredException e) {
156
                        log.debug("Error getting parameters", e);
157
                } catch (URISyntaxException e) {
158
            log.debug("Can't create URI from "+parameters.getHost(), e);
159
        }
160

    
161
                return params;
162
        }
163

    
164
        public void connect(ICancellable cancellable) throws ConnectException {
165
                connect(cancellable, false);
166
        }
167

    
168
        /**
169
         * Connects to the server and throws a getCapabilities. This loads
170
         * the basic information to make requests.
171
         * @throws RemoteServiceException
172
         */
173
        public void connect(ICancellable cancellable, boolean updateCache) throws ConnectException {
174
                URL url = null;
175

    
176
                try {
177
                        url = new URL(parameters.getHost());
178
                } catch (Exception e) {
179
                        throw new ConnectException(Messages.getText("malformed_url"), e);
180
                }
181
        try {
182
                connector = WMSProvider.getConnectorFromURL(url, updateCache);
183
                if (!connector.connect(this.getParameters(), updateCache, cancellable))
184
                        throw new ConnectException(Messages.getText("error_connecting"));
185
        } catch (IOException e) {
186
                        throw new ConnectException(Messages.getText("error_connecting"), e);
187
                }
188

    
189
        }
190

    
191
        /**
192
         * Checks if the network and host are reachable
193
         * @param timeout for the host
194
         * @return true if both are reachable and false if they are not
195
         */
196
        public boolean isHostReachable(int timeout) {
197
                URL url = null;
198
                try {
199
                        url = new URL(parameters.getHost());
200
                        URLConnection con = url.openConnection();
201
                        if(con == null)
202
                                return false;
203
                        con.connect();
204
                        InputStream stream = con.getInputStream();
205
                        if(stream == null)
206
                                return false;
207
                } catch (MalformedURLException e) {
208
                        return false;
209
                } catch (IOException e) {
210
                        return false;
211
                }
212

    
213
                return true;
214
        }
215

    
216
        /**
217
         * Checks if the network and host are reachable
218
         * @return true if both are reachable and false if they are not
219
         */
220
        public boolean isHostReachable() {
221
                int timeout = 10000;
222
                return isHostReachable(timeout);
223
        }
224

    
225
        /**
226
         * Returns true if this provider is connected to the server
227
         * @return
228
         */
229
        public boolean isConnected() {
230
                if(connector != null)
231
                        return true;
232
                return false;
233
        }
234

    
235
        /**
236
         * Gets the description of this service
237
         * @return
238
         */
239
        public String getAbstract() {
240
                if(connector != null)
241
                        return connector.getAbstract();
242
                return null;
243
        }
244

    
245
        /**
246
         * Gets the list of raster formats supported by the server
247
         * @return
248
         */
249
        @SuppressWarnings("unchecked")
250
        public String[] getFormats() {
251
                if(connector != null) {
252
                        Vector f = connector.getFormats();
253
                        ArrayList formatos = new ArrayList();
254
                        for (int i = 0; i < f.size(); i++) {
255
                                formatos.add(f.elementAt(i));
256
                        }
257
                        return (String[]) formatos.toArray(new String[0]);
258
                }
259
                return null;
260
        }
261

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

    
279
        /**
280
         * Gets a tree of nodes which represents the server information
281
         * @return
282
         */
283
        public WMSLayerNode getLayerTree() {
284
                if(connector != null) {
285
                        return connector.getLayersTree();
286
                }
287
                return null;
288
        }
289

    
290
        /**
291
         * Gets the server title
292
         * @return
293
         */
294
        public String getServerType() {
295
                if (getVersion() == null)
296
                        return "WMS";
297
        return "WMS "+ getVersion();
298
        }
299

    
300
        /**
301
         * Gets the online resources
302
         * @return
303
         */
304
        public Hashtable getOnlineResources() {
305
                if(connector != null) {
306
                        return connector.getOnlineResources();
307
                }
308
                return null;
309
        }
310

    
311
        /**
312
         * Gets the protocol supported by the server
313
         * @return
314
         */
315
        public String getVersion() {
316
                if(connector != null) {
317
                        return (connector.getVersion() == null) ? "" : connector.getVersion();
318
                }
319
                return null;
320
        }
321

    
322
    public Rectangle2D getLayersExtent(String[] layerName, String srs) {
323
            return connector.getLayersExtent(layerName, srs);
324
    }
325

    
326
    /**
327
         * Gets a layer using its name
328
         * @param layerName
329
         * @return
330
         */
331
    public WMSLayerNode getLayer(String layerName) {
332
            return connector.getLayer(layerName);
333
    }
334

    
335
    /**
336
         * Gets the host URI
337
         * @return
338
         */
339
        public String getHost() {
340
                return parameters.getHost();
341
        }
342

    
343
        public Rectangle2D getBoundingBox(String[] layerNames, String srs) {
344
                return connector.getLayersExtent(layerNames, srs);
345
    }
346

    
347
        public boolean isQueryable() {
348
            return connector.isQueryable();
349
    }
350

    
351
        /**
352
         * Gets the title
353
         * @return
354
         */
355
        public String getTitle() {
356
                return null;
357
        }
358

    
359
    /**
360
     * @return The title of the service offered by the WMS server.
361
     */
362
    public String getServiceTitle() {
363
                return connector.getServiceTitle();
364
    }
365

    
366
        public DataServerExplorerProviderServices getServerExplorerProviderServices() {
367
                return null;
368
        }
369
}