Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_914 / applications / appCatalogYNomenclatorClient / src / es / gva / cit / catalogClient / CatalogClient.java @ 11873

History | View | Annotate | Download (9.36 KB)

1

    
2
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
3
*
4
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
*
20
* For more information, contact:
21
*
22
*  Generalitat Valenciana
23
*   Conselleria d'Infraestructures i Transport
24
*   Av. Blasco Ib??ez, 50
25
*   46010 VALENCIA
26
*   SPAIN
27
*
28
*      +34 963862235
29
*   gvsig@gva.es
30
*      www.gvsig.gva.es
31
*
32
*    or
33
*
34
*   IVER T.I. S.A
35
*   Salamanca 50
36
*   46005 Valencia
37
*   Spain
38
*
39
*   +34 963163400
40
*   dac@iver.es
41
*/
42
package es.gva.cit.catalogClient;
43
import es.gva.cit.catalogClient.csw.drivers.CSWCatalogServiceDriver;
44
import es.gva.cit.catalogClient.drivers.ICatalogServiceDriver;
45
import es.gva.cit.catalogClient.querys.Query;
46
import es.gva.cit.catalogClient.schemas.Schemas;
47
import es.gva.cit.catalogClient.srw.drivers.SRWCatalogServiceDriver;
48
import com.iver.utiles.swing.jcomboServer.ServerData;
49
import es.gva.cit.catalogClient.z3950.drivers.Z3950CatalogServiceDriver;
50
import java.io.IOException;
51
import java.net.MalformedURLException;
52
import java.net.Socket;
53
import java.net.URL;
54
import java.net.UnknownHostException;
55
import java.util.Collection;
56

    
57
/**
58
 * This class represents a catalogClient. It must be created to
59
 * use the catalog service
60
 * 
61
 * 
62
 * @author Jorge Piera Llodra (piera_jor@gva.es)
63
 */
64
public class CatalogClient {
65

    
66
/**
67
 * 
68
 * 
69
 */
70
    private URL url = null;
71

    
72
/**
73
 * 
74
 * 
75
 */
76
    private String sURL = null;
77

    
78
/**
79
 * 
80
 * 
81
 */
82
    private String protocol = null;
83
/**
84
 * 
85
 * 
86
 */
87
    private ICatalogServiceDriver lnkICatalogServerDriver;
88

    
89
/**
90
 * 
91
 * 
92
 */
93
    private String serverStatus = null;
94

    
95
/**
96
 * Constructor
97
 * 
98
 * 
99
 * @param url Server url
100
 * @param protocol Server protocol
101
 * @param database Database name. This param only is used when the protocol
102
 * is z39.50
103
 */
104
    public  CatalogClient(String url, String protocol, String database) {        
105
        setProtocol(protocol);
106
        setSURL(url);
107
        //This is necessary. Otherwise it will throw the exception
108
        if (!(url.regionMatches(0, "http://", 0, 7))) {
109
            url = "http://" + url;
110
        }
111
        try {
112
            this.setUrl(new URL(url));
113
            //if protocol is z39.50 we have to add the database to the URL
114
            if (!(protocol.equals(new String(ServerData.SERVER_SUBTYPE_CATALOG_Z3950)))) {
115
                database = getUrl().getPath();
116
            }
117
            //if protocol is http, we have to set the port manually
118
            if (getUrl().getPort() == -1) {
119
                setUrl(new URL("http", getUrl().getHost(), 80, database));
120
            } else {
121
                setUrl(new URL("http", getUrl().getHost(), getUrl().getPort(),
122
                        database));
123
            }
124
        } catch (MalformedURLException e) {
125
            // TODO Auto-generated catch block
126
            System.out.println("La URL no es correcta");
127
        }
128
        //Casting to the respective driver
129
        if (protocol.equals(new String(ServerData.SERVER_SUBTYPE_CATALOG_SRW))) {
130
            lnkICatalogServerDriver = new SRWCatalogServiceDriver();
131
        }
132
        if (protocol.equals(new String(ServerData.SERVER_SUBTYPE_CATALOG_CSW))) {
133
            lnkICatalogServerDriver = new CSWCatalogServiceDriver();
134
        }
135
        if (protocol.equals(new String(ServerData.SERVER_SUBTYPE_CATALOG_Z3950))) {
136
            lnkICatalogServerDriver = new Z3950CatalogServiceDriver();
137
        }
138
    } 
139

    
140
/**
141
 * 
142
 * 
143
 * 
144
 * @return 
145
 */
146
    public String getCapabilities() {        
147
        //try to connect
148
        if (!serverReady()) {
149
            serverStatus = ("errorServerNotFound");
150
        } else if (!getLnkICatalogServerDriver().isProtocolSupported(getUrl())) {
151
            serverStatus = "errorNotSupportedProtocol";
152
        } else {
153
            //getCapabilities
154
            Collection nodesCapabilities = getLnkICatalogServerDriver()
155
                                              .getCapabilities(getUrl());
156
            if (nodesCapabilities == null) {
157
                serverStatus = "errorNotSupportedCapabilities";
158
            } else {
159
                //Configure the client
160
                if (!getLnkICatalogServerDriver().setParameters(nodesCapabilities,getUrl())) {
161
                    if (!(getLnkICatalogServerDriver()
162
                                     .getServerAnswerReady().equals(""))) {
163
                        serverStatus = getLnkICatalogServerDriver().getServerAnswerReady();
164
                    } else {
165
                        serverStatus = "errorNotParsedReply";
166
                    }
167
                } else {
168
                    //Show the answer
169
                    serverStatus = getLnkICatalogServerDriver().getServerAnswerReady();
170
                    return "2.0.0";
171
                }
172
            }
173
            if (getLnkICatalogServerDriver() instanceof CSWCatalogServiceDriver){
174
                    ((CSWCatalogServiceDriver)getLnkICatalogServerDriver()).setVersion("2.0.1");
175
                    ((CSWCatalogServiceDriver)getLnkICatalogServerDriver()).setService("http://www.opengis.net/cat/csw");   
176
                    ((CSWCatalogServiceDriver)getLnkICatalogServerDriver()).setServerProfile(Schemas.EBRIM);        
177
                       nodesCapabilities = getLnkICatalogServerDriver().getCapabilities(getUrl());
178
                    if (nodesCapabilities == null) {
179
                            serverStatus = "errorNotSupportedCapabilities";
180
                    } else {
181
                            //Configure the client
182
                            if (!getLnkICatalogServerDriver().setParameters(nodesCapabilities,getUrl())) {
183
                                    if (!(getLnkICatalogServerDriver()
184
                                                    .getServerAnswerReady().equals(""))) {
185
                                            serverStatus = getLnkICatalogServerDriver().getServerAnswerReady();
186
                                    } else {
187
                                            serverStatus = "errorNotParsedReply";
188
                                    }
189
                            } else {
190
                                    //Show the answer
191
                                    serverStatus = getLnkICatalogServerDriver().getServerAnswerReady();
192
                                    return "2.0.1";
193
                            }
194
                    }
195
            }
196
        }
197
    
198
        return null;
199
    } 
200

    
201
/**
202
 * 
203
 * 
204
 * 
205
 * @return Node array with the retrieved records
206
 * @param url Server URL
207
 * @param query It contains the values to do the query (title="XXX",abstract="YYY",...)
208
 * @param firstRecord Number of the first record to retrieve
209
 */
210
    public Collection getRecords(URL url, Query query, int firstRecord) {        
211
    
212
        return getLnkICatalogServerDriver().getRecords(url,query,firstRecord);
213
           
214
            
215
    } 
216

    
217
/**
218
 * This method is used to create a new Query
219
 * 
220
 * 
221
 * @return 
222
 */
223
    public Query createNewQuery() {        
224
        return new Query();   
225
    } 
226

    
227
/**
228
 * It tries if the server is ready
229
 * 
230
 * 
231
 * @return boolean
232
 * true --> server is ready
233
 * false --> server is not ready
234
 */
235
    private boolean serverReady() {        
236
            try {
237
                new Socket(this.getUrl().getHost(),this.getUrl().getPort());
238
            } catch (UnknownHostException e) {
239
                // TODO Auto-generated catch block
240
                return false;
241
            } catch (IOException e) {
242
                // TODO Auto-generated catch block
243
                return false;
244
            }
245
            return true;
246
    } 
247

    
248
/**
249
 * 
250
 * 
251
 * 
252
 * @return Returns the lnkICatalogServerDriver.
253
 */
254
    public ICatalogServiceDriver getLnkICatalogServerDriver() {        
255
        return lnkICatalogServerDriver;
256
    } 
257

    
258
/**
259
 * 
260
 * 
261
 * 
262
 * @param lnkICatalogServerDriver The lnkICatalogServerDriver to set.
263
 */
264
    public void setLnkICatalogServerDriver(ICatalogServiceDriver lnkICatalogServerDriver) {        
265
        this.lnkICatalogServerDriver = lnkICatalogServerDriver;
266
    } 
267

    
268
/**
269
 * 
270
 * 
271
 * 
272
 * @return Returns the protocol.
273
 */
274
    public String getProtocol() {        
275
        return protocol;
276
    } 
277

    
278
/**
279
 * 
280
 * 
281
 * 
282
 * @param protocol The protocol to set.
283
 */
284
    public void setProtocol(String protocol) {        
285
        this.protocol = protocol;
286
    } 
287

    
288
/**
289
 * 
290
 * 
291
 * 
292
 * @return Returns the url.
293
 */
294
    public URL getUrl() {        
295
        return url;
296
    } 
297

    
298
/**
299
 * 
300
 * 
301
 * 
302
 * @return Returns the sUrl.
303
 */
304
    public String getSUrl() {        
305
        return sURL;
306
    } 
307

    
308
/**
309
 * 
310
 * 
311
 * 
312
 * @param url The url to set.
313
 */
314
    public void setUrl(URL url) {        
315
        this.url = url;
316
    } 
317

    
318
/**
319
 * 
320
 * 
321
 * 
322
 * @return Returns the sURL.
323
 */
324
    public String getSURL() {        
325
        return sURL;
326
    } 
327

    
328
/**
329
 * 
330
 * 
331
 * 
332
 * @param surl The sURL to set.
333
 */
334
    public void setSURL(String surl) {        
335
        sURL = surl;
336
    } 
337

    
338
/**
339
 * 
340
 * 
341
 * 
342
 * @return Returns the serverStatus.
343
 */
344
    public String getServerStatus() {        
345
        return serverStatus;
346
    } 
347
    
348
    /**
349
     * Return true if the services search is supported
350
     * @return
351
     */
352
    public boolean isServicesSeachSupported(){
353
            if (url.getHost().equals("laits.gmu.edu")){ 
354
                    return true;
355
            }
356
            return false;
357
    }
358
 }