Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appCatalogYNomenclatorClient / src / es / gva / cit / catalogClient / csw / parsers / CswCapabilitiesParser.java @ 3613

History | View | Annotate | Download (14.4 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.parsers;
43
import es.gva.cit.catalogClient.csw.drivers.CSWCatalogServiceDriver;
44
import es.gva.cit.catalogClient.metadataXML.XMLNode;
45
import es.gva.cit.catalogClient.metadataXML.XMLTree;
46
import es.gva.cit.catalogClient.schemas.Schemas;
47
import es.gva.cit.catalogClient.schemas.discoverer.SOAPMessageParser;
48
import es.gva.cit.catalogClient.utils.Strings;
49
import java.net.URL;
50
import java.util.Vector;
51

    
52
/**
53
 * 
54
 * 
55
 * 
56
 * @author Jorge Piera Llodra (piera_jor@gva.es)
57
 */
58
public class CswCapabilitiesParser {
59
/**
60
 * 
61
 * 
62
 */
63
    private CSWCatalogServiceDriver driver;
64

    
65
/**
66
 * 
67
 * 
68
 */
69
    private URL url;
70
/**
71
 * 
72
 * 
73
 */
74
    private XMLNode rootNode;
75

    
76
/**
77
 * 
78
 * 
79
 * 
80
 * @param driver Protocol Driver
81
 * @param url Server Protocol
82
 */
83
    public  CswCapabilitiesParser(CSWCatalogServiceDriver driver, URL url) {        
84
        this.driver = driver;
85
        this.url = url;        
86
    } 
87

    
88
/**
89
 * 
90
 * 
91
 * 
92
 * @return 
93
 * @param node 
94
 */
95
    public boolean parse(XMLNode node) {        
96
        rootNode = node;
97
        
98
        String prefix = "";
99
        XMLNode[] operations = null;
100
        if (httpServerIsReady() == false){
101
            return false;
102
        }
103
        
104
        if (soapServerIsReady() == false){
105
            return false;
106
        }
107
       
108
        
109
        discoverServerProfile(node);
110
        
111
        if (driver.getServerProfile().equals(Schemas.EBRIM)){
112
            boolean b = new CswIDECCapabilitiesParser(driver).parse();
113
            if (b){
114
                setWelcomeMessage(node,"");
115
            }
116
            return b;
117
        }        
118
                        
119
        //Vamos a recorrer todas las operaciones. Para cada una de ellas
120
        //tomaremos las acciones correspondientes
121
        operations = XMLTree.searchMultipleNode(node,
122
                "ows:OperationsMetadata->ows:Operation");
123
        if (!(operations.length == 0)) {
124
            prefix = "ows:";
125
        } else {
126
            operations = XMLTree.searchMultipleNode(node,
127
                    "OperationsMetadata->Operation");
128
        }
129
        if (operations == null) {
130
            driver.setServerAnswerReady("errorNotCSWSupportedProtocol");
131
            
132
            return false;
133
        }
134
        if (this.driver.getCommunicationProtocol().equals("GET") &&
135
                getHTTPExceptions(node)) {
136
            return false;
137
        }
138
        if (this.driver.getCommunicationProtocol().equals("SOAP") &&
139
                getSOAPExceptions(node)) {
140
            return false;
141
        }
142
        
143
        //To save the protocols supported by each operation
144
        driver.setOperations(new CswSupportedProtocolOperations());
145
        
146
        //Operations name
147
        String sOperation;
148
        
149
        for (int i = 0; i < operations.length; i++) {
150
            sOperation = XMLTree.searchAtribute(operations[i], "name");
151
            //////////////////Get Capabilities////////////////////
152
            if (sOperation.equals("GetCapabilities") ||
153
                    sOperation.equals("CSW-Discovery.getCapabilities")) {
154
                parseCapabilities(operations[i], prefix);
155
            }
156
            ////////////////////DescribeRecord//////////////////
157
            if (sOperation.equals("DescribeRecord")) {
158
                parseDescribeRecord(operations[i], prefix);
159
            }
160
            ////////////////////GetDomain//////////////////
161
            if (sOperation.equals("GetDomain")) {
162
                parseGetDomain(operations[i], prefix);
163
            }
164
            ////////////////////GetRecords//////////////////
165
            if (sOperation.equals("GetRecords") ||
166
                    sOperation.equals("CSW-Discovery.getRecords")) {
167
                if (!(parseGetRecords(operations[i], prefix))) {
168
                    return false;
169
                }
170
            }
171
            ////////////////////GetRecordsById//////////////////
172
            if (sOperation.equals("GetRecordsById") ||
173
                    sOperation.equals("CSW-Discovery.getRecordById")) {
174
                parseGetRecordsByID(operations[i], prefix);
175
            }
176
            ////////////////////Transaction//////////////////
177
            if (sOperation.equals("Transaction") ||
178
                    sOperation.equals("CSW-Publication.transaction")) {
179
                parseTransaction(operations[i], prefix);
180
            }
181
            ////////////////////Harvest//////////////////
182
            if (sOperation.equals("Harvest") ||
183
                    sOperation.equals("CSW-Publication.harvest")) {
184
                parseHarvest(operations[i], prefix);
185
            }
186
        }      
187
        setWelcomeMessage(node,prefix);
188
        setConstantValues();
189
        return true;
190
    } 
191

    
192
/**
193
 * This method is used to detect some common problems when the
194
 * used protocol has been HTTP
195
 * 
196
 * 
197
 * @return true if all is OK
198
 */
199
    private boolean httpServerIsReady() {        
200
        if (rootNode == null){
201
            driver.setServerAnswerReady("errorNotSupportedProtocol");
202
            return false;
203
        }
204
        
205
        if (rootNode.getName().equals("SRW:explainResponse")){
206
            driver.setServerAnswerReady("errorIsASRWServer");
207
            return false;
208
        }  
209
        
210
        if (rootNode.getName().equals("ows:ServiceExceptionReport")){
211
            driver.setServerAnswerReady("errorServerException");
212
            return false;
213
        }  
214
        return true;
215
    } 
216

    
217
/**
218
 * This method is used to detect some common problems when the
219
 * used protocol has been SOAP
220
 * 
221
 * 
222
 * @return true if all is OK
223
 */
224
    private boolean soapServerIsReady() {        
225
        if (XMLTree.searchNode(rootNode,"SOAP-ENV:Body->SOAP-ENV:Fault") != null){
226
            driver.setServerAnswerReady("errorSOAPProtocol");
227
            return false;
228
        }  
229
        return true;
230
    } 
231

    
232
/**
233
 * This method is used to discover the CSW server profile. The value
234
 * can be ebRIM or ISO19115
235
 * 
236
 * 
237
 * @param node 
238
 */
239
    private void discoverServerProfile(XMLNode node) {        
240
        if ((url.getHost().equals("delta.icc.es")) || 
241
                (url.getHost().equals("indicio.demo.galdosinc.com"))){   
242
            driver.setServerProfile(Schemas.EBRIM);
243
        }else{
244
            driver.setServerProfile(Schemas.ISO19115);
245
        }
246
        
247
    } 
248

    
249
/**
250
 * Sets the welcome message
251
 * 
252
 * 
253
 * @param node 
254
 * @param prefix 
255
 */
256
    private void setWelcomeMessage(XMLNode node, String prefix) {        
257
        //Escribimos el mensaje de BienVenida
258
        driver.setServerAnswerReady(XMLTree.searchNodeValue(node,
259
                prefix + "ServiceIdentification->" + prefix + "Title") + "\n" +
260
            XMLTree.searchNodeValue(node,
261
                prefix + "ServiceIdentification->" + prefix + "Abstract"));
262
    } 
263

    
264
/**
265
 * Sets some constant values
266
 * 
267
 */
268
    private void setConstantValues() {        
269
        driver.setSortBy("true");
270
        driver.setStartPosition("1");
271
        driver.setMaxRecords("10");
272
    } 
273

    
274
/**
275
 * 
276
 * 
277
 * 
278
 * @param node 
279
 * @param prefix 
280
 */
281
    private void parseCapabilities(XMLNode node, String prefix) {        
282
            driver.getOperations().setGetCapabilities(setSupportedProtocols(node,prefix));
283
    } 
284

    
285
/**
286
 * 
287
 * 
288
 * 
289
 * @param node 
290
 * @param prefix 
291
 */
292
    private void parseDescribeRecord(XMLNode node, String prefix) {        
293
            driver.getOperations().setDescribeRecords(setSupportedProtocols(node,prefix));
294
    } 
295

    
296
/**
297
 * 
298
 * 
299
 * 
300
 * @param node 
301
 * @param prefix 
302
 */
303
    private void parseGetRecordsByID(XMLNode node, String prefix) {        
304
            driver.getOperations().setGetRecordsById(setSupportedProtocols(node,prefix));
305
    } 
306

    
307
/**
308
 * 
309
 * 
310
 * 
311
 * @param node 
312
 * @param prefix 
313
 */
314
    private void parseGetDomain(XMLNode node, String prefix) {        
315
            driver.getOperations().setGetDomain(setSupportedProtocols(node,prefix));
316
    } 
317

    
318
/**
319
 * 
320
 * 
321
 * 
322
 * @return 
323
 * @param node 
324
 * @param prefix 
325
 */
326
    private boolean parseGetRecords(XMLNode node, String prefix) {        
327
            driver.getOperations().setGetRecords(setSupportedProtocols(node,prefix));
328
            
329
            XMLNode[] parameters;
330
        //Alamacenamos los parametros y los escribimos
331
        parameters = XMLTree.searchMultipleNode(node, prefix + "Parameter");
332
        for (int j = 0; j < parameters.length; j++) {
333
            String sParameter = XMLTree.searchAtribute(parameters[j], "name");
334
            String[] values = XMLTree.searchMultipleNodeValue(parameters[j],
335
                    prefix + "Value");
336
            String[] defaultValue = XMLTree.searchMultipleNodeValue(parameters[j],
337
                    prefix + "DefaultValue");
338
            if (sParameter.equals("TypeName")) {
339
                driver.setTypeNames(Strings.join(defaultValue, values));
340
            }
341
            if (sParameter.equals("outputFormat")) {
342
                if (!(Strings.find("text/xml", defaultValue) ||
343
                        Strings.find("text/xml", values))) {
344
                    if (!(Strings.find("application/xml", defaultValue) ||
345
                            Strings.find("application/xml", values))) {
346
                        driver.setServerAnswerReady("El servidor no devuelve " +
347
                            "texto en formato XML");
348
                        return false;
349
                    }
350
                }
351
            }
352
            driver.setOutputFormat("text/xml");
353
            if (sParameter.equals("outputSchema")) {
354
                driver.setOutputSchema(Strings.join(defaultValue, values));
355
                driver.setServerProfile(Schemas.DUBLINCORE);
356
                for (int i=0 ; i<driver.getOutputSchema().length ; i++){
357
                    if (driver.getOutputSchema()[i].equals(Schemas.ISO19115))
358
                        driver.setServerProfile(Schemas.ISO19115);
359
                            break;
360
                }
361
            }
362
            
363
           
364
            if (sParameter.equals("resultType")) {
365
                driver.setResultType(Strings.join(defaultValue, values));
366
            }
367
            if (sParameter.equals("ElementSetName")) {
368
                driver.setElementSetName(Strings.join(defaultValue, values));
369
            }
370
            if (sParameter.equals("CONSTRAINTLANGUAGE")) {
371
                driver.setCONSTRAINTLANGUAGE(Strings.join(defaultValue, values));
372
                for (int i = 0; i < driver.getCONSTRAINTLANGUAGE().length;
373
                        i++) {
374
                    if (driver.getCONSTRAINTLANGUAGE()[i].toUpperCase().equals("FILTER")) {
375
                        break;
376
                    } else {
377
                        driver.setServerAnswerReady("errorFENotSupported");
378
                            
379
                    }
380
                }
381
            }
382
        }
383
        return true;
384
    } 
385

    
386
/**
387
 * 
388
 * 
389
 * 
390
 * @param node 
391
 * @param prefix 
392
 */
393
    private void parseTransaction(XMLNode node, String prefix) {        
394
            driver.getOperations().setTransaction(setSupportedProtocols(node,prefix));
395
    } 
396

    
397
/**
398
 * 
399
 * 
400
 * 
401
 * @param node 
402
 * @param prefix 
403
 */
404
    private void parseHarvest(XMLNode node, String prefix) {        
405
            driver.getOperations().setHarvest(setSupportedProtocols(node,prefix));
406
    } 
407

    
408
/**
409
 * This function returns the supported protocols of an operation
410
 * defined by node.
411
 * 
412
 * 
413
 * @return 
414
 * @param node Node that contains the operation information
415
 * @param prefix getCapabilities tag prefix
416
 */
417
    private String[] setSupportedProtocols(XMLNode node, String prefix) {        
418
            Vector operations = new Vector();
419
            
420
            XMLNode protocols = XMLTree.searchNode(node, prefix + "DCP");
421
            
422
            XMLNode HTTPoperations = XMLTree.searchNode(protocols, prefix + "HTTP");
423
            XMLNode[] childNodes = HTTPoperations.getSubnodes();
424
            
425
            for (int i=0 ; i<childNodes.length ; i++){
426
                    if ((childNodes[i].getName().equals("Get")) ||
427
                                    (childNodes[i].getName().equals(prefix + "Get"))){
428
                            operations.add(new String("GET"));
429
                    }
430
                    if ((childNodes[i].getName().equals("Post")) ||
431
                                    (childNodes[i].getName().equals(prefix + "Post"))){
432
                            operations.add(new String("POST"));
433
                    }
434
                    
435
            }
436
            
437
            String[] output = new String[operations.size()];
438
            for (int i=0 ; i<operations.size() ; i++)
439
                    output[i] = (String) operations.get(i);            
440
            
441
            return output;
442
    } 
443

    
444
/**
445
 * Parses the HTTP-CSW Exceptions
446
 * 
447
 * 
448
 * @return 
449
 * @param node 
450
 */
451
    private boolean getHTTPExceptions(XMLNode node) {        
452
        if (node.getName().equals("ServiceExceptionReport")) {
453
            driver.setServerAnswerReady(XMLTree.searchNodeValue(node,
454
                    "ServiceException"));
455
            return true;
456
        }
457
        return false;
458
    } 
459

    
460
/**
461
 * It returns the exception code
462
 * 
463
 * 
464
 * @return 
465
 * @param node Returned node.
466
 */
467
    public String getExceptionCode(XMLNode node) {        
468
        String code = XMLTree.searchNodeAtribute(node,"ows:OWSException","code");
469
        if (code == null)
470
            return "0";
471
        return code;
472
    } 
473

    
474
/**
475
 * It returns the exception Subcode
476
 * 
477
 * 
478
 * @return 
479
 * @param node Returned node.
480
 */
481
    public String getExceptionSubCode(XMLNode node) {        
482
        String code = XMLTree.searchNodeAtribute(node,"ows:OWSException","subcode");
483
        if (code == null)
484
            return "0";
485
        return code;
486
    
487
    } 
488

    
489
/**
490
 * Parses the SOAP-CSW Exceptions
491
 * 
492
 * 
493
 * @return 
494
 * @param node 
495
 */
496
    private boolean getSOAPExceptions(XMLNode node) {        
497
        String msg = SOAPMessageParser.getFault(node);
498
        if (msg != null) {
499
            driver.setServerAnswerReady(msg);
500
            return true;
501
        }
502
        return false;
503
    } 
504
 }