Statistics
| Revision:

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

History | View | Annotate | Download (4.73 KB)

1 2820 jorpiell
/* 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 2047 luisw
package es.gva.cit.catalogClient;
42 2150 jorpiell
43 2724 jorpiell
import es.gva.cit.catalogClient.drivers.CSWCatalogServiceDriver;
44
import es.gva.cit.catalogClient.drivers.ICatalogServiceDriver;
45
import es.gva.cit.catalogClient.drivers.SRWCatalogServiceDriver;
46
import es.gva.cit.catalogClient.drivers.Z3950CatalogServiceDriver;
47
48 2047 luisw
import java.io.IOException;
49 2724 jorpiell
50 2047 luisw
import java.net.MalformedURLException;
51
import java.net.Socket;
52
import java.net.URL;
53
import java.net.UnknownHostException;
54 2820 jorpiell
/**
55 2852 jorpiell
 * This class represents a catalogClient. It must be created to
56
 * use the catalog service
57
 *
58 2820 jorpiell
 * @author Jorge Piera Llodra (piera_jor@gva.es)
59
 */
60 2724 jorpiell
public class CatalogClient {
61
    private URL url = null;
62
    private String protocol = null;
63
    private ICatalogServiceDriver lnkICatalogServerDriver;
64 2047 luisw
65 2724 jorpiell
    public CatalogClient(String url, String protocol, String database) {
66
        setProtocol(protocol);
67 2047 luisw
68 2724 jorpiell
        //This is necessary. Otherwise it will throw the exception
69
        if (!(url.regionMatches(0, "http://", 0, 7))) {
70
            url = "http://" + url;
71
        }
72
73
        try {
74
            this.setUrl(new URL(url));
75
76
            //if protocol is z39.50 we have to add the database to the URL
77
            if (!(protocol.equals(new String("Z3950")))) {
78
                database = getUrl().getPath();
79
            }
80
81
            //if protocol is http, we have to set the port manually
82
            if (getUrl().getPort() == -1) {
83
                setUrl(new URL("http", getUrl().getHost(), 80, database));
84
            } else {
85
                setUrl(new URL("http", getUrl().getHost(), getUrl().getPort(),
86
                        database));
87
            }
88
        } catch (MalformedURLException e) {
89
            // TODO Auto-generated catch block
90
            System.out.println("La URL no es correcta");
91
        }
92
93
        //Casting to the respective driver
94
        if (protocol.equals(new String("SRW"))) {
95
            lnkICatalogServerDriver = new SRWCatalogServiceDriver();
96
        }
97
98
        if (protocol.equals(new String("CSW"))) {
99
            lnkICatalogServerDriver = new CSWCatalogServiceDriver();
100
        }
101
102
        if (protocol.equals(new String("Z3950"))) {
103
            lnkICatalogServerDriver = new Z3950CatalogServiceDriver();
104
        }
105 2047 luisw
    }
106
107
    /**
108 2724 jorpiell
     * @return Returns the lnkICatalogServerDriver.
109 2047 luisw
     */
110 2724 jorpiell
    public ICatalogServiceDriver getLnkICatalogServerDriver() {
111
        return lnkICatalogServerDriver;
112
    }
113 2047 luisw
114 2724 jorpiell
    /**
115
     * @param lnkICatalogServerDriver The lnkICatalogServerDriver to set.
116
     */
117
    public void setLnkICatalogServerDriver(
118
        ICatalogServiceDriver lnkICatalogServerDriver) {
119
        this.lnkICatalogServerDriver = lnkICatalogServerDriver;
120
    }
121 2145 jorpiell
122 2724 jorpiell
    public String getProtocol() {
123
        return protocol;
124
    }
125
126 2145 jorpiell
    /**
127 2724 jorpiell
     * @param protocol The protocol to set.
128
     */
129
    public void setProtocol(String protocol) {
130
        this.protocol = protocol;
131
    }
132
133
    /**
134
     * @return Returns the url.
135
     */
136
    public URL getUrl() {
137
        return url;
138
    }
139
140
    /**
141
     * @param url The url to set.
142
     */
143
    public void setUrl(URL url) {
144
        this.url = url;
145
    }
146
147
    /**
148 2852 jorpiell
     *  It tries if the server is ready
149 2724 jorpiell
     * @return boolean
150 2852 jorpiell
     *   true --> server is ready
151 2145 jorpiell
     *   false --> server is not ready
152 2724 jorpiell
     */
153
    public boolean serverReady() {
154
        try {
155
            Socket socket = new Socket(this.getUrl().getHost(),
156
                    this.getUrl().getPort());
157
        } catch (UnknownHostException e) {
158
            // TODO Auto-generated catch block
159
            return false;
160
        } catch (IOException e) {
161
            // TODO Auto-generated catch block
162
            return false;
163
        }
164 2145 jorpiell
165 2724 jorpiell
        return true;
166
    }
167 2047 luisw
}