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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/src/skrueger/geotools/io/WfsServerSettings.java revision 1161 by alfonx, Sun Oct 24 16:44:18 2010 UTC branches/2.3.x/src/skrueger/geotools/io/WfsServerSettings.java revision 1240 by alfonx, Fri Nov 5 13:02:30 2010 UTC
# Line 7  import java.util.HashMap; Line 7  import java.util.HashMap;
7  import java.util.regex.Pattern;  import java.util.regex.Pattern;
8    
9  import org.apache.commons.lang.ArrayUtils;  import org.apache.commons.lang.ArrayUtils;
 import org.geotools.data.DataUtilities;  
10  import org.geotools.data.wfs.WFSDataStoreFactory;  import org.geotools.data.wfs.WFSDataStoreFactory;
11    import org.jfree.util.Log;
12    
13    import schmitzm.swing.BooleanInputOption;
14  import schmitzm.swing.ManualInputOption;  import schmitzm.swing.ManualInputOption;
15    import schmitzm.swing.ManualInputOption.Integer;
16    import schmitzm.swing.ManualInputOption.PasswordViewable;
17  import schmitzm.swing.ManualInputOption.Text;  import schmitzm.swing.ManualInputOption.Text;
18  import schmitzm.swing.MultipleOptionPane;  import schmitzm.swing.MultipleOptionPane;
19  import schmitzm.swing.SelectionInputOption;  import schmitzm.swing.SelectionInputOption;
# Line 28  import schmitzm.swing.SelectionInputOpti Line 31  import schmitzm.swing.SelectionInputOpti
31   * connection into a {@link String} with {@link #toPropertiesString()} and   * connection into a {@link String} with {@link #toPropertiesString()} and
32   * re-import the String with {@link #parsePropertiesString(String)}.   * re-import the String with {@link #parsePropertiesString(String)}.
33   */   */
34  public class WfsServerSettings extends HashMap<Object, Object> {  public class WfsServerSettings extends ServerSettings<Object, Object> {
35    
36          public enum Key {          public enum Key {
37                  BASE_URL, VERSION                  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 {          public enum WfsProtocollVersion {
72                  v1_1_1("1.1.1"), v1_1_0("1.1.0"), v1_0_0("1.0.0");                  v1_0_0("1.0.0"), v1_1_0("1.1.0"), v1_1_1("1.1.1");
73    
74                  private final String versionString;                  private final String versionString;
75    
# Line 48  public class WfsServerSettings extends H Line 82  public class WfsServerSettings extends H
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           * Character used to separate the parameters when serializing settings to a           * @return transforms the settings to a String that can be stored in a
198           * String           *         .properties line. @see #parsePropertiesString
199             * @throws MalformedURLException
200           */           */
201          private static final String DELIMITER = "|";          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) {          public WfsServerSettings(URL baseUrl, WfsProtocollVersion version) {
237                  setVersion(version);                  setVersion(version);
238                  setBaseUrl(baseUrl);                  setBaseUrl(baseUrl);
239                    setLenient(false);
240          }          }
241    
242          public WfsServerSettings() {          public URL getBaseUrl() {
243                  try {                  return (URL) get(Key.BASE_URL);
244                          setBaseUrl(new URL("http://localhost:8080/geoserver/ows"));          }
245                  } catch (MalformedURLException e) {  
246                          throw new RuntimeException(e);          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          /**          /**
# Line 76  public class WfsServerSettings extends H Line 276  public class WfsServerSettings extends H
276          }          }
277    
278          /**          /**
279           * @return <code>null</code> if not correctly defined, otherwise the URL of           * Set the BaseUrl as a String. Any pending parameters are automatically cut
280           *         the GetCapabilites request.           * of.
281           */           */
282          public URL getCapabilitiesUrl() {          public void setBaseUrl(String urlString) {
283                  return (URL) get(WFSDataStoreFactory.URL.key);                  try {
284          }                          // Cutoff any parameters
285                            if (urlString.indexOf("?") > -1) {
286                                    urlString = urlString.substring(0, urlString.indexOf("?"));
287                            }
288    
289          public URL getBaseUrl() {                          setBaseUrl(new URL(urlString));
290                  return (URL) get(Key.BASE_URL);                  } catch (MalformedURLException e) {
291                            throw new RuntimeException(e);
292                    }
293          }          }
294    
295          public void setBaseUrl(URL baseUrl) {          public void setBaseUrl(URL baseUrl) {
# Line 98  public class WfsServerSettings extends H Line 303  public class WfsServerSettings extends H
303                  updateCapabilitesUrl();                  updateCapabilitesUrl();
304          }          }
305    
306          public boolean updateCapabilitesUrl() {          public void setCachedTypeNames(String[] cachedTypeNames) {
307                  remove(WFSDataStoreFactory.URL.key);                  this.cachedTypeNames = cachedTypeNames;
308            }
                 if (getBaseUrl() == null)  
                         return false;  
   
                 if (getVersion() == null)  
                         return false;  
   
                 try {  
                         URL fullUrl = DataUtilities.extendURL(getBaseUrl(),  
                                         "?service=wfs&version=" + getVersion().getVersionString()  
                                                         + "&request=GetCapabilities");  
   
                         put(WFSDataStoreFactory.URL.key, fullUrl);  
309    
310                          return true;          public void setMaxFeatures(java.lang.Integer maxFeatures) {
311                  } catch (MalformedURLException e) {                  put(WFSDataStoreFactory.MAXFEATURES.key, maxFeatures);
                         return false;  
                 }  
312          }          }
313    
314          public WfsProtocollVersion getVersion() {          public void setTimeout(java.lang.Integer timeout) {
315                  return (WfsProtocollVersion) get(Key.VERSION);                  put(WFSDataStoreFactory.TIMEOUT.key, timeout);
316    
317          }          }
318    
319          public void setVersion(WfsProtocollVersion version) {          public void setVersion(WfsProtocollVersion version) {
# Line 142  public class WfsServerSettings extends H Line 334  public class WfsServerSettings extends H
334    
335                  StringBuffer serialized = new StringBuffer(100);                  StringBuffer serialized = new StringBuffer(100);
336    
337                    // Title
338                    serialized.append(getTitle());
339                    serialized.append(DELIMITER);
340    
341                  // BaseUrl                  // BaseUrl
342                  serialized.append(getBaseUrl().toString());                  serialized.append(getBaseUrl().toString());
343                  serialized.append(DELIMITER);                  serialized.append(DELIMITER);
# Line 150  public class WfsServerSettings extends H Line 346  public class WfsServerSettings extends H
346                  serialized.append(getVersion().toString());                  serialized.append(getVersion().toString());
347                  serialized.append(DELIMITER);                  serialized.append(DELIMITER);
348    
349                  return serialized.toString();                  serialized.append(getMaxFeatures());
350          }                  serialized.append(DELIMITER);
351    
352          /**                  serialized.append(getTimeout());
353           * @return transforms the settings to a String that can be stored in a                  serialized.append(DELIMITER);
          *         .properties line. @see #parsePropertiesString  
          * @throws MalformedURLException  
          */  
         public static WfsServerSettings parsePropertiesString(String propString)  
                         throws MalformedURLException {  
354    
355                  String[] split = propString.split(Pattern.quote(DELIMITER));                  serialized.append(getLenient());
356                    serialized.append(DELIMITER);
357    
358                  // BaseUrl                  serialized.append(getHttpProtocol().getValue());
359                  URL baseUrl = new URL(split[0]);                  serialized.append(DELIMITER);
360    
361                  // Version                  serialized.append(getUsername());
362                  WfsProtocollVersion v = WfsProtocollVersion.valueOf(split[1]);                  serialized.append(DELIMITER);
363    
364                    serialized.append(getPassword());
365                    // serialized.append(DELIMITER);
366    
367                  return new WfsServerSettings(baseUrl, v);                  return serialized.toString();
368          }          }
369    
370          @Override          @Override
# Line 188  public class WfsServerSettings extends H Line 383  public class WfsServerSettings extends H
383                  return s.toString();                  return s.toString();
384          }          }
385    
386          public void setCachedTypeNames(String[] cachedTypeNames) {          public boolean updateCapabilitesUrl() {
387                  this.cachedTypeNames = cachedTypeNames;                  remove(WFSDataStoreFactory.URL.key);
         }  
   
         public String[] getCachedTypeNames() {  
                 return cachedTypeNames;  
         }  
   
         private String[] cachedTypeNames = null;  
   
         public String getTitle() {  
                 // TODO  
                 return "" + getBaseUrl() + " " + getVersion();  
         }  
388    
389          /**                  if (getBaseUrl() == null)
390           * Opens a GUI that asks the use define a DB connection.                          return false;
          *  
          * @param wfsServer  
          *            <code>null</code> to create a new instance, or an instance to  
          *            edit.  
          * @return <code>null</code> if the user cancelled the creation of a new  
          *         {@link DbServerSettings}, otherwise the edited instance.  
          */  
         public static WfsServerSettings createOrEdit(Component owner,  
                         WfsServerSettings wfsServer) {  
                 boolean newCreated = false;  
391    
392                  if (wfsServer == null) {                  if (getVersion() == null)
393                          newCreated = true;                          return false;
                         wfsServer = new WfsServerSettings();  
                 }  
394    
395                  Text hostInput = new ManualInputOption.Text(                  try {
396                                  "BaseURL (without any paramters)", true, wfsServer.getBaseUrl()                          URL base = getBaseUrl();
                                                 .toString());  
397    
398                  Combo<WfsProtocollVersion> versionInput = new SelectionInputOption.Combo<WfsProtocollVersion>(                          String a = base.toExternalForm();
399                                  "WFS Version", true, WfsProtocollVersion.values(),                          if (a.endsWith("/"))
400                                  ArrayUtils.indexOf(WfsProtocollVersion.values(),                                  a = a.substring(0, a.length() - 1);
401                                                  wfsServer.getVersion()), WfsProtocollVersion.values());  
402                            URL fullUrl = new URL(a + "?service=WFS&version="
403                                            + getVersion().getVersionString()
404                                            + "&request=GetCapabilities");
405    
406                  Object[] input = MultipleOptionPane.showMultipleInputDialog(owner,                          put(WFSDataStoreFactory.URL.key, fullUrl);
                                 "WFS Connection parameters", hostInput, versionInput);  
407    
408                  if (input == null) {                          return true;
409                          if (newCreated)                  } catch (MalformedURLException e) {
410                                  return null;                          return false;
                         else  
                                 return wfsServer;  
                 } else {  
                         wfsServer.setBaseUrl((String) input[0]);  
                         wfsServer.setVersion(((WfsProtocollVersion) input[1]));  
411                  }                  }
412            }
413    
414                  return wfsServer;          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           * Set the BaseUrl as a String. Any pending parameters are automatically cut                  if (username != null && username.isEmpty())
426           * of.                          username = null;
427           */                  put(WFSDataStoreFactory.USERNAME.key, username);
428          public void setBaseUrl(String urlString) {          }
                 try {  
                         // Cutoff any parameters  
                         if (urlString.indexOf("?") > -1) {  
                                 urlString = urlString.substring(0, urlString.indexOf("?"));  
                         }  
429    
430                          setBaseUrl(new URL(urlString));          public String getUsername() {
431                  } catch (MalformedURLException e) {                  return (String) get(WFSDataStoreFactory.USERNAME.key);
                         throw new RuntimeException(e);  
                 }  
432          }          }
433    
434  }  }

Legend:
Removed from v.1161  
changed lines
  Added in v.1240

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26