简体   繁体   English

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

[英]How to create Proxy Server using jetty8?

I had created a Proxy Server (Fwd and Reverse) using Java sockets. 我已经使用Java套接字创建了代理服务器(正向和反向)。

which would listen to Incoming Request on 8080 configured in browser and forward them to another Proxy Server2. 它将在浏览器中配置的8080上侦听传入请求,并将其转发到另一个Proxy Server2。

And Read the Response sent by the server2 and write it to the browser. 并读取server2发送的响应,并将其写入浏览器。

Meanwhile code will be logging requests and response and also blocking certain predefined request types from browser. 同时,代码将记录请求和响应,并阻止来自浏览器的某些预定义请求类型。

Now I want to do this using Jetty and also support HTTPS request. 现在,我想使用Jetty来做到这一点,并且还支持HTTPS请求。

I searched for example but I found none. 我搜索了例如,但没有找到。

This starts server at 8080 which I have configured as proxy port in Browser's proxy setting. 这将在8080处启动服务器,我已在浏览器的代理设置中将其配置为代理端口。

 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. 我需要帮助。

In the jetty-servlets artifact there is a ProxyServlet that will do async proxy work for you. 在jetty-servlets工件中,有一个ProxyServlet,它将为您完成异步代理工作。

I would just give that a try and see if it fits your needs. 我只是尝试一下,看看它是否适合您的需求。

In the tests of that project is an AsyncProxyServer that you can just start up and give a whirl. 在该项目的测试中,您可以启动AsyncProxyServer并进行旋转。

The underlying continuation and jetty clients used for the proxying are extensible through the customize methods. 可通过自定义方法扩展用于代理的基础连续性和码头客户端。

http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/ProxyServlet.java?h=jetty-8 http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/ProxyServlet.java?h=码头-8

and

http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/AsyncProxyServer.java?h=jetty-8 http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/AsyncProxyServer.java?h=码头-8

good luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM