Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / applications / appCatalogYNomenclatorClient / src / es / gva / cit / catalogClient / csw / drivers / CSWMessages.java @ 8604

History | View | Annotate | Download (11.9 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.URL;
44

    
45
import es.gva.cit.catalogClient.csw.filters.CSWFilter;
46
import es.gva.cit.catalogClient.drivers.IProtocolMessages;
47
import es.gva.cit.catalogClient.protocols.SOAPProtocol;
48
import es.gva.cit.catalogClient.querys.Query;
49
import es.gva.cit.catalogClient.schemas.Schemas;
50
import es.gva.cit.catalogClient.utils.Strings;
51
import org.apache.commons.httpclient.NameValuePair;
52

    
53
/**
54
 * This class has methods that return the CSW opearions messages.
55
 * 
56
 * 
57
 * @author Jorge Piera Llodra (piera_jor@gva.es)
58
 */
59
public class CSWMessages implements IProtocolMessages {
60
        private static URL url = null;
61
        /**
62
 * 
63
 * 
64
 */
65
    private CSWCatalogServiceDriver driver;
66

    
67
/**
68
 * 
69
 * 
70
 * 
71
 * @param driver 
72
 */
73
    public  CSWMessages(CSWCatalogServiceDriver driver) {        
74
        this.driver = driver;
75
    } 
76
/* (non-Javadoc)
77
     * @see es.gva.cit.catalogClient.drivers.messages.IProtocolOperations#getHTTPGETCapabilities()
78
     */
79

    
80
/**
81
 * 
82
 * 
83
 * 
84
 * @return 
85
 * @param upper 
86
 */
87
    public NameValuePair[] getHTTPGETCapabilities(boolean upper) {        
88
        NameValuePair nvp1 = null;
89
        if (upper){
90
            nvp1 = new NameValuePair("request", "GetCapabilities");
91
        }else{
92
            nvp1 = new NameValuePair("request", "getCapabilities");
93
        }
94
            
95
        NameValuePair nvp2 = new NameValuePair("service", driver.getService());
96
        NameValuePair nvp3 = new NameValuePair("acceptversion", driver.getVersion());
97
        return new NameValuePair[] { nvp1, nvp2, nvp3 };
98
    } 
99
/* (non-Javadoc)
100
     * @see es.gva.cit.catalogClient.drivers.messages.IProtocolOperations#getHTTPGETDescribeRecords()
101
     */
102

    
103
/**
104
 * 
105
 * 
106
 * 
107
 * @return 
108
 */
109
    public NameValuePair[] getHTTPGETDescribeRecords() {        
110
        NameValuePair nvp1 = new NameValuePair("request", "describeRecord");
111
        NameValuePair nvp2 = new NameValuePair("version", driver.getVersion());
112
        NameValuePair nvp3 = new NameValuePair("outputformat",
113
                driver.getOutputFormat());
114
        NameValuePair nvp4 = new NameValuePair("schemalanguage", "XMLSCHEMA");
115
        NameValuePair nvp5 = new NameValuePair("typename",
116
                driver.getTypeNames()[0]);
117
        NameValuePair nvp6 = new NameValuePair("namespace",
118
                "csw:http://www.opengis.org/csw");
119
        return new NameValuePair[] { nvp1, nvp2, nvp3, nvp4, nvp5, nvp6 };
120
    } 
121
/* (non-Javadoc)
122
     * @see es.gva.cit.catalogClient.drivers.messages.IProtocolOperations#getHTTPGETRecords(es.gva.cit.catalogClient.querys.Query, int)
123
     */
124

    
125
/**
126
 * 
127
 * 
128
 * 
129
 * @return 
130
 * @param query 
131
 * @param firstRecord 
132
 */
133
    public NameValuePair[] getHTTPGETRecords(Query query, int firstRecord) {        
134
        
135
        NameValuePair nvp1 = new NameValuePair("request", "GetRecords");
136
        NameValuePair nvp2 = new NameValuePair("version", driver.getVersion());
137
        NameValuePair nvp3 = new NameValuePair("service", "CSW");
138
        NameValuePair nvp4 = new NameValuePair("TypeNames",Strings.getComaSeparated(driver.getTypeNames()));
139
        
140
        NameValuePair nvp5 = new NameValuePair("outputschema",
141
                driver.getOutputSchema()[0]);
142
        
143
        NameValuePair nvp6 = new NameValuePair("namespace",
144
                "csw:http://www.opengis.org/csw");
145
       
146
        NameValuePair nvp7 = new NameValuePair("elementsetname", "full");
147
        
148
        NameValuePair nvp8 = new NameValuePair("constraintlanguage", "FILTER");
149
        
150
        NameValuePair nvp9 = new NameValuePair("constraint",
151
                new CSWFilter().getQuery(query,driver.getServerProfile()));
152
        
153
        
154
        return new NameValuePair[] {
155
            nvp1, nvp2, nvp3, nvp4, nvp5, nvp6, nvp7, nvp8, nvp9
156
        };
157
    } 
158
/* (non-Javadoc)
159
     * @see es.gva.cit.catalogClient.drivers.messages.IProtocolOperations#getHTTPPOSTCapabilities()
160
     */
161

    
162
/**
163
 * 
164
 * 
165
 * 
166
 * @return 
167
 */
168
    public String getHTTPPOSTCapabilities() {        
169
        return "<csw:GetCapabilities service=\"CSW\" version=\"2.0.0\" " +
170
        "xmlns:csw=\"http://www.opengis.net/cat/csw\" " +
171
        "xmlns:ogc=\"http://www.opengis.net/ogc\">" +
172
        "<AcceptVersions xmlns=\"http://www.opengis.net/ows\">" +
173
        "<Version>" + driver.getVersion() + "</Version>" + "</AcceptVersions>" +
174
        "<AcceptFormats xmlns=\"http://www.opengis.net/ows\">" +
175
        "<OutputFormat>text/xml</OutputFormat>" + "</AcceptFormats>" +
176
        "</csw:GetCapabilities>";
177
    } 
178
/* (non-Javadoc)
179
     * @see es.gva.cit.catalogClient.drivers.messages.IProtocolOperations#getHTTPPOSTDescribeRecords()
180
     */
181

    
182
/**
183
 * 
184
 * 
185
 * 
186
 * @return 
187
 */
188
    public String getHTTPPOSTDescribeRecords() {        
189
        return "<csw:DescribeRecord " +
190
        "xmlns:csw=\"http://www.opengis.net/cat/csw\" " +
191
        "xmlns:ogc=\"http://www.opengis.net/ogc\" " + "service=\"CSW\" " +
192
        "version=" + driver.getVersion() + " " + "outputFormat=\"test/xml\" " +
193
        "schemaLanguage=\"XMLSCHEMA\">" +
194
        "<csw:TypeName>csw:record</csw:TypeName>" +
195
        "</csw:DescribeRecord>";
196
    } 
197
/* (non-Javadoc)
198
     * @see es.gva.cit.catalogClient.drivers.messages.IProtocolOperations#getHTTPPOSTRecords(es.gva.cit.catalogClient.querys.Query, int)
199
     */
200

    
201
/**
202
 * 
203
 * 
204
 * 
205
 * @return 
206
 * @param query 
207
 * @param firstRecord 
208
 */
209
    public String getHTTPPOSTRecords(Query query, int firstRecord) {        
210
            StringBuffer answer = new StringBuffer();
211
            answer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 
212
            if (url.getHost().equals("laits.gmu.edu")){
213
                    answer.append("<csw:GetRecords");
214
        }else{
215
                answer.append("<GetRecords");
216
        }            
217
            answer.append(" " + "service=\"" + driver.getService() + "\" " +
218
                            "version=\"" + driver.getVersion() + "\" " + "xmlns=\"http://www.opengis.net/cat/csw\" " +
219
                            "xmlns:csw=\"http://www.opengis.net/cat/csw\" " + 
220
                            "xmlns:ogc=\"http://www.opengis.net/ogc\" " +
221
                            "xmlns:gml=\"http://www.opengis.net/gml\" " + "startPosition='" +
222
                            firstRecord  + "' " + "maxRecords='10' ");
223
            if (driver.getOutputFormat() != null){
224
                    answer.append("outputFormat=\"" + driver.getOutputFormat() + "\" ");
225
            }
226
            if (getOutputSchema(driver.getOutputSchema()) != null){
227
                    answer.append("outputSchema=\"" + getOutputSchema(driver.getOutputSchema()) + "\" ");
228
            }else{
229
                    answer.append("outputSchema=\"csw:IsoRecord\" ");
230
            }
231
            answer.append("resultType=\"" +
232
                            getResultType(driver.getResultType()) + "\">" +
233
            "<Query ");
234
            if (driver.getTypeNames() != null){
235
                    answer.append("typeNames=\"" +
236
                                    Strings.getBlankSeparated(driver.getTypeNames()) + "\"");
237
            }
238
            answer.append(">" + getElementSetNameLabel(query.getSearchType()));
239
            
240
            if ((driver.getService().equals("CSW"))){
241
                    answer.append("<Constraint version='1.0.20'>");
242
            }else{
243
                    answer.append("<Constraint version='1.1.0'>");
244
            }
245
            answer.append(new CSWFilter().getQuery(query,driver.getServerProfile()) +
246
            "</Constraint>" +
247
            "</Query>");
248
                 if (url.getHost().equals("laits.gmu.edu")){
249
                        answer.append("</csw:GetRecords>");
250
            }else{
251
                    answer.append("</GetRecords>");
252
            }   
253
            return answer.toString();
254
    } 
255

    
256
/**
257
 * The IDEC server has some different labels
258
 * @param searchType 
259
 * 
260
 * 
261
 * @return 
262
 */
263
    private String getElementSetNameLabel(int searchType) {        
264
        if (url.getHost().equals("laits.gmu.edu")){
265
                StringBuffer buffer = new StringBuffer();
266
                buffer.append("<ElementName>");
267
                if (searchType == Query.SEARCH_DATA){
268
                        buffer.append("/DataGranule");
269
                }else{
270
                        buffer.append("/Service");
271
                }
272
                buffer.append("</ElementName>");
273
                return buffer.toString();
274
        }
275
            StringBuffer buffer = new StringBuffer();
276
        if (driver.getServerProfile().equals(Schemas.EBRIM)){              
277
                buffer.append("<ElementSetName typeNames='Dataset'>");
278
        }else{
279
                buffer.append("<ElementSetName>");
280
        }
281
        buffer.append(getMaxSetName(driver.getElementSetName()) + "</ElementSetName>");
282
        return buffer.toString();
283
    } 
284

    
285
/**
286
 * It returns the maximum element set name
287
 * 
288
 * 
289
 * @return 
290
 * @param elementSetName 
291
 */
292
    private String getMaxSetName(String[] elementSetName) {        
293
        if ((driver.getService().equals("http://www.opengis.net/cat/csw"))){
294
                return "full";
295
        }
296
            
297
            if (elementSetName == null){
298
            return "summary";    
299
            }
300
           
301
        for (int i=0 ; i<elementSetName.length ; i++){
302
            if (elementSetName[i].toUpperCase().equals("FULL")){
303
                return elementSetName[i];
304
            }
305
        }
306
        
307
        return elementSetName[0];
308
    } 
309

    
310
/**
311
 * Returns a common reslut type if the getCapabilities doesn't have
312
 * one. Sometimes it works.
313
 * 
314
 * 
315
 * @return Just one String
316
 * @param resultType The array of result types parsed.
317
 */
318
    private String getResultType(String[] resultType) {        
319
        if (resultType == null)
320
            return "results";
321
        
322
        for (int i=0 ; i<resultType.length ; i++){
323
                
324
        }
325
        
326
        return resultType[0];
327
    } 
328

    
329
/**
330
 * Returns the OutputSchema. If the ISO19115 is supported,
331
 * we prefer this
332
 * 
333
 * @param OutputFormat The array of outputFormats parsed.
334
 * 
335
 * @return Just one String
336
 * @param outputSchemas 
337
 */
338
    private String getOutputSchema(String[] outputSchemas) {        
339
        if (outputSchemas == null){
340
                return null;
341
        }
342
            for (int i=0 ; i<outputSchemas.length ; i++){
343
            if (outputSchemas[i].equals(Schemas.ISO19115))
344
                return outputSchemas[i];
345
        }        
346
        return outputSchemas[0];
347
    } 
348
/* (non-Javadoc)
349
     * @see es.gva.cit.catalogClient.drivers.messages.IProtocolOperations#getSOAPCapabilities()
350
     */
351

    
352
/**
353
 * 
354
 * 
355
 * 
356
 * @return 
357
 */
358
    public String getSOAPCapabilities() {        
359
        return SOAPProtocol.setSOAPMessage(getHTTPPOSTCapabilities(),null);
360
    } 
361
/* (non-Javadoc)
362
     * @see es.gva.cit.catalogClient.drivers.messages.IProtocolOperations#getSOAPDescribeRecords()
363
     */
364

    
365
/**
366
 * 
367
 * 
368
 * 
369
 * @return 
370
 */
371
    public String getSOAPDescribeRecords() {        
372
        return SOAPProtocol.setSOAPMessage(getHTTPPOSTDescribeRecords(),null);
373
    } 
374
/* (non-Javadoc)
375
     * @see es.gva.cit.catalogClient.drivers.messages.IProtocolOperations#getSOAPRecords(es.gva.cit.catalogClient.querys.Query, int)
376
     */
377

    
378
/**
379
 * 
380
 * 
381
 * 
382
 * @return 
383
 * @param query 
384
 * @param firstRecord 
385
 */
386
    public String getSOAPRecords(Query query, int firstRecord) {        
387
            CSWMessages.setUrl(url);
388
            return SOAPProtocol.setSOAPMessage(getHTTPPOSTRecords(query,
389
                firstRecord),null);
390
    }
391
public static void setUrl(URL url) {
392
        CSWMessages.url = url;
393
}
394
public static URL getUrl() {
395
        return url;
396
} 
397
 }