Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.jvnet.hudson.test;
  
  import hudson.Util;
 
 import java.io.File;
 import java.net.URL;

Acts as a reverse proxy to https://hudson.dev.java.net/ , so that during a test we can avoid hitting java.net.

The contents are cached locally.

Author(s):
Kohsuke Kawaguchi
 
 public class JavaNetReverseProxy extends HttpServlet {
     private final Server server;
     public final int localPort;
 
     private final File cacheFolder;
 
     public JavaNetReverseProxy(File cacheFolderthrows Exception {
         this. = cacheFolder;
         cacheFolder.mkdirs();
 
          = new Server();
 
         ContextHandlerCollection contexts = new ContextHandlerCollection();
         .setHandler(contexts);
 
         Context root = new Context(contexts"/".);
         root.addServlet(new ServletHolder(this), "/");
 
         SocketConnector connector = new SocketConnector();
         .addConnector(connector);
         .start();
 
          = connector.getLocalPort();
     }
 
     public void stop() throws Exception {
         .stop();
     }
 
 //    class Response {
 //        final URL url;
 //        final String contentType;
 //        final ByteArrayOutputStream data = new ByteArrayOutputStream();
 //
 //        Response(URL url) throws IOException {
 //            this.url = url;
 //            URLConnection con = url.openConnection();
 //            contentType = con.getContentType();
 //            IOUtils.copy(con.getInputStream(),data);
 //        }
 //
 //        void reproduceTo(HttpServletResponse rsp) throws IOException {
 //            rsp.setContentType(contentType);
 //            data.writeTo(rsp.getOutputStream());
 //        }
 //    }
 
     @Override
     protected void service(HttpServletRequest reqHttpServletResponse respthrows ServletExceptionIOException {
         String path = req.getServletPath();
         String d = Util.getDigestOf(path);
 
         File cache = new File(d);
         if(!cache.exists()) {
             URL url = new URL("http://hudson-ci.org/" + path);
             FileUtils.copyURLToFile(url,cache);
         }
 
         resp.setContentType(getMimeType(path));
         IOUtils.copy(cache,resp.getOutputStream());
     }
 
     private String getMimeType(String path) {
         int idx = path.indexOf('?');
         if(idx>=0)
             path = path.substring(0,idx);
         if(path.endsWith(".json"))  return "text/javascript";
         return getServletContext().getMimeType(path);
     }
 
    private static volatile JavaNetReverseProxy INSTANCE;

    
Gets the default instance.
    public static synchronized JavaNetReverseProxy getInstance() throws Exception {
        if(==null)
            // TODO: think of a better location --- ideally inside the target/ dir so that clean would wipe them out
             = new JavaNetReverseProxy(new File(new File(System.getProperty("java.io.tmpdir")),"hudson-ci.org-cache2"));
        return ;
    }
New to GrepCode? Check out our FAQ X