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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1327 - (hide annotations)
Sun Dec 5 17:23:22 2010 UTC (14 years, 3 months ago) by alfonx
File MIME type: text/plain
File size: 11819 byte(s)
added "isAvailable" to ServerSettings interface
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 alfonx 1327 import org.apache.commons.lang.NotImplementedException;
11 alfonx 1161 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 alfonx 1295 * {@link GtWfsServerSettings.Key}.<br/>
31 alfonx 1161 * 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 1327 public class GtWfsServerSettings extends
36     AbstractGTServerSettings<Object, Object> {
37 alfonx 1161
38     public enum Key {
39     BASE_URL, VERSION
40     }
41    
42 alfonx 1163 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 alfonx 1161 public enum WfsProtocollVersion {
74 alfonx 1163 v1_0_0("1.0.0"), v1_1_0("1.1.0"), v1_1_1("1.1.1");
75 alfonx 1161
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 alfonx 1164 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 alfonx 1161
96 alfonx 1163 /**
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 alfonx 1295 * {@link GtDbServerSettings}, otherwise the edited instance.
104 alfonx 1163 */
105 alfonx 1295 public static GtWfsServerSettings createOrEdit(Component owner,
106     GtWfsServerSettings wfsServer) {
107 alfonx 1163 boolean newCreated = false;
108    
109     if (wfsServer == null) {
110     newCreated = true;
111 alfonx 1295 wfsServer = new GtWfsServerSettings();
112 alfonx 1163 }
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 alfonx 1161 }
180    
181 alfonx 1163 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 alfonx 1291 @Override
204 alfonx 1295 public boolean parsePropertiesString(String propString) {
205 alfonx 1163
206     try {
207     String[] split = propString.split(Pattern.quote(DELIMITER));
208    
209 alfonx 1295 // WfsServerSettings wfs = new WfsServerSettings();
210 alfonx 1163
211     int i = 0;
212 alfonx 1292 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 alfonx 1163
220 alfonx 1292 setUsername(stringOrNull(split[i++]));
221 alfonx 1163
222 alfonx 1292 setPassword(stringOrNull(split[i++]));
223 alfonx 1163
224 alfonx 1292 return true;
225 alfonx 1163 } catch (Exception e) {
226 alfonx 1164 Log.warn("couldn't parse " + propString, e);
227 alfonx 1292 return false;
228 alfonx 1163 }
229    
230     }
231    
232     private String[] cachedTypeNames = null;
233    
234 alfonx 1295 public GtWfsServerSettings() {
235 alfonx 1164 this(defaultURl, WfsProtocollVersion.v1_0_0);
236 alfonx 1161 }
237    
238 alfonx 1295 public GtWfsServerSettings(URL baseUrl, WfsProtocollVersion version) {
239 alfonx 1163 setVersion(version);
240     setBaseUrl(baseUrl);
241 alfonx 1164 setLenient(false);
242 alfonx 1163 }
243    
244 alfonx 1295 public GtWfsServerSettings(String s) {
245     parsePropertiesString(s);
246 alfonx 1291 }
247    
248 alfonx 1163 public URL getBaseUrl() {
249     return (URL) get(Key.BASE_URL);
250     }
251    
252     public String[] getCachedTypeNames() {
253     return cachedTypeNames;
254     }
255    
256 alfonx 1161 /**
257 alfonx 1163 * @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 alfonx 1161 * @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 alfonx 1163 * Set the BaseUrl as a String. Any pending parameters are automatically cut
286     * of.
287 alfonx 1161 */
288 alfonx 1163 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 alfonx 1161
295 alfonx 1163 setBaseUrl(new URL(urlString));
296     } catch (MalformedURLException e) {
297     throw new RuntimeException(e);
298     }
299 alfonx 1161 }
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 alfonx 1163 public void setCachedTypeNames(String[] cachedTypeNames) {
313     this.cachedTypeNames = cachedTypeNames;
314     }
315 alfonx 1161
316 alfonx 1163 public void setMaxFeatures(java.lang.Integer maxFeatures) {
317     put(WFSDataStoreFactory.MAXFEATURES.key, maxFeatures);
318     }
319 alfonx 1161
320 alfonx 1163 public void setTimeout(java.lang.Integer timeout) {
321     put(WFSDataStoreFactory.TIMEOUT.key, timeout);
322 alfonx 1161
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 alfonx 1163 // Title
344     serialized.append(getTitle());
345     serialized.append(DELIMITER);
346    
347 alfonx 1161 // BaseUrl
348     serialized.append(getBaseUrl().toString());
349     serialized.append(DELIMITER);
350    
351     // Version
352     serialized.append(getVersion().toString());
353     serialized.append(DELIMITER);
354    
355 alfonx 1163 serialized.append(getMaxFeatures());
356     serialized.append(DELIMITER);
357 alfonx 1161
358 alfonx 1163 serialized.append(getTimeout());
359     serialized.append(DELIMITER);
360 alfonx 1161
361 alfonx 1163 serialized.append(getLenient());
362     serialized.append(DELIMITER);
363 alfonx 1161
364 alfonx 1163 serialized.append(getHttpProtocol().getValue());
365     serialized.append(DELIMITER);
366 alfonx 1161
367 alfonx 1163 serialized.append(getUsername());
368     serialized.append(DELIMITER);
369 alfonx 1161
370 alfonx 1163 serialized.append(getPassword());
371 alfonx 1164 // serialized.append(DELIMITER);
372 alfonx 1163
373     return serialized.toString();
374 alfonx 1161 }
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 alfonx 1163 public boolean updateCapabilitesUrl() {
393     remove(WFSDataStoreFactory.URL.key);
394 alfonx 1161
395 alfonx 1163 if (getBaseUrl() == null)
396     return false;
397 alfonx 1161
398 alfonx 1163 if (getVersion() == null)
399     return false;
400 alfonx 1161
401 alfonx 1163 try {
402 alfonx 1237 URL base = getBaseUrl();
403 alfonx 1161
404 alfonx 1237 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 alfonx 1163 put(WFSDataStoreFactory.URL.key, fullUrl);
413 alfonx 1161
414 alfonx 1163 return true;
415     } catch (MalformedURLException e) {
416     return false;
417 alfonx 1161 }
418 alfonx 1163 }
419 alfonx 1161
420 alfonx 1163 public void setPassword(String password) {
421     if (password != null && password.isEmpty())
422     password = null;
423     put(WFSDataStoreFactory.PASSWORD.key, password);
424     }
425 alfonx 1161
426 alfonx 1163 public String getPassword() {
427     return (String) get(WFSDataStoreFactory.PASSWORD.key);
428 alfonx 1161 }
429    
430 alfonx 1163 public void setUsername(String username) {
431     if (username != null && username.isEmpty())
432     username = null;
433     put(WFSDataStoreFactory.USERNAME.key, username);
434     }
435 alfonx 1161
436 alfonx 1163 public String getUsername() {
437     return (String) get(WFSDataStoreFactory.USERNAME.key);
438 alfonx 1161 }
439 alfonx 1163
440 alfonx 1327 @Override
441     public boolean isAvailable() {
442     // TODO
443     throw new NotImplementedException("");
444     }
445    
446 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