File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\test\src\main\java\org\jvnet\hudson\test\JavaNetReverseProxy.java.html

1 package org.jvnet.hudson.test;
2
3 import hudson.Util;
4 import hudson.util.IOUtils;
5 import org.apache.commons.io.FileUtils;
6 import org.mortbay.jetty.Server;
7 import org.mortbay.jetty.bio.SocketConnector;
8 import org.mortbay.jetty.handler.ContextHandlerCollection;
9 import org.mortbay.jetty.servlet.Context;
10 import org.mortbay.jetty.servlet.ServletHolder;
11
12 import javax.servlet.ServletException;
13 import javax.servlet.http.HttpServlet;
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16 import java.io.File;
17 import java.io.IOException;
18 import java.net.URL;
19
20 /**
21 * Acts as a reverse proxy, so that during a test we can avoid hitting updates.jenkins-ci.org.
22 *
23 * <p>
24 * The contents are cached locally.
25 *
26 * @author Kohsuke Kawaguchi
27 */
28 public class JavaNetReverseProxy extends HttpServlet {
29 private final Server server;
30 public final int localPort;
31
32 private final File cacheFolder;
33
34 public JavaNetReverseProxy(File cacheFolder) throws Exception {
35 this.cacheFolder = cacheFolder;
36 cacheFolder.mkdirs();
37
38 server = new Server();
39
40 ContextHandlerCollection contexts = new ContextHandlerCollection();
41 server.setHandler(contexts);
42
43 Context root = new Context(contexts, "/", Context.SESSIONS);
44 root.addServlet(new ServletHolder(this), "/");
45
46 SocketConnector connector = new SocketConnector();
47 server.addConnector(connector);
48 server.start();
49
50 localPort = connector.getLocalPort();
51 }
52
53 public void stop() throws Exception {
54 server.stop();
55 }
56
57 // class Response {
58 // final URL url;
59 // final String contentType;
60 // final ByteArrayOutputStream data = new ByteArrayOutputStream();
61 //
62 // Response(URL url) throws IOException {
63 // this.url = url;
64 // URLConnection con = url.openConnection();
65 // contentType = con.getContentType();
66 // IOUtils.copy(con.getInputStream(),data);
67 // }
68 //
69 // void reproduceTo(HttpServletResponse rsp) throws IOException {
70 // rsp.setContentType(contentType);
71 // data.writeTo(rsp.getOutputStream());
72 // }
73 // }
74
75 @Override
76 protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
77 String path = req.getServletPath();
78 String d = Util.getDigestOf(path);
79
80 File cache = new File(cacheFolder, d);
81 if(!cache.exists()) {
82 URL url = new URL("http://updates.jenkins-ci.org/" + path);
83 FileUtils.copyURLToFile(url,cache);
84 }
85
86 resp.setContentType(getMimeType(path));
87 IOUtils.copy(cache,resp.getOutputStream());
88 }
89
90 private String getMimeType(String path) {
91 int idx = path.indexOf('?');
92 if(idx>=0)
93 path = path.substring(0,idx);
94 if(path.endsWith(".json")) return "text/javascript";
95 return getServletContext().getMimeType(path);
96 }
97
98 private static volatile JavaNetReverseProxy INSTANCE;
99
100 /**
101 * Gets the default instance.
102 */
103 public static synchronized JavaNetReverseProxy getInstance() throws Exception {
104 if(INSTANCE==null)
105 // TODO: think of a better location --- ideally inside the target/ dir so that clean would wipe them out
106 INSTANCE = new JavaNetReverseProxy(new File(new File(System.getProperty("java.io.tmpdir")),"jenkins-ci.org-cache2"));
107 return INSTANCE;
108 }
109 }
110