Statistics
| Revision:

root / trunk / applications / appCatalogYNomenclatorClient / src / es / gva / cit / catalogClient / CatalogClient.java @ 3486

History | View | Annotate | Download (5.58 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.srw.drivers.SRWCatalogServiceDriver;
46
import es.gva.cit.catalogClient.utils.comboServer.ServerData;
47
import es.gva.cit.catalogClient.z3950.drivers.Z3950CatalogServiceDriver;
48

    
49
import java.io.IOException;
50

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

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

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

    
86
        try {
87
            this.setUrl(new URL(url));
88

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

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

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

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

    
115
        if (protocol.equals(new String(ServerData.SERVER_SUBTYPE_CATALOG_Z3950))) {
116
            lnkICatalogServerDriver = new Z3950CatalogServiceDriver();
117
        }
118
    }
119

    
120
    /**
121
     * @return Returns the lnkICatalogServerDriver.
122
     */
123
    public ICatalogServiceDriver getLnkICatalogServerDriver() {
124
        return lnkICatalogServerDriver;
125
    }
126

    
127
    /**
128
     * @param lnkICatalogServerDriver The lnkICatalogServerDriver to set.
129
     */
130
    public void setLnkICatalogServerDriver(
131
        ICatalogServiceDriver lnkICatalogServerDriver) {
132
        this.lnkICatalogServerDriver = lnkICatalogServerDriver;
133
    }
134

    
135
    /**
136
     * @return Returns the protocol.
137
     */
138
    public String getProtocol() {
139
        return protocol;
140
    }
141
    
142

    
143

    
144
    /**
145
     * @param protocol The protocol to set.
146
     */
147
    public void setProtocol(String protocol) {
148
        this.protocol = protocol;
149
    }
150

    
151
    /**
152
     * @return Returns the url.
153
     */
154
    public URL getUrl() {
155
        return url;
156
    }
157
    
158
    /**
159
     * @return Returns the sUrl.
160
     */
161
    public String getSUrl(){
162
        return sURL;
163
    }
164

    
165
    /**
166
     * @param url The url to set.
167
     */
168
    public void setUrl(URL url) {
169
        this.url = url;
170
    }
171

    
172
    /**
173
     *  It tries if the server is ready
174
     * @return boolean
175
     *   true --> server is ready
176
     *   false --> server is not ready
177
     */
178
    public boolean serverReady() {
179
        try {
180
            Socket socket = new Socket(this.getUrl().getHost(),
181
                    this.getUrl().getPort());
182
        } catch (UnknownHostException e) {
183
            // TODO Auto-generated catch block
184
            return false;
185
        } catch (IOException e) {
186
            // TODO Auto-generated catch block
187
            return false;
188
        }
189

    
190
        return true;
191
    }
192
    /**
193
     * @return Returns the sURL.
194
     */
195
    public String getSURL() {
196
        return sURL;
197
    }
198
    /**
199
     * @param surl The sURL to set.
200
     */
201
    public void setSURL(String surl) {
202
        sURL = surl;
203
    }
204
}