如何使用jetty8创建代理服务器?

问题描述:

我已经使用Java套接字创建了代理服务器(正向和反向).

I had created a Proxy Server (Fwd and Reverse) using Java sockets.

,它将在浏览器中配置的8080上侦听传入请求,并将其转发到另一个Proxy Server2.

which would listen to Incoming Request on 8080 configured in browser and forward them to another Proxy Server2.

然后读取服务器2发送的响应,并将其写入浏览器.

And Read the Response sent by the server2 and write it to the browser.

同时,代码将记录请求和响应,并阻止来自浏览器的某些预定义请求类型.

Meanwhile code will be logging requests and response and also blocking certain predefined request types from browser.

现在,我想使用Jetty来做到这一点,并且还支持HTTPS请求.

Now I want to do this using Jetty and also support HTTPS request.

我搜索了例如,但没有找到.

I searched for example but I found none.

这将在8080处启动服务器,我已在浏览器的代理设置中将其配置为代理端口.

This starts server at 8080 which I have configured as proxy port in Browser's proxy setting.

 import org.eclipse.jetty.server.Server;

import Handler.HelloHandler;

public class StartJetty
{
    public static void main(String[] args) throws Exception
    {
        Server server = new Server(8080);

        server.setHandler(new HelloHandler());
        server.start();
        server.join();
    }
}

这是我用来侦听请求并将响应写回到浏览器的处理程序.

This is the handler which I use to listen to request and write response back to browser.

package Handler;

import java.io.IOException;

 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;

 import org.eclipse.jetty.server.Request;
 import org.eclipse.jetty.server.handler.AbstractHandler;

public class HelloHandler extends AbstractHandler
{
    final String _greeting;
          final String _body;

          public HelloHandler()
          {
              _greeting="Hello World";
              _body=null;
          }

          public HelloHandler(String greeting)
          {
              _greeting=greeting;
              _body=null;
          }

          public HelloHandler(String greeting,String body)
          {
              _greeting=greeting;
              _body=body;
          }

          public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
          {
              response.setContentType("text/html;charset=utf-8");
              response.setStatus(HttpServletResponse.SC_OK);
              baseRequest.setHandled(true);

              response.getWriter().println("<h1>"+_greeting+"</h1>");
              if (_body!=null)
                  response.getWriter().println(_body);
          }
}

一旦收到响应,我便要将其转发到代理服务器,等待其响应并将其写回到浏览器.我需要帮助.

Once I have the response I want to forward it to proxy server, wait for its response and write it back to the Browser. I need help with that.

在jetty-servlets工件中,有一个ProxyServlet将为您完成异步代理工作.

In the jetty-servlets artifact there is a ProxyServlet that will do async proxy work for you.

我只想尝试一下,看看它是否符合您的需求.

I would just give that a try and see if it fits your needs.

在该项目的测试中是一个AsyncProxyServer,您可以启动并旋转一下.

In the tests of that project is an AsyncProxyServer that you can just start up and give a whirl.

用于代理的基础延续和码头客户端可以通过自定义方法进行扩展.

The underlying continuation and jetty clients used for the proxying are extensible through the customize methods.

祝你好运