Statistics
| Revision:

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

History | View | Annotate | Download (14 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.parsers;
42

    
43
import java.net.URL;
44
import java.util.Vector;
45

    
46
import es.gva.cit.catalogClient.csw.drivers.CSWCatalogServiceDriver;
47
import es.gva.cit.catalogClient.metadataXML.XMLNode;
48
import es.gva.cit.catalogClient.metadataXML.XMLTree;
49
import es.gva.cit.catalogClient.parsers.SOAPMessageParser;
50
import es.gva.cit.catalogClient.profiles.Profile;
51
import es.gva.cit.catalogClient.utils.Strings;
52

    
53

    
54

    
55
/**
56
 * @author Jorge Piera Llodra (piera_jor@gva.es)
57
 */
58
public class CswCapabilitiesParser {
59
    private CSWCatalogServiceDriver driver;
60
    private URL url;
61
    private XMLNode rootNode;
62
    
63
       
64
    /**
65
     * @param driver
66
     * Protocol Driver
67
     * @param url
68
     * Server Protocol
69
    
70
     */
71
    public CswCapabilitiesParser(CSWCatalogServiceDriver driver,URL url) {
72
        this.driver = driver;
73
        this.url = url;        
74
    }
75

    
76
    public boolean parse(XMLNode node) {
77
        rootNode = node;
78
        
79
        String prefix = "";
80
        XMLNode[] operations = null;
81

    
82
        if (httpServerIsReady() == false){
83
            return false;
84
        }
85
        
86
        if (soapServerIsReady() == false){
87
            return false;
88
        }
89
       
90
        
91
        discoverServerProfile(node);
92
        
93
        if (driver.getServerProfile().equals(Profile.EBRIM)){
94
            boolean b = new CswIDECCapabilitiesParser(driver).parse();
95
            if (b){
96
                setWelcomeMessage(node,"");
97
            }
98
            return b;
99
        }        
100
                        
101
        //Vamos a recorrer todas las operaciones. Para cada una de ellas
102
        //tomaremos las acciones correspondientes
103
        operations = XMLTree.searchMultipleNode(node,
104
                "ows:OperationsMetadata->ows:Operation");
105

    
106
        if (!(operations.length == 0)) {
107
            prefix = "ows:";
108
        } else {
109
            operations = XMLTree.searchMultipleNode(node,
110
                    "OperationsMetadata->Operation");
111
        }
112

    
113
        if (operations == null) {
114
            driver.setServerAnswerReady("errorNotCSWSupportedProtocol");
115
            
116
            return false;
117
        }
118

    
119
        if (this.driver.getCommunicationProtocol().equals("GET") &&
120
                getHTTPExceptions(node)) {
121
            return false;
122
        }
123

    
124
        if (this.driver.getCommunicationProtocol().equals("SOAP") &&
125
                getSOAPExceptions(node)) {
126
            return false;
127
        }
128
        
129
        //To save the protocols supported by each operation
130
        driver.setOperations(new CswSupportedProtocolOperations());
131
        
132
        //Operations name
133
        String sOperation;
134
        
135
        for (int i = 0; i < operations.length; i++) {
136
            sOperation = XMLTree.searchAtribute(operations[i], "name");
137

    
138
            //////////////////Get Capabilities////////////////////
139
            if (sOperation.equals("GetCapabilities") ||
140
                    sOperation.equals("CSW-Discovery.getCapabilities")) {
141
                parseCapabilities(operations[i], prefix);
142
            }
143

    
144
            ////////////////////DescribeRecord//////////////////
145
            if (sOperation.equals("DescribeRecord")) {
146
                parseDescribeRecord(operations[i], prefix);
147
            }
148

    
149
            ////////////////////GetDomain//////////////////
150
            if (sOperation.equals("GetDomain")) {
151
                parseGetDomain(operations[i], prefix);
152
            }
153

    
154
            ////////////////////GetRecords//////////////////
155
            if (sOperation.equals("GetRecords") ||
156
                    sOperation.equals("CSW-Discovery.getRecords")) {
157
                if (!(parseGetRecords(operations[i], prefix))) {
158
                    return false;
159
                }
160
            }
161

    
162
            ////////////////////GetRecordsById//////////////////
163
            if (sOperation.equals("GetRecordsById") ||
164
                    sOperation.equals("CSW-Discovery.getRecordById")) {
165
                parseGetRecordsByID(operations[i], prefix);
166
            }
167

    
168
            ////////////////////Transaction//////////////////
169
            if (sOperation.equals("Transaction") ||
170
                    sOperation.equals("CSW-Publication.transaction")) {
171
                parseTransaction(operations[i], prefix);
172
            }
173

    
174
            ////////////////////Harvest//////////////////
175
            if (sOperation.equals("Harvest") ||
176
                    sOperation.equals("CSW-Publication.harvest")) {
177
                parseHarvest(operations[i], prefix);
178
            }
179
        }      
180

    
181
        setWelcomeMessage(node,prefix);
182
        setConstantValues();
183

    
184
        return true;
185
    }
186
    
187
    /**
188
     * This method is used to detect some common problems when the 
189
     * used protocol has been HTTP
190
     * @return
191
     * true if all is OK
192
     */
193
    private boolean httpServerIsReady(){
194
        if (rootNode == null){
195
            driver.setServerAnswerReady("errorNotSupportedProtocol");
196
            return false;
197
        }
198
        
199
        if (rootNode.getName().equals("SRW:explainResponse")){
200
            driver.setServerAnswerReady("errorIsASRWServer");
201
            return false;
202
        }  
203
        
204
        if (rootNode.getName().equals("ows:ServiceExceptionReport")){
205
            driver.setServerAnswerReady("errorServerException");
206
            return false;
207
        }  
208
        return true;
209
    }
210
    
211
    /**
212
     * This method is used to detect some common problems when the 
213
     * used protocol has been SOAP
214
     * @return
215
     * true if all is OK
216
     */
217
    private boolean soapServerIsReady(){
218
        if (XMLTree.searchNode(rootNode,"SOAP-ENV:Body->SOAP-ENV:Fault") != null){
219
            driver.setServerAnswerReady("errorSOAPProtocol");
220
            return false;
221
        }  
222
        return true;
223
    }
224
    
225
    /**
226
     * This method is used to discover the CSW server profile. The value
227
     * can be ebRIM or ISO19115
228
     * @param node
229
     */
230
    private void discoverServerProfile(XMLNode node){
231
        if ((url.getHost().equals("delta.icc.es")) || 
232
                (url.getHost().equals("indicio.demo.galdosinc.com"))){   
233
            driver.setServerProfile(Profile.EBRIM);
234
        }else{
235
            driver.setServerProfile(Profile.ISO19115);
236
        }
237
        
238
    }
239
    
240
    /**
241
     * Sets the welcome message
242
     *
243
     */
244
    private void setWelcomeMessage(XMLNode node,String prefix){
245
        //Escribimos el mensaje de BienVenida
246
        driver.setServerAnswerReady(XMLTree.searchNodeValue(node,
247
                prefix + "ServiceIdentification->" + prefix + "Title") + "\n" +
248
            XMLTree.searchNodeValue(node,
249
                prefix + "ServiceIdentification->" + prefix + "Abstract"));
250
    }
251
    
252
    
253
    /**
254
     * Sets some constant values
255
     *
256
     */
257
    private void setConstantValues() {
258
        driver.setSortBy("true");
259
        driver.setStartPosition("1");
260
        driver.setMaxRecords("10");
261
    }
262

    
263
    private void parseCapabilities(XMLNode node, String prefix) {
264
            driver.getOperations().setGetCapabilities(setSupportedProtocols(node,prefix));
265
    }
266

    
267
    private void parseDescribeRecord(XMLNode node, String prefix) {
268
            driver.getOperations().setDescribeRecords(setSupportedProtocols(node,prefix));
269
    }
270

    
271
    private void parseGetRecordsByID(XMLNode node, String prefix) {
272
            driver.getOperations().setGetRecordsById(setSupportedProtocols(node,prefix));
273
    }
274

    
275
    private void parseGetDomain(XMLNode node, String prefix) {
276
            driver.getOperations().setGetDomain(setSupportedProtocols(node,prefix));
277
    }
278

    
279
    private boolean parseGetRecords(XMLNode node, String prefix) {
280
            driver.getOperations().setGetRecords(setSupportedProtocols(node,prefix));
281
            
282
            XMLNode[] parameters;
283

    
284
        //Alamacenamos los parametros y los escribimos
285
        parameters = XMLTree.searchMultipleNode(node, prefix + "Parameter");
286

    
287
        for (int j = 0; j < parameters.length; j++) {
288
            String sParameter = XMLTree.searchAtribute(parameters[j], "name");
289

    
290
            String[] values = XMLTree.searchMultipleNodeValue(parameters[j],
291
                    prefix + "Value");
292

    
293
            String[] defaultValue = XMLTree.searchMultipleNodeValue(parameters[j],
294
                    prefix + "DefaultValue");
295

    
296
            if (sParameter.equals("TypeName")) {
297
                driver.setTypeNames(Strings.join(defaultValue, values));
298
            }
299

    
300
            if (sParameter.equals("outputFormat")) {
301
                if (!(Strings.find("text/xml", defaultValue) ||
302
                        Strings.find("text/xml", values))) {
303
                    if (!(Strings.find("application/xml", defaultValue) ||
304
                            Strings.find("application/xml", values))) {
305
                        driver.setServerAnswerReady("El servidor no devuelve " +
306
                            "texto en formato XML");
307

    
308
                        return false;
309
                    }
310
                }
311
            }
312

    
313
            driver.setOutputFormat("text/xml");
314

    
315
            if (sParameter.equals("outputSchema")) {
316
                driver.setOutputSchema(Strings.join(defaultValue, values));
317
                driver.setServerProfile(Profile.DUBLINCORE);
318
                for (int i=0 ; i<driver.getOutputSchema().length ; i++){
319
                    if (driver.getOutputSchema()[i].equals(Profile.ISO19115))
320
                        driver.setServerProfile(Profile.ISO19115);
321
                            break;
322
                }
323
            }
324
            
325
           
326

    
327
            if (sParameter.equals("resultType")) {
328
                driver.setResultType(Strings.join(defaultValue, values));
329
            }
330

    
331
            if (sParameter.equals("ElementSetName")) {
332
                driver.setElementSetName(Strings.join(defaultValue, values));
333
            }
334

    
335
            if (sParameter.equals("CONSTRAINTLANGUAGE")) {
336
                driver.setCONSTRAINTLANGUAGE(Strings.join(defaultValue, values));
337

    
338
                for (int i = 0; i < driver.getCONSTRAINTLANGUAGE().length;
339
                        i++) {
340
                    if (driver.getCONSTRAINTLANGUAGE()[i].toUpperCase().equals("FILTER")) {
341
                        break;
342
                    } else {
343
                        driver.setServerAnswerReady("errorFENotSupported");
344
                            
345
                    }
346
                }
347
            }
348
        }
349

    
350
        return true;
351
    }
352

    
353
    private void parseTransaction(XMLNode node, String prefix) {
354
            driver.getOperations().setTransaction(setSupportedProtocols(node,prefix));
355
    }
356

    
357
    private void parseHarvest(XMLNode node, String prefix) {
358
            driver.getOperations().setHarvest(setSupportedProtocols(node,prefix));
359
    }
360
    
361
    /**
362
     * This function returns the supported protocols of an operation 
363
     * defined by node.
364
     * @param node
365
     * Node that contains the operation information
366
     * @param prefix
367
     * getCapabilities tag prefix
368
     */
369
    private String[] setSupportedProtocols(XMLNode node,String prefix){
370
            Vector operations = new Vector();
371
            
372
            XMLNode protocols = XMLTree.searchNode(node, prefix + "DCP");
373
            
374
            XMLNode HTTPoperations = XMLTree.searchNode(protocols, prefix + "HTTP");
375
            XMLNode[] childNodes = HTTPoperations.getSubnodes();
376
            
377
            for (int i=0 ; i<childNodes.length ; i++){
378
                    if ((childNodes[i].getName().equals("Get")) ||
379
                                    (childNodes[i].getName().equals(prefix + "Get"))){
380
                            operations.add(new String("GET"));
381
                    }
382
                    if ((childNodes[i].getName().equals("Post")) ||
383
                                    (childNodes[i].getName().equals(prefix + "Post"))){
384
                            operations.add(new String("POST"));
385
                    }
386
                    
387
            }
388
            
389
            String[] output = new String[operations.size()];
390
            for (int i=0 ; i<operations.size() ; i++)
391
                    output[i] = (String) operations.get(i);            
392
            
393
            return output;
394
    }
395

    
396
    /**
397
     * Parses the HTTP-CSW Exceptions
398
     * @param node
399
     * @return
400
     */
401
    private boolean getHTTPExceptions(XMLNode node) {
402
        if (node.getName().equals("ServiceExceptionReport")) {
403
            driver.setServerAnswerReady(XMLTree.searchNodeValue(node,
404
                    "ServiceException"));
405

    
406
            return true;
407
        }
408

    
409
        return false;
410
    }
411
    
412
    /**
413
     * It returns the exception code
414
     * @param node
415
     * Returned node.
416
     * @return
417
     */
418
    public String getExceptionCode(XMLNode node){
419
        String code = XMLTree.searchNodeAtribute(node,"ows:OWSException","code");
420
        if (code == null)
421
            return "0";
422
        return code;
423
    }
424
    
425
    /**
426
     * It returns the exception Subcode
427
     * @param node
428
     * Returned node.
429
     * @return
430
     */
431
    public String getExceptionSubCode(XMLNode node){
432
        String code = XMLTree.searchNodeAtribute(node,"ows:OWSException","subcode");
433
        if (code == null)
434
            return "0";
435
        return code;
436
    
437
    }
438
    
439
    
440

    
441
    /**
442
     * Parses the SOAP-CSW Exceptions
443
     * @param node
444
     * @return
445
     */
446
    private boolean getSOAPExceptions(XMLNode node) {
447
        String msg = SOAPMessageParser.getFault(node);
448

    
449
        if (msg != null) {
450
            driver.setServerAnswerReady(msg);
451

    
452
            return true;
453
        }
454

    
455
        return false;
456
    }
457
    
458
}