Statistics
| Revision:

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

History | View | Annotate | Download (7.85 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.srw.drivers.SRWCatalogServiceDriver;
47
import com.iver.utiles.swing.jcomboServer.ServerData;
48
import es.gva.cit.catalogClient.z3950.drivers.Z3950CatalogServiceDriver;
49
import java.io.IOException;
50
import java.net.MalformedURLException;
51
import java.net.Socket;
52
import java.net.URL;
53
import java.net.UnknownHostException;
54
import java.util.Collection;
55

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

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

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

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

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

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

    
139
/**
140
 * 
141
 * 
142
 * 
143
 * @return 
144
 */
145
    public boolean getCapabilities() {        
146
        //try to connect
147
        if (!serverReady()) {
148
            serverStatus = ("errorServerNotFound");
149
        } else if (!getLnkICatalogServerDriver().isProtocolSupported(getUrl())) {
150
            serverStatus = "errorNotSupportedProtocol";
151
        } else {
152
            //getCapabilities
153
            Collection nodesCapabilities = getLnkICatalogServerDriver()
154
                                              .getCapabilities(getUrl());
155
            if (nodesCapabilities == null) {
156
                serverStatus = "errorNotSupportedCapabilities";
157
            } else {
158
                //Configure the client
159
                if (!getLnkICatalogServerDriver().setParameters(nodesCapabilities,getUrl())) {
160
                    if (!(getLnkICatalogServerDriver()
161
                                     .getServerAnswerReady().equals(""))) {
162
                        serverStatus = getLnkICatalogServerDriver().getServerAnswerReady();
163
                    } else {
164
                        serverStatus = "errorNotParsedReply";
165
                    }
166
                } else {
167
                    //Show the answer
168
                    serverStatus = getLnkICatalogServerDriver().getServerAnswerReady();
169
                    return true;
170
                }
171
            }
172
        }
173
    
174
        return false;
175
    } 
176

    
177
/**
178
 * 
179
 * 
180
 * 
181
 * @return Node array with the retrieved records
182
 * @param url Server URL
183
 * @param query It contains the values to do the query (title="XXX",abstract="YYY",...)
184
 * @param firstRecord Number of the first record to retrieve
185
 */
186
    public Collection getRecords(URL url, Query query, int firstRecord) {        
187
    
188
        return getLnkICatalogServerDriver().getRecords(url,query,firstRecord);
189
           
190
            
191
    } 
192

    
193
/**
194
 * This method is used to create a new Query
195
 * 
196
 * 
197
 * @return 
198
 */
199
    public Query createNewQuery() {        
200
        return new Query();   
201
    } 
202

    
203
/**
204
 * It tries if the server is ready
205
 * 
206
 * 
207
 * @return boolean
208
 * true --> server is ready
209
 * false --> server is not ready
210
 */
211
    private boolean serverReady() {        
212
            try {
213
                new Socket(this.getUrl().getHost(),this.getUrl().getPort());
214
            } catch (UnknownHostException e) {
215
                // TODO Auto-generated catch block
216
                return false;
217
            } catch (IOException e) {
218
                // TODO Auto-generated catch block
219
                return false;
220
            }
221
            return true;
222
    } 
223

    
224
/**
225
 * 
226
 * 
227
 * 
228
 * @return Returns the lnkICatalogServerDriver.
229
 */
230
    public ICatalogServiceDriver getLnkICatalogServerDriver() {        
231
        return lnkICatalogServerDriver;
232
    } 
233

    
234
/**
235
 * 
236
 * 
237
 * 
238
 * @param lnkICatalogServerDriver The lnkICatalogServerDriver to set.
239
 */
240
    public void setLnkICatalogServerDriver(ICatalogServiceDriver lnkICatalogServerDriver) {        
241
        this.lnkICatalogServerDriver = lnkICatalogServerDriver;
242
    } 
243

    
244
/**
245
 * 
246
 * 
247
 * 
248
 * @return Returns the protocol.
249
 */
250
    public String getProtocol() {        
251
        return protocol;
252
    } 
253

    
254
/**
255
 * 
256
 * 
257
 * 
258
 * @param protocol The protocol to set.
259
 */
260
    public void setProtocol(String protocol) {        
261
        this.protocol = protocol;
262
    } 
263

    
264
/**
265
 * 
266
 * 
267
 * 
268
 * @return Returns the url.
269
 */
270
    public URL getUrl() {        
271
        return url;
272
    } 
273

    
274
/**
275
 * 
276
 * 
277
 * 
278
 * @return Returns the sUrl.
279
 */
280
    public String getSUrl() {        
281
        return sURL;
282
    } 
283

    
284
/**
285
 * 
286
 * 
287
 * 
288
 * @param url The url to set.
289
 */
290
    public void setUrl(URL url) {        
291
        this.url = url;
292
    } 
293

    
294
/**
295
 * 
296
 * 
297
 * 
298
 * @return Returns the sURL.
299
 */
300
    public String getSURL() {        
301
        return sURL;
302
    } 
303

    
304
/**
305
 * 
306
 * 
307
 * 
308
 * @param surl The sURL to set.
309
 */
310
    public void setSURL(String surl) {        
311
        sURL = surl;
312
    } 
313

    
314
/**
315
 * 
316
 * 
317
 * 
318
 * @return Returns the serverStatus.
319
 */
320
    public String getServerStatus() {        
321
        return serverStatus;
322
    } 
323
 }