Statistics
| Revision:

gvsig-catalog / org.gvsig.catalog / branches / org.gvsig.catalog-CSW2.0.2 / org.gvsig.catalog / org.gvsig.catalog.lib / src / main / java / org / gvsig / catalog / srw / drivers / SRWCatalogServiceDriver.java @ 55

History | View | Annotate | Download (6.67 KB)

1

    
2
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
3
 *
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
 *   Av. Blasco Ib??ez, 50
25
 *   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
package org.gvsig.catalog.srw.drivers;
43
import java.net.MalformedURLException;
44
import java.net.URI;
45
import java.net.URL;
46
import java.util.Collection;
47
import java.util.StringTokenizer;
48

    
49
import org.gvsig.catalog.drivers.AbstractCatalogServiceDriver;
50
import org.gvsig.catalog.drivers.CatalogCapabilities;
51
import org.gvsig.catalog.drivers.DiscoveryServiceCapabilities;
52
import org.gvsig.catalog.drivers.GetRecordsReply;
53
import org.gvsig.catalog.metadataxml.XMLNode;
54
import org.gvsig.catalog.metadataxml.XMLTree;
55
import org.gvsig.catalog.protocols.HTTPGetProtocol;
56
import org.gvsig.catalog.protocols.SOAPProtocol;
57
import org.gvsig.catalog.querys.CatalogQuery;
58
import org.gvsig.catalog.srw.parsers.SrwCapabilitiesParser;
59
import org.gvsig.utils.swing.jcomboServer.ServerData;
60

    
61
/**
62
 * This class implements the CSW protocol.
63
 * 
64
 * 
65
 * @author Jorge Piera Llodra (piera_jor@gva.es)
66
 * @see http://www.loc.gov/z3950/agency/zing/srw/
67
 */
68
public class SRWCatalogServiceDriver extends AbstractCatalogServiceDriver {
69
        private String version = "1.1";
70
        private String recordXPath;
71
        private String resultSetTTL;
72

    
73
        /*
74
         * (non-Javadoc)
75
         * @see es.gva.cit.catalogClient.drivers.ICatalogServiceDriver#getCapabilities(java.net.URI)
76
         */
77
        public DiscoveryServiceCapabilities getCapabilities(URI uri) {        
78
                URL url = null;
79
                try {
80
                        url = uri.toURL();
81
                } catch (MalformedURLException e) {
82
                        setServerAnswerReady("errorServerNotFound");
83
                        return null;
84
                }        
85
                SRWMessages messages = new SRWMessages(this);
86
                Collection nodes = new HTTPGetProtocol().doQuery(url,
87
                                messages.getHTTPGETCapabilities(true), 0);
88

    
89
                nodes = new SOAPProtocol().doQuery(url, messages.getSOAPCapabilities(), 0);
90
                new SrwCapabilitiesParser(this).parse((XMLNode)nodes.toArray()[0]);
91
                CatalogCapabilities capabilities = new CatalogCapabilities();
92
                capabilities.setVersion(version);
93
                capabilities.setServerMessage(getServerAnswerReady());
94
                return capabilities;
95
        } 
96

    
97
        /*
98
         * (non-Javadoc)
99
         * @see es.gva.cit.catalogClient.drivers.ICatalogServiceDriver#getRecords(java.net.URI, es.gva.cit.catalogClient.querys.Query, int)
100
         */
101
        public GetRecordsReply getRecords(URI uri, CatalogQuery query, int firstRecord) {        
102
                URL url = null;
103
                try {
104
                        url = uri.toURL();
105
                } catch (MalformedURLException e) {
106
                        setServerAnswerReady("errorServerNotFound");
107
                        return null;
108
                }        
109
                setQuery(query);
110
                SRWMessages messages = new SRWMessages(this);
111
                Collection nodes = new java.util.ArrayList();
112

    
113
                //TODO remove this comparation
114
                if (url.getHost().equals("idee.unizar.es")){
115
                        try {
116
                                url = new URL("http://idee.unizar.es/SRW/servlet/search/SRW");
117
                        } catch (MalformedURLException e) {
118
                                // It will never throws
119
                        }
120
                }
121
              
122
                XMLNode root = null;
123
                nodes = new SOAPProtocol().doQuery(url,
124
                                messages.getSOAPRecords(getQuery(), firstRecord), firstRecord);
125
                root = (XMLNode)nodes.toArray()[0];
126
                root = root.getSubNode(0).getSubNode(0);
127

    
128
                String prefix = new StringTokenizer(root.getName(), ":").nextToken();
129
                if (prefix.equals(root.getName())) {
130
                        prefix = "";
131
                } else {
132
                        prefix = prefix + ":";
133
                }        
134
                int numberOfRecords = getNumberOfRecords(root,
135
                                prefix + "numberOfRecords",
136
                                null);
137

    
138
                if (numberOfRecords == -1) {
139
                        return null;
140
                }   
141
                GetRecordsReply recordsReply = new GetRecordsReply(numberOfRecords);
142

    
143
                parseRecords(root,recordsReply,uri,prefix,numberOfRecords,firstRecord);                
144
                return recordsReply;
145
        } 
146

    
147
        /**
148
         * Parser the XML
149
         * @param node
150
         * @param recordsReply
151
         * @param uri
152
         * @param prefix
153
         * @param numberOfRecords
154
         * @param firstRecord
155
         */
156
        private void parseRecords(XMLNode node, GetRecordsReply recordsReply, URI uri, String prefix, int numberOfRecords, int firstRecord) {        
157

    
158
                XMLNode[] auxNodes = XMLTree.searchMultipleNode(node,
159
                                prefix + "records->" + prefix + "record");
160
                for (int i = 1;        (i <= numberOfRecords) && (i <= 10) &&        (i <= (numberOfRecords - firstRecord + 1)); i++){
161
                        recordsReply.addRecord(uri, auxNodes[i - 1]);
162
                }
163

    
164
        } 
165

    
166
        /**
167
         * 
168
         * 
169
         * 
170
         * @return Returns the recordXPath.
171
         */
172
        public String getRecordXPath() {        
173
                return recordXPath;
174
        } 
175

    
176
        /**
177
         * 
178
         * 
179
         * 
180
         * @param recordXPath The recordXPath to set.
181
         */
182
        public void setRecordXPath(String recordXPath) {        
183
                this.recordXPath = recordXPath;
184
        } 
185

    
186
        /**
187
         * 
188
         * 
189
         * 
190
         * @return Returns the resultSetTTL.
191
         */
192
        public String getResultSetTTL() {        
193
                return resultSetTTL;
194
        } 
195

    
196
        /**
197
         * 
198
         * 
199
         * 
200
         * @param resultSetTTL The resultSetTTL to set.
201
         */
202
        public void setResultSetTTL(String resultSetTTL) {        
203
                this.resultSetTTL = resultSetTTL;
204
        } 
205

    
206
        /**
207
         * 
208
         * 
209
         * 
210
         * @return Returns the version.
211
         */
212
        public String getVersion() {        
213
                return version;
214
        } 
215

    
216
        /**
217
         * 
218
         * 
219
         * 
220
         * @param version The version to set.
221
         */
222
        public void setVersion(String version) {        
223
                this.version = version;
224
        } 
225

    
226
        /*
227
         * (non-Javadoc)
228
         * @see es.gva.cit.catalogClient.drivers.ICatalogServiceDriver#isProtocolSupported(java.net.URI)
229
         */
230
        public boolean isProtocolSupported(URI uri) {        
231
                return SOAPProtocol.isProtocolSupported(null);
232
        } 
233

    
234
        /*
235
         * (non-Javadoc)
236
         * @see es.gva.cit.catalogClient.drivers.ICatalogServiceDriver#getServiceName()
237
         */
238
        public String getServiceName() {
239
                return ServerData.SERVER_SUBTYPE_CATALOG_SRW;
240
        }
241

    
242
        /*
243
         * (non-Javadoc)
244
         * @see es.gva.cit.catalogClient.drivers.ICatalogServiceDriver#getDefaultPort()
245
         */
246
        public int getDefaultPort() {                
247
                return 80;
248
        }
249

    
250
        /*
251
         * (non-Javadoc)
252
         * @see es.gva.cit.catalogClient.drivers.ICatalogServiceDriver#getDefaultSchema()
253
         */
254
        public String getDefaultSchema() {
255
                return "http";
256
        } 
257
}