1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package fr.cnes.doi.settings;
20
21 import fr.cnes.doi.utils.Utils;
22 import fr.cnes.doi.utils.spec.Requirement;
23 import fr.cnes.httpclient.HttpClientFactory.Type;
24 import fr.cnes.httpclient.configuration.ProxyConfiguration;
25 import fr.cnes.httpclient.configuration.ProxySPNegoJAASConfiguration;
26 import org.apache.logging.log4j.LogManager;
27 import org.apache.logging.log4j.Logger;
28
29
30
31
32
33
34 @Requirement(reqId = Requirement.DOI_CONFIG_010, reqName = Requirement.DOI_CONFIG_010_NAME)
35 public final class ProxySettings {
36
37
38
39
40 private static final Logger LOG = LogManager.getLogger(ProxySettings.class.getName());
41
42
43
44
45 private String proxyHost;
46
47
48
49
50 private String proxyPort;
51
52
53
54
55 private String proxyUser;
56
57
58
59
60 private String proxyPassword;
61
62
63
64
65 private String nonProxyHosts;
66
67
68
69
70 private boolean proxySet = false;
71
72
73
74
75 private String proxyType;
76
77
78
79
80 private String proxySpn;
81
82
83
84
85 private String proxyJaasFile;
86
87
88
89
90 private String proxyJaasCtx;
91
92
93
94
95 private ProxySettings() {
96 LOG.traceEntry();
97 init();
98 LOG.traceExit();
99 }
100
101
102
103
104
105
106 public static ProxySettings getInstance() {
107 LOG.traceEntry();
108 return LOG.traceExit(ProxySettingsHolder.INSTANCE);
109 }
110
111
112
113
114 public void init() {
115 LOG.traceEntry();
116 final DoiSettings settings = DoiSettings.getInstance();
117 LOG.info("----- proxy parameters ----");
118
119 this.proxyHost = settings.getString(Consts.SERVER_PROXY_HOST, "");
120 LOG.info("proxyHost : {}", this.proxyHost);
121
122 this.proxyPort = settings.getString(Consts.SERVER_PROXY_PORT, "");
123 LOG.info("proxyPort : {}", this.proxyPort);
124
125 this.proxyUser = settings.getString(Consts.SERVER_PROXY_LOGIN, "");
126 LOG.info("proxyUser : {}", this.proxyUser);
127
128 this.proxyPassword = settings.getSecret(Consts.SERVER_PROXY_PWD);
129 LOG.info("proxyPassword : {}", Utils.transformPasswordToStars(this.proxyPassword));
130
131 this.nonProxyHosts = settings.getString(Consts.SERVER_NONPROXY_HOSTS, "localhost");
132 LOG.info("nonProxyHosts : {}", this.nonProxyHosts);
133
134 this.proxySet = !Type.NO_PROXY.toString().equals(settings.
135 getString(Consts.SERVER_PROXY_TYPE));
136 LOG.info("proxySet : {}", this.proxySet);
137
138 this.proxyType = settings.getString(Consts.SERVER_PROXY_TYPE, Type.NO_PROXY.name());
139 LOG.info("proxyType : {}", this.proxyType);
140
141 this.proxySpn = settings.getString(Consts.SERVER_PROXY_JAAS_SPN, "");
142 LOG.info("proxy SPN : {}", this.proxySpn);
143
144 this.proxyJaasFile = settings.getString(Consts.SERVER_PROXY_JAAS_FILE, "");
145 LOG.info("proxy JAAS file : {}", this.proxyJaasFile);
146
147 this.proxyJaasCtx = settings.getString(Consts.SERVER_PROXY_JAAS_CONTEXT, "");
148 LOG.info("proxy JAAS ctx : {}", this.proxyJaasCtx);
149
150 LOG.info("Proxy settings have been loaded");
151 LOG.info("--------------------------");
152
153 LOG.traceExit();
154 }
155
156
157
158
159
160
161 public boolean isWithProxy() {
162 LOG.traceEntry();
163 return LOG.traceExit(this.proxySet);
164 }
165
166
167
168
169
170
171 public boolean isAuthenticate() {
172 LOG.traceEntry();
173 return LOG.traceExit(
174 !this.getProxyUser().isEmpty() && !this.getProxyPassword().isEmpty() && this.
175 isWithProxy());
176 }
177
178
179
180
181
182
183 public String getProxyHost() {
184 LOG.traceEntry();
185 return LOG.traceExit(proxyHost);
186 }
187
188
189
190
191
192
193 public String getProxyPort() {
194 LOG.traceEntry();
195 return LOG.traceExit(proxyPort);
196 }
197
198
199
200
201
202
203 public String getProxyUser() {
204 LOG.traceEntry();
205 return LOG.traceExit(proxyUser);
206 }
207
208
209
210
211
212
213 public String getProxyPassword() {
214 LOG.traceEntry();
215 return LOG.traceExit(proxyPassword);
216 }
217
218
219
220
221
222
223 public String getNonProxyHosts() {
224 LOG.traceEntry();
225 return LOG.traceExit(nonProxyHosts);
226 }
227
228
229
230
231
232
233 public String getProxyType() {
234 LOG.traceEntry();
235 return LOG.traceExit(this.proxyType);
236 }
237
238
239
240
241 public void configureProxy() {
242 final Type type = Type.valueOf(this.getProxyType());
243 LOG.info("Starting with proxy : {}", this.proxySet);
244 if (this.proxySet) {
245 switch (type) {
246 case PROXY_BASIC:
247 LOG.info("Proxy with Basic authentication");
248 ProxyConfiguration.HTTP_PROXY.setValue(this.getProxyHost() + ":" + this.
249 getProxyPort());
250 ProxyConfiguration.NO_PROXY.setValue(this.getNonProxyHosts());
251 if (this.isAuthenticate()) {
252 ProxyConfiguration.USERNAME.setValue(this.getProxyUser());
253 ProxyConfiguration.PASSWORD.setValue(this.getProxyPassword());
254 }
255 break;
256 case PROXY_SPNEGO_API:
257 LOG.info("Proxy with SPNego using API");
258 throw new IllegalArgumentException(
259 "SPNego trough API not supported, use JAAS configuration file");
260 case PROXY_SPNEGO_JAAS:
261 LOG.info("Proxy with SPNego using JAAS configuration file");
262 ProxySPNegoJAASConfiguration.HTTP_PROXY.setValue(
263 this.getProxyHost() + ":" + this.getProxyPort());
264 ProxySPNegoJAASConfiguration.NO_PROXY.setValue(this.getNonProxyHosts());
265 ProxySPNegoJAASConfiguration.JAAS.setValue(this.proxyJaasFile);
266 ProxySPNegoJAASConfiguration.JAAS_CONTEXT.setValue(this.proxyJaasCtx);
267 ProxySPNegoJAASConfiguration.SERVICE_PROVIDER_NAME.setValue(this.proxySpn);
268 break;
269 default:
270 throw new IllegalArgumentException("Proxy " + type + " is not implemented");
271 }
272 }
273 }
274
275
276
277
278 private static class ProxySettingsHolder {
279
280
281
282
283 private static final ProxySettings INSTANCE = new ProxySettings();
284 }
285
286 }