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