Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appCatalogYNomenclatorClient / src / es / gva / cit / catalogClient / CatalogClient.java @ 3510

History | View | Annotate | Download (7.34 KB)

1
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
2
*
3
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
*
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
9
*
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
* GNU General Public License for more details.
14
*
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
*
19
* For more information, contact:
20
*
21
*  Generalitat Valenciana
22
*   Conselleria d'Infraestructures i Transport
23
*   Av. Blasco Ib��ez, 50
24
*   46010 VALENCIA
25
*   SPAIN
26
*
27
*      +34 963862235
28
*   gvsig@gva.es
29
*      www.gvsig.gva.es
30
*
31
*    or
32
*
33
*   IVER T.I. S.A
34
*   Salamanca 50
35
*   46005 Valencia
36
*   Spain
37
*
38
*   +34 963163400
39
*   dac@iver.es
40
*/
41
package es.gva.cit.catalogClient;
42

    
43
import es.gva.cit.catalogClient.csw.drivers.CSWCatalogServiceDriver;
44
import es.gva.cit.catalogClient.drivers.ICatalogServiceDriver;
45
import es.gva.cit.catalogClient.metadataXML.XMLNode;
46
import es.gva.cit.catalogClient.srw.drivers.SRWCatalogServiceDriver;
47
import es.gva.cit.catalogClient.utils.comboServer.ServerData;
48
import es.gva.cit.catalogClient.z3950.drivers.Z3950CatalogServiceDriver;
49

    
50
import java.io.IOException;
51

    
52
import java.net.MalformedURLException;
53
import java.net.Socket;
54
import java.net.URL;
55
import java.net.UnknownHostException;
56
/**
57
 * This class represents a catalogClient. It must be created to
58
 * use the catalog service
59
 * 
60
 * @author Jorge Piera Llodra (piera_jor@gva.es)
61
 */
62
public class CatalogClient {
63
    private URL url = null;
64
    private String sURL = null;
65
    private String protocol = null;
66
    private ICatalogServiceDriver lnkICatalogServerDriver;
67
    private String serverStatus = null;
68

    
69
    /**
70
     * Constructor
71
     * @param url
72
     * Server url
73
     * @param protocol
74
     * Server protocol
75
     * @param database
76
     * Database name. This param only is used when the protocol
77
     * is z39.50
78
     */
79
    public CatalogClient(String url, String protocol, String database) {
80
        setProtocol(protocol);
81
        setSURL(url);
82

    
83
        //This is necessary. Otherwise it will throw the exception
84
        if (!(url.regionMatches(0, "http://", 0, 7))) {
85
            url = "http://" + url;
86
        }
87

    
88
        try {
89
            this.setUrl(new URL(url));
90

    
91
            //if protocol is z39.50 we have to add the database to the URL
92
            if (!(protocol.equals(new String(ServerData.SERVER_SUBTYPE_CATALOG_Z3950)))) {
93
                database = getUrl().getPath();
94
            }
95

    
96
            //if protocol is http, we have to set the port manually
97
            if (getUrl().getPort() == -1) {
98
                setUrl(new URL("http", getUrl().getHost(), 80, database));
99
            } else {
100
                setUrl(new URL("http", getUrl().getHost(), getUrl().getPort(),
101
                        database));
102
            }
103
        } catch (MalformedURLException e) {
104
            // TODO Auto-generated catch block
105
            System.out.println("La URL no es correcta");
106
        }
107

    
108
        //Casting to the respective driver
109
        if (protocol.equals(new String(ServerData.SERVER_SUBTYPE_CATALOG_SRW))) {
110
            lnkICatalogServerDriver = new SRWCatalogServiceDriver();
111
        }
112

    
113
        if (protocol.equals(new String(ServerData.SERVER_SUBTYPE_CATALOG_CSW))) {
114
            lnkICatalogServerDriver = new CSWCatalogServiceDriver();
115
        }
116

    
117
        if (protocol.equals(new String(ServerData.SERVER_SUBTYPE_CATALOG_Z3950))) {
118
            lnkICatalogServerDriver = new Z3950CatalogServiceDriver();
119
        }
120
    }
121
    
122
    /**
123
     * This function is a link between the user interface and
124
     * the driver. It tryes if the server exists, if the server 
125
     * supports the protocolo and finally it sents a getCapabilities
126
     * request.   
127
     * @return
128
     * True if all is OK.
129
     */
130
    public boolean getCapabilities(){
131
        //try to connect
132
        if (!serverReady()) {
133
            serverStatus = ("errorServerNotFound");
134
        } else if (!getLnkICatalogServerDriver().isProtocolSupported(getUrl())) {
135
            serverStatus = "errorNotSupportedProtocol";
136
        } else {
137
            //getCapabilities
138
            XMLNode[] nodesCapabilities = getLnkICatalogServerDriver()
139
                                              .getCapabilities(getUrl());
140

    
141
            if (nodesCapabilities == null) {
142
                serverStatus = "errorNotSupportedCapabilities";
143
            } else {
144
                //Configure the client
145
                if (!getLnkICatalogServerDriver().setParameters(nodesCapabilities,getUrl())) {
146
                    if (!(getLnkICatalogServerDriver()
147
                                     .getServerAnswerReady().equals(""))) {
148
                        serverStatus = getLnkICatalogServerDriver().getServerAnswerReady();
149
                    } else {
150
                        serverStatus = "errorNotParsedReply";
151
                    }
152
                } else {
153
                    //Show the answer
154
                    serverStatus = getLnkICatalogServerDriver().getServerAnswerReady();
155
                    return true;
156
                }
157
            }
158
        }
159
    
160
        return false;
161
    }
162

    
163
    /**
164
     * @return Returns the lnkICatalogServerDriver.
165
     */
166
    public ICatalogServiceDriver getLnkICatalogServerDriver() {
167
        return lnkICatalogServerDriver;
168
    }
169

    
170
    /**
171
     * @param lnkICatalogServerDriver The lnkICatalogServerDriver to set.
172
     */
173
    public void setLnkICatalogServerDriver(
174
        ICatalogServiceDriver lnkICatalogServerDriver) {
175
        this.lnkICatalogServerDriver = lnkICatalogServerDriver;
176
    }
177

    
178
    /**
179
     * @return Returns the protocol.
180
     */
181
    public String getProtocol() {
182
        return protocol;
183
    }
184
    
185

    
186

    
187
    /**
188
     * @param protocol The protocol to set.
189
     */
190
    public void setProtocol(String protocol) {
191
        this.protocol = protocol;
192
    }
193

    
194
    /**
195
     * @return Returns the url.
196
     */
197
    public URL getUrl() {
198
        return url;
199
    }
200
    
201
    /**
202
     * @return Returns the sUrl.
203
     */
204
    public String getSUrl(){
205
        return sURL;
206
    }
207

    
208
    /**
209
     * @param url The url to set.
210
     */
211
    public void setUrl(URL url) {
212
        this.url = url;
213
    }
214

    
215
    /**
216
     *  It tries if the server is ready
217
     * @return boolean
218
     *   true --> server is ready
219
     *   false --> server is not ready
220
     */
221
    private boolean serverReady() {
222
        try {
223
            new Socket(this.getUrl().getHost(),this.getUrl().getPort());
224
        } catch (UnknownHostException e) {
225
            // TODO Auto-generated catch block
226
            return false;
227
        } catch (IOException e) {
228
            // TODO Auto-generated catch block
229
            return false;
230
        }
231

    
232
        return true;
233
    }
234
    /**
235
     * @return Returns the sURL.
236
     */
237
    public String getSURL() {
238
        return sURL;
239
    }
240
    /**
241
     * @param surl The sURL to set.
242
     */
243
    public void setSURL(String surl) {
244
        sURL = surl;
245
    }
246

    
247
    /**
248
     * @return Returns the serverStatus.
249
     */
250
    public String getServerStatus() {
251
        return serverStatus;
252
    }
253
}