Statistics
| Revision:

root / trunk / applications / appCatalogYNomenclatorClient / src / es / gva / cit / catalogClient / drivers / SRWCatalogServiceDriver.java @ 3073

History | View | Annotate | Download (10.5 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.drivers;
42

    
43
import es.gva.cit.catalogClient.metadataXML.XMLNode;
44
import es.gva.cit.catalogClient.metadataXML.XMLTree;
45
import es.gva.cit.catalogClient.metadataXML.XMLTreeNumberOfRecordsAnswer;
46
import es.gva.cit.catalogClient.protocols.HTTPGetProtocol;
47
import es.gva.cit.catalogClient.protocols.HTTPPostProtocol;
48
import es.gva.cit.catalogClient.protocols.SOAPProtocol;
49
import es.gva.cit.catalogClient.querys.Query;
50
import es.gva.cit.catalogClient.querys.SRWQuery;
51

    
52
import org.apache.commons.httpclient.NameValuePair;
53

    
54
import java.net.URL;
55

    
56
import java.util.StringTokenizer;
57
/**
58
 * This class implements the CSW protocol.
59
 * 
60
 * @see http://www.loc.gov/z3950/agency/zing/srw/
61
 * @author Jorge Piera Llodra (piera_jor@gva.es)
62
 */
63
public class SRWCatalogServiceDriver extends AbstractCatalogServiceDriver {
64
    private String version = "1.1";
65
    private String recordXPath;
66
    private String resultSetTTL;
67

    
68
    /* (non-Javadoc)
69
     * @see ICatalogServerDriver#GetCapabilities(java.lang.String, java.lang.String)
70
    */
71
    public XMLNode[] getCapabilities(URL url) {
72
        XMLNode[] nodos = new HTTPGetProtocol().doQuery(url,
73
                getMessageCapabilities(), 0);
74

    
75
        if (nodos != null) {
76
            setCommunicationProtocol("GET");
77

    
78
            return nodos;
79
        }
80

    
81
        nodos = new HTTPPostProtocol().doQuery(url, getMessageCapabilities(), 0);
82

    
83
        if (nodos != null) {
84
            setCommunicationProtocol("POST");
85

    
86
            return nodos;
87
        }
88

    
89
        nodos = new SOAPProtocol().doQuery(url, getSOAPMessageCapabilities(), 0);
90

    
91
        if (nodos != null) {
92
            setCommunicationProtocol("SOAP");
93
        }
94

    
95
        return nodos;
96
    }
97

    
98
    /* (non-Javadoc)
99
     * @see ICatalogServerDriver#GetRecords(java.lang.String, java.lang.String, java.lang.String, int, int, java.lang.String, java.lang.String)
100
     */
101
    public XMLNode[] getRecords(URL url, Query query, int firstRecord) {
102
        setQuery(query);
103

    
104
        XMLNode[] answerNodes = null;
105
        XMLNode[] nodes = null;
106

    
107
        if (getCommunicationProtocol().equals("GET")) {
108
            nodes = new HTTPGetProtocol().doQuery(url,
109
                    getMessageRecords(getQuery(), firstRecord), firstRecord);
110
        }
111

    
112
        if (getCommunicationProtocol().equals("POST")) {
113
            nodes = new HTTPPostProtocol().doQuery(url,
114
                    getMessageRecords(getQuery(), firstRecord), firstRecord);
115
        }
116

    
117
        if (getCommunicationProtocol().equals("SOAP")) {
118
            nodes = new SOAPProtocol().doQuery(url,
119
                    getMessageRecords(getQuery(), firstRecord), firstRecord);
120
        }
121

    
122
        String prefix = new StringTokenizer(nodes[0].getName(), ":").nextToken();
123

    
124
        if (prefix.equals(nodes[0].getName())) {
125
            prefix = "";
126
        } else {
127
            prefix = prefix + ":";
128
        }
129

    
130
        int numberOfRecords = getNumberOfRecords(nodes[0],
131
                        prefix + "numberOfRecords",
132
                                null);
133

    
134
        if (numberOfRecords == -1) {
135
            return null;
136
        }   
137
        
138
        answerNodes = createAnswerTree(numberOfRecords, firstRecord, nodes[0],
139
                url,prefix);
140

    
141
        return answerNodes;
142
    }
143
    
144
    /**
145
     * It returns an array of nodes with:
146
     *         1st position -> tree with information of the answer
147
     *          Next positions -> One result for each position
148
     * @param numberOfRecords
149
     * Number of records returned by the query
150
     * @param firstRecord
151
     * Number of the first record
152
     * @param node
153
     * Tree returned by the server
154
     * @param url
155
     * Server URL
156
     * @param prefix
157
     * Tags Prefix
158
     * @return
159
     */
160
    public XMLNode[] createAnswerTree(int numberOfRecords, int firstRecord,
161
        XMLNode node, URL url,String prefix) {
162
        XMLNode[] answerNodes = null;
163

    
164
        if (numberOfRecords > 10) {
165
            answerNodes = new XMLNode[11];
166
        } else {
167
            answerNodes = new XMLNode[numberOfRecords + 1];
168
        }
169

    
170
        answerNodes[0] = XMLTreeNumberOfRecordsAnswer.getNode(numberOfRecords,
171
                firstRecord, firstRecord + numberOfRecords);
172

    
173
        XMLNode[] auxNodes = XMLTree.searchMultipleNode(node,
174
                prefix + "records->" + prefix + "record");
175

    
176
        for (int i = 1;
177
                (i <= numberOfRecords) && (i <= 10) &&
178
                (i <= (numberOfRecords - firstRecord + 1)); i++)
179
            answerNodes[i] = auxNodes[i - 1];
180

    
181
        return answerNodes;
182
    }
183
    
184
    /**
185
     * It creates the name-value pairs for the getCapabilities request 
186
     * @return
187
     * Name-Value pair array
188
     */
189
    public NameValuePair[] getMessageCapabilities() {
190
        NameValuePair nvp1 = new NameValuePair("OPERATION", "explain");
191
        NameValuePair nvp2 = new NameValuePair("VERSION", this.version);
192

    
193
        return new NameValuePair[] { nvp1, nvp2 };
194
    }
195
    
196
    /**
197
     * It creates the name-value pairs for the getRecords request 
198
     * @return
199
     * Name-Value pair array
200
     */
201
    public NameValuePair[] getMessageRecords(Query query, int firstRecord) {
202
        SRWQuery qsrwd = new SRWQuery(query);
203

    
204
        NameValuePair nvp1 = new NameValuePair("operation", "searchRetrieve");
205
        NameValuePair nvp2 = new NameValuePair("version", this.version);
206
        NameValuePair nvp3 = new NameValuePair("query", qsrwd.getQuery(null));
207
        NameValuePair nvp4 = new NameValuePair("maximumRecords", "10");
208
        NameValuePair nvp5 = new NameValuePair("recordPacking",
209
                getOutputFormat());
210
        NameValuePair nvp6 = new NameValuePair("startRecord",
211
                new String(new Integer(firstRecord).toString()));
212

    
213
        return new NameValuePair[] { nvp1, nvp2, nvp3, nvp4, nvp5, nvp6 };
214
    }
215
    
216
    /**
217
     * It creates the SOAP message for the getCapabilities request
218
     * 
219
     * @return
220
     * String with the SOAP message
221
     */
222
    public String getSOAPMessageCapabilities() {
223
        String soapMessage =
224
            "<SRW:explainRequest xmlns:SRW=\"http://www.loc.gov/zing/srw/\">" +
225
            "<SRW:version>" + version + "</SRW:version>" +
226
            "</SRW:explainRequest>";
227

    
228
        return SOAPProtocol.setSOAPMessage(soapMessage,null);
229
    }
230
    
231
    /**
232
     * It creates the SOAP message for the getRecords request
233
     * 
234
     * @return
235
     * String with the SOAP message
236
     */
237
    public String getSOAPMessageRecords(Query query) {
238
        SRWQuery qsrwd = new SRWQuery(query);
239

    
240
        String soapMessage =
241
            "<srw:searchRetrieveRequest xmlns:srw=\"http://www.loc.gov/zing/srw/\">" +
242
            "<srw:query><![CDATA[" + qsrwd.getQuery(null) + "]]></srw:query>" +
243
            "<srw:sortKeys xsi:nil=\"true\"/>" + "<srw:startRecord>" +
244
            getStartPosition() + "</srw:startRecord>" + "<srw:maximumRecords>" +
245
            getMaxRecords() + "</srw:maximumRecords>" + "<srw:recordPacking>" +
246
            getOutputFormat() + "</srw:recordPacking>" + "<srw:recordSchema>" +
247
            getOutputSchema()[0] + "</srw:recordSchema>" +
248
            "<srw:resultSetTTL>" + getResultSetTTL() + "</srw:resultSetTTL>" +
249
            "</srw:searchRetrieveRequest>";
250

    
251
        return SOAPProtocol.setSOAPMessage(soapMessage,null);
252
    }
253

    
254
    /**
255
     * @return Returns the recordXPath.
256
     */
257
    public String getRecordXPath() {
258
        return recordXPath;
259
    }
260

    
261
    /**
262
     * @param recordXPath The recordXPath to set.
263
     */
264
    public void setRecordXPath(String recordXPath) {
265
        this.recordXPath = recordXPath;
266
    }
267

    
268
    /**
269
     * @return Returns the resultSetTTL.
270
     */
271
    public String getResultSetTTL() {
272
        return resultSetTTL;
273
    }
274

    
275
    /**
276
     * @param resultSetTTL The resultSetTTL to set.
277
     */
278
    public void setResultSetTTL(String resultSetTTL) {
279
        this.resultSetTTL = resultSetTTL;
280
    }
281

    
282
    /**
283
     * @return Returns the version.
284
     */
285
    public String getVersion() {
286
        return version;
287
    }
288

    
289
    /**
290
     * @param version The version to set.
291
     */
292
    public void setVersion(String version) {
293
        this.version = version;
294
    }
295

    
296
    /* (non-Javadoc)
297
     * @see catalogClient.ICatalogServerDriver#setParameters(java.util.Properties)
298
     */
299
    public boolean setParameters(XMLNode[] nodes) {
300
        if (nodes[0].getName() != null) {
301
            String prefix = new StringTokenizer(nodes[0].getName(), ":").nextToken();
302
            prefix = prefix + ":";
303
            this.setOutputSchema(XMLTree.searchNodeValue(nodes[0],
304
                    prefix + "record->" + prefix + "recordSchema"));
305
            this.setOutputFormat(XMLTree.searchNodeValue(nodes[0],
306
                    prefix + "record->" + prefix + "recordPacking"));
307
            this.setStartPosition("1");
308
            this.setMaxRecords("10");
309
            this.setRecordXPath("");
310
            this.setResultSetTTL("0");
311
            this.setServerAnswerReady("Titulo: " +
312
                XMLTree.searchNodeValue(nodes[0],
313
                    prefix + "record->" + prefix +
314
                    "recordData->explain->databaseInfo->title") +
315
                "\nDescripcion: " +
316
                XMLTree.searchNodeValue(nodes[0],
317
                    prefix + "record->" + prefix +
318
                    "recordData->explain->databaseInfo->description"));
319
        }
320

    
321
        return true;
322
    }
323

    
324
    /* (non-Javadoc)
325
     * @see es.gva.cit.catalogClient.drivers.ICatalogServiveDriver#isTheProtocol(java.net.URL)
326
     */
327
    public boolean isProtocolSupported(URL url) {
328
        return SOAPProtocol.isProtocolSupported(url);
329
    }
330
}