Statistics
| Revision:

gvsig-catalog / trunk / appCatalog / src / org / gvsig / catalog / csw / drivers / CSWCatalogServiceDriver.java @ 6

History | View | Annotate | Download (7.1 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 org.gvsig.catalog.csw.drivers;
43
import java.net.MalformedURLException;
44
import java.net.URI;
45
import java.net.URL;
46
import java.util.Collection;
47

    
48
import org.gvsig.catalog.csw.drivers.profiles.CSWAbstractProfile;
49
import org.gvsig.catalog.csw.messages.CSWAbstractMessages;
50
import org.gvsig.catalog.csw.messages.CSWMessagesFactory;
51
import org.gvsig.catalog.csw.parsers.CSWCapabilitiesParser;
52
import org.gvsig.catalog.csw.parsers.CSWDescribeRecordParser;
53
import org.gvsig.catalog.drivers.AbstractCatalogServiceDriver;
54
import org.gvsig.catalog.drivers.DiscoveryServiceCapabilities;
55
import org.gvsig.catalog.drivers.GetRecordsReply;
56
import org.gvsig.catalog.exceptions.NotSupportedVersionException;
57
import org.gvsig.catalog.metadataxml.XMLNode;
58
import org.gvsig.catalog.protocols.HTTPGetProtocol;
59
import org.gvsig.catalog.protocols.HTTPPostProtocol;
60
import org.gvsig.catalog.querys.CatalogQuery;
61

    
62
import java.util.Hashtable;
63

    
64

    
65
/**
66
 * This class implements the CSW protocol.
67
 * 
68
 * 
69
 * @author Jorge Piera Llodra (piera_jor@gva.es)
70
 * @see http://portal.opengeospatial.org/files/?artifact_id=5929&version=1
71
 */
72
public abstract class CSWCatalogServiceDriver extends AbstractCatalogServiceDriver {
73
        protected CSWCapabilities capabilities = null;
74
        protected CSWAbstractProfile profile = null;
75
        private static Hashtable servers = new Hashtable();
76
        
77
        /**
78
         * @return 
79
         * @param url 
80
         * @throws NotSupportedVersionException 
81
         */
82
        public DiscoveryServiceCapabilities getCapabilities(URI uri) throws NotSupportedVersionException {        
83
                URL url = null;
84
                try {
85
                        url = uri.toURL();
86
                } catch (MalformedURLException e) {
87
                        setServerAnswerReady("errorServerNotFound");
88
                        return null;
89
                }        
90
                Collection nodes = new java.util.ArrayList();
91
                nodes = new HTTPGetProtocol().doQuery(url, CSWAbstractMessages.getHTTPGETCapabilities(), 0);
92
                CSWCapabilitiesParser parser = new CSWCapabilitiesParser(url, this);
93
                capabilities = parser.parse((XMLNode)nodes.toArray()[0]);
94
                //There are servers that need the getCapabilities operation (instead of GetCapabilities)
95
                if (!(capabilities.isAvailable()) && (capabilities.getException() != null)){
96
                        CSWException exception = capabilities.getException();
97
                        if ((exception.getCode().equals(CSWException.CODE_ERROR)) &&
98
                                        (exception.getSubcode().equals(CSWException.SUBCODE_ERROR))){
99
                                nodes = new HTTPGetProtocol().doQuery(url, CSWAbstractMessages.getHTTPGETCapabilitiesLower(), 0);
100
                                capabilities = parser.parse((XMLNode)nodes.toArray()[0]);
101
                        }
102
                }
103
                //If the version can be retrieved the CSWAbstractMessages object
104
                //cant be created
105
                setMessages(uri,url);
106
                return capabilities;
107
        }
108

    
109
        /*
110
         * (non-Javadoc)
111
         * @see es.gva.cit.catalog.drivers.ICatalogServiceDriver#getRecords(java.net.URI, es.gva.cit.catalog.querys.CatalogQuery, int)
112
         */
113
        public GetRecordsReply getRecords(URI uri, CatalogQuery query,
114
                        int firstRecord) {                        
115
                URL url = capabilities.getOperations().getGetRecordsURLPost();               
116
                setQuery(query);
117
                Collection nodes = new java.util.ArrayList();
118
                try {
119
                        System.out.println(getMessages(uri).getHTTPPostGetRecordsMessage(capabilities, getQuery(), getServerData(),  firstRecord));
120
                        nodes = new HTTPPostProtocol().doQuery(url,
121
                                        getMessages(uri).getHTTPPostGetRecordsMessage(capabilities, getQuery(), getServerData(), firstRecord),
122
                                        firstRecord);
123
                } catch (NotSupportedVersionException e) {
124
                        //This exception never will be thrown
125
                }                   
126
                
127
                if (nodes == null) 
128
                        return null;
129

    
130
                XMLNode root = (XMLNode)nodes.toArray()[0];
131
                int numberOfRecords = getNumberOfRecords(root);
132
                if (numberOfRecords == -1) {
133
                        return null;
134
                }
135
                
136
                getRecordsReply().setNumRecords(numberOfRecords);
137
                XMLNode[] results = retrieveResults(root);
138
                for (int i=0 ; i<results.length ; i++){
139
                        getRecordsReply().addRecord(uri, results[i]);
140
                }
141
                return getRecordsReply();
142
        }
143
        
144
        /**
145
         * Retrieve and parse the results
146
         * @param root
147
         * XML root node
148
         * @return
149
         */
150
        protected abstract XMLNode[] retrieveResults(XMLNode root);
151

    
152
        /**
153
         * This function returns the number of records that have been retrieved.
154
         * It Reads a Node value.
155
         * 
156
         * 
157
         * @return The number of records
158
         * @param node The answer tree
159
         */
160
        protected int getNumberOfRecords(XMLNode node) {        
161
                int numberOfRecords = getNumberOfRecords(node,"csw:SearchResults","numberOfRecordsMatched");
162
                if (numberOfRecords == -1)
163
                        numberOfRecords = getNumberOfRecords(node,"SearchResults","numberOfRecordsMatched");
164

    
165
                return numberOfRecords;
166
        } 
167

    
168
        /*
169
         * (non-Javadoc)
170
         * @see es.gva.cit.catalogClient.drivers.ICatalogServiceDriver#getDefaultPort()
171
         */
172
        public int getDefaultPort() {
173
                return 80;
174
        }
175

    
176
        /*
177
         * (non-Javadoc)
178
         * @see es.gva.cit.catalogClient.drivers.ICatalogServiceDriver#getDefaultSchema()
179
         */
180
        public String getDefaultSchema() {
181
                return "http";
182
        }
183

    
184
        /*
185
         * (non-Javadoc)
186
         * @see es.gva.cit.catalogClient.drivers.ICatalogServiceDriver#isProtocolSupported(java.net.URI)
187
         */
188
        public boolean isProtocolSupported(URI uri) {
189
                return true;
190
        } 
191
        
192
        /**
193
         * @return the class to generate the messages. It depends
194
         * of the server version
195
         * @throws NotSupportedVersionException 
196
         */
197
        private CSWAbstractMessages getMessages(URI uri) throws NotSupportedVersionException{
198
                if (servers.containsKey(uri)){
199
                        return (CSWAbstractMessages)servers.get(uri);
200
                }
201
                CSWAbstractMessages messages = CSWMessagesFactory.getMessages(capabilities.getVersion(),
202
                                (CSWAbstractProfile)getProfile());
203
                servers.put(uri, messages);
204
                return messages;
205
        }
206
        
207
        /**
208
         * Create and sets the Messages obejct if it is possible
209
         * @param uri
210
         * Server URI
211
         * @param url
212
         * Server url
213
         * @throws NotSupportedVersionException
214
         */
215
        private void setMessages(URI uri,URL url)throws NotSupportedVersionException{
216
                Collection nodes = new java.util.ArrayList();
217
                nodes = new HTTPGetProtocol().doQuery(url, CSWAbstractMessages.getHTTPGETDescribeRecord(capabilities.getVersion()), 0);
218
                CSWDescribeRecordParser parser = new CSWDescribeRecordParser(url, this);
219
                parser.parse((XMLNode)nodes.toArray()[0]);
220
        }        
221
}