1 |
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 |
|
13 |
import schmitzm.swing.ManualInputOption; |
14 |
import schmitzm.swing.ManualInputOption.Text; |
15 |
import schmitzm.swing.MultipleOptionPane; |
16 |
import schmitzm.swing.SelectionInputOption; |
17 |
import schmitzm.swing.SelectionInputOption.Combo; |
18 |
|
19 |
/** |
20 |
* This class describes all settings needed to connect to a WFS server. This |
21 |
* class extends a {@link HashMap} and contains two groups of keys:<br/> |
22 |
* The first group of keys, are all keys provided by Geotools to create a WFS |
23 |
* datastore, like: {@link WFSDataStoreFactory#URL} or |
24 |
* {@link WFSDataStoreFactory#USERNAME} .<br/> |
25 |
* The second group are additional keys defined in the enum |
26 |
* {@link WfsServerSettings.Key}.<br/> |
27 |
* This class can serialize all important parameters needed to define the |
28 |
* connection into a {@link String} with {@link #toPropertiesString()} and |
29 |
* re-import the String with {@link #parsePropertiesString(String)}. |
30 |
*/ |
31 |
public class WfsServerSettings extends HashMap<Object, Object> { |
32 |
|
33 |
public enum Key { |
34 |
BASE_URL, VERSION |
35 |
} |
36 |
|
37 |
public enum WfsProtocollVersion { |
38 |
v1_1_1("1.1.1"), v1_1_0("1.1.0"), v1_0_0("1.0.0"); |
39 |
|
40 |
private final String versionString; |
41 |
|
42 |
private WfsProtocollVersion(String versionString) { |
43 |
this.versionString = versionString; |
44 |
} |
45 |
|
46 |
public String getVersionString() { |
47 |
return versionString; |
48 |
} |
49 |
} |
50 |
|
51 |
/** |
52 |
* Character used to separate the parameters when serializing settings to a |
53 |
* String |
54 |
*/ |
55 |
private static final String DELIMITER = "|"; |
56 |
|
57 |
public WfsServerSettings(URL baseUrl, WfsProtocollVersion version) { |
58 |
setVersion(version); |
59 |
setBaseUrl(baseUrl); |
60 |
} |
61 |
|
62 |
public WfsServerSettings() { |
63 |
try { |
64 |
setBaseUrl(new URL("http://localhost:8080/geoserver/ows")); |
65 |
} catch (MalformedURLException e) { |
66 |
throw new RuntimeException(e); |
67 |
} |
68 |
} |
69 |
|
70 |
/** |
71 |
* @return <code>true</code>, if all parameters look OK, without actually |
72 |
* opening any connection. |
73 |
*/ |
74 |
public boolean isWellDefined() { |
75 |
return updateCapabilitesUrl(); |
76 |
} |
77 |
|
78 |
/** |
79 |
* @return <code>null</code> if not correctly defined, otherwise the URL of |
80 |
* the GetCapabilites request. |
81 |
*/ |
82 |
public URL getCapabilitiesUrl() { |
83 |
return (URL) get(WFSDataStoreFactory.URL.key); |
84 |
} |
85 |
|
86 |
public URL getBaseUrl() { |
87 |
return (URL) get(Key.BASE_URL); |
88 |
} |
89 |
|
90 |
public void setBaseUrl(URL baseUrl) { |
91 |
|
92 |
if (get(Key.BASE_URL) != baseUrl) { |
93 |
setCachedTypeNames(null); |
94 |
} |
95 |
|
96 |
put(Key.BASE_URL, baseUrl); |
97 |
|
98 |
updateCapabilitesUrl(); |
99 |
} |
100 |
|
101 |
public boolean updateCapabilitesUrl() { |
102 |
remove(WFSDataStoreFactory.URL.key); |
103 |
|
104 |
if (getBaseUrl() == null) |
105 |
return false; |
106 |
|
107 |
if (getVersion() == null) |
108 |
return false; |
109 |
|
110 |
try { |
111 |
URL fullUrl = DataUtilities.extendURL(getBaseUrl(), |
112 |
"?service=wfs&version=" + getVersion().getVersionString() |
113 |
+ "&request=GetCapabilities"); |
114 |
|
115 |
put(WFSDataStoreFactory.URL.key, fullUrl); |
116 |
|
117 |
return true; |
118 |
} catch (MalformedURLException e) { |
119 |
return false; |
120 |
} |
121 |
} |
122 |
|
123 |
public WfsProtocollVersion getVersion() { |
124 |
return (WfsProtocollVersion) get(Key.VERSION); |
125 |
} |
126 |
|
127 |
public void setVersion(WfsProtocollVersion version) { |
128 |
|
129 |
if (get(Key.VERSION) != version) { |
130 |
setCachedTypeNames(null); |
131 |
} |
132 |
|
133 |
put(Key.VERSION, version); |
134 |
updateCapabilitesUrl(); |
135 |
} |
136 |
|
137 |
/** |
138 |
* @return transforms the settings to a String that can be stored in a |
139 |
* .properties line. @see #parsePropertiesString |
140 |
*/ |
141 |
public String toPropertiesString() { |
142 |
|
143 |
StringBuffer serialized = new StringBuffer(100); |
144 |
|
145 |
// BaseUrl |
146 |
serialized.append(getBaseUrl().toString()); |
147 |
serialized.append(DELIMITER); |
148 |
|
149 |
// Version |
150 |
serialized.append(getVersion().toString()); |
151 |
serialized.append(DELIMITER); |
152 |
|
153 |
return serialized.toString(); |
154 |
} |
155 |
|
156 |
/** |
157 |
* @return transforms the settings to a String that can be stored in a |
158 |
* .properties line. @see #parsePropertiesString |
159 |
* @throws MalformedURLException |
160 |
*/ |
161 |
public static WfsServerSettings parsePropertiesString(String propString) |
162 |
throws MalformedURLException { |
163 |
|
164 |
String[] split = propString.split(Pattern.quote(DELIMITER)); |
165 |
|
166 |
// BaseUrl |
167 |
URL baseUrl = new URL(split[0]); |
168 |
|
169 |
// Version |
170 |
WfsProtocollVersion v = WfsProtocollVersion.valueOf(split[1]); |
171 |
|
172 |
return new WfsServerSettings(baseUrl, v); |
173 |
} |
174 |
|
175 |
@Override |
176 |
public String toString() { |
177 |
StringBuffer s = new StringBuffer(); |
178 |
|
179 |
URL baseUrl = getBaseUrl(); |
180 |
WfsProtocollVersion version = getVersion(); |
181 |
|
182 |
if (baseUrl != null) |
183 |
s.append(baseUrl.toString() + " "); |
184 |
|
185 |
if (version != null) |
186 |
s.append(version.getVersionString()); |
187 |
|
188 |
return s.toString(); |
189 |
} |
190 |
|
191 |
public void setCachedTypeNames(String[] cachedTypeNames) { |
192 |
this.cachedTypeNames = cachedTypeNames; |
193 |
} |
194 |
|
195 |
public String[] getCachedTypeNames() { |
196 |
return cachedTypeNames; |
197 |
} |
198 |
|
199 |
private String[] cachedTypeNames = null; |
200 |
|
201 |
public String getTitle() { |
202 |
// TODO |
203 |
return "" + getBaseUrl() + " " + getVersion(); |
204 |
} |
205 |
|
206 |
/** |
207 |
* Opens a GUI that asks the use define a DB connection. |
208 |
* |
209 |
* @param wfsServer |
210 |
* <code>null</code> to create a new instance, or an instance to |
211 |
* edit. |
212 |
* @return <code>null</code> if the user cancelled the creation of a new |
213 |
* {@link DbServerSettings}, otherwise the edited instance. |
214 |
*/ |
215 |
public static WfsServerSettings createOrEdit(Component owner, |
216 |
WfsServerSettings wfsServer) { |
217 |
boolean newCreated = false; |
218 |
|
219 |
if (wfsServer == null) { |
220 |
newCreated = true; |
221 |
wfsServer = new WfsServerSettings(); |
222 |
} |
223 |
|
224 |
Text hostInput = new ManualInputOption.Text( |
225 |
"BaseURL (without any paramters)", true, wfsServer.getBaseUrl() |
226 |
.toString()); |
227 |
|
228 |
Combo<WfsProtocollVersion> versionInput = new SelectionInputOption.Combo<WfsProtocollVersion>( |
229 |
"WFS Version", true, WfsProtocollVersion.values(), |
230 |
ArrayUtils.indexOf(WfsProtocollVersion.values(), |
231 |
wfsServer.getVersion()), WfsProtocollVersion.values()); |
232 |
|
233 |
Object[] input = MultipleOptionPane.showMultipleInputDialog(owner, |
234 |
"WFS Connection parameters", hostInput, versionInput); |
235 |
|
236 |
if (input == null) { |
237 |
if (newCreated) |
238 |
return null; |
239 |
else |
240 |
return wfsServer; |
241 |
} else { |
242 |
wfsServer.setBaseUrl((String) input[0]); |
243 |
wfsServer.setVersion(((WfsProtocollVersion) input[1])); |
244 |
} |
245 |
|
246 |
return wfsServer; |
247 |
|
248 |
} |
249 |
|
250 |
/** |
251 |
* Set the BaseUrl as a String. Any pending parameters are automatically cut |
252 |
* of. |
253 |
*/ |
254 |
public void setBaseUrl(String urlString) { |
255 |
try { |
256 |
// Cutoff any parameters |
257 |
if (urlString.indexOf("?") > -1) { |
258 |
urlString = urlString.substring(0, urlString.indexOf("?")); |
259 |
} |
260 |
|
261 |
setBaseUrl(new URL(urlString)); |
262 |
} catch (MalformedURLException e) { |
263 |
throw new RuntimeException(e); |
264 |
} |
265 |
} |
266 |
} |