Statistics
| Revision:

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

History | View | Annotate | Download (16.3 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 es.gva.cit.catalogClient.csw.parsers.CswCapabilitiesParser;
44
import es.gva.cit.catalogClient.csw.parsers.CswSupportedProtocolOperations;
45
import es.gva.cit.catalogClient.drivers.AbstractCatalogServiceDriver;
46
import es.gva.cit.catalogClient.metadataXML.XMLNode;
47
import es.gva.cit.catalogClient.metadataXML.XMLTree;
48
import es.gva.cit.catalogClient.metadataXML.XMLTreeNumberOfRecordsAnswer;
49
import es.gva.cit.catalogClient.protocols.HTTPGetProtocol;
50
import es.gva.cit.catalogClient.protocols.HTTPPostProtocol;
51
import es.gva.cit.catalogClient.protocols.SOAPProtocol;
52
import es.gva.cit.catalogClient.querys.Query;
53
import es.gva.cit.catalogClient.schemas.Schemas;
54
import java.net.URL;
55
import java.util.Collection;
56
import org.apache.commons.httpclient.NameValuePair;
57

    
58
/**
59
 * This class implements the CSW protocol.
60
 * 
61
 * 
62
 * @author Jorge Piera Llodra (piera_jor@gva.es)
63
 * @see http://portal.opengeospatial.org/files/?artifact_id=5929&version=1
64
 */
65
public class CSWCatalogServiceDriver extends AbstractCatalogServiceDriver {
66
private String service = "CSW";
67
/**
68
 * 
69
 * 
70
 */
71
    private String responseHandler = null;
72

    
73
/**
74
 * 
75
 * 
76
 */
77
    private String hopCount = null;
78

    
79
/**
80
 * 
81
 * 
82
 */
83
    private String distributedSearch = null;
84

    
85
/**
86
 * 
87
 * 
88
 */
89
    private String constraint = null;
90

    
91
/**
92
 * 
93
 * 
94
 */
95
    private String[] CONSTRAINTLANGUAGE = null;
96

    
97
/**
98
 * 
99
 * 
100
 */
101
    private String[] elementSetName = null;
102

    
103
/**
104
 * 
105
 * 
106
 */
107
    private String[] typeNames = null;
108

    
109
/**
110
 * 
111
 * 
112
 */
113
    private String[] resultType = null;
114

    
115
/**
116
 * 
117
 * 
118
 */
119
    private String[] NAMESPACE = null;
120

    
121
/**
122
 * 
123
 * 
124
 */
125
    private String version = "2.0.0";
126
/**
127
 * 
128
 * 
129
 */
130
    private CswSupportedProtocolOperations operations = null;
131
/*
132
     * (non-Javadoc)
133
     *
134
     * @see ICatalogServerDriver#GetCapabilities(java.lang.String,
135
     *      java.lang.String)
136
     */
137

    
138
/**
139
 * 
140
 * 
141
 * 
142
 * @return 
143
 * @param url 
144
 */
145
    public Collection getCapabilities(URL url) {        
146
        Collection nodes = new java.util.ArrayList();
147
        CSWMessages messages = new CSWMessages(this);
148
        CswCapabilitiesParser parser = new CswCapabilitiesParser(this,url);
149
        System.out.println("**************GET*************");
150
        nodes = new HTTPGetProtocol().doQuery(url, messages.getHTTPGETCapabilities(true), 0);
151
        if ((nodes != null) && (parser.getExceptionCode((XMLNode)nodes.toArray()[0]).equals("8001")) &&
152
                (parser.getExceptionSubCode((XMLNode)nodes.toArray()[0]).equals("5002")))
153
            nodes = new HTTPGetProtocol().doQuery(url, messages.getHTTPGETCapabilities(false), 0);
154
        
155
        if ((nodes == null) || 
156
                        ((nodes.size() >0) && (((XMLNode)nodes.toArray()[0]).getName().equals("html")))) {
157
            System.out.println("**************POST*************");
158
            System.out.println(messages.getHTTPPOSTCapabilities());
159
            nodes = new HTTPPostProtocol().doQuery(url,
160
                    messages.getHTTPPOSTCapabilities(), 0);
161
            if (nodes == null) {
162
                System.out.println("**************SOAP*************");
163
                nodes = new SOAPProtocol().doQuery(url,
164
                        messages.getSOAPCapabilities(), 0);
165
                if (nodes == null) {
166
                    return null;
167
                } else {
168
                    setCommunicationProtocol("SOAP");
169
                }
170
            } else {
171
                setCommunicationProtocol("POST");
172
            }
173
        } else {
174
            setCommunicationProtocol("GET");
175
        }
176
        
177
       return nodes;
178
    } 
179

    
180
/**
181
 * this method implements the describeRecords operation
182
 * 
183
 * 
184
 * @return 
185
 * @param url 
186
 */
187
    public XMLNode[] describeRecords(URL url) {        
188
        //return new HTTPPostProtocol().doQuery(url, getMessageDescribeRecords(),0);
189
        return null;
190
    } 
191
/*
192
     * (non-Javadoc)
193
     *
194
     * @see ICatalogServerDriver#GetRecords(java.lang.String, java.lang.String,
195
     *      java.lang.String, int, int, java.lang.String, java.lang.String)
196
     */
197

    
198
/**
199
 * 
200
 * 
201
 * 
202
 * @return 
203
 * @param url 
204
 * @param query 
205
 * @param firstRecord 
206
 */
207
    public Collection getRecords(URL url, Query query, int firstRecord) {        
208
        setQuery(query);
209
        CSWMessages messages = new CSWMessages(this);
210
        Collection answerNodes = new java.util.ArrayList();
211
        Collection nodes = new java.util.ArrayList();
212
        
213
        if (getOperations().getGetRecords()==null)
214
                return null;
215
      
216
        if (getServerProfile().equals(Schemas.ISO19115)){
217
                 if (getService().equals("CSW")){
218
                         firstRecord = firstRecord - 1;
219
                 }
220
        }
221
    
222
        
223
        for (int i=0 ; i<getOperations().getGetRecords().length; i++){
224
            /*
225
            if (getOperations().getGetRecords()[i].equals("GET")){
226
                    System.out.println("**************GET*************");
227
                    nodes = new HTTPGetProtocol().doQuery(url,
228
                        messages.getHTTPGETRecords(getQuery() , firstRecord), firstRecord);
229
                    
230
                    setCommunicationProtocol("GET");                    
231
                break;
232
            }
233
            */
234
           
235
            if (getOperations().getGetRecords()[i].equals("POST")){
236
                    System.out.println("**************POST*************");
237
                System.out.println(messages.getHTTPPOSTRecords(getQuery(), firstRecord));                    
238
                    nodes = new HTTPPostProtocol().doQuery(url,
239
                        messages.getHTTPPOSTRecords(getQuery(), firstRecord), firstRecord);
240
                
241
                setCommunicationProtocol("POST");                
242
                break;
243
            }
244
            
245
            /*
246
            if (getOperations().getGetRecords()[i].equals("SOAP")){
247
                         System.out.println("**************SOAP*************");
248
                         nodes = new SOAPProtocol().doQuery(url,
249
                        messages.getSOAPRecords(getQuery(), firstRecord), firstRecord);
250
                         
251
                         setCommunicationProtocol("SOAP");
252
                 //We must cut the SOAP Head
253
                 nodes[0] = SOAPMessageParser.cutHead(nodes[0]);
254
                         break;
255
                } */ 
256
            
257
            
258
        }
259
       
260
        if (nodes == null) 
261
                return null;
262
         
263
        int numberOfRecords = getNumberOfRecords((XMLNode)nodes.toArray()[0]);
264
        if (numberOfRecords == -1) {
265
            return null;
266
        }
267
        answerNodes = createAnswerTree(numberOfRecords, firstRecord, (XMLNode)nodes.toArray()[0],
268
                url);
269
        return answerNodes;
270
    } 
271

    
272
/**
273
 * It returns an array of nodes with:
274
 * 1st position -> tree with information of the answer
275
 * Next positions -> One result for each position
276
 * 
277
 * 
278
 * @return 
279
 * @param numberOfRecords Number of records returned by the query
280
 * @param firstRecord Number of the first record
281
 * @param node Tree returned by the server
282
 * @param url Server URL
283
 */
284
    private Collection createAnswerTree(int numberOfRecords, int firstRecord, XMLNode node, URL url) {        
285
        Collection answerNodes = new java.util.ArrayList();
286
      
287
        XMLNode[] auxNodes = cutMetadata(node);
288
        if (getServerProfile().equals(Schemas.EBRIM)) {
289
            auxNodes = getEbRIMNodes(auxNodes, url);
290
        }
291
        //Solves some problems with the maxNumberOfRecords attribute
292
        numberOfRecords = auxNodes.length;
293
        answerNodes.add(XMLTreeNumberOfRecordsAnswer.getNode(numberOfRecords,
294
                firstRecord, firstRecord + numberOfRecords));
295
        
296
        for (int i = 1;
297
                (i <= numberOfRecords) && (i <= 10) &&
298
                (i <= (numberOfRecords - firstRecord + 1)); i++)
299
            answerNodes.add(auxNodes[i - 1]);
300
        return answerNodes;
301
    } 
302

    
303
/**
304
 * This function retrieve the nodes for one ebRIM answer
305
 * 
306
 * 
307
 * @return Medatada Nodes.
308
 * @param nodes Server URL
309
 * @param url 
310
 */
311
    private XMLNode[] getEbRIMNodes(XMLNode[] nodes, URL url) {        
312
        XMLNode[] auxNodes = new XMLNode[nodes.length];
313
        for (int i = 0; i < nodes.length; i++) {
314
            String id = XMLTree.searchAtribute(nodes[i], "id");
315
            auxNodes[i] = (XMLNode)new HTTPGetProtocol().doQuery(url,
316
                    getEbRIMRequestParameters(id), 0).toArray()[0];
317
        }
318
        return auxNodes;
319
    } 
320

    
321
/**
322
 * It Returns the parameters needed by getExtrinsicContent
323
 * 
324
 * 
325
 * @return 
326
 * @param id Record id
327
 */
328
    private NameValuePair[] getEbRIMRequestParameters(String id) {        
329
        NameValuePair nvp1 = new NameValuePair("request", "getExtrinsicContent");
330
        NameValuePair nvp2 = new NameValuePair("id", id);
331
        return new NameValuePair[] { nvp1, nvp2 };
332
    } 
333

    
334
/**
335
 * This function returns the number of records that have been retrieved.
336
 * It Reads a Node value.
337
 * 
338
 * 
339
 * @return The number of records
340
 * @param node The answer tree
341
 */
342
    private int getNumberOfRecords(XMLNode node) {        
343
            int numberOfRecords = getNumberOfRecords(node,"csw:SearchResults","numberOfRecordsMatched");
344
            if (numberOfRecords == -1)
345
                    numberOfRecords = getNumberOfRecords(node,"SearchResults","numberOfRecordsMatched");
346
            
347
            return numberOfRecords;
348
    } 
349

    
350
/**
351
 * This function finds and separates metadata from a full tree
352
 * 
353
 * 
354
 * @return An array of trees that contain metadata.
355
 * @param node full tree
356
 */
357
    private XMLNode[] cutMetadata(XMLNode node) {        
358
        XMLNode[] auxNodes = XMLTree.searchMultipleNode(node,
359
                "csw:SearchResults->brief:MD_Metadata");
360
        if (auxNodes.length == 0) {
361
            auxNodes = XMLTree.searchMultipleNode(node,
362
                    "SearchResults->ebrim:Organization");
363
        }
364
        if (auxNodes.length == 0) {
365
            auxNodes = XMLTree.searchMultipleNode(node,
366
                    "csw:SearchResults->csw:SummaryRecord");
367
        }
368
        if (auxNodes.length == 0) {
369
            auxNodes = XMLTree.searchMultipleNode(node,
370
                    "csw:SearchResults->csw:Record");
371
        }
372
        if (auxNodes.length == 0) {
373
            auxNodes = XMLTree.searchMultipleNode(node,
374
                    "csw:SearchResults->DS_DataSet");
375
        }
376
        
377
        if (auxNodes.length == 0) {
378
            auxNodes = XMLTree.searchMultipleNode(node,
379
                    "SearchResults->wrs:WRSExtrinsicObject");
380
        }
381
        
382
        if (auxNodes.length == 0) {
383
            auxNodes = XMLTree.searchMultipleNode(node,
384
                    "SearchResults->WRSExtrinsicObject");
385
        }        
386
        
387
        if (auxNodes.length == 0) {
388
            auxNodes = XMLTree.searchMultipleNode(node,
389
                    "csw:SearchResults->dc:metadata");
390
        }
391
        
392
        if (auxNodes.length == 0) {
393
            auxNodes = XMLTree.searchMultipleNode(node,
394
                    "csw:SearchResults->iso19115:MD_Metadata");
395
        }
396
  
397
        return auxNodes;
398
    } 
399
/*
400
     * (non-Javadoc)
401
     *
402
     * @see catalogClient.ICatalogServerDriver#setParameters(java.util.Properties)
403
     */
404

    
405
/**
406
 * 
407
 * 
408
 * 
409
 * @return 
410
 * @param nodes 
411
 * @param url 
412
 */
413
    public boolean setParameters(Collection nodes, URL url) {        
414
        return new CswCapabilitiesParser(this,url).parse((XMLNode)nodes.toArray()[0]);
415
    } 
416

    
417
/**
418
 * 
419
 * 
420
 * 
421
 * @param typeNames The typeNames to set.
422
 */
423
    public void setTypeNames(String[] typeNames) {        
424
        this.typeNames = typeNames;
425
    } 
426
/* (non-Javadoc)
427
     * @see es.gva.cit.catalogClient.drivers.ICatalogServiveDriver#isTheProtocol(java.net.URL)
428
     */
429

    
430
/**
431
 * 
432
 * 
433
 * 
434
 * @return 
435
 * @param url 
436
 */
437
    public boolean isProtocolSupported(URL url) {        
438
        return true;
439
    } 
440

    
441
/**
442
 * 
443
 * 
444
 * 
445
 * @return Returns the distributedSearch.
446
 */
447
    public String getDistributedSearch() {        
448
        return distributedSearch;
449
    } 
450

    
451
/**
452
 * 
453
 * 
454
 * 
455
 * @param distributedSearch The distributedSearch to set.
456
 */
457
    public void setDistributedSearch(String distributedSearch) {        
458
        this.distributedSearch = distributedSearch;
459
    } 
460

    
461
/**
462
 * 
463
 * 
464
 * 
465
 * @return Returns the hopCount.
466
 */
467
    public String getHopCount() {        
468
        return hopCount;
469
    } 
470

    
471
/**
472
 * 
473
 * 
474
 * 
475
 * @param hopCount The hopCount to set.
476
 */
477
    public void setHopCount(String hopCount) {        
478
        this.hopCount = hopCount;
479
    } 
480

    
481
/**
482
 * 
483
 * 
484
 * 
485
 * @return Returns the nAMESPACE.
486
 */
487
    public String[] getNAMESPACE() {        
488
        return NAMESPACE;
489
    } 
490

    
491
/**
492
 * 
493
 * 
494
 * 
495
 * @param namespace The nAMESPACE to set.
496
 */
497
    public void setNAMESPACE(String[] namespace) {        
498
        NAMESPACE = namespace;
499
    } 
500

    
501
/**
502
 * 
503
 * 
504
 * 
505
 * @return Returns the responseHandler.
506
 */
507
    public String getResponseHandler() {        
508
        return responseHandler;
509
    } 
510

    
511
/**
512
 * 
513
 * 
514
 * 
515
 * @param responseHandler The responseHandler to set.
516
 */
517
    public void setResponseHandler(String responseHandler) {        
518
        this.responseHandler = responseHandler;
519
    } 
520

    
521
/**
522
 * 
523
 * 
524
 * 
525
 * @return Returns the resultType.
526
 */
527
    public String[] getResultType() {        
528
        return resultType;
529
    } 
530

    
531
/**
532
 * 
533
 * 
534
 * 
535
 * @param resultType The resultType to set.
536
 */
537
    public void setResultType(String[] resultType) {        
538
        this.resultType = resultType;
539
    } 
540

    
541
/**
542
 * 
543
 * 
544
 * 
545
 * @return Returns the typeNames.
546
 */
547
    public String[] getTypeNames() {        
548
        return typeNames;
549
    } 
550

    
551
/**
552
 * 
553
 * 
554
 * 
555
 * @return Returns the version.
556
 */
557
    public String getVersion() {        
558
        return version;
559
    } 
560

    
561
/**
562
 * 
563
 * 
564
 * 
565
 * @param version The version to set.
566
 */
567
    public void setVersion(String version) {        
568
        this.version = version;
569
    } 
570

    
571
/**
572
 * 
573
 * 
574
 * 
575
 * @return Returns the constraint.
576
 */
577
    public String getConstraint() {        
578
        return constraint;
579
    } 
580

    
581
/**
582
 * 
583
 * 
584
 * 
585
 * @param constraint The constraint to set.
586
 */
587
    public void setConstraint(String constraint) {        
588
        this.constraint = constraint;
589
    } 
590

    
591
/**
592
 * 
593
 * 
594
 * 
595
 * @return Returns the cONSTRAINTLANGUAGE.
596
 */
597
    public String[] getCONSTRAINTLANGUAGE() {        
598
        return CONSTRAINTLANGUAGE;
599
    } 
600

    
601
/**
602
 * 
603
 * 
604
 * 
605
 * @param constraintlanguage The cONSTRAINTLANGUAGE to set.
606
 */
607
    public void setCONSTRAINTLANGUAGE(String[] constraintlanguage) {        
608
        CONSTRAINTLANGUAGE = constraintlanguage;
609
    } 
610

    
611
/**
612
 * 
613
 * 
614
 * 
615
 * @return Returns the elementSetName.
616
 */
617
    public String[] getElementSetName() {        
618
        return elementSetName;
619
    } 
620

    
621
/**
622
 * 
623
 * 
624
 * 
625
 * @param elementSetName The elementSetName to set.
626
 */
627
    public void setElementSetName(String[] elementSetName) {        
628
        this.elementSetName = elementSetName;
629
    } 
630

    
631
/**
632
 * 
633
 * 
634
 * 
635
 * @return Returns the operations.
636
 */
637
    public CswSupportedProtocolOperations getOperations() {        
638
                return operations;
639
    } 
640

    
641
/**
642
 * 
643
 * 
644
 * 
645
 * @param operations The operations to set.
646
 */
647
    public void setOperations(CswSupportedProtocolOperations operations) {        
648
                this.operations = operations;
649
    } 
650
/* (non-Javadoc)
651
     * @see es.gva.cit.catalogClient.drivers.ICatalogServiceDriver#getServerPofile()
652
     */
653

    
654
/**
655
 * 
656
 * 
657
 * 
658
 * @return 
659
 */
660
    public String getQueryProfile() {        
661
        // TODO Auto-generated method stub
662
        return getServerProfile();
663
    }
664

    
665
public String getService() {
666
        return service;
667
}
668

    
669
public void setService(String service) {
670
        this.service = service;
671
} 
672
 }