svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.remoteclient / src / main / java / org / gvsig / remoteclient / ogc / OGCProtocolHandler.java @ 40559
History | View | Annotate | Download (7.71 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40559 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
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 | 40559 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * 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 | 40559 | jjdelcerro | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | * MA 02110-1301, USA.
|
||
20 | 40435 | jjdelcerro | *
|
21 | 40559 | jjdelcerro | * For any additional information, do not hesitate to contact us
|
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
24 | package org.gvsig.remoteclient.ogc; |
||
25 | |||
26 | import java.io.File; |
||
27 | import java.io.FileInputStream; |
||
28 | import java.io.IOException; |
||
29 | |||
30 | import org.kxml2.io.KXmlParser; |
||
31 | import org.xmlpull.v1.XmlPullParserException; |
||
32 | |||
33 | import org.gvsig.compat.CompatLocator; |
||
34 | import org.gvsig.compat.net.Downloader; |
||
35 | import org.gvsig.remoteclient.utils.CapabilitiesTags; |
||
36 | |||
37 | public abstract class OGCProtocolHandler { |
||
38 | protected static final Downloader downloader = CompatLocator.getDownloader(); |
||
39 | |||
40 | /**
|
||
41 | * procotol handler name
|
||
42 | */
|
||
43 | protected String name; |
||
44 | /**
|
||
45 | * protocol handler version
|
||
46 | */
|
||
47 | protected String version; |
||
48 | /**
|
||
49 | * host of the WMS to connect
|
||
50 | */
|
||
51 | protected String host; |
||
52 | /**
|
||
53 | * port number of the comunication channel of the WMS to connect
|
||
54 | */
|
||
55 | protected String port; |
||
56 | |||
57 | /**
|
||
58 | * @return Returns the host.
|
||
59 | */
|
||
60 | public String getHost() { |
||
61 | return host;
|
||
62 | } |
||
63 | /**
|
||
64 | * @param host The host to set.
|
||
65 | */
|
||
66 | public void setHost(String host) { |
||
67 | this.host = host;
|
||
68 | } |
||
69 | /**
|
||
70 | * @return Returns the name.
|
||
71 | */
|
||
72 | public String getName() { |
||
73 | return name;
|
||
74 | } |
||
75 | /**
|
||
76 | * @param name The name to set.
|
||
77 | */
|
||
78 | public void setName(String name) { |
||
79 | this.name = name;
|
||
80 | } |
||
81 | /**
|
||
82 | * @return Returns the port.
|
||
83 | */
|
||
84 | public String getPort() { |
||
85 | return port;
|
||
86 | } |
||
87 | /**
|
||
88 | * @param port The port to set.
|
||
89 | */
|
||
90 | public void setPort(String port) { |
||
91 | this.port = port;
|
||
92 | } |
||
93 | /**
|
||
94 | * @return Returns the version.
|
||
95 | */
|
||
96 | public String getVersion() { |
||
97 | return version;
|
||
98 | } |
||
99 | /**
|
||
100 | * @param version The version to set.
|
||
101 | */
|
||
102 | public void setVersion(String version) { |
||
103 | this.version = version;
|
||
104 | } |
||
105 | |||
106 | /**
|
||
107 | * parses the data retrieved by the Capabilities XML document
|
||
108 | */
|
||
109 | public abstract boolean parseCapabilities(File f); |
||
110 | |||
111 | public abstract OGCServiceInformation getServiceInformation(); |
||
112 | |||
113 | /**
|
||
114 | * Just for not repeat code. Gets the correct separator according
|
||
115 | * to the server URL
|
||
116 | * @param h
|
||
117 | * @return
|
||
118 | */
|
||
119 | protected static String getSymbol(String h) { |
||
120 | String symbol;
|
||
121 | if (h.indexOf("?")==-1) |
||
122 | symbol = "?";
|
||
123 | else if (h.indexOf("?")!=h.length()-1) |
||
124 | symbol = "&";
|
||
125 | else
|
||
126 | symbol = "";
|
||
127 | return symbol;
|
||
128 | } |
||
129 | |||
130 | /**
|
||
131 | * Parse an operation into a DcpType tag
|
||
132 | * @param parser
|
||
133 | * The KXMLParser
|
||
134 | * @param operation
|
||
135 | * The WFS operation to parse
|
||
136 | * @throws IOException
|
||
137 | * @throws XmlPullParserException
|
||
138 | */
|
||
139 | protected void parserDcpType(KXmlParser parser, String operation) throws XmlPullParserException, IOException { |
||
140 | int currentTag;
|
||
141 | boolean end = false; |
||
142 | |||
143 | currentTag = parser.next(); |
||
144 | |||
145 | while (!end)
|
||
146 | { |
||
147 | switch(currentTag)
|
||
148 | { |
||
149 | case KXmlParser.START_TAG:
|
||
150 | if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0){ |
||
151 | parseHTTPTag(parser, operation); |
||
152 | } |
||
153 | break;
|
||
154 | case KXmlParser.END_TAG:
|
||
155 | if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE) == 0) |
||
156 | end = true;
|
||
157 | break;
|
||
158 | case KXmlParser.TEXT:
|
||
159 | break;
|
||
160 | } |
||
161 | if (!end){
|
||
162 | currentTag = parser.next(); |
||
163 | } |
||
164 | } |
||
165 | } |
||
166 | |||
167 | /**
|
||
168 | * Parse an operation into a HTTP tag
|
||
169 | * @param parser
|
||
170 | * The KXMLParser
|
||
171 | * @param operation
|
||
172 | * The WFS operation to parse
|
||
173 | * @throws IOException
|
||
174 | * @throws XmlPullParserException
|
||
175 | */
|
||
176 | protected void parseHTTPTag(KXmlParser parser, String operation) throws XmlPullParserException, IOException { |
||
177 | int currentTag;
|
||
178 | boolean end = false; |
||
179 | |||
180 | currentTag = parser.next(); |
||
181 | int protocol = -1; |
||
182 | |||
183 | while (!end)
|
||
184 | { |
||
185 | switch(currentTag)
|
||
186 | { |
||
187 | case KXmlParser.START_TAG:
|
||
188 | String value = null; |
||
189 | if(parser.getName().compareTo(CapabilitiesTags.GET)==0){ |
||
190 | protocol = OGCClientOperation.PROTOCOL_GET; |
||
191 | addOperationByAttribute(parser, operation, protocol); |
||
192 | }else if(parser.getName().compareTo(CapabilitiesTags.POST)==0){ |
||
193 | protocol = OGCClientOperation.PROTOCOL_POST; |
||
194 | addOperationByAttribute(parser, operation, protocol); |
||
195 | }else if(parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0){ |
||
196 | addOperationByAttribute(parser, operation, protocol); |
||
197 | } |
||
198 | break;
|
||
199 | case KXmlParser.END_TAG:
|
||
200 | if (parser.getName().compareTo(CapabilitiesTags.HTTP) == 0) |
||
201 | end = true;
|
||
202 | break;
|
||
203 | case KXmlParser.TEXT:
|
||
204 | break;
|
||
205 | } |
||
206 | if (!end){
|
||
207 | currentTag = parser.next(); |
||
208 | } |
||
209 | } |
||
210 | } |
||
211 | |||
212 | /**
|
||
213 | * Add an operation and the online resource
|
||
214 | * @param parser
|
||
215 | * The parser
|
||
216 | * @param operation
|
||
217 | * The operation to add
|
||
218 | * @param protocol
|
||
219 | * The parser to add
|
||
220 | */
|
||
221 | protected void addOperationByAttribute(KXmlParser parser, String operation, int protocol){ |
||
222 | String value = null; |
||
223 | if (protocol > -1){ |
||
224 | for (int i=0 ; i<parser.getAttributeCount() ; i++){ |
||
225 | if ((parser.getAttributeName(i).toUpperCase().compareTo(CapabilitiesTags.ONLINERESOURCE.toUpperCase()) == 0) || |
||
226 | (parser.getAttributeName(i).equals(CapabilitiesTags.XLINK_HREF))){ |
||
227 | value = parser.getAttributeValue(i); |
||
228 | } |
||
229 | } |
||
230 | if (value == null){ |
||
231 | getServiceInformation().addOperation(operation, protocol); |
||
232 | }else{
|
||
233 | getServiceInformation().addOperation(operation, protocol, value); |
||
234 | } |
||
235 | } |
||
236 | } |
||
237 | |||
238 | /**
|
||
239 | * Copy the file in a byte array
|
||
240 | * @param file
|
||
241 | * The file to copy
|
||
242 | * @return
|
||
243 | * An array of bytes
|
||
244 | * @throws IOException
|
||
245 | */
|
||
246 | protected byte[] fileToBytes(File file) throws IOException{ |
||
247 | FileInputStream fis = null; |
||
248 | byte[] bytes = null; |
||
249 | try{
|
||
250 | fis = new FileInputStream(file); |
||
251 | |||
252 | long length = file.length();
|
||
253 | bytes = new byte[(int)file.length()]; |
||
254 | |||
255 | int offset = 0; |
||
256 | int numRead = 0; |
||
257 | while (offset < bytes.length && (numRead=fis.read(bytes, offset, bytes.length-offset)) >= 0) |
||
258 | { |
||
259 | offset += numRead; |
||
260 | } |
||
261 | }catch (IOException e) { |
||
262 | throw e;
|
||
263 | }finally{
|
||
264 | if (fis != null){ |
||
265 | fis.close(); |
||
266 | } |
||
267 | } |
||
268 | return bytes;
|
||
269 | } |
||
270 | } |