Jetty Servlet+ HttpClient 制作的 Java HttpProxy

最近学习了一下Jetty,从http://iamrunning.iteye.com/blog/410253 看到不错的代码,所以转过来

 所需要的Jar包

jetty-6.1.18.jar

jetty-client-6.1.18.jar

 jetty-sslengine-6.1.18.jar

jetty-util-6.1.18.jar

servlet-api-2.5-20081211.jar

Java代码  
  1. package net.chenpu.http;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.Enumeration;  
  5.   
  6. import javax.servlet.ServletException;  
  7. import javax.servlet.http.HttpServlet;  
  8. import javax.servlet.http.HttpServletRequest;  
  9. import javax.servlet.http.HttpServletResponse;  
  10.   
  11. import org.mortbay.io.Buffer;  
  12. import org.mortbay.io.BufferUtil;  
  13. import org.mortbay.jetty.Server;  
  14. import org.mortbay.jetty.client.Address;  
  15. import org.mortbay.jetty.client.ContentExchange;  
  16. import org.mortbay.jetty.client.HttpClient;  
  17. import org.mortbay.jetty.client.security.ProxyAuthorization;  
  18. import org.mortbay.jetty.servlet.Context;  
  19. import org.mortbay.jetty.servlet.ServletHolder;  
  20.   
  21. public class HttpProxy {  
  22.     public static void main(String[] args) throws Exception {  
  23.         Server server = new Server(8001);  
  24.         Context context = new Context(server, "/", Context.SESSIONS);  
  25.         context.addServlet(new ServletHolder(new LocalProxyServlet()), "/*");  
  26.   
  27.         server.start();  
  28.         server.join();  
  29.     }  
  30.   
  31.     @SuppressWarnings("serial")  
  32.     public static class LocalProxyServlet extends HttpServlet {  
  33.   
  34.         private HttpClient client = new HttpClient();  
  35.   
  36.         public LocalProxyServlet() {  
  37.             client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);  
  38.             // if you need,set proxy gate  
  39.             // client.setProxy(new Address("127.0.0.1",8080));  
  40.             // try {  
  41.             // client.setProxyAuthentication(new  
  42.             // ProxyAuthorization("user","password"));  
  43.             // } catch (IOException e1) {  
  44.             // // TODO Auto-generated catch block  
  45.             // e1.printStackTrace();  
  46.             // }  
  47.             try {  
  48.                 client.start();  
  49.             } catch (Exception e) {  
  50.                 throw new RuntimeException(e);  
  51.             }  
  52.   
  53.         }  
  54.   
  55.         @Override  
  56.         protected void service(HttpServletRequest request,  
  57.                 final HttpServletResponse response) throws ServletException,  
  58.                 IOException {  
  59.             StringBuilder accessPath = new StringBuilder(request  
  60.                     .getRequestURL().toString());  
  61.             System.out.println("access " + accessPath);  
  62.             ContentExchange exchange = new ContentExchange() {  
  63.                 // improve performance... waiting  
  64.                 @Override  
  65.                 protected void onResponseHeader(Buffer name, Buffer value)  
  66.                         throws IOException {  
  67.                     String nameStr = BufferUtil.to8859_1_String(name);  
  68.                     String valueStr = BufferUtil.to8859_1_String(value);  
  69.                     response.setHeader(nameStr, valueStr);  
  70.                 }  
  71.   
  72.                 @Override  
  73.                 protected void onResponseContent(Buffer content)  
  74.                         throws IOException {  
  75.                     response.getOutputStream().write(content.asArray());  
  76.                 }  
  77.   
  78.                 @Override  
  79.                 protected void onResponseStatus(Buffer version, int status,  
  80.                         Buffer reason) throws IOException {  
  81.                     response.setStatus(status);  
  82.                 }  
  83.   
  84.                 @Override  
  85.                 protected void onConnectionFailed(Throwable ex) {  
  86.                     // TODO Auto-generated method stub  
  87.                     ex.printStackTrace();  
  88.                     response.setStatus(500);  
  89.                 }  
  90.   
  91.                 @Override  
  92.                 protected void onException(Throwable ex) {  
  93.                     // TODO Auto-generated method stub  
  94.                     ex.printStackTrace();  
  95.                     response.setStatus(500);  
  96.                 }  
  97.             };  
  98.   
  99.             // set jetty http client request params  
  100.             exchange.setMethod(request.getMethod());  
  101.             if (request.getMethod().equalsIgnoreCase("GET")) {  
  102.                 Enumeration<String> params = request.getParameterNames();  
  103.                 if (params != null) {  
  104.                     if (params.hasMoreElements()) {  
  105.                         accessPath.append('?');  
  106.                     }  
  107.                     while (params.hasMoreElements()) {  
  108.                         String name = params.nextElement();  
  109.                         String value = request.getParameter(name);  
  110.                         accessPath.append(name);  
  111.                         if (value != null) {  
  112.                             accessPath.append('=').append(value);  
  113.                         }  
  114.                         if (params.hasMoreElements()) {  
  115.                             accessPath.append('&');  
  116.                         }  
  117.                     }  
  118.                 }  
  119.             }  
  120.             // set request url  
  121.             exchange.setURL(accessPath.toString());  
  122.             // set http header  
  123.             Enumeration<String> headers = request.getHeaderNames();  
  124.             while (headers.hasMoreElements()) {  
  125.                 String name = headers.nextElement();  
  126.                 String value = request.getHeader(name);  
  127.                 exchange.setRequestHeader(name, value);  
  128.             }  
  129.   
  130.             if (request.getInputStream() != null)  
  131.                 exchange.setRequestContentSource(request.getInputStream());  
  132.   
  133.             // start the exchange  
  134.             client.send(exchange);  
  135.             try {  
  136.                 exchange.waitForDone();  
  137.             } catch (InterruptedException e) {  
  138.                 e.printStackTrace();  
  139.             }  
  140.         }  
  141.     }  
  142. }  

你可能感兴趣的:(Jetty Servlet+ HttpClient 制作的 Java HttpProxy)