最近學習了一下Jetty,從http://iamrunning.iteye.com/blog/410253 看到不錯的代碼,因此轉過來java
所須要的Jar包api
jetty-6.1.18.jarapp
jetty-client-6.1.18.jaride
jetty-sslengine-6.1.18.jar學習
jetty-util-6.1.18.jarui
servlet-api-2.5-20081211.jarurl
- package net.chenpu.http;
-
- import java.io.IOException;
- import java.util.Enumeration;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.mortbay.io.Buffer;
- import org.mortbay.io.BufferUtil;
- import org.mortbay.jetty.Server;
- import org.mortbay.jetty.client.Address;
- import org.mortbay.jetty.client.ContentExchange;
- import org.mortbay.jetty.client.HttpClient;
- import org.mortbay.jetty.client.security.ProxyAuthorization;
- import org.mortbay.jetty.servlet.Context;
- import org.mortbay.jetty.servlet.ServletHolder;
-
- public class HttpProxy {
- public static void main(String[] args) throws Exception {
- Server server = new Server(8001);
- Context context = new Context(server, "/", Context.SESSIONS);
- context.addServlet(new ServletHolder(new LocalProxyServlet()), "/*");
-
- server.start();
- server.join();
- }
-
- @SuppressWarnings("serial")
- public static class LocalProxyServlet extends HttpServlet {
-
- private HttpClient client = new HttpClient();
-
- public LocalProxyServlet() {
- client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
- // if you need,set proxy gate
- // client.setProxy(new Address("127.0.0.1",8080));
- // try {
- // client.setProxyAuthentication(new
- // ProxyAuthorization("user","password"));
- // } catch (IOException e1) {
- // // TODO Auto-generated catch block
- // e1.printStackTrace();
- // }
- try {
- client.start();
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
-
- }
-
- @Override
- protected void service(HttpServletRequest request,
- final HttpServletResponse response) throws ServletException,
- IOException {
- StringBuilder accessPath = new StringBuilder(request
- .getRequestURL().toString());
- System.out.println("access " + accessPath);
- ContentExchange exchange = new ContentExchange() {
- // improve performance... waiting
- @Override
- protected void onResponseHeader(Buffer name, Buffer value)
- throws IOException {
- String nameStr = BufferUtil.to8859_1_String(name);
- String valueStr = BufferUtil.to8859_1_String(value);
- response.setHeader(nameStr, valueStr);
- }
-
- @Override
- protected void onResponseContent(Buffer content)
- throws IOException {
- response.getOutputStream().write(content.asArray());
- }
-
- @Override
- protected void onResponseStatus(Buffer version, int status,
- Buffer reason) throws IOException {
- response.setStatus(status);
- }
-
- @Override
- protected void onConnectionFailed(Throwable ex) {
- // TODO Auto-generated method stub
- ex.printStackTrace();
- response.setStatus(500);
- }
-
- @Override
- protected void onException(Throwable ex) {
- // TODO Auto-generated method stub
- ex.printStackTrace();
- response.setStatus(500);
- }
- };
-
- // set jetty http client request params
- exchange.setMethod(request.getMethod());
- if (request.getMethod().equalsIgnoreCase("GET")) {
- Enumeration<String> params = request.getParameterNames();
- if (params != null) {
- if (params.hasMoreElements()) {
- accessPath.append('?');
- }
- while (params.hasMoreElements()) {
- String name = params.nextElement();
- String value = request.getParameter(name);
- accessPath.append(name);
- if (value != null) {
- accessPath.append('=').append(value);
- }
- if (params.hasMoreElements()) {
- accessPath.append('&');
- }
- }
- }
- }
- // set request url
- exchange.setURL(accessPath.toString());
- // set http header
- Enumeration<String> headers = request.getHeaderNames();
- while (headers.hasMoreElements()) {
- String name = headers.nextElement();
- String value = request.getHeader(name);
- exchange.setRequestHeader(name, value);
- }
-
- if (request.getInputStream() != null)
- exchange.setRequestContentSource(request.getInputStream());
-
- // start the exchange
- client.send(exchange);
- try {
- exchange.waitForDone();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }