Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.framework / org.gvsig.andami / src / main / java / org / gvsig / andami / firewall / DefaultFirewallConfiguration.java @ 41524

History | View | Annotate | Download (5.8 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( this.getHost()!=null ) {
40
                if ("https://".equalsIgnoreCase(this.getHost().getProtocol())) {
41
                    systemSettings.put("https.proxyHost", this.getHost().getHost());
42
                    systemSettings.put("https.proxyPort", String.valueOf(this.getHost().getPort()));
43
                } else {
44
                    systemSettings.put("http.proxyHost", this.getHost().getHost());
45
                    systemSettings.put("http.proxyPort", String.valueOf(this.getHost().getPort()));
46
                }
47
            }
48
            systemSettings.put("http.proxyUserName", this.getUserName());
49
            systemSettings.put("http.proxyPassword", this.getPassword());
50
            systemSettings.put("http.nonProxyHosts", this.getNonProxyHostsAsString());
51
            if( this.isAuthenticate() ) {
52
                Authenticator.setDefault(
53
                        new ProxyAuth(
54
                                this.getUserName(), 
55
                                this.getPassword()
56
                        )
57
                );
58
            }
59
        } else {
60
            systemSettings.put("http.proxySet", "false");
61
            systemSettings.remove("http.proxyHost");
62
            systemSettings.remove("http.proxyPort");
63
            systemSettings.remove("http.proxyUserName");
64
            systemSettings.remove("http.proxyPassword");
65
            systemSettings.remove("http.nonProxyHosts");
66
        }
67
    }
68

    
69
    public boolean isAuthenticate() {
70
        if (StringUtils.isBlank(this.getUserName())
71
                || StringUtils.isBlank(this.getPassword())) {
72
            return false;
73
        }
74
        return true;
75
    }
76

    
77
    public boolean isEnabled() {
78
        return prefs.getBoolean("firewall.http.enabled", false);
79
    }
80

    
81
    public void setEnabled(boolean enabled) {
82
        prefs.putBoolean("firewall.http.enabled", enabled);
83
    }
84
    
85
    public URL getHost() {
86
        if (this.host == null) {
87
            String host = prefs.get("firewall.socks.host", "");
88
            String port = prefs.get("firewall.socks.port", "");
89
            this.setHost(host, port);
90
        }
91
        return this.host;
92
    }
93
    
94
    public void setHost(URL host) {
95
        if (host == null) {
96
            prefs.put("firewall.socks.host", "");
97
            prefs.put("firewall.socks.port", "");
98
        } else {
99
            prefs.put("firewall.socks.host", host.getHost());
100
            prefs.put("firewall.socks.port", String.valueOf(host.getPort()));
101
        }
102
        this.host = host;
103
    }
104

    
105
    public boolean setHost(String host, String port) {
106
        StringBuilder b = new StringBuilder();
107
        URL hosturl = null;
108
        
109
        if (StringUtils.isBlank(host)) {
110
            hosturl = null;
111
        } else {
112
            if (!(host.toLowerCase().startsWith("http://")
113
                    || host.toLowerCase().startsWith("https://"))) {
114
                b.append("http://");
115
            }
116
            b.append(host);
117
            if (!StringUtils.isBlank(port)) {
118
                b.append(":");
119
                b.append(port);
120
            }
121
            try {
122
                hosturl = new URL(b.toString());
123
            } catch (MalformedURLException ex) {
124
                return false;
125
            }
126
        }
127
        this.setHost(hosturl);
128
        return true;
129
    }
130

    
131
    public String getUserName() {
132
        return prefs.get("firewall.http.user", "");
133
    }
134

    
135
    public String getPassword() {
136
        return prefs.get("firewall.http.password", "");
137
    }
138

    
139
    public void setUserName(String userName) {
140
        prefs.put("firewall.http.user", userName);
141
    }
142

    
143
    public void setPassword(String password) {
144
        prefs.put("firewall.http.password", password);
145
    }
146

    
147
    public List<String> getNonProxyHosts() {
148
        if (this.nomProxyHosts == null) {
149
            String s = prefs.get("firewall.http.nonProxyHosts", "");
150
            this.setNonProxyHosts(s);
151
        }
152
        return Collections.unmodifiableList(this.nomProxyHosts);
153
    }
154

    
155
    public String getNonProxyHostsAsString() {
156
        StringBuilder builder = new StringBuilder();
157

    
158
        this.getNonProxyHosts();
159
        for (int i = 0; i < this.nomProxyHosts.size(); i++) {
160
            if (builder.length() == 0) {
161
                builder.append("|");
162
            }
163
            builder.append(this.nomProxyHosts.get(i));
164
        }
165
        return builder.toString();
166
    }
167

    
168
    public void setNonProxyHosts(String nonProxyHosts) {
169
        this.nomProxyHosts = new ArrayList<String>();
170
        String ss[] = nonProxyHosts.split("|");
171
        for (int i = 0; i < ss.length; i++) {
172
            if (StringUtils.isNotBlank(ss[i])) {
173
                this.nomProxyHosts.add(ss[i]);
174
            }
175
        }
176
    }
177

    
178
    public void addNonProxyHost(String host) {
179
        this.getNonProxyHosts();
180
        this.nomProxyHosts.add(host);
181
    }
182
}