Statistics
| Revision:

root / trunk / applications / appCatalogAndGazetteerClient / src / es / gva / cit / catalog / DiscoveryServiceClient.java @ 30182

History | View | Annotate | Download (6.59 KB)

1
package es.gva.cit.catalog;
2

    
3
import java.io.IOException;
4
import java.net.Authenticator;
5
import java.net.PasswordAuthentication;
6
import java.net.Socket;
7
import java.net.URI;
8
import java.net.URISyntaxException;
9
import java.net.UnknownHostException;
10
import java.util.Properties;
11

    
12
import org.apache.commons.httpclient.HttpConnection;
13

    
14
import sun.net.www.http.HttpClient;
15

    
16

    
17
import com.iver.utiles.swing.jcomboServer.ServerData;
18

    
19
import es.gva.cit.catalog.drivers.DiscoveryServiceCapabilities;
20
import es.gva.cit.catalog.drivers.IDiscoveryServiceDriver;
21
import es.gva.cit.catalog.exceptions.NotSupportedProtocolException;
22
import es.gva.cit.catalog.exceptions.NotSupportedVersionException;
23
import es.gva.cit.catalog.exceptions.ServerIsNotReadyException;
24
import es.gva.cit.catalog.querys.DiscoveryServiceQuery;
25
import es.gva.cit.catalog.ui.search.SearchAditionalPropertiesPanel;
26
import es.gva.cit.catalog.utils.URIUtils;
27

    
28
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
29
 *
30
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
31
 *
32
 * This program is free software; you can redistribute it and/or
33
 * modify it under the terms of the GNU General Public License
34
 * as published by the Free Software Foundation; either version 2
35
 * of the License, or (at your option) any later version.
36
 *
37
 * This program is distributed in the hope that it will be useful,
38
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
40
 * GNU General Public License for more details.
41
 *
42
 * You should have received a copy of the GNU General Public License
43
 * along with this program; if not, write to the Free Software
44
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
45
 *
46
 * For more information, contact:
47
 *
48
 *  Generalitat Valenciana
49
 *   Conselleria d'Infraestructures i Transport
50
 *   Av. Blasco Ib??ez, 50
51
 *   46010 VALENCIA
52
 *   SPAIN
53
 *
54
 *      +34 963862235
55
 *   gvsig@gva.es
56
 *      www.gvsig.gva.es
57
 *
58
 *    or
59
 *
60
 *   IVER T.I. S.A
61
 *   Salamanca 50
62
 *   46005 Valencia
63
 *   Spain
64
 *
65
 *   +34 963163400
66
 *   dac@iver.es
67
 */
68
/* CVS MESSAGES:
69
 *
70
 * $Id$
71
 * $Log$
72
 *
73
 */
74
/**
75
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
76
 */
77
public class DiscoveryServiceClient {
78
        /**
79
         * The server URI
80
         */
81
        private URI uri = null;
82
        /**
83
         * The driver to make the querys
84
         */
85
        private IDiscoveryServiceDriver driver;
86
        /**
87
         * The service capabilities
88
         */
89
        private DiscoveryServiceCapabilities capabilities;
90
        /**
91
         * The server status message
92
         */
93
        private String serverStatus = null;
94

    
95
        public DiscoveryServiceClient(String sUri,IDiscoveryServiceDriver driver) {
96
                setDriver(driver);
97
                if (driver == null){
98
                        serverStatus = "errorServerNotFound";
99
                }else{
100
                        try {
101
                                this.uri = URIUtils.createUri(sUri,
102
                                                driver.getDefaultSchema(),
103
                                                driver.getDefaultPort());
104
                        } catch (URISyntaxException e) {
105
                                serverStatus = "errorServerNotFound";
106
                        }
107
                }
108
        }
109

    
110
        /**
111
         * It make a getCapabilities operation
112
         * @return the service version
113
         * @throws ServerIsNotReadyException 
114
         */
115
        public DiscoveryServiceCapabilities getCapabilities() throws ServerIsNotReadyException {        
116
                if (serverIsReady()){
117
                        try {
118
                                if (getDriver().isProtocolSupported(getUri())) {
119
                                        capabilities = getDriver().getCapabilities(getUri());
120
                                        return capabilities;
121
                                }
122
                        } catch (NotSupportedProtocolException e) {
123
                                capabilities = new DiscoveryServiceCapabilities();
124
                                capabilities.setAvailable(false);
125
                                capabilities.setServerMessage("notSupportedProtocol");
126
                        } catch (NotSupportedVersionException e) {
127
                                capabilities = new DiscoveryServiceCapabilities();
128
                                capabilities.setAvailable(false);
129
                                capabilities.setServerMessage("notSupportedVersion");
130
                        } 
131
                }
132
                return capabilities;    
133
        } 
134

    
135
        /**
136
         * It tries if the server is ready 
137
         * @return boolean
138
         * true --> server is ready
139
         * false --> server is not ready
140
         */
141
        public boolean serverIsReady() throws ServerIsNotReadyException {        
142
                Properties systemSettings = System.getProperties();
143

    
144

    
145
                Object isProxyEnabled = systemSettings.get("http.proxySet"); 
146
                if ((isProxyEnabled == null) || (isProxyEnabled.equals("false"))){
147
                        Socket sock;
148
                        try{                                
149
                                sock = new Socket(getUri().getHost(),
150
                                                getUri().getPort());
151
                        } catch (UnknownHostException e) {
152
                                throw new ServerIsNotReadyException(e);
153
                        } catch (IOException e) {
154
                                throw new ServerIsNotReadyException(e);
155
                        }
156
                        return (sock != null);
157
                }else{
158
                        Object host = systemSettings.get("http.proxyHost"); 
159
                        Object port = systemSettings.get("http.proxyPort");
160
                        Object user = systemSettings.get("http.proxyUserName");
161
                        Object password = systemSettings.get("http.proxyPassword");
162
                        if ((host != null) && (port != null)){
163
                                int iPort = 80;
164
                                try{
165
                                        iPort = Integer.parseInt((String)port);
166
                                }catch (Exception e) {
167
                                        //Use 80
168
                                }
169
                                HttpConnection connection = new HttpConnection(getUri().getHost(), 
170
                                                getUri().getPort());
171
                                connection.setProxyHost((String)host);
172
                                connection.setProxyPort(iPort);
173
                                Authenticator.setDefault(new SimpleAuthenticator(
174
                        user,password));
175
                                
176
                                try {
177
                                        connection.open();
178
                                        connection.close();                                                
179
                                } catch (IOException e) {
180
                                        throw new ServerIsNotReadyException(e);                                        
181
                                }
182
                        }                        
183
                }                
184
                return true;
185
        } 
186

    
187
        /**
188
         * @return the server URI
189
         */
190
        public URI getUri() {
191
                return uri;
192
        }
193

    
194
        /**
195
         * @return Return the server URI like a String
196
         */
197
        public String getSUri() {
198
                return uri.toString();
199
        } 
200

    
201
        /**
202
         * @return Returns the driver.
203
         */
204
        protected IDiscoveryServiceDriver getDriver() {        
205
                return driver;
206
        }
207

    
208
        /**
209
         * 
210
         * @param driver the driver to set
211
         */
212
        protected void setDriver(IDiscoveryServiceDriver driver) {
213
                this.driver = driver;
214
        } 
215

    
216
        /**
217
         * @return the server protocol
218
         */
219
        public String getProtocol() {        
220
                return driver.getServiceName();
221
        }
222

    
223
        /**
224
         * Gets the aditional panel
225
         * @return
226
         */
227
        public SearchAditionalPropertiesPanel getAditionalSearchPanel(){
228
                return driver.getAditionalSearchPanel();
229
        }
230

    
231
        /**
232
         * Gets a query
233
         * @return
234
         */
235
        public DiscoveryServiceQuery createQuery(){
236
                return driver.createQuery();
237
        }
238
        
239
        private class SimpleAuthenticator
240
           extends Authenticator
241
        {
242
           private String username,
243
                          password;
244
                             
245
           public SimpleAuthenticator(Object username, Object password)
246
           {
247
                   if (username != null){
248
                           this.username = (String)username;
249
                   }
250
                   if (password != null){
251
                           this.password = (String)password;
252
                   }
253
           }
254
           
255
           protected PasswordAuthentication getPasswordAuthentication()
256
           {
257
              return new PasswordAuthentication(
258
                     username,password.toCharArray());
259
           }
260
        }
261

    
262
}