/[schmitzm]/trunk/src/skrueger/geotools/io/WfsServerSettings.java
ViewVC logotype

Annotation of /trunk/src/skrueger/geotools/io/WfsServerSettings.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1163 - (hide annotations)
Sun Oct 24 22:55:43 2010 UTC (14 years, 4 months ago) by alfonx
File MIME type: text/plain
File size: 12173 byte(s)
Improved the AtlasStyler Import-Wizard GUIwise
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     public class WfsServerSettings extends HashMap<Object, Object> {
36    
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     /**
87     * Character used to separate the parameters when serializing settings to a
88     * String
89     */
90     private static final String DELIMITER = "|";
91    
92 alfonx 1163 /**
93     * Opens a GUI that asks the use define a DB connection.
94     *
95     * @param wfsServer
96     * <code>null</code> to create a new instance, or an instance to
97     * edit.
98     * @return <code>null</code> if the user cancelled the creation of a new
99     * {@link DbServerSettings}, otherwise the edited instance.
100     */
101     public static WfsServerSettings createOrEdit(Component owner,
102     WfsServerSettings wfsServer) {
103     boolean newCreated = false;
104    
105     if (wfsServer == null) {
106     newCreated = true;
107     wfsServer = new WfsServerSettings();
108     }
109    
110     Text hostInput = new ManualInputOption.Text(
111     "BaseURL (without any paramters)", true, wfsServer.getBaseUrl()
112     .toString());
113    
114     Combo<WfsProtocollVersion> versionInput = new SelectionInputOption.Combo<WfsProtocollVersion>(
115     "WFS Version", true, WfsProtocollVersion.values(),
116     ArrayUtils.indexOf(WfsProtocollVersion.values(),
117     wfsServer.getVersion()), WfsProtocollVersion.values());
118     versionInput
119     .setToolTipText("If you have problems with Curves in GML3, try version 1.0.0.");
120    
121     Integer maxFeaturesInput = new ManualInputOption.Integer(
122     "Max. features per request (0=no limit)", false,
123     wfsServer.getMaxFeatures());
124     maxFeaturesInput
125     .setToolTipText(WFSDataStoreFactory.MAXFEATURES.description
126     .toString());
127    
128     Integer timeoutInput = new ManualInputOption.Integer("Timout in ms:",
129     false, wfsServer.getTimeout());
130     timeoutInput.setToolTipText(WFSDataStoreFactory.TIMEOUT.description
131     .toString());
132    
133     BooleanInputOption lenientInput = new BooleanInputOption(
134     "lenient (=ignore errors)", wfsServer.getLenient());
135     lenientInput.setToolTipText(WFSDataStoreFactory.LENIENT.description
136     .toString());
137    
138     SelectionInputOption.Combo<HttpProtocol> httpInput = new SelectionInputOption.Combo<HttpProtocol>(
139     "Ust HTTP GET or POST:", true, HttpProtocol.values(),
140     ArrayUtils.indexOf(HttpProtocol.values(),
141     wfsServer.getHttpProtocol()), HttpProtocol.values());
142     httpInput.setToolTipText(WFSDataStoreFactory.PROTOCOL.description
143     .toString());
144    
145     Text userInput = new ManualInputOption.Text(
146     "Optional HTTPAuth Username", false, wfsServer.getUsername());
147    
148     PasswordViewable passwdInput = new ManualInputOption.PasswordViewable(
149     "Optional HTTPAuth Password", false, wfsServer.getPassword());
150    
151     // Show the options
152     Object[] input = MultipleOptionPane.showMultipleInputDialog(owner,
153     "WFS Connection parameters", hostInput, versionInput,
154     maxFeaturesInput, timeoutInput, lenientInput, httpInput,
155     userInput, passwdInput);
156    
157     if (input == null) {
158     if (newCreated)
159     return null;
160     else
161     return wfsServer;
162     } else {
163     wfsServer.setBaseUrl((String) input[0]);
164     wfsServer.setVersion(((WfsProtocollVersion) input[1]));
165     wfsServer.setMaxFeatures((java.lang.Integer) input[2]);
166     wfsServer.setTimeout((java.lang.Integer) input[3]);
167     wfsServer.setLenient((java.lang.Boolean) input[4]);
168     wfsServer.setHttpProtocol((HttpProtocol) input[5]);
169     wfsServer.setUsername((String) input[6]);
170     wfsServer.setPassword(String.valueOf((char[]) input[7]));
171     }
172    
173     return wfsServer;
174    
175 alfonx 1161 }
176    
177 alfonx 1163 public HttpProtocol getHttpProtocol() {
178     return HttpProtocol
179     .parse((Boolean) get(WFSDataStoreFactory.PROTOCOL.key));
180     }
181    
182     public void setHttpProtocol(HttpProtocol protocol) {
183     put(WFSDataStoreFactory.PROTOCOL.key, protocol.getValue());
184     }
185    
186     private Boolean getLenient() {
187     return (Boolean) get(WFSDataStoreFactory.LENIENT.key);
188     }
189    
190     private void setLenient(Boolean lenient) {
191     put(WFSDataStoreFactory.LENIENT.key, lenient);
192     }
193    
194     /**
195     * @return transforms the settings to a String that can be stored in a
196     * .properties line. @see #parsePropertiesString
197     * @throws MalformedURLException
198     */
199     public static WfsServerSettings parsePropertiesString(String propString)
200     throws MalformedURLException {
201    
202     try {
203     String[] split = propString.split(Pattern.quote(DELIMITER));
204    
205     WfsServerSettings wfs = new WfsServerSettings();
206    
207     int i = 0;
208     wfs.setTitle(split[i++]);
209     wfs.setBaseUrl(new URL(split[i++]));
210     wfs.setVersion(WfsProtocollVersion.valueOf(split[i++]));
211     wfs.setMaxFeatures(java.lang.Integer.valueOf(split[i++]));
212     wfs.setTimeout(java.lang.Integer.valueOf(split[i++]));
213     wfs.setLenient(java.lang.Boolean.valueOf(split[i++]));
214     wfs.setHttpProtocol(HttpProtocol.parse(split[i++]));
215    
216     String userRaw = split[i++];
217     wfs.setUsername(userRaw.equals("null") ? null : userRaw);
218    
219     String pwdRaw = split[i++];
220     wfs.setPassword(pwdRaw.equals("null") ? null : pwdRaw);
221    
222     return wfs;
223     } catch (Exception e) {
224     Log.warn("couldn't parse " + propString);
225     return null;
226     }
227    
228     }
229    
230     private String[] cachedTypeNames = null;
231     private String title;
232    
233 alfonx 1161 public WfsServerSettings() {
234     try {
235     setBaseUrl(new URL("http://localhost:8080/geoserver/ows"));
236     } catch (MalformedURLException e) {
237     throw new RuntimeException(e);
238     }
239     }
240    
241 alfonx 1163 public WfsServerSettings(URL baseUrl, WfsProtocollVersion version) {
242     setVersion(version);
243     setBaseUrl(baseUrl);
244     }
245    
246     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 String getTitle() {
271     if (title == null)
272     return toString();
273     return title;
274     }
275    
276     public WfsProtocollVersion getVersion() {
277     return (WfsProtocollVersion) get(Key.VERSION);
278     }
279    
280     /**
281 alfonx 1161 * @return <code>true</code>, if all parameters look OK, without actually
282     * opening any connection.
283     */
284     public boolean isWellDefined() {
285     return updateCapabilitesUrl();
286     }
287    
288     /**
289 alfonx 1163 * Set the BaseUrl as a String. Any pending parameters are automatically cut
290     * of.
291 alfonx 1161 */
292 alfonx 1163 public void setBaseUrl(String urlString) {
293     try {
294     // Cutoff any parameters
295     if (urlString.indexOf("?") > -1) {
296     urlString = urlString.substring(0, urlString.indexOf("?"));
297     }
298 alfonx 1161
299 alfonx 1163 setBaseUrl(new URL(urlString));
300     } catch (MalformedURLException e) {
301     throw new RuntimeException(e);
302     }
303 alfonx 1161 }
304    
305     public void setBaseUrl(URL baseUrl) {
306    
307     if (get(Key.BASE_URL) != baseUrl) {
308     setCachedTypeNames(null);
309     }
310    
311     put(Key.BASE_URL, baseUrl);
312    
313     updateCapabilitesUrl();
314     }
315    
316 alfonx 1163 public void setCachedTypeNames(String[] cachedTypeNames) {
317     this.cachedTypeNames = cachedTypeNames;
318     }
319 alfonx 1161
320 alfonx 1163 public void setMaxFeatures(java.lang.Integer maxFeatures) {
321     put(WFSDataStoreFactory.MAXFEATURES.key, maxFeatures);
322     }
323 alfonx 1161
324 alfonx 1163 public void setTimeout(java.lang.Integer timeout) {
325     put(WFSDataStoreFactory.TIMEOUT.key, timeout);
326 alfonx 1161
327     }
328    
329     public void setVersion(WfsProtocollVersion version) {
330    
331     if (get(Key.VERSION) != version) {
332     setCachedTypeNames(null);
333     }
334    
335     put(Key.VERSION, version);
336     updateCapabilitesUrl();
337     }
338    
339     /**
340     * @return transforms the settings to a String that can be stored in a
341     * .properties line. @see #parsePropertiesString
342     */
343     public String toPropertiesString() {
344    
345     StringBuffer serialized = new StringBuffer(100);
346    
347 alfonx 1163 // Title
348     serialized.append(getTitle());
349     serialized.append(DELIMITER);
350    
351 alfonx 1161 // BaseUrl
352     serialized.append(getBaseUrl().toString());
353     serialized.append(DELIMITER);
354    
355     // Version
356     serialized.append(getVersion().toString());
357     serialized.append(DELIMITER);
358    
359 alfonx 1163 serialized.append(getMaxFeatures());
360     serialized.append(DELIMITER);
361 alfonx 1161
362 alfonx 1163 serialized.append(getTimeout());
363     serialized.append(DELIMITER);
364 alfonx 1161
365 alfonx 1163 serialized.append(getLenient());
366     serialized.append(DELIMITER);
367 alfonx 1161
368 alfonx 1163 serialized.append(getHttpProtocol().getValue());
369     serialized.append(DELIMITER);
370 alfonx 1161
371 alfonx 1163 serialized.append(getUsername());
372     serialized.append(DELIMITER);
373 alfonx 1161
374 alfonx 1163 serialized.append(getPassword());
375     serialized.append(DELIMITER);
376    
377     return serialized.toString();
378 alfonx 1161 }
379    
380     @Override
381     public String toString() {
382     StringBuffer s = new StringBuffer();
383    
384     URL baseUrl = getBaseUrl();
385     WfsProtocollVersion version = getVersion();
386    
387     if (baseUrl != null)
388     s.append(baseUrl.toString() + " ");
389    
390     if (version != null)
391     s.append(version.getVersionString());
392    
393     return s.toString();
394     }
395    
396 alfonx 1163 public boolean updateCapabilitesUrl() {
397     remove(WFSDataStoreFactory.URL.key);
398 alfonx 1161
399 alfonx 1163 if (getBaseUrl() == null)
400     return false;
401 alfonx 1161
402 alfonx 1163 if (getVersion() == null)
403     return false;
404 alfonx 1161
405 alfonx 1163 try {
406     URL fullUrl = DataUtilities.extendURL(getBaseUrl(),
407     "?service=wfs&version=" + getVersion().getVersionString()
408     + "&request=GetCapabilities");
409 alfonx 1161
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 setTitle(String title) {
419     if (title != null) {
420     if (title.contains(DELIMITER))
421     throw new IllegalArgumentException("Title may not contain "
422     + DELIMITER);
423     if (title.contains(WfsServerList.DELIMITER))
424     throw new IllegalArgumentException("Title may not contain "
425     + WfsServerList.DELIMITER);
426 alfonx 1161 }
427 alfonx 1163 this.title = title;
428     }
429 alfonx 1161
430 alfonx 1163 public void setPassword(String password) {
431     if (password != null && password.isEmpty())
432     password = null;
433     put(WFSDataStoreFactory.PASSWORD.key, password);
434     }
435 alfonx 1161
436 alfonx 1163 public String getPassword() {
437     return (String) get(WFSDataStoreFactory.PASSWORD.key);
438 alfonx 1161 }
439    
440 alfonx 1163 public void setUsername(String username) {
441     if (username != null && username.isEmpty())
442     username = null;
443     put(WFSDataStoreFactory.USERNAME.key, username);
444     }
445 alfonx 1161
446 alfonx 1163 public String getUsername() {
447     return (String) get(WFSDataStoreFactory.USERNAME.key);
448 alfonx 1161 }
449 alfonx 1163
450 alfonx 1161 }

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