1 |
package skrueger.geotools.io; |
2 |
|
3 |
import java.io.IOException; |
4 |
import java.net.URL; |
5 |
import java.util.HashMap; |
6 |
import java.util.regex.Pattern; |
7 |
|
8 |
import org.apache.commons.lang.StringUtils; |
9 |
import org.jfree.util.Log; |
10 |
|
11 |
/** |
12 |
* This class describes all settings needed to connect to a Geoserver server via |
13 |
* REST configuration. This class extends a {@link HashMap} and contains two |
14 |
* groups of keys:<br/> |
15 |
* The first group of keys, are all keys provided by Geotools to create a WFS <br/> |
16 |
* This class can serialize all important parameters needed to define the |
17 |
* connection into a {@link String} with {@link #toPropertiesString()} and |
18 |
* re-import the String with {@link #parsePropertiesString(String)}. |
19 |
* |
20 |
* TODO THis is wrong, should not extend AbstractGTServerSettings since it is |
21 |
* not a GeoTools specific server setting |
22 |
*/ |
23 |
public class GsServerSettings extends AbstractGTServerSettings<Void, Void> { |
24 |
|
25 |
@Override |
26 |
public String toString() { |
27 |
return "GsServerSettings [url=" + url + ", username=" + username |
28 |
+ ", password=" + password + "]"; |
29 |
} |
30 |
|
31 |
public GsServerSettings() { |
32 |
} |
33 |
|
34 |
public GsServerSettings(String s) { |
35 |
super(s); |
36 |
} |
37 |
|
38 |
@Override |
39 |
public boolean parsePropertiesString(String propString) { |
40 |
if (propString == null || propString.isEmpty()) |
41 |
return false; |
42 |
try { |
43 |
|
44 |
String[] split = propString.split(Pattern.quote(DELIMITER)); |
45 |
|
46 |
int i = 0; |
47 |
setUrl(split[i++]); |
48 |
setTitle(split[i++]); |
49 |
setUsername(StringUtils.stripToNull(split[i++])); |
50 |
setPassword(split[i++]); |
51 |
|
52 |
return true; |
53 |
} catch (Exception e) { |
54 |
Log.warn("couldn't parse " + propString, e); |
55 |
return false; |
56 |
} |
57 |
} |
58 |
|
59 |
String url; |
60 |
|
61 |
public String getUrl() { |
62 |
return url; |
63 |
} |
64 |
|
65 |
public void setUrl(String url) { |
66 |
this.url = url; |
67 |
} |
68 |
|
69 |
public String getUsername() { |
70 |
return username; |
71 |
} |
72 |
|
73 |
public void setUsername(String username) { |
74 |
this.username = username; |
75 |
} |
76 |
|
77 |
public String getPassword() { |
78 |
return password; |
79 |
} |
80 |
|
81 |
public void setPassword(String password) { |
82 |
this.password = password; |
83 |
} |
84 |
|
85 |
String username; |
86 |
String password; |
87 |
|
88 |
@Override |
89 |
public String toPropertiesString() { |
90 |
StringBuffer serialized = new StringBuffer(100); |
91 |
|
92 |
serialized.append(getUrl()); |
93 |
serialized.append(DELIMITER); |
94 |
|
95 |
serialized.append(getTitle()); |
96 |
serialized.append(DELIMITER); |
97 |
|
98 |
serialized.append(getUsername()); |
99 |
serialized.append(DELIMITER); |
100 |
|
101 |
serialized.append(getPassword()); |
102 |
serialized.append(DELIMITER); |
103 |
|
104 |
return serialized.toString(); |
105 |
} |
106 |
|
107 |
@Override |
108 |
public boolean isAvailable() { |
109 |
try { |
110 |
new URL(getUrl()).openStream().close(); |
111 |
return true; |
112 |
} catch (IOException e) { |
113 |
return false; |
114 |
} |
115 |
|
116 |
} |
117 |
} |