/[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

revision 1161 by alfonx, Sun Oct 24 16:44:18 2010 UTC revision 1164 by alfonx, Mon Oct 25 07:25:26 2010 UTC
# Line 9  import java.util.regex.Pattern; Line 9  import java.util.regex.Pattern;
9  import org.apache.commons.lang.ArrayUtils;  import org.apache.commons.lang.ArrayUtils;
10  import org.geotools.data.DataUtilities;  import org.geotools.data.DataUtilities;
11  import org.geotools.data.wfs.WFSDataStoreFactory;  import org.geotools.data.wfs.WFSDataStoreFactory;
12    import org.jfree.util.Log;
13    
14    import schmitzm.swing.BooleanInputOption;
15  import schmitzm.swing.ManualInputOption;  import schmitzm.swing.ManualInputOption;
16    import schmitzm.swing.ManualInputOption.Integer;
17    import schmitzm.swing.ManualInputOption.PasswordViewable;
18  import schmitzm.swing.ManualInputOption.Text;  import schmitzm.swing.ManualInputOption.Text;
19  import schmitzm.swing.MultipleOptionPane;  import schmitzm.swing.MultipleOptionPane;
20  import schmitzm.swing.SelectionInputOption;  import schmitzm.swing.SelectionInputOption;
# Line 28  import schmitzm.swing.SelectionInputOpti Line 32  import schmitzm.swing.SelectionInputOpti
32   * connection into a {@link String} with {@link #toPropertiesString()} and   * connection into a {@link String} with {@link #toPropertiesString()} and
33   * re-import the String with {@link #parsePropertiesString(String)}.   * re-import the String with {@link #parsePropertiesString(String)}.
34   */   */
35  public class WfsServerSettings extends HashMap<Object, Object> {  public class WfsServerSettings extends ServerSettings<Object, Object> {
36    
37          public enum Key {          public enum Key {
38                  BASE_URL, VERSION                  BASE_URL, VERSION
39          }          }
40    
41            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          public enum WfsProtocollVersion {          public enum WfsProtocollVersion {
73                  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");
74    
75                  private final String versionString;                  private final String versionString;
76    
# Line 48  public class WfsServerSettings extends H Line 83  public class WfsServerSettings extends H
83                  }                  }
84          }          }
85    
86            private static final URL defaultURl;
87            static {
88                    try {
89                            defaultURl = new URL("http://localhost:8080/geoserver/ows");
90                    } catch (MalformedURLException e) {
91                            throw new RuntimeException(e);
92                    }
93            }
94    
95            /**
96             * Opens a GUI that asks the use define a DB connection.
97             *
98             * @param wfsServer
99             *            <code>null</code> to create a new instance, or an instance to
100             *            edit.
101             * @return <code>null</code> if the user cancelled the creation of a new
102             *         {@link DbServerSettings}, otherwise the edited instance.
103             */
104            public static WfsServerSettings createOrEdit(Component owner,
105                            WfsServerSettings wfsServer) {
106                    boolean newCreated = false;
107    
108                    if (wfsServer == null) {
109                            newCreated = true;
110                            wfsServer = new WfsServerSettings();
111                    }
112    
113                    Text hostInput = new ManualInputOption.Text(
114                                    "BaseURL (without any paramters)", true, wfsServer.getBaseUrl()
115                                                    .toString());
116    
117                    Combo<WfsProtocollVersion> versionInput = new SelectionInputOption.Combo<WfsProtocollVersion>(
118                                    "WFS Version", true, WfsProtocollVersion.values(),
119                                    ArrayUtils.indexOf(WfsProtocollVersion.values(),
120                                                    wfsServer.getVersion()), WfsProtocollVersion.values());
121                    versionInput
122                                    .setToolTipText("If you have problems with Curves in GML3, try version 1.0.0.");
123    
124                    Integer maxFeaturesInput = new ManualInputOption.Integer(
125                                    "Max. features per request (0=no limit)", false,
126                                    wfsServer.getMaxFeatures());
127                    maxFeaturesInput
128                                    .setToolTipText(WFSDataStoreFactory.MAXFEATURES.description
129                                                    .toString());
130    
131                    Integer timeoutInput = new ManualInputOption.Integer("Timout in ms:",
132                                    false, wfsServer.getTimeout());
133                    timeoutInput.setToolTipText(WFSDataStoreFactory.TIMEOUT.description
134                                    .toString());
135    
136                    BooleanInputOption lenientInput = new BooleanInputOption(
137                                    "lenient (=ignore errors)", wfsServer.getLenient());
138                    lenientInput.setToolTipText(WFSDataStoreFactory.LENIENT.description
139                                    .toString());
140    
141                    SelectionInputOption.Combo<HttpProtocol> httpInput = new SelectionInputOption.Combo<HttpProtocol>(
142                                    "Ust HTTP GET or POST:", true, HttpProtocol.values(),
143                                    ArrayUtils.indexOf(HttpProtocol.values(),
144                                                    wfsServer.getHttpProtocol()), HttpProtocol.values());
145                    httpInput.setToolTipText(WFSDataStoreFactory.PROTOCOL.description
146                                    .toString());
147    
148                    Text userInput = new ManualInputOption.Text(
149                                    "Optional HTTPAuth Username", false, wfsServer.getUsername());
150    
151                    PasswordViewable passwdInput = new ManualInputOption.PasswordViewable(
152                                    "Optional HTTPAuth Password", false, wfsServer.getPassword());
153    
154                    // Show the options
155                    Object[] input = MultipleOptionPane.showMultipleInputDialog(owner,
156                                    "WFS Connection parameters", hostInput, versionInput,
157                                    maxFeaturesInput, timeoutInput, lenientInput, httpInput,
158                                    userInput, passwdInput);
159    
160                    if (input == null) {
161                            if (newCreated)
162                                    return null;
163                            else
164                                    return wfsServer;
165                    } else {
166                            wfsServer.setBaseUrl((String) input[0]);
167                            wfsServer.setVersion(((WfsProtocollVersion) input[1]));
168                            wfsServer.setMaxFeatures((java.lang.Integer) input[2]);
169                            wfsServer.setTimeout((java.lang.Integer) input[3]);
170                            wfsServer.setLenient((java.lang.Boolean) input[4]);
171                            wfsServer.setHttpProtocol((HttpProtocol) input[5]);
172                            wfsServer.setUsername((String) input[6]);
173                            wfsServer.setPassword(String.valueOf((char[]) input[7]));
174                    }
175    
176                    return wfsServer;
177    
178            }
179    
180            public HttpProtocol getHttpProtocol() {
181                    return HttpProtocol
182                                    .parse((Boolean) get(WFSDataStoreFactory.PROTOCOL.key));
183            }
184    
185            public void setHttpProtocol(HttpProtocol protocol) {
186                    put(WFSDataStoreFactory.PROTOCOL.key, protocol.getValue());
187            }
188    
189            private Boolean getLenient() {
190                    return (Boolean) get(WFSDataStoreFactory.LENIENT.key);
191            }
192    
193            private void setLenient(Boolean lenient) {
194                    put(WFSDataStoreFactory.LENIENT.key, lenient);
195            }
196    
197          /**          /**
198           * Character used to separate the parameters when serializing settings to a           * @return transforms the settings to a String that can be stored in a
199           * String           *         .properties line. @see #parsePropertiesString
200             * @throws MalformedURLException
201           */           */
202          private static final String DELIMITER = "|";          public static WfsServerSettings parsePropertiesString(String propString)
203                            throws MalformedURLException {
204    
205                    try {
206                            String[] split = propString.split(Pattern.quote(DELIMITER));
207    
208                            WfsServerSettings wfs = new WfsServerSettings();
209    
210                            int i = 0;
211                            wfs.setTitle(split[i++]);
212                            wfs.setBaseUrl(new URL(split[i++]));
213                            wfs.setVersion(WfsProtocollVersion.valueOf(split[i++]));
214                            wfs.setMaxFeatures(intOrNull(split[i++]));
215                            wfs.setTimeout(intOrNull(split[i++]));
216                            wfs.setLenient(java.lang.Boolean.valueOf(split[i++]));
217                            wfs.setHttpProtocol(HttpProtocol.parse(split[i++]));
218    
219                            wfs.setUsername(stringOrNull(split[i++]));
220    
221                            wfs.setPassword(stringOrNull(split[i++]));
222    
223                            return wfs;
224                    } catch (Exception e) {
225                            Log.warn("couldn't parse " + propString, e);
226                            return null;
227                    }
228    
229            }
230    
231            private String[] cachedTypeNames = null;
232    
233            public WfsServerSettings() {
234                    this(defaultURl, WfsProtocollVersion.v1_0_0);
235            }
236    
237          public WfsServerSettings(URL baseUrl, WfsProtocollVersion version) {          public WfsServerSettings(URL baseUrl, WfsProtocollVersion version) {
238                  setVersion(version);                  setVersion(version);
239                  setBaseUrl(baseUrl);                  setBaseUrl(baseUrl);
240                    setLenient(false);
241          }          }
242    
243          public WfsServerSettings() {          public URL getBaseUrl() {
244                  try {                  return (URL) get(Key.BASE_URL);
245                          setBaseUrl(new URL("http://localhost:8080/geoserver/ows"));          }
246                  } catch (MalformedURLException e) {  
247                          throw new RuntimeException(e);          public String[] getCachedTypeNames() {
248                  }                  return cachedTypeNames;
249            }
250    
251            /**
252             * @return <code>null</code> if not correctly defined, otherwise the URL of
253             *         the GetCapabilites request.
254             */
255            public URL getCapabilitiesUrl() {
256                    return (URL) get(WFSDataStoreFactory.URL.key);
257            }
258    
259            public java.lang.Integer getMaxFeatures() {
260                    return (java.lang.Integer) get(WFSDataStoreFactory.MAXFEATURES.key);
261            }
262    
263            public java.lang.Integer getTimeout() {
264                    return (java.lang.Integer) get(WFSDataStoreFactory.TIMEOUT.key);
265            }
266    
267            public WfsProtocollVersion getVersion() {
268                    return (WfsProtocollVersion) get(Key.VERSION);
269          }          }
270    
271          /**          /**
# Line 76  public class WfsServerSettings extends H Line 277  public class WfsServerSettings extends H
277          }          }
278    
279          /**          /**
280           * @return <code>null</code> if not correctly defined, otherwise the URL of           * Set the BaseUrl as a String. Any pending parameters are automatically cut
281           *         the GetCapabilites request.           * of.
282           */           */
283          public URL getCapabilitiesUrl() {          public void setBaseUrl(String urlString) {
284                  return (URL) get(WFSDataStoreFactory.URL.key);                  try {
285          }                          // Cutoff any parameters
286                            if (urlString.indexOf("?") > -1) {
287                                    urlString = urlString.substring(0, urlString.indexOf("?"));
288                            }
289    
290          public URL getBaseUrl() {                          setBaseUrl(new URL(urlString));
291                  return (URL) get(Key.BASE_URL);                  } catch (MalformedURLException e) {
292                            throw new RuntimeException(e);
293                    }
294          }          }
295    
296          public void setBaseUrl(URL baseUrl) {          public void setBaseUrl(URL baseUrl) {
# Line 98  public class WfsServerSettings extends H Line 304  public class WfsServerSettings extends H
304                  updateCapabilitesUrl();                  updateCapabilitesUrl();
305          }          }
306    
307          public boolean updateCapabilitesUrl() {          public void setCachedTypeNames(String[] cachedTypeNames) {
308                  remove(WFSDataStoreFactory.URL.key);                  this.cachedTypeNames = cachedTypeNames;
309            }
                 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);  
310    
311                          return true;          public void setMaxFeatures(java.lang.Integer maxFeatures) {
312                  } catch (MalformedURLException e) {                  put(WFSDataStoreFactory.MAXFEATURES.key, maxFeatures);
                         return false;  
                 }  
313          }          }
314    
315          public WfsProtocollVersion getVersion() {          public void setTimeout(java.lang.Integer timeout) {
316                  return (WfsProtocollVersion) get(Key.VERSION);                  put(WFSDataStoreFactory.TIMEOUT.key, timeout);
317    
318          }          }
319    
320          public void setVersion(WfsProtocollVersion version) {          public void setVersion(WfsProtocollVersion version) {
# Line 142  public class WfsServerSettings extends H Line 335  public class WfsServerSettings extends H
335    
336                  StringBuffer serialized = new StringBuffer(100);                  StringBuffer serialized = new StringBuffer(100);
337    
338                    // Title
339                    serialized.append(getTitle());
340                    serialized.append(DELIMITER);
341    
342                  // BaseUrl                  // BaseUrl
343                  serialized.append(getBaseUrl().toString());                  serialized.append(getBaseUrl().toString());
344                  serialized.append(DELIMITER);                  serialized.append(DELIMITER);
# Line 150  public class WfsServerSettings extends H Line 347  public class WfsServerSettings extends H
347                  serialized.append(getVersion().toString());                  serialized.append(getVersion().toString());
348                  serialized.append(DELIMITER);                  serialized.append(DELIMITER);
349    
350                  return serialized.toString();                  serialized.append(getMaxFeatures());
351          }                  serialized.append(DELIMITER);
352    
353          /**                  serialized.append(getTimeout());
354           * @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 {  
355    
356                  String[] split = propString.split(Pattern.quote(DELIMITER));                  serialized.append(getLenient());
357                    serialized.append(DELIMITER);
358    
359                  // BaseUrl                  serialized.append(getHttpProtocol().getValue());
360                  URL baseUrl = new URL(split[0]);                  serialized.append(DELIMITER);
361    
362                  // Version                  serialized.append(getUsername());
363                  WfsProtocollVersion v = WfsProtocollVersion.valueOf(split[1]);                  serialized.append(DELIMITER);
364    
365                    serialized.append(getPassword());
366                    // serialized.append(DELIMITER);
367    
368                  return new WfsServerSettings(baseUrl, v);                  return serialized.toString();
369          }          }
370    
371          @Override          @Override
# Line 188  public class WfsServerSettings extends H Line 384  public class WfsServerSettings extends H
384                  return s.toString();                  return s.toString();
385          }          }
386    
387          public void setCachedTypeNames(String[] cachedTypeNames) {          public boolean updateCapabilitesUrl() {
388                  this.cachedTypeNames = cachedTypeNames;                  remove(WFSDataStoreFactory.URL.key);
         }  
   
         public String[] getCachedTypeNames() {  
                 return cachedTypeNames;  
         }  
   
         private String[] cachedTypeNames = null;  
   
         public String getTitle() {  
                 // TODO  
                 return "" + getBaseUrl() + " " + getVersion();  
         }  
   
         /**  
          * Opens a GUI that asks the use define a DB connection.  
          *  
          * @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;  
389    
390                  if (wfsServer == null) {                  if (getBaseUrl() == null)
391                          newCreated = true;                          return false;
                         wfsServer = new WfsServerSettings();  
                 }  
392    
393                  Text hostInput = new ManualInputOption.Text(                  if (getVersion() == null)
394                                  "BaseURL (without any paramters)", true, wfsServer.getBaseUrl()                          return false;
                                                 .toString());  
395    
396                  Combo<WfsProtocollVersion> versionInput = new SelectionInputOption.Combo<WfsProtocollVersion>(                  try {
397                                  "WFS Version", true, WfsProtocollVersion.values(),                          URL fullUrl = DataUtilities.extendURL(getBaseUrl(),
398                                  ArrayUtils.indexOf(WfsProtocollVersion.values(),                                          "?service=wfs&version=" + getVersion().getVersionString()
399                                                  wfsServer.getVersion()), WfsProtocollVersion.values());                                                          + "&request=GetCapabilities");
400    
401                  Object[] input = MultipleOptionPane.showMultipleInputDialog(owner,                          put(WFSDataStoreFactory.URL.key, fullUrl);
                                 "WFS Connection parameters", hostInput, versionInput);  
402    
403                  if (input == null) {                          return true;
404                          if (newCreated)                  } catch (MalformedURLException e) {
405                                  return null;                          return false;
                         else  
                                 return wfsServer;  
                 } else {  
                         wfsServer.setBaseUrl((String) input[0]);  
                         wfsServer.setVersion(((WfsProtocollVersion) input[1]));  
406                  }                  }
407            }
408    
409                  return wfsServer;          public void setPassword(String password) {
410                    if (password != null && password.isEmpty())
411                            password = null;
412                    put(WFSDataStoreFactory.PASSWORD.key, password);
413            }
414    
415            public String getPassword() {
416                    return (String) get(WFSDataStoreFactory.PASSWORD.key);
417          }          }
418    
419          /**          public void setUsername(String username) {
420           * Set the BaseUrl as a String. Any pending parameters are automatically cut                  if (username != null && username.isEmpty())
421           * of.                          username = null;
422           */                  put(WFSDataStoreFactory.USERNAME.key, username);
423          public void setBaseUrl(String urlString) {          }
                 try {  
                         // Cutoff any parameters  
                         if (urlString.indexOf("?") > -1) {  
                                 urlString = urlString.substring(0, urlString.indexOf("?"));  
                         }  
424    
425                          setBaseUrl(new URL(urlString));          public String getUsername() {
426                  } catch (MalformedURLException e) {                  return (String) get(WFSDataStoreFactory.USERNAME.key);
                         throw new RuntimeException(e);  
                 }  
427          }          }
428    
429  }  }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26