1 |
alfonx |
1161 |
package skrueger.geotools.io; |
2 |
|
|
|
3 |
|
|
import java.awt.Component; |
4 |
|
|
import java.net.MalformedURLException; |
5 |
|
|
import java.net.URL; |
6 |
|
|
import java.util.HashMap; |
7 |
|
|
import java.util.regex.Pattern; |
8 |
|
|
|
9 |
|
|
import org.apache.commons.lang.ArrayUtils; |
10 |
|
|
import org.geotools.data.wfs.WFSDataStoreFactory; |
11 |
alfonx |
1163 |
import org.jfree.util.Log; |
12 |
alfonx |
1161 |
|
13 |
alfonx |
1163 |
import schmitzm.swing.BooleanInputOption; |
14 |
alfonx |
1161 |
import schmitzm.swing.ManualInputOption; |
15 |
alfonx |
1163 |
import schmitzm.swing.ManualInputOption.Integer; |
16 |
|
|
import schmitzm.swing.ManualInputOption.PasswordViewable; |
17 |
alfonx |
1161 |
import schmitzm.swing.ManualInputOption.Text; |
18 |
|
|
import schmitzm.swing.MultipleOptionPane; |
19 |
|
|
import schmitzm.swing.SelectionInputOption; |
20 |
|
|
import schmitzm.swing.SelectionInputOption.Combo; |
21 |
|
|
|
22 |
|
|
/** |
23 |
|
|
* This class describes all settings needed to connect to a WFS server. This |
24 |
|
|
* class extends a {@link HashMap} and contains two groups of keys:<br/> |
25 |
|
|
* The first group of keys, are all keys provided by Geotools to create a WFS |
26 |
|
|
* datastore, like: {@link WFSDataStoreFactory#URL} or |
27 |
|
|
* {@link WFSDataStoreFactory#USERNAME} .<br/> |
28 |
|
|
* The second group are additional keys defined in the enum |
29 |
alfonx |
1295 |
* {@link GtWfsServerSettings.Key}.<br/> |
30 |
alfonx |
1161 |
* This class can serialize all important parameters needed to define the |
31 |
|
|
* connection into a {@link String} with {@link #toPropertiesString()} and |
32 |
|
|
* re-import the String with {@link #parsePropertiesString(String)}. |
33 |
|
|
*/ |
34 |
alfonx |
1295 |
public class GtWfsServerSettings extends AbstractGTServerSettings<Object, Object> { |
35 |
alfonx |
1161 |
|
36 |
|
|
public enum Key { |
37 |
|
|
BASE_URL, VERSION |
38 |
|
|
} |
39 |
|
|
|
40 |
alfonx |
1163 |
public enum HttpProtocol { |
41 |
|
|
AUTO(null), POST(Boolean.TRUE), GET(Boolean.FALSE); |
42 |
|
|
|
43 |
|
|
private final Boolean value; |
44 |
|
|
|
45 |
|
|
private HttpProtocol(Boolean value) { |
46 |
|
|
this.value = value; |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
public Boolean getValue() { |
50 |
|
|
return value; |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
public static HttpProtocol parse(String object) { |
54 |
|
|
if (object.equalsIgnoreCase("true")) |
55 |
|
|
return POST; |
56 |
|
|
if (object.equals("false")) |
57 |
|
|
return GET; |
58 |
|
|
return AUTO; |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
public static HttpProtocol parse(Boolean object) { |
62 |
|
|
if (object == Boolean.TRUE) |
63 |
|
|
return POST; |
64 |
|
|
if (object == Boolean.FALSE) |
65 |
|
|
return GET; |
66 |
|
|
return AUTO; |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
} |
70 |
|
|
|
71 |
alfonx |
1161 |
public enum WfsProtocollVersion { |
72 |
alfonx |
1163 |
v1_0_0("1.0.0"), v1_1_0("1.1.0"), v1_1_1("1.1.1"); |
73 |
alfonx |
1161 |
|
74 |
|
|
private final String versionString; |
75 |
|
|
|
76 |
|
|
private WfsProtocollVersion(String versionString) { |
77 |
|
|
this.versionString = versionString; |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
public String getVersionString() { |
81 |
|
|
return versionString; |
82 |
|
|
} |
83 |
|
|
} |
84 |
|
|
|
85 |
alfonx |
1164 |
private static final URL defaultURl; |
86 |
|
|
static { |
87 |
|
|
try { |
88 |
|
|
defaultURl = new URL("http://localhost:8080/geoserver/ows"); |
89 |
|
|
} catch (MalformedURLException e) { |
90 |
|
|
throw new RuntimeException(e); |
91 |
|
|
} |
92 |
|
|
} |
93 |
alfonx |
1161 |
|
94 |
alfonx |
1163 |
/** |
95 |
|
|
* Opens a GUI that asks the use define a DB connection. |
96 |
|
|
* |
97 |
|
|
* @param wfsServer |
98 |
|
|
* <code>null</code> to create a new instance, or an instance to |
99 |
|
|
* edit. |
100 |
|
|
* @return <code>null</code> if the user cancelled the creation of a new |
101 |
alfonx |
1295 |
* {@link GtDbServerSettings}, otherwise the edited instance. |
102 |
alfonx |
1163 |
*/ |
103 |
alfonx |
1295 |
public static GtWfsServerSettings createOrEdit(Component owner, |
104 |
|
|
GtWfsServerSettings wfsServer) { |
105 |
alfonx |
1163 |
boolean newCreated = false; |
106 |
|
|
|
107 |
|
|
if (wfsServer == null) { |
108 |
|
|
newCreated = true; |
109 |
alfonx |
1295 |
wfsServer = new GtWfsServerSettings(); |
110 |
alfonx |
1163 |
} |
111 |
|
|
|
112 |
|
|
Text hostInput = new ManualInputOption.Text( |
113 |
|
|
"BaseURL (without any paramters)", true, wfsServer.getBaseUrl() |
114 |
|
|
.toString()); |
115 |
|
|
|
116 |
|
|
Combo<WfsProtocollVersion> versionInput = new SelectionInputOption.Combo<WfsProtocollVersion>( |
117 |
|
|
"WFS Version", true, WfsProtocollVersion.values(), |
118 |
|
|
ArrayUtils.indexOf(WfsProtocollVersion.values(), |
119 |
|
|
wfsServer.getVersion()), WfsProtocollVersion.values()); |
120 |
|
|
versionInput |
121 |
|
|
.setToolTipText("If you have problems with Curves in GML3, try version 1.0.0."); |
122 |
|
|
|
123 |
|
|
Integer maxFeaturesInput = new ManualInputOption.Integer( |
124 |
|
|
"Max. features per request (0=no limit)", false, |
125 |
|
|
wfsServer.getMaxFeatures()); |
126 |
|
|
maxFeaturesInput |
127 |
|
|
.setToolTipText(WFSDataStoreFactory.MAXFEATURES.description |
128 |
|
|
.toString()); |
129 |
|
|
|
130 |
|
|
Integer timeoutInput = new ManualInputOption.Integer("Timout in ms:", |
131 |
|
|
false, wfsServer.getTimeout()); |
132 |
|
|
timeoutInput.setToolTipText(WFSDataStoreFactory.TIMEOUT.description |
133 |
|
|
.toString()); |
134 |
|
|
|
135 |
|
|
BooleanInputOption lenientInput = new BooleanInputOption( |
136 |
|
|
"lenient (=ignore errors)", wfsServer.getLenient()); |
137 |
|
|
lenientInput.setToolTipText(WFSDataStoreFactory.LENIENT.description |
138 |
|
|
.toString()); |
139 |
|
|
|
140 |
|
|
SelectionInputOption.Combo<HttpProtocol> httpInput = new SelectionInputOption.Combo<HttpProtocol>( |
141 |
|
|
"Ust HTTP GET or POST:", true, HttpProtocol.values(), |
142 |
|
|
ArrayUtils.indexOf(HttpProtocol.values(), |
143 |
|
|
wfsServer.getHttpProtocol()), HttpProtocol.values()); |
144 |
|
|
httpInput.setToolTipText(WFSDataStoreFactory.PROTOCOL.description |
145 |
|
|
.toString()); |
146 |
|
|
|
147 |
|
|
Text userInput = new ManualInputOption.Text( |
148 |
|
|
"Optional HTTPAuth Username", false, wfsServer.getUsername()); |
149 |
|
|
|
150 |
|
|
PasswordViewable passwdInput = new ManualInputOption.PasswordViewable( |
151 |
|
|
"Optional HTTPAuth Password", false, wfsServer.getPassword()); |
152 |
|
|
|
153 |
|
|
// Show the options |
154 |
|
|
Object[] input = MultipleOptionPane.showMultipleInputDialog(owner, |
155 |
|
|
"WFS Connection parameters", hostInput, versionInput, |
156 |
|
|
maxFeaturesInput, timeoutInput, lenientInput, httpInput, |
157 |
|
|
userInput, passwdInput); |
158 |
|
|
|
159 |
|
|
if (input == null) { |
160 |
|
|
if (newCreated) |
161 |
|
|
return null; |
162 |
|
|
else |
163 |
|
|
return wfsServer; |
164 |
|
|
} else { |
165 |
|
|
wfsServer.setBaseUrl((String) input[0]); |
166 |
|
|
wfsServer.setVersion(((WfsProtocollVersion) input[1])); |
167 |
|
|
wfsServer.setMaxFeatures((java.lang.Integer) input[2]); |
168 |
|
|
wfsServer.setTimeout((java.lang.Integer) input[3]); |
169 |
|
|
wfsServer.setLenient((java.lang.Boolean) input[4]); |
170 |
|
|
wfsServer.setHttpProtocol((HttpProtocol) input[5]); |
171 |
|
|
wfsServer.setUsername((String) input[6]); |
172 |
|
|
wfsServer.setPassword(String.valueOf((char[]) input[7])); |
173 |
|
|
} |
174 |
|
|
|
175 |
|
|
return wfsServer; |
176 |
|
|
|
177 |
alfonx |
1161 |
} |
178 |
|
|
|
179 |
alfonx |
1163 |
public HttpProtocol getHttpProtocol() { |
180 |
|
|
return HttpProtocol |
181 |
|
|
.parse((Boolean) get(WFSDataStoreFactory.PROTOCOL.key)); |
182 |
|
|
} |
183 |
|
|
|
184 |
|
|
public void setHttpProtocol(HttpProtocol protocol) { |
185 |
|
|
put(WFSDataStoreFactory.PROTOCOL.key, protocol.getValue()); |
186 |
|
|
} |
187 |
|
|
|
188 |
|
|
private Boolean getLenient() { |
189 |
|
|
return (Boolean) get(WFSDataStoreFactory.LENIENT.key); |
190 |
|
|
} |
191 |
|
|
|
192 |
|
|
private void setLenient(Boolean lenient) { |
193 |
|
|
put(WFSDataStoreFactory.LENIENT.key, lenient); |
194 |
|
|
} |
195 |
|
|
|
196 |
|
|
/** |
197 |
|
|
* @return transforms the settings to a String that can be stored in a |
198 |
|
|
* .properties line. @see #parsePropertiesString |
199 |
|
|
* @throws MalformedURLException |
200 |
|
|
*/ |
201 |
alfonx |
1291 |
@Override |
202 |
alfonx |
1295 |
public boolean parsePropertiesString(String propString) { |
203 |
alfonx |
1163 |
|
204 |
|
|
try { |
205 |
|
|
String[] split = propString.split(Pattern.quote(DELIMITER)); |
206 |
|
|
|
207 |
alfonx |
1295 |
// WfsServerSettings wfs = new WfsServerSettings(); |
208 |
alfonx |
1163 |
|
209 |
|
|
int i = 0; |
210 |
alfonx |
1292 |
setTitle(split[i++]); |
211 |
|
|
setBaseUrl(new URL(split[i++])); |
212 |
|
|
setVersion(WfsProtocollVersion.valueOf(split[i++])); |
213 |
|
|
setMaxFeatures(intOrNull(split[i++])); |
214 |
|
|
setTimeout(intOrNull(split[i++])); |
215 |
|
|
setLenient(java.lang.Boolean.valueOf(split[i++])); |
216 |
|
|
setHttpProtocol(HttpProtocol.parse(split[i++])); |
217 |
alfonx |
1163 |
|
218 |
alfonx |
1292 |
setUsername(stringOrNull(split[i++])); |
219 |
alfonx |
1163 |
|
220 |
alfonx |
1292 |
setPassword(stringOrNull(split[i++])); |
221 |
alfonx |
1163 |
|
222 |
alfonx |
1292 |
return true; |
223 |
alfonx |
1163 |
} catch (Exception e) { |
224 |
alfonx |
1164 |
Log.warn("couldn't parse " + propString, e); |
225 |
alfonx |
1292 |
return false; |
226 |
alfonx |
1163 |
} |
227 |
|
|
|
228 |
|
|
} |
229 |
|
|
|
230 |
|
|
private String[] cachedTypeNames = null; |
231 |
|
|
|
232 |
alfonx |
1295 |
public GtWfsServerSettings() { |
233 |
alfonx |
1164 |
this(defaultURl, WfsProtocollVersion.v1_0_0); |
234 |
alfonx |
1161 |
} |
235 |
|
|
|
236 |
alfonx |
1295 |
public GtWfsServerSettings(URL baseUrl, WfsProtocollVersion version) { |
237 |
alfonx |
1163 |
setVersion(version); |
238 |
|
|
setBaseUrl(baseUrl); |
239 |
alfonx |
1164 |
setLenient(false); |
240 |
alfonx |
1163 |
} |
241 |
|
|
|
242 |
alfonx |
1295 |
public GtWfsServerSettings(String s) { |
243 |
|
|
parsePropertiesString(s); |
244 |
alfonx |
1291 |
} |
245 |
|
|
|
246 |
alfonx |
1163 |
public URL getBaseUrl() { |
247 |
|
|
return (URL) get(Key.BASE_URL); |
248 |
|
|
} |
249 |
|
|
|
250 |
|
|
public String[] getCachedTypeNames() { |
251 |
|
|
return cachedTypeNames; |
252 |
|
|
} |
253 |
|
|
|
254 |
alfonx |
1161 |
/** |
255 |
alfonx |
1163 |
* @return <code>null</code> if not correctly defined, otherwise the URL of |
256 |
|
|
* the GetCapabilites request. |
257 |
|
|
*/ |
258 |
|
|
public URL getCapabilitiesUrl() { |
259 |
|
|
return (URL) get(WFSDataStoreFactory.URL.key); |
260 |
|
|
} |
261 |
|
|
|
262 |
|
|
public java.lang.Integer getMaxFeatures() { |
263 |
|
|
return (java.lang.Integer) get(WFSDataStoreFactory.MAXFEATURES.key); |
264 |
|
|
} |
265 |
|
|
|
266 |
|
|
public java.lang.Integer getTimeout() { |
267 |
|
|
return (java.lang.Integer) get(WFSDataStoreFactory.TIMEOUT.key); |
268 |
|
|
} |
269 |
|
|
|
270 |
|
|
public WfsProtocollVersion getVersion() { |
271 |
|
|
return (WfsProtocollVersion) get(Key.VERSION); |
272 |
|
|
} |
273 |
|
|
|
274 |
|
|
/** |
275 |
alfonx |
1161 |
* @return <code>true</code>, if all parameters look OK, without actually |
276 |
|
|
* opening any connection. |
277 |
|
|
*/ |
278 |
|
|
public boolean isWellDefined() { |
279 |
|
|
return updateCapabilitesUrl(); |
280 |
|
|
} |
281 |
|
|
|
282 |
|
|
/** |
283 |
alfonx |
1163 |
* Set the BaseUrl as a String. Any pending parameters are automatically cut |
284 |
|
|
* of. |
285 |
alfonx |
1161 |
*/ |
286 |
alfonx |
1163 |
public void setBaseUrl(String urlString) { |
287 |
|
|
try { |
288 |
|
|
// Cutoff any parameters |
289 |
|
|
if (urlString.indexOf("?") > -1) { |
290 |
|
|
urlString = urlString.substring(0, urlString.indexOf("?")); |
291 |
|
|
} |
292 |
alfonx |
1161 |
|
293 |
alfonx |
1163 |
setBaseUrl(new URL(urlString)); |
294 |
|
|
} catch (MalformedURLException e) { |
295 |
|
|
throw new RuntimeException(e); |
296 |
|
|
} |
297 |
alfonx |
1161 |
} |
298 |
|
|
|
299 |
|
|
public void setBaseUrl(URL baseUrl) { |
300 |
|
|
|
301 |
|
|
if (get(Key.BASE_URL) != baseUrl) { |
302 |
|
|
setCachedTypeNames(null); |
303 |
|
|
} |
304 |
|
|
|
305 |
|
|
put(Key.BASE_URL, baseUrl); |
306 |
|
|
|
307 |
|
|
updateCapabilitesUrl(); |
308 |
|
|
} |
309 |
|
|
|
310 |
alfonx |
1163 |
public void setCachedTypeNames(String[] cachedTypeNames) { |
311 |
|
|
this.cachedTypeNames = cachedTypeNames; |
312 |
|
|
} |
313 |
alfonx |
1161 |
|
314 |
alfonx |
1163 |
public void setMaxFeatures(java.lang.Integer maxFeatures) { |
315 |
|
|
put(WFSDataStoreFactory.MAXFEATURES.key, maxFeatures); |
316 |
|
|
} |
317 |
alfonx |
1161 |
|
318 |
alfonx |
1163 |
public void setTimeout(java.lang.Integer timeout) { |
319 |
|
|
put(WFSDataStoreFactory.TIMEOUT.key, timeout); |
320 |
alfonx |
1161 |
|
321 |
|
|
} |
322 |
|
|
|
323 |
|
|
public void setVersion(WfsProtocollVersion version) { |
324 |
|
|
|
325 |
|
|
if (get(Key.VERSION) != version) { |
326 |
|
|
setCachedTypeNames(null); |
327 |
|
|
} |
328 |
|
|
|
329 |
|
|
put(Key.VERSION, version); |
330 |
|
|
updateCapabilitesUrl(); |
331 |
|
|
} |
332 |
|
|
|
333 |
|
|
/** |
334 |
|
|
* @return transforms the settings to a String that can be stored in a |
335 |
|
|
* .properties line. @see #parsePropertiesString |
336 |
|
|
*/ |
337 |
|
|
public String toPropertiesString() { |
338 |
|
|
|
339 |
|
|
StringBuffer serialized = new StringBuffer(100); |
340 |
|
|
|
341 |
alfonx |
1163 |
// Title |
342 |
|
|
serialized.append(getTitle()); |
343 |
|
|
serialized.append(DELIMITER); |
344 |
|
|
|
345 |
alfonx |
1161 |
// BaseUrl |
346 |
|
|
serialized.append(getBaseUrl().toString()); |
347 |
|
|
serialized.append(DELIMITER); |
348 |
|
|
|
349 |
|
|
// Version |
350 |
|
|
serialized.append(getVersion().toString()); |
351 |
|
|
serialized.append(DELIMITER); |
352 |
|
|
|
353 |
alfonx |
1163 |
serialized.append(getMaxFeatures()); |
354 |
|
|
serialized.append(DELIMITER); |
355 |
alfonx |
1161 |
|
356 |
alfonx |
1163 |
serialized.append(getTimeout()); |
357 |
|
|
serialized.append(DELIMITER); |
358 |
alfonx |
1161 |
|
359 |
alfonx |
1163 |
serialized.append(getLenient()); |
360 |
|
|
serialized.append(DELIMITER); |
361 |
alfonx |
1161 |
|
362 |
alfonx |
1163 |
serialized.append(getHttpProtocol().getValue()); |
363 |
|
|
serialized.append(DELIMITER); |
364 |
alfonx |
1161 |
|
365 |
alfonx |
1163 |
serialized.append(getUsername()); |
366 |
|
|
serialized.append(DELIMITER); |
367 |
alfonx |
1161 |
|
368 |
alfonx |
1163 |
serialized.append(getPassword()); |
369 |
alfonx |
1164 |
// serialized.append(DELIMITER); |
370 |
alfonx |
1163 |
|
371 |
|
|
return serialized.toString(); |
372 |
alfonx |
1161 |
} |
373 |
|
|
|
374 |
|
|
@Override |
375 |
|
|
public String toString() { |
376 |
|
|
StringBuffer s = new StringBuffer(); |
377 |
|
|
|
378 |
|
|
URL baseUrl = getBaseUrl(); |
379 |
|
|
WfsProtocollVersion version = getVersion(); |
380 |
|
|
|
381 |
|
|
if (baseUrl != null) |
382 |
|
|
s.append(baseUrl.toString() + " "); |
383 |
|
|
|
384 |
|
|
if (version != null) |
385 |
|
|
s.append(version.getVersionString()); |
386 |
|
|
|
387 |
|
|
return s.toString(); |
388 |
|
|
} |
389 |
|
|
|
390 |
alfonx |
1163 |
public boolean updateCapabilitesUrl() { |
391 |
|
|
remove(WFSDataStoreFactory.URL.key); |
392 |
alfonx |
1161 |
|
393 |
alfonx |
1163 |
if (getBaseUrl() == null) |
394 |
|
|
return false; |
395 |
alfonx |
1161 |
|
396 |
alfonx |
1163 |
if (getVersion() == null) |
397 |
|
|
return false; |
398 |
alfonx |
1161 |
|
399 |
alfonx |
1163 |
try { |
400 |
alfonx |
1237 |
URL base = getBaseUrl(); |
401 |
alfonx |
1161 |
|
402 |
alfonx |
1237 |
String a = base.toExternalForm(); |
403 |
|
|
if (a.endsWith("/")) |
404 |
|
|
a = a.substring(0, a.length() - 1); |
405 |
|
|
|
406 |
|
|
URL fullUrl = new URL(a + "?service=WFS&version=" |
407 |
|
|
+ getVersion().getVersionString() |
408 |
|
|
+ "&request=GetCapabilities"); |
409 |
|
|
|
410 |
alfonx |
1163 |
put(WFSDataStoreFactory.URL.key, fullUrl); |
411 |
alfonx |
1161 |
|
412 |
alfonx |
1163 |
return true; |
413 |
|
|
} catch (MalformedURLException e) { |
414 |
|
|
return false; |
415 |
alfonx |
1161 |
} |
416 |
alfonx |
1163 |
} |
417 |
alfonx |
1161 |
|
418 |
alfonx |
1163 |
public void setPassword(String password) { |
419 |
|
|
if (password != null && password.isEmpty()) |
420 |
|
|
password = null; |
421 |
|
|
put(WFSDataStoreFactory.PASSWORD.key, password); |
422 |
|
|
} |
423 |
alfonx |
1161 |
|
424 |
alfonx |
1163 |
public String getPassword() { |
425 |
|
|
return (String) get(WFSDataStoreFactory.PASSWORD.key); |
426 |
alfonx |
1161 |
} |
427 |
|
|
|
428 |
alfonx |
1163 |
public void setUsername(String username) { |
429 |
|
|
if (username != null && username.isEmpty()) |
430 |
|
|
username = null; |
431 |
|
|
put(WFSDataStoreFactory.USERNAME.key, username); |
432 |
|
|
} |
433 |
alfonx |
1161 |
|
434 |
alfonx |
1163 |
public String getUsername() { |
435 |
|
|
return (String) get(WFSDataStoreFactory.USERNAME.key); |
436 |
alfonx |
1161 |
} |
437 |
alfonx |
1163 |
|
438 |
alfonx |
1161 |
} |