/[schmitzm]/branches/2.4.x/src/skrueger/geotools/io/GtWfsServerSettings.java
ViewVC logotype

Contents of /branches/2.4.x/src/skrueger/geotools/io/GtWfsServerSettings.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1383 - (show annotations)
Wed Jan 26 13:46:20 2011 UTC (14 years, 1 month ago) by alfonx
File MIME type: text/plain
File size: 11819 byte(s)
trunk becomes 2.4.x ... starting to create multiple modules

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

Properties

Name Value
svn:eol-style native
svn:keywords Id URL
svn:mime-type text/plain

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26