File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\ProxyConfiguration.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson;
25
26 import com.google.common.collect.Lists;
27 import hudson.model.AbstractDescribableImpl;
28 import hudson.model.Descriptor;
29 import hudson.util.FormValidation;
30 import jenkins.model.Jenkins;
31 import hudson.model.Saveable;
32 import hudson.model.listeners.SaveableListener;
33 import hudson.util.Scrambler;
34 import hudson.util.Secret;
35 import hudson.util.XStream2;
36
37 import java.io.File;
38 import java.io.IOException;
39 import java.io.Serializable;
40 import java.net.Authenticator;
41 import java.net.InetSocketAddress;
42 import java.net.PasswordAuthentication;
43 import java.net.Proxy;
44 import java.net.URL;
45 import java.net.URLConnection;
46
47 import com.thoughtworks.xstream.XStream;
48 import java.io.InputStream;
49 import java.util.Collections;
50 import java.util.List;
51 import java.util.regex.Pattern;
52
53 import org.jvnet.robust_http_client.RetryableHttpStream;
54 import org.kohsuke.stapler.DataBoundConstructor;
55 import org.kohsuke.stapler.QueryParameter;
56
57 /**
58 * HTTP proxy configuration.
59 *
60 * <p>
61 * Use {@link #open(URL)} to open a connection with the proxy setting.
62 * <p>
63 * Proxy authentication (including NTLM) is implemented by setting a default
64 * {@link Authenticator} which provides a {@link PasswordAuthentication}
65 * (as described in the Java 6 tech note
66 * <a href="http://java.sun.com/javase/6/docs/technotes/guides/net/http-auth.html">
67 * Http Authentication</a>).
68 *
69 * @see jenkins.model.Jenkins#proxy
70 */
71 public final class ProxyConfiguration extends AbstractDescribableImpl<ProxyConfiguration> implements Saveable, Serializable {
72 public final String name;
73 public final int port;
74
75 /**
76 * Possibly null proxy user name.
77 */
78 private final String userName;
79
80 /**
81 * List of host names that shouldn't use proxy, as typed by users.
82 *
83 * @see #getNoProxyHostPatterns()
84 */
85 public final String noProxyHost;
86
87 @Deprecated
88 private String password;
89
90 /**
91 * encrypted password
92 */
93 private Secret secretPassword;
94
95 public ProxyConfiguration(String name, int port) {
96 this(name,port,null,null);
97 }
98
99 public ProxyConfiguration(String name, int port, String userName, String password) {
100 this(name,port,userName,password,null);
101 }
102
103 @DataBoundConstructor
104 public ProxyConfiguration(String name, int port, String userName, String password, String noProxyHost) {
105 this.name = Util.fixEmptyAndTrim(name);
106 this.port = port;
107 this.userName = Util.fixEmptyAndTrim(userName);
108 this.secretPassword = Secret.fromString(password);
109 this.noProxyHost = Util.fixEmptyAndTrim(noProxyHost);
110 }
111
112 public String getUserName() {
113 return userName;
114 }
115
116 // This method is public, if it was public only for jelly, then should make it private (or inline contents)
117 // Have left public, as can't tell if anyone else is using from plugins
118 /**
119 * @return the password in plain text
120 */
121 public String getPassword() {
122 return Secret.toString(secretPassword);
123 }
124
125 public String getEncryptedPassword() {
126 return (secretPassword == null) ? null : secretPassword.getEncryptedValue();
127 }
128
129 /**
130 * Returns the list of properly formatted no proxy host names.
131 */
132 public List<Pattern> getNoProxyHostPatterns() {
133 return getNoProxyHostPatterns(noProxyHost);
134 }
135
136 /**
137 * Returns the list of properly formatted no proxy host names.
138 */
139 public static List<Pattern> getNoProxyHostPatterns(String noProxyHost) {
140 if (noProxyHost==null) return Collections.emptyList();
141
142 List<Pattern> r = Lists.newArrayList();
143 for (String s : noProxyHost.split("[ \t\n,|]+")) {
144 if (s.length()==0) continue;
145 r.add(Pattern.compile(s.replace(".", "\\.").replace("*", ".*")));
146 }
147 return r;
148 }
149
150 /**
151 * @deprecated
152 * Use {@link #createProxy(String)}
153 */
154 public Proxy createProxy() {
155 return createProxy(null);
156 }
157
158 public Proxy createProxy(String host) {
159 return createProxy(host, name, port, noProxyHost);
160 }
161
162 public static Proxy createProxy(String host, String name, int port, String noProxyHost) {
163 if (host!=null && noProxyHost!=null) {
164 for (Pattern p : getNoProxyHostPatterns(noProxyHost)) {
165 if (p.matcher(host).matches())
166 return Proxy.NO_PROXY;
167 }
168 }
169 return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(name,port));
170 }
171
172 public void save() throws IOException {
173 if(BulkChange.contains(this)) return;
174 XmlFile config = getXmlFile();
175 config.write(this);
176 SaveableListener.fireOnChange(this, config);
177 }
178
179 public Object readResolve() {
180 if (secretPassword == null)
181 // backward compatibility : get crambled password and store it encrypted
182 secretPassword = Secret.fromString(Scrambler.descramble(password));
183 password = null;
184 return this;
185 }
186
187 public static XmlFile getXmlFile() {
188 return new XmlFile(XSTREAM, new File(Jenkins.getInstance().getRootDir(), "proxy.xml"));
189 }
190
191 public static ProxyConfiguration load() throws IOException {
192 XmlFile f = getXmlFile();
193 if(f.exists())
194 return (ProxyConfiguration) f.read();
195 else
196 return null;
197 }
198
199 /**
200 * This method should be used wherever {@link URL#openConnection()} to internet URLs is invoked directly.
201 */
202 public static URLConnection open(URL url) throws IOException {
203 Jenkins h = Jenkins.getInstance(); // this code might run on slaves
204 ProxyConfiguration p = h!=null ? h.proxy : null;
205 if(p==null)
206 return url.openConnection();
207
208 URLConnection con = url.openConnection(p.createProxy(url.getHost()));
209 if(p.getUserName()!=null) {
210 // Add an authenticator which provides the credentials for proxy authentication
211 Authenticator.setDefault(new Authenticator() {
212 @Override
213 public PasswordAuthentication getPasswordAuthentication() {
214 if (getRequestorType()!=RequestorType.PROXY) return null;
215 ProxyConfiguration p = Jenkins.getInstance().proxy;
216 return new PasswordAuthentication(p.getUserName(),
217 p.getPassword().toCharArray());
218 }
219 });
220 }
221
222 for (URLConnectionDecorator d : URLConnectionDecorator.all())
223 d.decorate(con);
224
225 return con;
226 }
227
228 public static InputStream getInputStream(URL url) throws IOException {
229 Jenkins h = Jenkins.getInstance(); // this code might run on slaves
230 final ProxyConfiguration p = (h != null) ? h.proxy : null;
231 if (p == null)
232 return new RetryableHttpStream(url);
233
234 InputStream is = new RetryableHttpStream(url, p.createProxy(url.getHost()));
235 if (p.getUserName() != null) {
236 // Add an authenticator which provides the credentials for proxy authentication
237 Authenticator.setDefault(new Authenticator() {
238
239 @Override
240 public PasswordAuthentication getPasswordAuthentication() {
241 if (getRequestorType() != RequestorType.PROXY) {
242 return null;
243 }
244 return new PasswordAuthentication(p.getUserName(), p.getPassword().toCharArray());
245 }
246 });
247 }
248
249 return is;
250 }
251
252 private static final XStream XSTREAM = new XStream2();
253
254 private static final long serialVersionUID = 1L;
255
256 static {
257 XSTREAM.alias("proxy", ProxyConfiguration.class);
258 }
259
260 @Extension
261 public static class DescriptorImpl extends Descriptor<ProxyConfiguration> {
262 @Override
263 public String getDisplayName() {
264 return "Proxy Configuration";
265 }
266
267 public FormValidation doCheckPort(@QueryParameter String value) {
268 value = Util.fixEmptyAndTrim(value);
269 if (value == null) {
270 return FormValidation.ok();
271 }
272 int port;
273 try {
274 port = Integer.parseInt(value);
275 } catch (NumberFormatException e) {
276 return FormValidation.error(Messages.PluginManager_PortNotANumber());
277 }
278 if (port < 0 || port > 65535) {
279 return FormValidation.error(Messages.PluginManager_PortNotInRange(0, 65535));
280 }
281 return FormValidation.ok();
282 }
283 }
284 }
285