Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appCatalogAndGazetteerClient / src / es / gva / cit / catalog / csw / drivers / CSWCatalogServiceDriver.java @ 39586

History | View | Annotate | Download (7.21 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.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.apache.log4j.Logger;
49

    
50
import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;
51

    
52
import es.gva.cit.catalog.csw.drivers.profiles.CSWAbstractProfile;
53
import es.gva.cit.catalog.csw.messages.CSWAbstractMessages;
54
import es.gva.cit.catalog.csw.messages.CSWMessagesFactory;
55
import es.gva.cit.catalog.csw.parsers.CSWCapabilitiesParser;
56
import es.gva.cit.catalog.csw.parsers.CSWDescribeRecordParser;
57
import es.gva.cit.catalog.drivers.AbstractCatalogServiceDriver;
58
import es.gva.cit.catalog.drivers.DiscoveryServiceCapabilities;
59
import es.gva.cit.catalog.drivers.GetRecordsReply;
60
import es.gva.cit.catalog.exceptions.NotSupportedVersionException;
61
import es.gva.cit.catalog.metadataxml.XMLNode;
62
import es.gva.cit.catalog.protocols.HTTPGetProtocol;
63
import es.gva.cit.catalog.protocols.HTTPPostProtocol;
64
import es.gva.cit.catalog.querys.CatalogQuery;
65

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

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

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

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

    
167
                return numberOfRecords;
168
        } 
169

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

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

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