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 3566 jorpiell
2 3593 jorpiell
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
3 2820 jorpiell
*
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 3593 jorpiell
*   Av. Blasco Ib??ez, 50
25 2820 jorpiell
*   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 2047 luisw
package es.gva.cit.catalogClient;
43 3613 jorpiell
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 4392 jorpiell
import com.iver.utiles.swing.jcomboServer.ServerData;
48 3613 jorpiell
import es.gva.cit.catalogClient.z3950.drivers.Z3950CatalogServiceDriver;
49 3566 jorpiell
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 2150 jorpiell
56 2820 jorpiell
/**
57 2852 jorpiell
 * This class represents a catalogClient. It must be created to
58
 * use the catalog service
59
 *
60 3566 jorpiell
 *
61 2820 jorpiell
 * @author Jorge Piera Llodra (piera_jor@gva.es)
62
 */
63 2724 jorpiell
public class CatalogClient {
64 3566 jorpiell
65
/**
66
 *
67
 *
68
 */
69 2724 jorpiell
    private URL url = null;
70 3566 jorpiell
71
/**
72
 *
73
 *
74
 */
75 3225 jorpiell
    private String sURL = null;
76 3566 jorpiell
77
/**
78
 *
79
 *
80
 */
81 2724 jorpiell
    private String protocol = null;
82 3566 jorpiell
/**
83
 *
84
 *
85
 */
86 2724 jorpiell
    private ICatalogServiceDriver lnkICatalogServerDriver;
87 3566 jorpiell
88
/**
89
 *
90
 *
91
 */
92 3510 jorpiell
    private String serverStatus = null;
93 2047 luisw
94 3566 jorpiell
/**
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 2724 jorpiell
        setProtocol(protocol);
105 3225 jorpiell
        setSURL(url);
106 2724 jorpiell
        //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 3486 jorpiell
            if (!(protocol.equals(new String(ServerData.SERVER_SUBTYPE_CATALOG_Z3950)))) {
114 2724 jorpiell
                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 3486 jorpiell
        if (protocol.equals(new String(ServerData.SERVER_SUBTYPE_CATALOG_SRW))) {
129 2724 jorpiell
            lnkICatalogServerDriver = new SRWCatalogServiceDriver();
130
        }
131 3486 jorpiell
        if (protocol.equals(new String(ServerData.SERVER_SUBTYPE_CATALOG_CSW))) {
132 2724 jorpiell
            lnkICatalogServerDriver = new CSWCatalogServiceDriver();
133
        }
134 3486 jorpiell
        if (protocol.equals(new String(ServerData.SERVER_SUBTYPE_CATALOG_Z3950))) {
135 2724 jorpiell
            lnkICatalogServerDriver = new Z3950CatalogServiceDriver();
136
        }
137 3566 jorpiell
    }
138
139
/**
140
 *
141
 *
142 3613 jorpiell
 *
143 3566 jorpiell
 * @return
144
 */
145
    public boolean getCapabilities() {
146 3510 jorpiell
        //try to connect
147
        if (!serverReady()) {
148
            serverStatus = ("errorServerNotFound");
149
        } else if (!getLnkICatalogServerDriver().isProtocolSupported(getUrl())) {
150
            serverStatus = "errorNotSupportedProtocol";
151
        } else {
152
            //getCapabilities
153 3566 jorpiell
            Collection nodesCapabilities = getLnkICatalogServerDriver()
154 3510 jorpiell
                                              .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 3566 jorpiell
    }
176 3613 jorpiell
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 3598 jorpiell
188
        return getLnkICatalogServerDriver().getRecords(url,query,firstRecord);
189
190
191 3613 jorpiell
    }
192
193 3566 jorpiell
/**
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 2724 jorpiell
        return lnkICatalogServerDriver;
232 3566 jorpiell
    }
233 2047 luisw
234 3566 jorpiell
/**
235
 *
236
 *
237
 *
238
 * @param lnkICatalogServerDriver The lnkICatalogServerDriver to set.
239
 */
240
    public void setLnkICatalogServerDriver(ICatalogServiceDriver lnkICatalogServerDriver) {
241 2724 jorpiell
        this.lnkICatalogServerDriver = lnkICatalogServerDriver;
242 3566 jorpiell
    }
243 2145 jorpiell
244 3566 jorpiell
/**
245
 *
246
 *
247
 *
248
 * @return Returns the protocol.
249
 */
250
    public String getProtocol() {
251 2724 jorpiell
        return protocol;
252 3566 jorpiell
    }
253 2724 jorpiell
254 3566 jorpiell
/**
255
 *
256
 *
257
 *
258
 * @param protocol The protocol to set.
259
 */
260
    public void setProtocol(String protocol) {
261 2724 jorpiell
        this.protocol = protocol;
262 3566 jorpiell
    }
263 2724 jorpiell
264 3566 jorpiell
/**
265
 *
266
 *
267
 *
268
 * @return Returns the url.
269
 */
270
    public URL getUrl() {
271 2724 jorpiell
        return url;
272 3566 jorpiell
    }
273
274
/**
275
 *
276
 *
277
 *
278
 * @return Returns the sUrl.
279
 */
280
    public String getSUrl() {
281 3225 jorpiell
        return sURL;
282 3566 jorpiell
    }
283 2724 jorpiell
284 3566 jorpiell
/**
285
 *
286
 *
287
 *
288
 * @param url The url to set.
289
 */
290
    public void setUrl(URL url) {
291 2724 jorpiell
        this.url = url;
292 3566 jorpiell
    }
293 2724 jorpiell
294 3566 jorpiell
/**
295
 *
296
 *
297
 *
298
 * @return Returns the sURL.
299
 */
300
    public String getSURL() {
301
        return sURL;
302
    }
303 2145 jorpiell
304 3566 jorpiell
/**
305
 *
306
 *
307
 *
308
 * @param surl The sURL to set.
309
 */
310
    public void setSURL(String surl) {
311 3225 jorpiell
        sURL = surl;
312 3566 jorpiell
    }
313 3510 jorpiell
314 3566 jorpiell
/**
315
 *
316
 *
317
 *
318
 * @return Returns the serverStatus.
319
 */
320
    public String getServerStatus() {
321 3510 jorpiell
        return serverStatus;
322 3566 jorpiell
    }
323
 }