How to create Proxy Server using jetty8?

情到浓时终转凉″ 提交于 2019-12-25 02:28:22

问题


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

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

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.

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

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.

 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.


回答1:


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.

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.

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

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

good luck



来源:https://stackoverflow.com/questions/9596668/how-to-create-proxy-server-using-jetty8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!