Statistics
| Revision:

gvsig-raster / org.gvsig.raster.wmts / trunk / org.gvsig.raster.wmts / org.gvsig.raster.wmts.io / src / main / java / org / gvsig / raster / wmts / io / WMTSServerExplorer.java @ 4272

History | View | Annotate | Download (6.48 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.wmts.io;
29

    
30
import java.io.IOException;
31
import java.io.InputStream;
32
import java.net.MalformedURLException;
33
import java.net.URI;
34
import java.net.URISyntaxException;
35
import java.net.URL;
36
import java.net.URLConnection;
37
import java.util.List;
38

    
39
import org.slf4j.Logger;
40
import org.slf4j.LoggerFactory;
41

    
42
import org.gvsig.compat.net.ICancellable;
43
import org.gvsig.fmap.dal.DALLocator;
44
import org.gvsig.fmap.dal.DataManager;
45
import org.gvsig.fmap.dal.DataServerExplorerParameters;
46
import org.gvsig.fmap.dal.DataStoreParameters;
47
import org.gvsig.fmap.dal.NewDataStoreParameters;
48
import org.gvsig.fmap.dal.coverage.exception.ConnectException;
49
import org.gvsig.fmap.dal.coverage.store.RasterDataServerExplorer;
50
import org.gvsig.fmap.dal.coverage.store.parameter.TileDataParameters;
51
import org.gvsig.fmap.dal.exception.DataException;
52
import org.gvsig.fmap.dal.exception.InitializeException;
53
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
54
import org.gvsig.fmap.dal.spi.DataServerExplorerProvider;
55
import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices;
56
import org.gvsig.raster.wmts.ogc.WMTSClient;
57
import org.gvsig.raster.wmts.ogc.WMTSOGCLocator;
58

    
59
/**
60
 * Explorer for a WMTS server
61
 * @author Nacho Brodin (nachobrodin@gmail.com)
62
 */
63
public class WMTSServerExplorer implements RasterDataServerExplorer, DataServerExplorerProvider {
64
        public static final String           NAME                     = WMTSProvider.NAME;
65
        private WMTSClient                   ogcClient                = null;
66
        private WMTSServerExplorerParameters parameters               = null;
67
        private static final Logger         logger                    = LoggerFactory.getLogger(WMTSServerExplorer.class);
68

    
69
        public WMTSServerExplorer(
70
                        WMTSServerExplorerParameters parameters,
71
                        DataServerExplorerProviderServices services)
72
                        throws InitializeException {
73
                this.parameters = parameters;
74
        }
75

    
76
        public String getDataStoreProviderName() {
77
                return WMTSProvider.NAME;
78
        }
79

    
80
        public String getDescription() {
81
                return WMTSProvider.DESCRIPTION;
82
        }
83

    
84
        public boolean add(String provider, NewDataStoreParameters parameters,
85
                        boolean overwrite) throws DataException {
86
                return false;
87
        }
88

    
89
        public boolean canAdd() {
90
                return false;
91
        }
92

    
93
        public boolean canAdd(String storeName) throws DataException {
94
                return false;
95
        }
96

    
97
        public NewDataStoreParameters getAddParameters(String storeName)
98
                        throws DataException {
99
                return null;
100
        }
101

    
102
        public List<?> getDataStoreProviderNames() {
103
                return null;
104
        }
105

    
106
        public DataServerExplorerParameters getParameters() {
107
                return parameters;
108
        }
109

    
110
        public List<?> list() throws DataException {
111
                return null;
112
        }
113

    
114
        public List<?> list(int mode) throws DataException {
115
                return null;
116
        }
117

    
118
        public void remove(DataStoreParameters parameters) throws DataException {
119

    
120
        }
121

    
122
        public void dispose() {
123

    
124
        }
125

    
126
        public String getProviderName() {
127
                return WMTSProvider.NAME;
128
        }
129

    
130
        //**********************************************
131
        //Connector
132
        //**********************************************
133

    
134
        public DataStoreParameters getStoredParameters() {
135
                DataManager manager = DALLocator.getDataManager();
136
                WMTSDataParameters wmtsParams = null;
137
                try {
138
                        wmtsParams = (WMTSDataParameters) manager.createStoreParameters(this.getDataStoreProviderName());
139
                        String host = parameters.getHost();
140
            try {
141
                wmtsParams.setURI(new URI(host));
142
            } catch (URISyntaxException e) {
143
                logger.warn("Can't create URI from "+host, e);
144
            }
145
                        wmtsParams.setOGCClient(getOGCClient());
146

    
147
                        if(WMTSProvider.TILED) {
148
                                TileDataParameters tileParams = (TileDataParameters) manager.createStoreParameters("Tile Store");
149
                                tileParams.setDataParameters(wmtsParams);
150
                                return tileParams;
151
                        }
152
                } catch (InitializeException e) {
153
            logger.warn("Can't get DataStoreParameters from WMTSServerExplorer", e);
154
                } catch (ProviderNotRegisteredException e) {
155
            logger.warn("Can't get DataStoreParameters from WMTSServerExplorer", e);
156
                }
157
                return wmtsParams;
158
        }
159

    
160
        public void connect(ICancellable cancellable) throws ConnectException {
161
                URL url = null;
162
                boolean override = false;
163

    
164
                try {
165
                        url = new URL(parameters.getHost());
166
                } catch (Exception e) {
167
                        throw new ConnectException("Malformed URL",e);
168
                }
169
        try {
170
                ogcClient = WMTSOGCLocator.getManager().createWMTSClient(url.toString());
171
                ogcClient.setForceChangeAxisOrder(parameters.isLongitudeFirst());
172
                if (!ogcClient.connect(override, cancellable))
173
                        throw new ConnectException("Error connecting");
174
        } catch (IOException e) {
175
                        throw new ConnectException(e.getMessage(), e);
176
                }
177
        }
178

    
179
        public boolean isHostReachable(int timeout) {
180
                URL url = null;
181
                URLConnection con = null;
182
                try {
183
                        url = new URL(parameters.getHost());
184
                        con = url.openConnection();
185
                        if(con == null)
186
                                return false;
187
                        con.connect();
188
                } catch (MalformedURLException e) {
189
                        return false;
190
                } catch (IOException e) {
191
                        return false;
192
                }
193

    
194
                InputStream stream = null;
195
                try {
196
                        stream = con.getInputStream();
197
                        if(stream == null)
198
                                return false;
199
                } catch (IOException e) {
200
                }
201

    
202
                return true;
203
        }
204

    
205
        /**
206
         * Checks if the network and host are reachable
207
         * @return true if both are reachable and false if they are not
208
         */
209
        public boolean isHostReachable() {
210
                return isHostReachable(RasterDataServerExplorer.TIME);
211
        }
212

    
213
        public boolean isConnected() {
214
                if(ogcClient != null)
215
                        return true;
216
                return false;
217
        }
218

    
219
        public WMTSClient getOGCClient() {
220
                return ogcClient;
221
        }
222

    
223
        public String getHost() {
224
                return parameters.getHost();
225
        }
226

    
227
        public DataServerExplorerProviderServices getServerExplorerProviderServices() {
228
                return null;
229
        }
230
}