Statistics
| Revision:

root / branches / CatalogYNomenclator_v1_1_0_1005 / applications / appCatalogYNomenclatorClient / src / es / gva / cit / catalogClient / csw / drivers / CSWCatalogServiceDriver.java @ 12733

History | View | Annotate | Download (13.7 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 es.gva.cit.catalogClient.csw.drivers;
43
import java.net.MalformedURLException;
44
import java.net.URI;
45
import java.net.URL;
46
import java.util.Collection;
47

    
48
import org.apache.commons.httpclient.NameValuePair;
49

    
50
import com.iver.utiles.swing.jcomboServer.ServerData;
51

    
52
import es.gva.cit.catalogClient.csw.parsers.CSWCapabilitiesParserv2_0_0;
53
import es.gva.cit.catalogClient.csw.parsers.CswSupportedProtocolOperations;
54
import es.gva.cit.catalogClient.drivers.AbstractCatalogServiceDriver;
55
import es.gva.cit.catalogClient.drivers.CatalogCapabilities;
56
import es.gva.cit.catalogClient.drivers.GetRecordsReply;
57
import es.gva.cit.catalogClient.metadataxml.XMLNode;
58
import es.gva.cit.catalogClient.metadataxml.XMLTree;
59
import es.gva.cit.catalogClient.metadataxml.XMLTreeNumberOfRecordsAnswer;
60
import es.gva.cit.catalogClient.protocols.HTTPGetProtocol;
61
import es.gva.cit.catalogClient.protocols.HTTPPostProtocol;
62
import es.gva.cit.catalogClient.protocols.SOAPProtocol;
63
import es.gva.cit.catalogClient.querys.Query;
64
import es.gva.cit.catalogClient.schemas.Schemas;
65
import es.gva.cit.catalogClient.schemas.records.Record;
66
import es.gva.cit.catalogClient.utils.CatalogConstants;
67

    
68
/**
69
 * This class implements the CSW protocol.
70
 * 
71
 * 
72
 * @author Jorge Piera Llodra (piera_jor@gva.es)
73
 * @see http://portal.opengeospatial.org/files/?artifact_id=5929&version=1
74
 */
75
public class CSWCatalogServiceDriver extends AbstractCatalogServiceDriver {
76
        private String responseHandler = null;
77
        private String hopCount = null;
78
        private String distributedSearch = null;
79
        private String constraint = null;
80
        private String[] CONSTRAINTLANGUAGE = null;
81
        private String[] elementSetName = null;
82
        private String[] typeNames = null;
83
        private String[] resultType = null;
84
        private String[] NAMESPACE = null;
85
        private String version = "2.0.0";
86
        private CswSupportedProtocolOperations operations = null;
87

    
88
        /**
89
         * @return 
90
         * @param url 
91
         */
92
        public CatalogCapabilities getCapabilities(URI uri) {        
93
                URL url = null;
94
                try {
95
                        url = uri.toURL();
96
                } catch (MalformedURLException e) {
97
                        setServerAnswerReady("errorServerNotFound");
98
                        return null;
99
                }        
100
                Collection nodes = new java.util.ArrayList();
101
                CSWMessages messages = new CSWMessages(this);
102
                nodes = new HTTPGetProtocol().doQuery(url, messages.getHTTPGETCapabilities(true), 0);
103
                CSWCapabilitiesParserv2_0_0 parser = new CSWCapabilitiesParserv2_0_0(url,this);
104
                CatalogCapabilities capabilities = parser.parse((XMLNode)nodes.toArray()[0]);
105
                return capabilities;
106
        } 
107

    
108
        /**
109
         * @return 
110
         * @param url 
111
         * @param query 
112
         * @param firstRecord 
113
         */
114
        public GetRecordsReply getRecords(URI uri, Query query, int firstRecord) {        
115
                URL url = null;
116
                try {
117
                        url = uri.toURL();
118
                } catch (MalformedURLException e) {
119
                        setServerAnswerReady("errorServerNotFound");
120
                        return null;
121
                }        
122
                setQuery(query);
123
                CSWMessages messages = new CSWMessages(this);
124
                CSWMessages.setUrl(url);
125
                Collection answerNodes = new java.util.ArrayList();
126
                Collection nodes = new java.util.ArrayList();
127

    
128
                if (getOperations().getGetRecords()==null)
129
                        return null;
130

    
131
                if (getServerProfile().equals(Schemas.ISO19115)){
132
//                        if (getService().equals("CSW")){
133
//                                firstRecord = firstRecord - 1;
134
//                        }
135
                }
136

    
137

    
138
                for (int i=0 ; i<getOperations().getGetRecords().length; i++){
139
                        /*
140
            if (getOperations().getGetRecords()[i].equals("GET")){
141
                    System.out.println("**************GET*************");
142
                    nodes = new HTTPGetProtocol().doQuery(url,
143
                        messages.getHTTPGETRecords(getQuery() , firstRecord), firstRecord);
144

145
                    setCommunicationProtocol("GET");                    
146
                break;
147
            }
148
                         */
149

    
150
                        if (getOperations().getGetRecords()[i].equals("POST")){
151
                                System.out.println("**************POST*************");
152
                                System.out.println(messages.getHTTPPOSTRecords(getQuery(), firstRecord));                    
153
                                nodes = new HTTPPostProtocol().doQuery(url,
154
                                                messages.getHTTPPOSTRecords(getQuery(), firstRecord), firstRecord);
155
                                      
156
                                break;
157
                        }
158

    
159
                        /*
160
            if (getOperations().getGetRecords()[i].equals("SOAP")){
161
                         System.out.println("**************SOAP*************");
162
                         nodes = new SOAPProtocol().doQuery(url,
163
                        messages.getSOAPRecords(getQuery(), firstRecord), firstRecord);
164

165
                         setCommunicationProtocol("SOAP");
166
                 //We must cut the SOAP Head
167
                 nodes[0] = SOAPMessageParser.cutHead(nodes[0]);
168
                         break;
169
                } */ 
170

    
171

    
172
                }
173

    
174
                if (nodes == null) 
175
                        return null;
176

    
177
                int numberOfRecords = getNumberOfRecords((XMLNode)nodes.toArray()[0]);
178
                if (numberOfRecords == -1) {
179
                        return null;
180
                }
181
                answerNodes = createAnswerTree(numberOfRecords, firstRecord, (XMLNode)nodes.toArray()[0],
182
                                url);
183
                //return answerNodes;
184
                return null;
185
        } 
186

    
187
        /**
188
         * It returns an array of nodes with:
189
         * 1st position -> tree with information of the answer
190
         * Next positions -> One result for each position
191
         * 
192
         * 
193
         * @return 
194
         * @param numberOfRecords Number of records returned by the query
195
         * @param firstRecord Number of the first record
196
         * @param node Tree returned by the server
197
         * @param url Server URL
198
         */
199
        private Collection createAnswerTree(int numberOfRecords, int firstRecord, XMLNode node, URL url) {        
200
                Collection answerNodes = new java.util.ArrayList();
201

    
202
                XMLNode[] auxNodes = cutMetadata(node);
203
                if (getServerProfile().equals(Schemas.EBRIM)) {
204
                        if (!url.getHost().equals("laits.gmu.edu")){
205
                                auxNodes = getEbRIMNodes(auxNodes, url);
206
                        }
207
                }
208
                //Solves some problems with the maxNumberOfRecords attribute
209
                numberOfRecords = auxNodes.length;
210
                answerNodes.add(XMLTreeNumberOfRecordsAnswer.getNode(numberOfRecords,
211
                                firstRecord, firstRecord + numberOfRecords));
212

    
213
                for (int i = 1;
214
                (i <= numberOfRecords) && (i <= 10) &&
215
                (i <= (numberOfRecords - firstRecord + 1)); i++)
216
                        answerNodes.add(auxNodes[i - 1]);
217
                return answerNodes;
218
        } 
219

    
220
        /**
221
         * This function retrieve the nodes for one ebRIM answer
222
         * 
223
         * 
224
         * @return Medatada Nodes.
225
         * @param nodes Server URL
226
         * @param url 
227
         */
228
        private XMLNode[] getEbRIMNodes(XMLNode[] nodes, URL url) {        
229
                XMLNode[] auxNodes = new XMLNode[nodes.length];
230
                for (int i = 0; i < nodes.length; i++) {
231
                        String id = XMLTree.searchAtribute(nodes[i], "id");
232
                        auxNodes[i] = (XMLNode)new HTTPGetProtocol().doQuery(url,
233
                                        getEbRIMRequestParameters(id), 0).toArray()[0];
234
                }
235
                return auxNodes;
236
        } 
237

    
238
        /**
239
         * It Returns the parameters needed by getExtrinsicContent
240
         * 
241
         * 
242
         * @return 
243
         * @param id Record id
244
         */
245
        private NameValuePair[] getEbRIMRequestParameters(String id) {        
246
                NameValuePair nvp1 = new NameValuePair("request", "getExtrinsicContent");
247
                NameValuePair nvp2 = new NameValuePair("id", id);
248
                return new NameValuePair[] { nvp1, nvp2 };
249
        } 
250

    
251
        /**
252
         * This function returns the number of records that have been retrieved.
253
         * It Reads a Node value.
254
         * 
255
         * 
256
         * @return The number of records
257
         * @param node The answer tree
258
         */
259
        private int getNumberOfRecords(XMLNode node) {        
260
                int numberOfRecords = getNumberOfRecords(node,"csw:SearchResults","numberOfRecordsMatched");
261
                if (numberOfRecords == -1)
262
                        numberOfRecords = getNumberOfRecords(node,"SearchResults","numberOfRecordsMatched");
263

    
264
                return numberOfRecords;
265
        } 
266

    
267
        /**
268
         * This function finds and separates metadata from a full tree
269
         * 
270
         * 
271
         * @return An array of trees that contain metadata.
272
         * @param node full tree
273
         */
274
        private XMLNode[] cutMetadata(XMLNode node) {        
275
                XMLNode[] auxNodes = XMLTree.searchMultipleNode(node,
276
                "csw:SearchResults->brief:MD_Metadata");
277
                if (auxNodes.length == 0) {
278
                        auxNodes = XMLTree.searchMultipleNode(node,
279
                        "SearchResults->ebrim:Organization");
280
                }
281
                if (auxNodes.length == 0) {
282
                        auxNodes = XMLTree.searchMultipleNode(node,
283
                        "csw:SearchResults->csw:SummaryRecord");
284
                }
285
                if (auxNodes.length == 0) {
286
                        auxNodes = XMLTree.searchMultipleNode(node,
287
                        "csw:SearchResults->csw:Record");
288
                }
289
                if (auxNodes.length == 0) {
290
                        auxNodes = XMLTree.searchMultipleNode(node,
291
                        "csw:SearchResults->DS_DataSet");
292
                }
293

    
294
                if (auxNodes.length == 0) {
295
                        auxNodes = XMLTree.searchMultipleNode(node,
296
                        "SearchResults->wrs:WRSExtrinsicObject");
297
                }
298

    
299
                if (auxNodes.length == 0) {
300
                        auxNodes = XMLTree.searchMultipleNode(node,
301
                        "SearchResults->WRSExtrinsicObject");
302
                }        
303

    
304
                if (auxNodes.length == 0) {
305
                        auxNodes = XMLTree.searchMultipleNode(node,
306
                        "csw:SearchResults->dc:metadata");
307
                }
308

    
309
                if (auxNodes.length == 0) {
310
                        auxNodes = XMLTree.searchMultipleNode(node,
311
                        "csw:SearchResults->iso19115:MD_Metadata");
312
                }
313
                if (auxNodes.length == 0) {
314
                        auxNodes = XMLTree.searchMultipleNode(node,
315
                        "csw:SearchResults->laitscsw:DataGranule");
316
                }          
317
                if (auxNodes.length == 0) {
318
                        auxNodes = XMLTree.searchMultipleNode(node,
319
                        "csw:SearchResults->rim:Service");
320
                }  
321
                return auxNodes;
322
        } 
323

    
324
        /**
325
         * @param typeNames The typeNames to set.
326
         */
327
        public void setTypeNames(String[] typeNames) {        
328
                this.typeNames = typeNames;
329
        } 
330
        
331
/*
332
 * (non-Javadoc)
333
 * @see es.gva.cit.catalogClient.drivers.ICatalogServiceDriver#isProtocolSupported(java.net.URI)
334
 */
335
        public boolean isProtocolSupported(URI uri) {        
336
                return true;
337
        } 
338

    
339
        /**
340
         * @return Returns the distributedSearch.
341
         */
342
        public String getDistributedSearch() {        
343
                return distributedSearch;
344
        } 
345

    
346
        /**
347
         * @param distributedSearch The distributedSearch to set.
348
         */
349
        public void setDistributedSearch(String distributedSearch) {        
350
                this.distributedSearch = distributedSearch;
351
        } 
352

    
353
        /**
354
         * @return Returns the hopCount.
355
         */
356
        public String getHopCount() {        
357
                return hopCount;
358
        } 
359

    
360
        /**
361
         * @param hopCount The hopCount to set.
362
         */
363
        public void setHopCount(String hopCount) {        
364
                this.hopCount = hopCount;
365
        } 
366

    
367
        /**
368
         * @return Returns the nAMESPACE.
369
         */
370
        public String[] getNAMESPACE() {        
371
                return NAMESPACE;
372
        } 
373

    
374
        /**
375
         * @param namespace The nAMESPACE to set.
376
         */
377
        public void setNAMESPACE(String[] namespace) {        
378
                NAMESPACE = namespace;
379
        } 
380

    
381
        /**
382
         * @return Returns the responseHandler.
383
         */
384
        public String getResponseHandler() {        
385
                return responseHandler;
386
        } 
387

    
388
        /**
389
         * @param responseHandler The responseHandler to set.
390
         */
391
        public void setResponseHandler(String responseHandler) {        
392
                this.responseHandler = responseHandler;
393
        } 
394

    
395
        /**
396
         * @return Returns the resultType.
397
         */
398
        public String[] getResultType() {        
399
                return resultType;
400
        } 
401

    
402
        /**
403
         * @param resultType The resultType to set.
404
         */
405
        public void setResultType(String[] resultType) {        
406
                this.resultType = resultType;
407
        } 
408

    
409
        /**
410
         * @return Returns the typeNames.
411
         */
412
        public String[] getTypeNames() {        
413
                return typeNames;
414
        } 
415

    
416
        /**
417
         * @return Returns the version.
418
         */
419
        public String getVersion() {        
420
                return version;
421
        } 
422

    
423
        /**
424
         * @param version The version to set.
425
         */
426
        public void setVersion(String version) {        
427
                this.version = version;
428
        } 
429

    
430
        /**
431
         * @return Returns the constraint.
432
         */
433
        public String getConstraint() {        
434
                return constraint;
435
        } 
436

    
437
        /**
438
         * @param constraint The constraint to set.
439
         */
440
        public void setConstraint(String constraint) {        
441
                this.constraint = constraint;
442
        } 
443

    
444
        /**
445
         * @return Returns the cONSTRAINTLANGUAGE.
446
         */
447
        public String[] getCONSTRAINTLANGUAGE() {        
448
                return CONSTRAINTLANGUAGE;
449
        } 
450

    
451
        /**
452
         * @param constraintlanguage The cONSTRAINTLANGUAGE to set.
453
         */
454
        public void setCONSTRAINTLANGUAGE(String[] constraintlanguage) {        
455
                CONSTRAINTLANGUAGE = constraintlanguage;
456
        } 
457

    
458
        /**
459
         * @return Returns the elementSetName.
460
         */
461
        public String[] getElementSetName() {        
462
                return elementSetName;
463
        } 
464

    
465
        /**
466
         * @param elementSetName The elementSetName to set.
467
         */
468
        public void setElementSetName(String[] elementSetName) {        
469
                this.elementSetName = elementSetName;
470
        } 
471

    
472
        /**
473
         * @return Returns the operations.
474
         */
475
        public CswSupportedProtocolOperations getOperations() {        
476
                return operations;
477
        } 
478

    
479
        /**
480
         * @param operations The operations to set.
481
         */
482
        public void setOperations(CswSupportedProtocolOperations operations) {        
483
                this.operations = operations;
484
        } 
485

    
486
        /**
487
         * 
488
         * @return
489
         */
490
        public String getQueryProfile() {        
491
                // TODO Auto-generated method stub
492
                return getServerProfile();
493
        }
494

    
495
        /*
496
         * (non-Javadoc)
497
         * @see es.gva.cit.catalogClient.drivers.ICatalogServiceDriver#getServiceName()
498
         */
499
        public String getServiceName() {
500
                return ServerData.SERVER_SUBTYPE_CATALOG_CSW;
501
        }
502

    
503
        /*
504
         * (non-Javadoc)
505
         * @see es.gva.cit.catalogClient.drivers.ICatalogServiceDriver#getDefaultPort()
506
         */
507
        public int getDefaultPort() {
508
                return 80;
509
        }
510

    
511
        /*
512
         * (non-Javadoc)
513
         * @see es.gva.cit.catalogClient.drivers.ICatalogServiceDriver#getDefaultSchema()
514
         */
515
        public String getDefaultSchema() {
516
                return "http";
517
        } 
518

    
519
}