Statistics
| Revision:

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

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

    
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

    
55
import org.apache.commons.httpclient.NameValuePair;
56

    
57
import java.net.URL;
58
/**
59
 * This class implements the CSW protocol.
60
 * 
61
 * @see http://portal.opengeospatial.org/files/?artifact_id=5929&version=1
62
 * @author Jorge Piera Llodra (piera_jor@gva.es)
63
 */
64
public class CSWCatalogServiceDriver extends AbstractCatalogServiceDriver {
65
    private String responseHandler = null;
66
    private String hopCount = null;
67
    private String distributedSearch = null;
68
    private String constraint = null;
69
    private String[] CONSTRAINTLANGUAGE = null;
70
    private String[] elementSetName = null;
71
    private String[] typeNames = null;
72
    private String[] resultType = null;
73
    private String[] NAMESPACE = null;
74
  private String version = "2.0.0";
75
    private CswSupportedProtocolOperations operations = null;
76

    
77
    /*
78
     * (non-Javadoc)
79
     *
80
     * @see ICatalogServerDriver#GetCapabilities(java.lang.String,
81
     *      java.lang.String)
82
     */
83
    public XMLNode[] getCapabilities(URL url) {
84
        XMLNode[] nodes = null;
85
        CSWMessages messages = new CSWMessages(this);
86
        CswCapabilitiesParser parser = new CswCapabilitiesParser(this,url);
87

    
88
        System.out.println("**************GET*************");
89
        nodes = new HTTPGetProtocol().doQuery(url, messages.getHTTPGETCapabilities(true), 0);
90

    
91
        if ((nodes != null) && (parser.getExceptionCode(nodes[0]).equals("8001")) &&
92
                (parser.getExceptionSubCode(nodes[0]).equals("5002")))
93
            nodes = new HTTPGetProtocol().doQuery(url, messages.getHTTPGETCapabilities(false), 0);
94
        
95
        if (nodes == null) {
96
            System.out.println("**************POST*************");
97
            nodes = new HTTPPostProtocol().doQuery(url,
98
                    messages.getHTTPPOSTCapabilities(), 0);
99

    
100
            if (nodes == null) {
101
                System.out.println("**************SOAP*************");
102
                nodes = new SOAPProtocol().doQuery(url,
103
                        messages.getSOAPCapabilities(), 0);
104

    
105
                if (nodes == null) {
106
                    return null;
107
                } else {
108
                    setCommunicationProtocol("SOAP");
109
                }
110
            } else {
111
                setCommunicationProtocol("POST");
112
            }
113
        } else {
114
            setCommunicationProtocol("GET");
115
        }
116

    
117
        return nodes;
118
    }
119

    
120
    /**
121
     * this method implements the describeRecords operation
122
     * @param url
123
     * @return
124
     */
125
    public XMLNode[] describeRecords(URL url) {
126
        //return new HTTPPostProtocol().doQuery(url, getMessageDescribeRecords(),0);
127
        return null;
128
    }
129

    
130
    /*
131
     * (non-Javadoc)
132
     *
133
     * @see ICatalogServerDriver#GetRecords(java.lang.String, java.lang.String,
134
     *      java.lang.String, int, int, java.lang.String, java.lang.String)
135
     */
136
    public XMLNode[] getRecords(URL url, Query query, int firstRecord) {
137
        setQuery(query);
138
        CSWMessages messages = new CSWMessages(this);
139
        XMLNode[] answerNodes = null;
140
        XMLNode[] nodes = null;
141
        
142
        if (getOperations().getGetRecords()==null)
143
                return null;
144
      
145
        if (getServerProfile().equals(Schemas.ISO19115)){
146
            firstRecord = firstRecord - 1;
147
        }
148
        
149
        for (int i=0 ; i<getOperations().getGetRecords().length; i++){
150
            /*
151
            if (getOperations().getGetRecords()[i].equals("GET")){
152
                    System.out.println("**************GET*************");
153
                    nodes = new HTTPGetProtocol().doQuery(url,
154
                        messages.getHTTPGETRecords(getQuery() , firstRecord), firstRecord);
155
                    
156
                    setCommunicationProtocol("GET");                    
157
                break;
158
            }
159
            */
160
           
161
            if (getOperations().getGetRecords()[i].equals("POST")){
162
                    System.out.println("**************POST*************");
163
                System.out.println(messages.getHTTPPOSTRecords(getQuery(), firstRecord));                    
164
                    nodes = new HTTPPostProtocol().doQuery(url,
165
                        messages.getHTTPPOSTRecords(getQuery(), firstRecord), firstRecord);
166
                
167
                setCommunicationProtocol("POST");                
168
                break;
169
            }
170
            
171
            /*
172
            if (getOperations().getGetRecords()[i].equals("SOAP")){
173
                         System.out.println("**************SOAP*************");
174
                         nodes = new SOAPProtocol().doQuery(url,
175
                        messages.getSOAPRecords(getQuery(), firstRecord), firstRecord);
176
                         
177
                         setCommunicationProtocol("SOAP");
178

179
                 //We must cut the SOAP Head
180
                 nodes[0] = SOAPMessageParser.cutHead(nodes[0]);
181
                         break;
182
                } */ 
183
            
184
            
185
        }
186
       
187
        if (nodes == null) 
188
                return null;
189
         
190
        int numberOfRecords = getNumberOfRecords(nodes[0]);
191

    
192
        if (numberOfRecords == -1) {
193
            return null;
194
        }
195

    
196
        answerNodes = createAnswerTree(numberOfRecords, firstRecord, nodes[0],
197
                url);
198

    
199
        return answerNodes;
200
    }
201

    
202
    /**
203
     * It returns an array of nodes with:
204
     *         1st position -> tree with information of the answer
205
     *  Next positions -> One result for each position
206
     * @param numberOfRecords
207
     * Number of records returned by the query
208
     * @param firstRecord
209
     * Number of the first record
210
     * @param node
211
     * Tree returned by the server
212
     * @param url
213
     * Server URL
214
     * @return
215
     */
216
    private XMLNode[] createAnswerTree(int numberOfRecords, int firstRecord,
217
        XMLNode node, URL url) {
218
        XMLNode[] answerNodes = null;
219

    
220
        if (numberOfRecords > 10) {
221
            answerNodes = new XMLNode[11];
222
        } else {
223
            answerNodes = new XMLNode[numberOfRecords + 1];
224
        }
225

    
226
        answerNodes[0] = XMLTreeNumberOfRecordsAnswer.getNode(numberOfRecords,
227
                firstRecord, firstRecord + numberOfRecords);
228

    
229
        XMLNode[] auxNodes = cutMetadata(node);
230

    
231
        if (getServerProfile().equals(Schemas.EBRIM)) {
232
            auxNodes = getEbRIMNodes(auxNodes, url);
233
        }
234

    
235
        for (int i = 1;
236
                (i <= numberOfRecords) && (i <= 10) &&
237
                (i <= (numberOfRecords - firstRecord + 1)); i++)
238
            answerNodes[i] = auxNodes[i - 1];
239

    
240
        return answerNodes;
241
    }
242

    
243
    /**
244
     * This function retrieve the nodes for one ebRIM answer
245
     * @param nodes
246
     * Nodes of the ebRIM answer
247
     * @param nodes
248
     * Server URL
249
     * @return
250
     * Medatada Nodes.
251
     */
252
    private XMLNode[] getEbRIMNodes(XMLNode[] nodes, URL url) {
253
        XMLNode[] auxNodes = new XMLNode[nodes.length];
254

    
255
        for (int i = 0; i < nodes.length; i++) {
256
            String id = XMLTree.searchAtribute(nodes[i], "id");
257
            auxNodes[i] = new HTTPGetProtocol().doQuery(url,
258
                    getEbRIMRequestParameters(id), 0)[0];
259
        }
260

    
261
        return auxNodes;
262
    }
263

    
264
    /**
265
     * It Returns the parameters needed by getExtrinsicContent
266
     * @param id
267
     * Record id
268
     * @return
269
     */
270
    private NameValuePair[] getEbRIMRequestParameters(String id) {
271
        NameValuePair nvp1 = new NameValuePair("request", "getExtrinsicContent");
272
        NameValuePair nvp2 = new NameValuePair("id", id);
273

    
274
        return new NameValuePair[] { nvp1, nvp2 };
275
    }
276
    
277
    /**
278
     * This function returns the number of records that have been retrieved.
279
     * It Reads a Node value.
280
     * @param node
281
     * The answer tree
282
     * @return
283
     * The number of records
284
     */
285
    private int getNumberOfRecords(XMLNode node){
286
            int numberOfRecords = getNumberOfRecords(node,"csw:SearchResults","numberOfRecordsMatched");
287
            if (numberOfRecords == -1)
288
                    numberOfRecords = getNumberOfRecords(node,"SearchResults","numberOfRecordsMatched");
289
            
290
            return numberOfRecords;
291
    }
292

    
293
   
294
    /**
295
     * This function finds and separates metadata from a full tree
296
     * @param node
297
     * full tree
298
     * @return
299
     * An array of trees that contain metadata.
300
     */
301
    private XMLNode[] cutMetadata(XMLNode node) {
302
        XMLNode[] auxNodes = XMLTree.searchMultipleNode(node,
303
                "csw:SearchResults->brief:MD_Metadata");
304

    
305
        if (auxNodes.length == 0) {
306
            auxNodes = XMLTree.searchMultipleNode(node,
307
                    "SearchResults->ebrim:Organization");
308
        }
309

    
310
        if (auxNodes.length == 0) {
311
            auxNodes = XMLTree.searchMultipleNode(node,
312
                    "SearchResults->wrs:WRSExtrinsicObject");
313
        }
314
        
315
        if (auxNodes.length == 0) {
316
            auxNodes = XMLTree.searchMultipleNode(node,
317
                    "SearchResults->WRSExtrinsicObject");
318
        }        
319
        
320
        if (auxNodes.length == 0) {
321
            auxNodes = XMLTree.searchMultipleNode(node,
322
                    "csw:SearchResults->dc:metadata");
323
        }
324
        
325
        if (auxNodes.length == 0) {
326
            auxNodes = XMLTree.searchMultipleNode(node,
327
                    "csw:SearchResults->iso19115:MD_Metadata");
328
        }
329
  
330
        return auxNodes;
331
    }
332
 
333

    
334

    
335
    /*
336
     * (non-Javadoc)
337
     *
338
     * @see catalogClient.ICatalogServerDriver#setParameters(java.util.Properties)
339
     */
340
    public boolean setParameters(XMLNode[] nodes,URL url) {
341
        return new CswCapabilitiesParser(this,url).parse(nodes[0]);
342
    }
343
    
344
    /**
345
     * @param typeNames
346
     * The typeNames to set.
347
     */
348
    public void setTypeNames(String[] typeNames) {
349
        this.typeNames = typeNames;
350

    
351
    }
352
    
353
    /* (non-Javadoc)
354
     * @see es.gva.cit.catalogClient.drivers.ICatalogServiveDriver#isTheProtocol(java.net.URL)
355
     */
356
    public boolean isProtocolSupported(URL url) {
357
        return true;
358
    }
359

    
360
    /**
361
     * @return Returns the distributedSearch.
362
     */
363
    public String getDistributedSearch() {
364
        return distributedSearch;
365
    }
366

    
367
    /**
368
     * @param distributedSearch
369
     *            The distributedSearch to set.
370
     */
371
    public void setDistributedSearch(String distributedSearch) {
372
        this.distributedSearch = distributedSearch;
373
    }
374

    
375
    /**
376
     * @return Returns the hopCount.
377
     */
378
    public String getHopCount() {
379
        return hopCount;
380
    }
381

    
382
    /**
383
     * @param hopCount
384
     *            The hopCount to set.
385
     */
386
    public void setHopCount(String hopCount) {
387
        this.hopCount = hopCount;
388
    }
389

    
390
    /**
391
     * @return Returns the nAMESPACE.
392
     */
393
    public String[] getNAMESPACE() {
394
        return NAMESPACE;
395
    }
396

    
397
    /**
398
     * @param namespace
399
     *            The nAMESPACE to set.
400
     */
401
    public void setNAMESPACE(String[] namespace) {
402
        NAMESPACE = namespace;
403
    }
404

    
405
    /**
406
     * @return Returns the responseHandler.
407
     */
408
    public String getResponseHandler() {
409
        return responseHandler;
410
    }
411

    
412
    /**
413
     * @param responseHandler
414
     *            The responseHandler to set.
415
     */
416
    public void setResponseHandler(String responseHandler) {
417
        this.responseHandler = responseHandler;
418
    }
419

    
420
    /**
421
     * @return Returns the resultType.
422
     */
423
    public String[] getResultType() {
424
        return resultType;
425
    }
426

    
427
    /**
428
     * @param resultType
429
     *            The resultType to set.
430
     */
431
    public void setResultType(String[] resultType) {
432
        this.resultType = resultType;
433
    }
434

    
435
    /**
436
     * @return Returns the typeNames.
437
     */
438
    public String[] getTypeNames() {
439
        return typeNames;
440
    }
441
    /**
442
     * @return Returns the version.
443
     */
444
    public String getVersion() {
445
        return version;
446
    }
447

    
448
    /**
449
     * @param version
450
     *            The version to set.
451
     */
452
    public void setVersion(String version) {
453
        this.version = version;
454
    }
455

    
456
    /**
457
     * @return Returns the constraint.
458
     */
459
    public String getConstraint() {
460
        return constraint;
461
    }
462

    
463
    /**
464
     * @param constraint
465
     *            The constraint to set.
466
     */
467
    public void setConstraint(String constraint) {
468
        this.constraint = constraint;
469
    }
470

    
471
    /**
472
     * @return Returns the cONSTRAINTLANGUAGE.
473
     */
474
    public String[] getCONSTRAINTLANGUAGE() {
475
        return CONSTRAINTLANGUAGE;
476
    }
477

    
478
    /**
479
     * @param constraintlanguage
480
     *            The cONSTRAINTLANGUAGE to set.
481
     */
482
    public void setCONSTRAINTLANGUAGE(String[] constraintlanguage) {
483
        CONSTRAINTLANGUAGE = constraintlanguage;
484
    }
485

    
486
    /**
487
     * @return Returns the elementSetName.
488
     */
489
    public String[] getElementSetName() {
490
        return elementSetName;
491
    }
492

    
493
    /**
494
     * @param elementSetName
495
     *            The elementSetName to set.
496
     */
497
    public void setElementSetName(String[] elementSetName) {
498
        this.elementSetName = elementSetName;
499
    }
500

    
501
        /**
502
         * @return Returns the operations.
503
         */
504
        public CswSupportedProtocolOperations getOperations() {
505
                return operations;
506
        }
507
        /**
508
         * @param operations The operations to set.
509
         */
510
        public void setOperations(CswSupportedProtocolOperations operations) {
511
                this.operations = operations;
512
        }
513

    
514

    
515
    /* (non-Javadoc)
516
     * @see es.gva.cit.catalogClient.drivers.ICatalogServiceDriver#getServerPofile()
517
     */
518
    public String getQueryProfile() {
519
        // TODO Auto-generated method stub
520
        return getServerProfile();
521
    }
522
}