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 |
|
|
* {@link WfsServerSettings.Key}.<br/> |
30 |
|
|
* 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 |
1164 |
public class WfsServerSettings extends ServerSettings<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 |
|
|
* {@link DbServerSettings}, otherwise the edited instance. |
102 |
|
|
*/ |
103 |
|
|
public static WfsServerSettings createOrEdit(Component owner, |
104 |
|
|
WfsServerSettings wfsServer) { |
105 |
|
|
boolean newCreated = false; |
106 |
|
|
|
107 |
|
|
if (wfsServer == null) { |
108 |
|
|
newCreated = true; |
109 |
|
|
wfsServer = new WfsServerSettings(); |
110 |
|
|
} |
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 |
|
|
public static WfsServerSettings parsePropertiesString(String propString) |
202 |
|
|
throws MalformedURLException { |
203 |
|
|
|
204 |
|
|
try { |
205 |
|
|
String[] split = propString.split(Pattern.quote(DELIMITER)); |
206 |
|
|
|
207 |
|
|
WfsServerSettings wfs = new WfsServerSettings(); |
208 |
|
|
|
209 |
|
|
int i = 0; |
210 |
|
|
wfs.setTitle(split[i++]); |
211 |
|
|
wfs.setBaseUrl(new URL(split[i++])); |
212 |
|
|
wfs.setVersion(WfsProtocollVersion.valueOf(split[i++])); |
213 |
alfonx |
1164 |
wfs.setMaxFeatures(intOrNull(split[i++])); |
214 |
|
|
wfs.setTimeout(intOrNull(split[i++])); |
215 |
alfonx |
1163 |
wfs.setLenient(java.lang.Boolean.valueOf(split[i++])); |
216 |
|
|
wfs.setHttpProtocol(HttpProtocol.parse(split[i++])); |
217 |
|
|
|
218 |
alfonx |
1164 |
wfs.setUsername(stringOrNull(split[i++])); |
219 |
alfonx |
1163 |
|
220 |
alfonx |
1164 |
wfs.setPassword(stringOrNull(split[i++])); |
221 |
alfonx |
1163 |
|
222 |
|
|
return wfs; |
223 |
|
|
} catch (Exception e) { |
224 |
alfonx |
1164 |
Log.warn("couldn't parse " + propString, e); |
225 |
alfonx |
1163 |
return null; |
226 |
|
|
} |
227 |
|
|
|
228 |
|
|
} |
229 |
|
|
|
230 |
|
|
private String[] cachedTypeNames = null; |
231 |
|
|
|
232 |
alfonx |
1161 |
public WfsServerSettings() { |
233 |
alfonx |
1164 |
this(defaultURl, WfsProtocollVersion.v1_0_0); |
234 |
alfonx |
1161 |
} |
235 |
|
|
|
236 |
alfonx |
1163 |
public WfsServerSettings(URL baseUrl, WfsProtocollVersion version) { |
237 |
|
|
setVersion(version); |
238 |
|
|
setBaseUrl(baseUrl); |
239 |
alfonx |
1164 |
setLenient(false); |
240 |
alfonx |
1163 |
} |
241 |
|
|
|
242 |
|
|
public URL getBaseUrl() { |
243 |
|
|
return (URL) get(Key.BASE_URL); |
244 |
|
|
} |
245 |
|
|
|
246 |
|
|
public String[] getCachedTypeNames() { |
247 |
|
|
return cachedTypeNames; |
248 |
|
|
} |
249 |
|
|
|
250 |
alfonx |
1161 |
/** |
251 |
alfonx |
1163 |
* @return <code>null</code> if not correctly defined, otherwise the URL of |
252 |
|
|
* the GetCapabilites request. |
253 |
|
|
*/ |
254 |
|
|
public URL getCapabilitiesUrl() { |
255 |
|
|
return (URL) get(WFSDataStoreFactory.URL.key); |
256 |
|
|
} |
257 |
|
|
|
258 |
|
|
public java.lang.Integer getMaxFeatures() { |
259 |
|
|
return (java.lang.Integer) get(WFSDataStoreFactory.MAXFEATURES.key); |
260 |
|
|
} |
261 |
|
|
|
262 |
|
|
public java.lang.Integer getTimeout() { |
263 |
|
|
return (java.lang.Integer) get(WFSDataStoreFactory.TIMEOUT.key); |
264 |
|
|
} |
265 |
|
|
|
266 |
|
|
public WfsProtocollVersion getVersion() { |
267 |
|
|
return (WfsProtocollVersion) get(Key.VERSION); |
268 |
|
|
} |
269 |
|
|
|
270 |
|
|
/** |
271 |
alfonx |
1161 |
* @return <code>true</code>, if all parameters look OK, without actually |
272 |
|
|
* opening any connection. |
273 |
|
|
*/ |
274 |
|
|
public boolean isWellDefined() { |
275 |
|
|
return updateCapabilitesUrl(); |
276 |
|
|
} |
277 |
|
|
|
278 |
|
|
/** |
279 |
alfonx |
1163 |
* Set the BaseUrl as a String. Any pending parameters are automatically cut |
280 |
|
|
* of. |
281 |
alfonx |
1161 |
*/ |
282 |
alfonx |
1163 |
public void setBaseUrl(String urlString) { |
283 |
|
|
try { |
284 |
|
|
// Cutoff any parameters |
285 |
|
|
if (urlString.indexOf("?") > -1) { |
286 |
|
|
urlString = urlString.substring(0, urlString.indexOf("?")); |
287 |
|
|
} |
288 |
alfonx |
1161 |
|
289 |
alfonx |
1163 |
setBaseUrl(new URL(urlString)); |
290 |
|
|
} catch (MalformedURLException e) { |
291 |
|
|
throw new RuntimeException(e); |
292 |
|
|
} |
293 |
alfonx |
1161 |
} |
294 |
|
|
|
295 |
|
|
public void setBaseUrl(URL baseUrl) { |
296 |
|
|
|
297 |
|
|
if (get(Key.BASE_URL) != baseUrl) { |
298 |
|
|
setCachedTypeNames(null); |
299 |
|
|
} |
300 |
|
|
|
301 |
|
|
put(Key.BASE_URL, baseUrl); |
302 |
|
|
|
303 |
|
|
updateCapabilitesUrl(); |
304 |
|
|
} |
305 |
|
|
|
306 |
alfonx |
1163 |
public void setCachedTypeNames(String[] cachedTypeNames) { |
307 |
|
|
this.cachedTypeNames = cachedTypeNames; |
308 |
|
|
} |
309 |
alfonx |
1161 |
|
310 |
alfonx |
1163 |
public void setMaxFeatures(java.lang.Integer maxFeatures) { |
311 |
|
|
put(WFSDataStoreFactory.MAXFEATURES.key, maxFeatures); |
312 |
|
|
} |
313 |
alfonx |
1161 |
|
314 |
alfonx |
1163 |
public void setTimeout(java.lang.Integer timeout) { |
315 |
|
|
put(WFSDataStoreFactory.TIMEOUT.key, timeout); |
316 |
alfonx |
1161 |
|
317 |
|
|
} |
318 |
|
|
|
319 |
|
|
public void setVersion(WfsProtocollVersion version) { |
320 |
|
|
|
321 |
|
|
if (get(Key.VERSION) != version) { |
322 |
|
|
setCachedTypeNames(null); |
323 |
|
|
} |
324 |
|
|
|
325 |
|
|
put(Key.VERSION, version); |
326 |
|
|
updateCapabilitesUrl(); |
327 |
|
|
} |
328 |
|
|
|
329 |
|
|
/** |
330 |
|
|
* @return transforms the settings to a String that can be stored in a |
331 |
|
|
* .properties line. @see #parsePropertiesString |
332 |
|
|
*/ |
333 |
|
|
public String toPropertiesString() { |
334 |
|
|
|
335 |
|
|
StringBuffer serialized = new StringBuffer(100); |
336 |
|
|
|
337 |
alfonx |
1163 |
// Title |
338 |
|
|
serialized.append(getTitle()); |
339 |
|
|
serialized.append(DELIMITER); |
340 |
|
|
|
341 |
alfonx |
1161 |
// BaseUrl |
342 |
|
|
serialized.append(getBaseUrl().toString()); |
343 |
|
|
serialized.append(DELIMITER); |
344 |
|
|
|
345 |
|
|
// Version |
346 |
|
|
serialized.append(getVersion().toString()); |
347 |
|
|
serialized.append(DELIMITER); |
348 |
|
|
|
349 |
alfonx |
1163 |
serialized.append(getMaxFeatures()); |
350 |
|
|
serialized.append(DELIMITER); |
351 |
alfonx |
1161 |
|
352 |
alfonx |
1163 |
serialized.append(getTimeout()); |
353 |
|
|
serialized.append(DELIMITER); |
354 |
alfonx |
1161 |
|
355 |
alfonx |
1163 |
serialized.append(getLenient()); |
356 |
|
|
serialized.append(DELIMITER); |
357 |
alfonx |
1161 |
|
358 |
alfonx |
1163 |
serialized.append(getHttpProtocol().getValue()); |
359 |
|
|
serialized.append(DELIMITER); |
360 |
alfonx |
1161 |
|
361 |
alfonx |
1163 |
serialized.append(getUsername()); |
362 |
|
|
serialized.append(DELIMITER); |
363 |
alfonx |
1161 |
|
364 |
alfonx |
1163 |
serialized.append(getPassword()); |
365 |
alfonx |
1164 |
// serialized.append(DELIMITER); |
366 |
alfonx |
1163 |
|
367 |
|
|
return serialized.toString(); |
368 |
alfonx |
1161 |
} |
369 |
|
|
|
370 |
|
|
@Override |
371 |
|
|
public String toString() { |
372 |
|
|
StringBuffer s = new StringBuffer(); |
373 |
|
|
|
374 |
|
|
URL baseUrl = getBaseUrl(); |
375 |
|
|
WfsProtocollVersion version = getVersion(); |
376 |
|
|
|
377 |
|
|
if (baseUrl != null) |
378 |
|
|
s.append(baseUrl.toString() + " "); |
379 |
|
|
|
380 |
|
|
if (version != null) |
381 |
|
|
s.append(version.getVersionString()); |
382 |
|
|
|
383 |
|
|
return s.toString(); |
384 |
|
|
} |
385 |
|
|
|
386 |
alfonx |
1163 |
public boolean updateCapabilitesUrl() { |
387 |
|
|
remove(WFSDataStoreFactory.URL.key); |
388 |
alfonx |
1161 |
|
389 |
alfonx |
1163 |
if (getBaseUrl() == null) |
390 |
|
|
return false; |
391 |
alfonx |
1161 |
|
392 |
alfonx |
1163 |
if (getVersion() == null) |
393 |
|
|
return false; |
394 |
alfonx |
1161 |
|
395 |
alfonx |
1163 |
try { |
396 |
alfonx |
1237 |
URL base = getBaseUrl(); |
397 |
alfonx |
1161 |
|
398 |
alfonx |
1237 |
String a = base.toExternalForm(); |
399 |
|
|
if (a.endsWith("/")) |
400 |
|
|
a = a.substring(0, a.length() - 1); |
401 |
|
|
|
402 |
|
|
URL fullUrl = new URL(a + "?service=WFS&version=" |
403 |
|
|
+ getVersion().getVersionString() |
404 |
|
|
+ "&request=GetCapabilities"); |
405 |
|
|
|
406 |
alfonx |
1163 |
put(WFSDataStoreFactory.URL.key, fullUrl); |
407 |
alfonx |
1161 |
|
408 |
alfonx |
1163 |
return true; |
409 |
|
|
} catch (MalformedURLException e) { |
410 |
|
|
return false; |
411 |
alfonx |
1161 |
} |
412 |
alfonx |
1163 |
} |
413 |
alfonx |
1161 |
|
414 |
alfonx |
1163 |
public void setPassword(String password) { |
415 |
|
|
if (password != null && password.isEmpty()) |
416 |
|
|
password = null; |
417 |
|
|
put(WFSDataStoreFactory.PASSWORD.key, password); |
418 |
|
|
} |
419 |
alfonx |
1161 |
|
420 |
alfonx |
1163 |
public String getPassword() { |
421 |
|
|
return (String) get(WFSDataStoreFactory.PASSWORD.key); |
422 |
alfonx |
1161 |
} |
423 |
|
|
|
424 |
alfonx |
1163 |
public void setUsername(String username) { |
425 |
|
|
if (username != null && username.isEmpty()) |
426 |
|
|
username = null; |
427 |
|
|
put(WFSDataStoreFactory.USERNAME.key, username); |
428 |
|
|
} |
429 |
alfonx |
1161 |
|
430 |
alfonx |
1163 |
public String getUsername() { |
431 |
|
|
return (String) get(WFSDataStoreFactory.USERNAME.key); |
432 |
alfonx |
1161 |
} |
433 |
alfonx |
1163 |
|
434 |
alfonx |
1161 |
} |