Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appCatalogYNomenclatorClient / src / es / gva / cit / catalogClient / drivers / CSWCatalogServiceDriver.java @ 2658

History | View | Annotate | Download (11.1 KB)

1
package es.gva.cit.catalogClient.drivers;
2

    
3
import java.net.URL;
4
import java.util.StringTokenizer;
5

    
6
import org.apache.commons.httpclient.NameValuePair;
7
import org.w3c.dom.Node;
8

    
9
import es.gva.cit.catalogClient.metadataXML.XMLTree;
10
import es.gva.cit.catalogClient.metadataXML.XMLTreeNumberOfRecordsAnswer;
11
import es.gva.cit.catalogClient.parsers.SOAPMessageParser;
12
import es.gva.cit.catalogClient.parsers.csw.cswCapabilitiesParser;
13
import es.gva.cit.catalogClient.protocols.HTTPGetProtocol;
14
import es.gva.cit.catalogClient.protocols.HTTPPostProtocol;
15
import es.gva.cit.catalogClient.protocols.SOAPProtocol;
16
import es.gva.cit.catalogClient.querys.CSWQuery;
17
import es.gva.cit.catalogClient.querys.Query;
18
import es.gva.cit.catalogClient.utils.Strings;
19

    
20

    
21
/* Generated by Together */
22

    
23
public class CSWCatalogServiceDriver extends AbstractCatalogServiceDriver {
24
        private String responseHandler = null;
25
        private String hopCount = null;
26
        private String distributedSearch = null;
27
        private String constraint = null;
28
        private String[] CONSTRAINTLANGUAGE = null;
29
        private String[] elementSetName = null;
30
        private String[] typeNames = null;
31
        private String[] resultType = null;
32
        private String[] NAMESPACE = null;
33
        private String service = null;
34
        private String version = "2.0.0";
35

    
36
        /*
37
         * (non-Javadoc)
38
         * 
39
         * @see ICatalogServerDriver#GetCapabilities(java.lang.String,
40
         *      java.lang.String)
41
         */
42
        public Node[] getCapabilities(URL url) {
43
                
44
                Node[] nodos = new SOAPProtocol().doQuery(url,getSOAPMessageCapabilities(),0);
45
                if (nodos != null){
46
                        setCommunicationProtocol("SOAP");
47
                        return nodos;
48
                }
49
                
50
                nodos = new HTTPGetProtocol().doQuery(url, getMessageCapabilities(),0);
51
                if (nodos != null){
52
                        setCommunicationProtocol("GET");
53
                        return nodos;
54
                }
55
                
56
                nodos = new HTTPPostProtocol().doQuery(url,getMessageCapabilities(),0);
57
                if (nodos != null){
58
                        setCommunicationProtocol("POST");
59
                        return nodos;
60
                }
61
                        
62
                return nodos;                        
63
        }
64
        
65
        /**
66
         * this method implements the describeRecords operation 
67
         * @param url
68
         * @return
69
         */
70

    
71
        public Node[] describeRecords(URL url) {
72
                //return new HTTPPostProtocol().doQuery(url, getMessageDescribeRecords(),0);
73
                return null;
74
        }
75

    
76
        /*
77
         * (non-Javadoc)
78
         * 
79
         * @see ICatalogServerDriver#GetRecords(java.lang.String, java.lang.String,
80
         *      java.lang.String, int, int, java.lang.String, java.lang.String)
81
         */
82
        public Node[] getRecords(URL url, Query query, int firstRecord) {
83
                setQuery(query);
84
                Node[] answerNodes = null;
85
                
86
                Node[] nodes = new SOAPProtocol().doQuery(url,getSOAPMessageRecords(getQuery()),firstRecord);
87
                if (nodes == null){
88
                        nodes = new HTTPPostProtocol().doQuery(url, getMessageCapabilities(),firstRecord);
89
                        if (nodes == null){
90
                                nodes = new HTTPGetProtocol().doQuery(url, getMessageCapabilities(),firstRecord);
91
                                if (nodes == null)
92
                                        return null;
93
                                else{
94
                                        setCommunicationProtocol("GET");                                
95
                                }
96
                        }else{
97
                                setCommunicationProtocol("POST");
98
                        }
99
                }else{
100
                        setCommunicationProtocol("SOAP");
101
                        //We must cut the SOAP Head
102
                        nodes[0] = SOAPMessageParser.cutHead(nodes[0]);        
103
                }
104
                        
105
                
106
                //Hay que capturar la excepci?n. 
107
                int numberOfRecords = Integer.parseInt(XMLTree.searchNodeAtribute(nodes[0],"csw:SearchResults","numberOfRecordsMatched"));
108
                        
109
                if (numberOfRecords > 10)
110
                        answerNodes = new Node[11];
111
                else
112
                        answerNodes = new Node[numberOfRecords + 1];
113
                                                
114
                answerNodes[0] = XMLTreeNumberOfRecordsAnswer.getNode(numberOfRecords,
115
                                firstRecord,
116
                                firstRecord + numberOfRecords );
117
                                
118
                
119
                nodes = XMLTree.searchMultipleNode(nodes[0],"csw:SearchResults->brief:MD_Metadata");
120
                                
121
                for (int i = 1; (i <= numberOfRecords) && (i <= 10) ; i++)
122
                        answerNodes[i] = nodes[i-1];
123
                        
124
                return answerNodes;                
125
                                
126
        }
127
                
128

    
129
        /**
130
         * @return Devuelve los pares con los atributos para hacer la invocaci?n a
131
         *         la llamada getCapabilities usando el m?todo de HTTP Get
132
         */
133

    
134
        public NameValuePair[] getMessageCapabilities() {
135
                NameValuePair nvp1= new NameValuePair("request","getCapabilities");
136
                NameValuePair nvp2= new NameValuePair("service","CSW");
137
                NameValuePair nvp3= new NameValuePair("acceptversion",this.version);
138
                    
139
                return new NameValuePair[]{nvp1,nvp2, nvp3};
140
        }
141
        
142
        /**
143
         *  
144
         * @return
145
         */
146
        public NameValuePair[] getMessageDescribeRecords() {
147
                NameValuePair nvp1= new NameValuePair("request","describeRecord");
148
                NameValuePair nvp2= new NameValuePair("version",this.version);
149
                NameValuePair nvp3= new NameValuePair("outputformat",this.getOutputFormat());
150
                NameValuePair nvp4= new NameValuePair("schemalanguage","XMLSCHEMA");
151
                NameValuePair nvp5= new NameValuePair("typename",this.getTypeNames()[0]);
152
                NameValuePair nvp6= new NameValuePair("namespace","csw:http://www.opengis.org/csw");
153
                    
154
                return new NameValuePair[]{nvp1,nvp2, nvp3,nvp4,nvp5,nvp6};
155
        }
156
                
157
        /**
158
         * @param query
159
         * @return
160
         */
161
        public NameValuePair[] getMessageRecords(Query query){
162
                CSWQuery cswq = new CSWQuery(query); 
163
                
164
                NameValuePair nvp1= new NameValuePair("request","getRecords");
165
            NameValuePair nvp2= new NameValuePair("version",this.version);
166
            NameValuePair nvp3= new NameValuePair("service","CSW");
167
            NameValuePair nvp4= new NameValuePair("outputformat",this.getOutputFormat());
168
            System.out.println(this.getOutputFormat());
169
            NameValuePair nvp5= new NameValuePair("outputschema",this.getOutputSchema()[0]);
170
            //NameValuePair nvp6= new NameValuePair("resulttype",this.getResultType()[0]);
171
            NameValuePair nvp7= new NameValuePair("namespace","csw:http://www.opengis.org/csw");
172
            NameValuePair nvp8= new NameValuePair("typename",Strings.getComaSeparated(this.getTypeNames()));
173
            NameValuePair nvp9= new NameValuePair("elementsetname","full");
174
            NameValuePair nvp10= new NameValuePair("constraintlanguage","FILTER");
175
            NameValuePair nvp11= new NameValuePair("constraint",cswq.getQuery());
176
            
177
            System.out.println(cswq.getQuery());
178
            
179
             return new NameValuePair[]{nvp1,nvp2, nvp3, nvp4, nvp5, nvp7, nvp8, nvp9, nvp10, nvp11};
180
        }
181
                
182
        public String getSOAPMessageCapabilities(){
183
                return "<SOAP:Envelope xmlns:SOAP=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
184
                "<SOAP:Body>" +
185
                "<csw:GetCapabilities service=\"CSW\" version=\"2.0.0\" "+ 
186
                                "xmlns:csw=\"http://www.opengis.net/cat/csw\" "+ 
187
                                "xmlns:ogc=\"http://www.opengis.net/ogc\">" +
188
                        "<AcceptVersions xmlns=\"http://www.opengis.net/ows\">" +
189
                        "<Version>" + version + "</Version>" +
190
                        "</AcceptVersions>" +
191
                        "<AcceptFormats xmlns=\"http://www.opengis.net/ows\">" +
192
                                        "<OutputFormat>text/xml</OutputFormat>" +
193
                        "</AcceptFormats>" +
194
                "</csw:GetCapabilities>" +
195
                "</SOAP:Body>" +
196
                "</SOAP:Envelope>";
197
        }
198
        
199
        public String getSOAPMessageDescribeRecords() {
200
                return "<SOAP:Envelope xmlns:SOAP=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
201
                "<SOAP:Body>" +
202
                "<csw:DescribeRecord " +
203
                          "xmlns:csw=\"http://www.opengis.net/cat/csw\" " +
204
                        "xmlns:ogc=\"http://www.opengis.net/ogc\" " +
205
                        "service=\"CSW\" " +
206
                        "version=" + version + " " +
207
                        "outputFormat=\"test/xml\" " +
208
                        "schemaLanguage=\"XMLSCHEMA\">" +
209
                        "<csw:TypeName>csw:record</csw:TypeName>" +
210
                "</csw:DescribeRecord>" +
211
                "</SOAP:Body>" +
212
                "</SOAP:Envelope>";
213
                
214
        }
215
        
216
        public String getSOAPMessageRecords(Query query) {
217
                CSWQuery cswq = new CSWQuery(query); 
218
                String cadena = "<SOAP:Envelope xmlns:SOAP=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
219
                        "<SOAP:Body>" +
220
                        "<csw:GetRecords " +
221
                                  "xmlns:csw=\"http://www.opengis.net/cat/csw\" " +
222
                                "xmlns:ogc=\"http://www.opengis.net/ogc\" " +
223
                                "service=\"CSW\" " +
224
                                "version=\"2.0.0\" "+
225
                                "resultType=\"results\">" +
226
                                "<csw:Query typeNames=\"Dataset\">" +
227
                                    "<csw:ElementSetName>brief</csw:ElementSetName>" +
228
                                    "<csw:Constraint version=\"2.0.0\">" +
229
                                    cswq.getQuery() +
230
                                        "</csw:Constraint>" +
231
                                "</csw:Query>" +
232
                        "</csw:GetRecords>" +
233
                        "</SOAP:Body>" +
234
                        "</SOAP:Envelope>";
235
                System.out.println(cadena);
236
                return cadena;
237
                
238
        }                
239
                
240

    
241
        /*
242
         * (non-Javadoc)
243
         * 
244
         * @see catalogClient.ICatalogServerDriver#setParameters(java.util.Properties)
245
         */
246
        public boolean setParameters(Node[] nodes) {
247
                return new cswCapabilitiesParser(this).parse(nodes[0]); 
248
        }
249

    
250
        /**
251
         * @return Returns the distributedSearch.
252
         */
253
        public String getDistributedSearch() {
254
                return distributedSearch;
255
        }
256

    
257
        /**
258
         * @param distributedSearch
259
         *            The distributedSearch to set.
260
         */
261
        public void setDistributedSearch(String distributedSearch) {
262
                this.distributedSearch = distributedSearch;
263
        }
264

    
265
        /**
266
         * @return Returns the hopCount.
267
         */
268
        public String getHopCount() {
269
                return hopCount;
270
        }
271

    
272
        /**
273
         * @param hopCount
274
         *            The hopCount to set.
275
         */
276
        public void setHopCount(String hopCount) {
277
                this.hopCount = hopCount;
278
        }
279

    
280
        /**
281
         * @return Returns the nAMESPACE.
282
         */
283
        public String[]  getNAMESPACE() {
284
                return NAMESPACE;
285
        }
286

    
287
        /**
288
         * @param namespace
289
         *            The nAMESPACE to set.
290
         */
291
        public void setNAMESPACE(String[] namespace) {
292
                NAMESPACE = namespace;
293
        }
294

    
295
        /**
296
         * @return Returns the responseHandler.
297
         */
298
        public String getResponseHandler() {
299
                return responseHandler;
300
        }
301

    
302
        /**
303
         * @param responseHandler
304
         *            The responseHandler to set.
305
         */
306
        public void setResponseHandler(String responseHandler) {
307
                this.responseHandler = responseHandler;
308
        }
309

    
310
        /**
311
         * @return Returns the resultType.
312
         */
313
        public String[] getResultType() {
314
                return resultType;
315
        }
316

    
317
        /**
318
         * @param resultType
319
         *            The resultType to set.
320
         */
321
        public void setResultType(String[] resultType) {
322
                this.resultType = resultType;
323
        }
324

    
325
        /**
326
         * @return Returns the service.
327
         */
328
        public String getService() {
329
                return service;
330
        }
331

    
332
        /**
333
         * @param service
334
         *            The service to set.
335
         */
336
        public void setService(String service) {
337
                this.service = service;
338
        }
339

    
340
        /**
341
         * @return Returns the typeNames.
342
         */
343
        public String[] getTypeNames() {
344
                return typeNames;
345
        }
346

    
347
        /**
348
         * @param typeNames
349
         *            The typeNames to set.
350
         */
351
        public void setTypeNames(String[] typeNames) {
352
                this.typeNames = typeNames;
353
        }
354

    
355
        /**
356
         * @return Returns the version.
357
         */
358
        public String getVersion() {
359
                return version;
360
        }
361

    
362
        /**
363
         * @param version
364
         *            The version to set.
365
         */
366
        public void setVersion(String version) {
367
                this.version = version;
368
        }
369

    
370
        /**
371
         * @return Returns the constraint.
372
         */
373
        public String getConstraint() {
374
                return constraint;
375
        }
376

    
377
        /**
378
         * @param constraint
379
         *            The constraint to set.
380
         */
381
        public void setConstraint(String constraint) {
382
                this.constraint = constraint;
383
        }
384

    
385
        /**
386
         * @return Returns the cONSTRAINTLANGUAGE.
387
         */
388
        public String[] getCONSTRAINTLANGUAGE() {
389
                return CONSTRAINTLANGUAGE;
390
        }
391

    
392
        /**
393
         * @param constraintlanguage
394
         *            The cONSTRAINTLANGUAGE to set.
395
         */
396
        public void setCONSTRAINTLANGUAGE(String[] constraintlanguage) {
397
                CONSTRAINTLANGUAGE = constraintlanguage;
398
        }
399

    
400
        /**
401
         * @return Returns the elementSetName.
402
         */
403
        public String[] getElementSetName() {
404
                return elementSetName;
405
        }
406

    
407
        /**
408
         * @param elementSetName
409
         *            The elementSetName to set.
410
         */
411
        public void setElementSetName(String[] elementSetName) {
412
                this.elementSetName = elementSetName;
413
        }
414

    
415
        /* (non-Javadoc)
416
         * @see es.gva.cit.catalogClient.drivers.ICatalogServiveDriver#isTheProtocol(java.net.URL)
417
         */
418
        public boolean isProtocolSupported(URL url) {
419
                return true;
420
        }
421

    
422
}