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

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

Parent Directory Parent Directory | Revision Log Revision Log


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