/[schmitzm]/branches/2.3.x/src/skrueger/geotools/io/WfsServerSettings.java
ViewVC logotype

Contents of /branches/2.3.x/src/skrueger/geotools/io/WfsServerSettings.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1237 - (show annotations)
Fri Nov 5 10:08:21 2010 UTC (14 years, 3 months ago) by alfonx
Original Path: trunk/src/skrueger/geotools/io/WfsServerSettings.java
File MIME type: text/plain
File size: 11634 byte(s)
Made the WFS connector working  a bit with deegree 3
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.geotools.data.wfs.WFSDataStoreFactory;
11 import org.jfree.util.Log;
12
13 import schmitzm.swing.BooleanInputOption;
14 import schmitzm.swing.ManualInputOption;
15 import schmitzm.swing.ManualInputOption.Integer;
16 import schmitzm.swing.ManualInputOption.PasswordViewable;
17 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 public class WfsServerSettings extends ServerSettings<Object, Object> {
35
36 public enum Key {
37 BASE_URL, VERSION
38 }
39
40 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 public enum WfsProtocollVersion {
72 v1_0_0("1.0.0"), v1_1_0("1.1.0"), v1_1_1("1.1.1");
73
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 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
94 /**
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 }
178
179 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 wfs.setMaxFeatures(intOrNull(split[i++]));
214 wfs.setTimeout(intOrNull(split[i++]));
215 wfs.setLenient(java.lang.Boolean.valueOf(split[i++]));
216 wfs.setHttpProtocol(HttpProtocol.parse(split[i++]));
217
218 wfs.setUsername(stringOrNull(split[i++]));
219
220 wfs.setPassword(stringOrNull(split[i++]));
221
222 return wfs;
223 } catch (Exception e) {
224 Log.warn("couldn't parse " + propString, e);
225 return null;
226 }
227
228 }
229
230 private String[] cachedTypeNames = null;
231
232 public WfsServerSettings() {
233 this(defaultURl, WfsProtocollVersion.v1_0_0);
234 }
235
236 public WfsServerSettings(URL baseUrl, WfsProtocollVersion version) {
237 setVersion(version);
238 setBaseUrl(baseUrl);
239 setLenient(false);
240 }
241
242 public URL getBaseUrl() {
243 return (URL) get(Key.BASE_URL);
244 }
245
246 public String[] getCachedTypeNames() {
247 return cachedTypeNames;
248 }
249
250 /**
251 * @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 * @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 * Set the BaseUrl as a String. Any pending parameters are automatically cut
280 * of.
281 */
282 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
289 setBaseUrl(new URL(urlString));
290 } catch (MalformedURLException e) {
291 throw new RuntimeException(e);
292 }
293 }
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 public void setCachedTypeNames(String[] cachedTypeNames) {
307 this.cachedTypeNames = cachedTypeNames;
308 }
309
310 public void setMaxFeatures(java.lang.Integer maxFeatures) {
311 put(WFSDataStoreFactory.MAXFEATURES.key, maxFeatures);
312 }
313
314 public void setTimeout(java.lang.Integer timeout) {
315 put(WFSDataStoreFactory.TIMEOUT.key, timeout);
316
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 // Title
338 serialized.append(getTitle());
339 serialized.append(DELIMITER);
340
341 // BaseUrl
342 serialized.append(getBaseUrl().toString());
343 serialized.append(DELIMITER);
344
345 // Version
346 serialized.append(getVersion().toString());
347 serialized.append(DELIMITER);
348
349 serialized.append(getMaxFeatures());
350 serialized.append(DELIMITER);
351
352 serialized.append(getTimeout());
353 serialized.append(DELIMITER);
354
355 serialized.append(getLenient());
356 serialized.append(DELIMITER);
357
358 serialized.append(getHttpProtocol().getValue());
359 serialized.append(DELIMITER);
360
361 serialized.append(getUsername());
362 serialized.append(DELIMITER);
363
364 serialized.append(getPassword());
365 // serialized.append(DELIMITER);
366
367 return serialized.toString();
368 }
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 public boolean updateCapabilitesUrl() {
387 remove(WFSDataStoreFactory.URL.key);
388
389 if (getBaseUrl() == null)
390 return false;
391
392 if (getVersion() == null)
393 return false;
394
395 try {
396 URL base = getBaseUrl();
397
398 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 put(WFSDataStoreFactory.URL.key, fullUrl);
407
408 return true;
409 } catch (MalformedURLException e) {
410 return false;
411 }
412 }
413
414 public void setPassword(String password) {
415 if (password != null && password.isEmpty())
416 password = null;
417 put(WFSDataStoreFactory.PASSWORD.key, password);
418 }
419
420 public String getPassword() {
421 return (String) get(WFSDataStoreFactory.PASSWORD.key);
422 }
423
424 public void setUsername(String username) {
425 if (username != null && username.isEmpty())
426 username = null;
427 put(WFSDataStoreFactory.USERNAME.key, username);
428 }
429
430 public String getUsername() {
431 return (String) get(WFSDataStoreFactory.USERNAME.key);
432 }
433
434 }

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