svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.framework / org.gvsig.andami / src / main / java / org / gvsig / andami / firewall / DefaultFirewallConfiguration.java @ 41387
History | View | Annotate | Download (5.79 KB)
1 |
package org.gvsig.andami.firewall; |
---|---|
2 |
|
3 |
import java.net.Authenticator; |
4 |
import java.net.MalformedURLException; |
5 |
import java.net.PasswordAuthentication; |
6 |
import java.net.URL; |
7 |
import java.util.ArrayList; |
8 |
import java.util.Collections; |
9 |
import java.util.List; |
10 |
import java.util.Locale; |
11 |
import java.util.Properties; |
12 |
import java.util.prefs.Preferences; |
13 |
import org.apache.commons.lang3.StringUtils; |
14 |
|
15 |
public class DefaultFirewallConfiguration implements FirewallConfiguration { |
16 |
|
17 |
private static Preferences prefs = Preferences.userRoot().node("gvsig.connection"); |
18 |
|
19 |
private List<String> nomProxyHosts = null; |
20 |
private URL host = null; |
21 |
|
22 |
private static final class ProxyAuth extends Authenticator { |
23 |
|
24 |
private PasswordAuthentication auth; |
25 |
|
26 |
private ProxyAuth(String user, String pass) { |
27 |
auth = new PasswordAuthentication(user, pass.toCharArray()); |
28 |
} |
29 |
|
30 |
protected PasswordAuthentication getPasswordAuthentication() { |
31 |
return auth;
|
32 |
} |
33 |
} |
34 |
|
35 |
public void apply() { |
36 |
|
37 |
Properties systemSettings = System.getProperties(); |
38 |
if (this.isEnabled()) { |
39 |
if ("https://".equalsIgnoreCase(this.getHost().getProtocol())) { |
40 |
systemSettings.put("https.proxyHost", this.getHost().getHost()); |
41 |
systemSettings.put("https.proxyPort", String.valueOf(this.getHost().getPort())); |
42 |
} else {
|
43 |
systemSettings.put("http.proxyHost", this.getHost().getHost()); |
44 |
systemSettings.put("http.proxyPort", String.valueOf(this.getHost().getPort())); |
45 |
} |
46 |
systemSettings.put("http.proxyUserName", this.getUserName()); |
47 |
systemSettings.put("http.proxyPassword", this.getPassword()); |
48 |
systemSettings.put("http.nonProxyHosts", this.getNonProxyHostsAsString()); |
49 |
if( this.isAuthenticate() ) { |
50 |
Authenticator.setDefault(
|
51 |
new ProxyAuth(
|
52 |
this.getUserName(),
|
53 |
this.getPassword()
|
54 |
) |
55 |
); |
56 |
} |
57 |
} else {
|
58 |
systemSettings.put("http.proxySet", "false"); |
59 |
systemSettings.remove("http.proxyHost");
|
60 |
systemSettings.remove("http.proxyPort");
|
61 |
systemSettings.remove("http.proxyUserName");
|
62 |
systemSettings.remove("http.proxyPassword");
|
63 |
systemSettings.remove("http.nonProxyHosts");
|
64 |
} |
65 |
} |
66 |
|
67 |
public boolean isAuthenticate() { |
68 |
if (StringUtils.isBlank(this.getUserName()) |
69 |
|| StringUtils.isBlank(this.getPassword())) {
|
70 |
return false; |
71 |
} |
72 |
return true; |
73 |
} |
74 |
|
75 |
public boolean isEnabled() { |
76 |
return prefs.getBoolean("firewall.http.enabled", false); |
77 |
} |
78 |
|
79 |
public void setEnabled(boolean enabled) { |
80 |
prefs.putBoolean("firewall.http.enabled", enabled);
|
81 |
} |
82 |
|
83 |
public URL getHost() { |
84 |
if (this.host == null) { |
85 |
String host = prefs.get("firewall.socks.host", ""); |
86 |
String port = prefs.get("firewall.socks.port", ""); |
87 |
this.setHost(host, port);
|
88 |
} |
89 |
return this.host; |
90 |
} |
91 |
|
92 |
public void setHost(URL host) { |
93 |
prefs.put("firewall.socks.host", host.getHost());
|
94 |
prefs.put("firewall.socks.port", String.valueOf(host.getPort())); |
95 |
this.host = host;
|
96 |
} |
97 |
|
98 |
public boolean setHost(String host, String port) { |
99 |
StringBuilder b = new StringBuilder(); |
100 |
|
101 |
if (StringUtils.isBlank(host)) {
|
102 |
this.host = null; |
103 |
} else {
|
104 |
if (!(host.toLowerCase().startsWith("http://") |
105 |
|| host.toLowerCase().startsWith("https://"))) {
|
106 |
b.append("http://");
|
107 |
} |
108 |
b.append(host); |
109 |
if (!StringUtils.isBlank(port)) {
|
110 |
b.append(":");
|
111 |
b.append(port); |
112 |
} |
113 |
try {
|
114 |
this.host = new URL(b.toString()); |
115 |
} catch (MalformedURLException ex) { |
116 |
return false; |
117 |
} |
118 |
} |
119 |
if (this.host == null) { |
120 |
prefs.put("firewall.http.host", ""); |
121 |
prefs.put("firewall.http.port", ""); |
122 |
} else {
|
123 |
prefs.put("firewall.http.host", this.host.getHost()); |
124 |
prefs.put("firewall.http.port", String.valueOf(this.host.getPort())); |
125 |
} |
126 |
return true; |
127 |
} |
128 |
|
129 |
public String getUserName() { |
130 |
return prefs.get("firewall.http.user", ""); |
131 |
} |
132 |
|
133 |
public String getPassword() { |
134 |
return prefs.get("firewall.http.password", ""); |
135 |
} |
136 |
|
137 |
public void setUserName(String userName) { |
138 |
prefs.put("firewall.http.user", userName);
|
139 |
} |
140 |
|
141 |
public void setPassword(String password) { |
142 |
prefs.put("firewall.http.password", password);
|
143 |
} |
144 |
|
145 |
public List<String> getNonProxyHosts() { |
146 |
if (this.nomProxyHosts == null) { |
147 |
String s = prefs.get("firewall.http.nonProxyHosts", ""); |
148 |
this.setNonProxyHosts(s);
|
149 |
} |
150 |
return Collections.unmodifiableList(this.nomProxyHosts); |
151 |
} |
152 |
|
153 |
public String getNonProxyHostsAsString() { |
154 |
StringBuilder builder = new StringBuilder(); |
155 |
|
156 |
this.getNonProxyHosts();
|
157 |
for (int i = 0; i < this.nomProxyHosts.size(); i++) { |
158 |
if (builder.length() == 0) { |
159 |
builder.append("|");
|
160 |
} |
161 |
builder.append(this.nomProxyHosts.get(i));
|
162 |
} |
163 |
return builder.toString();
|
164 |
} |
165 |
|
166 |
public void setNonProxyHosts(String nonProxyHosts) { |
167 |
this.nomProxyHosts = new ArrayList<String>(); |
168 |
String ss[] = nonProxyHosts.split("|"); |
169 |
for (int i = 0; i < ss.length; i++) { |
170 |
if (StringUtils.isNotBlank(ss[i])) {
|
171 |
this.nomProxyHosts.add(ss[i]);
|
172 |
} |
173 |
} |
174 |
} |
175 |
|
176 |
public void addNonProxyHost(String host) { |
177 |
this.getNonProxyHosts();
|
178 |
this.nomProxyHosts.add(host);
|
179 |
} |
180 |
} |