-
Notifications
You must be signed in to change notification settings - Fork 15
/
ProxySystemProperties.java
172 lines (145 loc) · 4.39 KB
/
ProxySystemProperties.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package com.teamscale.client;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Reads and writes Java system properties values for
* <ul>
* <li>http.proxyHost</li>
* <li>http.proxyPort</li>
* <li>http.proxyUser</li>
* <li>http.proxyPassword</li>
* </ul>
* or the corresponding HTTPS counterpart (starting with https instead of http).
* These values set the proxy server and credentials that should be used later to reach Teamscale.
*/
public class ProxySystemProperties {
private static final Logger LOGGER = LoggerFactory.getLogger(ProxySystemProperties.class);
private static final String PROXY_HOST_SYSTEM_PROPERTY = ".proxyHost";
private static final String PROXY_PORT_SYSTEM_PROPERTY = ".proxyPort";
private static final String PROXY_USER_SYSTEM_PROPERTY = ".proxyUser";
private static final String PROXY_PASSWORD_SYSTEM_PROPERTY = ".proxyPassword";
private final Protocol protocol;
/**
* Indicates, whether the {@link ProxySystemProperties} should return values for the http.proxy* system properties
* or the https.proxy* ones
*/
public enum Protocol {
HTTP,
HTTPS;
@Override
public String toString() {
return name().toLowerCase();
}
}
/**
* @param protocol Indicates, whether the {@link ProxySystemProperties} should use values for the http.proxy* system
* properties or the https.proxy* ones
*/
public ProxySystemProperties(Protocol protocol) {
this.protocol = protocol;
}
/**
* Checks whether proxyHost and proxyPort are set
*/
public boolean proxyServerIsSet() {
return !StringUtils.isEmpty(getProxyHost()) && getProxyPort() > 0;
}
/**
* Checks whether proxyUser and proxyPassword are set
*/
public boolean proxyAuthIsSet() {
return !StringUtils.isEmpty(getProxyUser()) && !StringUtils.isEmpty(getProxyPassword());
}
/**
* Read the http(s).proxyHost system variable
*/
public String getProxyHost() {
return System.getProperty(getProxyHostSystemPropertyName());
}
/**
* Read the http(s).proxyPort system variable.
* Returns -1 if no or an invalid port was set.
*/
public int getProxyPort() {
return parsePort(System.getProperty(getProxyPortSystemPropertyName()));
}
/**
* Set the http(s).proxyHost system variable
*/
public void setProxyHost(String proxyHost) {
System.setProperty(getProxyHostSystemPropertyName(), proxyHost);
}
@NotNull
private String getProxyHostSystemPropertyName() {
return protocol + PROXY_HOST_SYSTEM_PROPERTY;
}
/**
* Set the http(s).proxyPort system variable
*/
public void setProxyPort(int proxyPort) {
setProxyPort(proxyPort + "");
}
/**
* Set the http(s).proxyPort system variable
*/
public void setProxyPort(String proxyPort) {
System.setProperty(getProxyPortSystemPropertyName(), proxyPort);
}
/**
* Removes the http(s).proxyPort system variable.
* For testing.
*/
/*package*/ void removeProxyPort() {
System.clearProperty(getProxyPortSystemPropertyName());
}
@NotNull
private String getProxyPortSystemPropertyName() {
return protocol + PROXY_PORT_SYSTEM_PROPERTY;
}
/**
* Get the http(s).proxyUser system variable
*/
public String getProxyUser() {
return System.getProperty(getProxyUserSystemPropertyName());
}
/**
* Set the http(s).proxyUser system variable
*/
public void setProxyUser(String proxyUser) {
System.setProperty(getProxyUserSystemPropertyName(), proxyUser);
}
@NotNull
private String getProxyUserSystemPropertyName() {
return protocol + PROXY_USER_SYSTEM_PROPERTY;
}
/**
* Get the http(s).proxyPassword system variable
*/
public String getProxyPassword() {
return System.getProperty(getProxyPasswordSystemPropertyName());
}
/**
* Set the http(s).proxyPassword system variable
*/
public void setProxyPassword(String proxyPassword) {
System.setProperty(getProxyPasswordSystemPropertyName(), proxyPassword);
}
@NotNull
private String getProxyPasswordSystemPropertyName() {
return protocol + PROXY_PASSWORD_SYSTEM_PROPERTY;
}
/** Parses the given port string. Returns -1 if the string is null or not a valid number. */
private int parsePort(String portString) {
if (portString == null) {
return -1;
}
try {
return Integer.parseInt(portString);
} catch (NumberFormatException e) {
LOGGER.warn("Could not parse proxy port \"" + portString +
"\" set via \"" + getProxyPortSystemPropertyName() + "\"");
return -1;
}
}
}