Search in sources :

Example 1 with ConnectHandler

use of org.eclipse.jetty.proxy.ConnectHandler in project jetty.project by eclipse.

the class ProxyServer method main.

public static void main(String[] args) throws Exception {
    Server server = new Server();
    ServerConnector connector = new ServerConnector(server);
    connector.setPort(8888);
    server.addConnector(connector);
    // Setup proxy handler to handle CONNECT methods
    ConnectHandler proxy = new ConnectHandler();
    server.setHandler(proxy);
    // Setup proxy servlet
    ServletContextHandler context = new ServletContextHandler(proxy, "/", ServletContextHandler.SESSIONS);
    ServletHolder proxyServlet = new ServletHolder(ProxyServlet.class);
    proxyServlet.setInitParameter("blackList", "www.eclipse.org");
    context.addServlet(proxyServlet, "/*");
    server.start();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) ConnectHandler(org.eclipse.jetty.proxy.ConnectHandler) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 2 with ConnectHandler

use of org.eclipse.jetty.proxy.ConnectHandler in project async-http-client by AsyncHttpClient.

the class BasicHttpProxyToHttpsTest method setUpGlobal.

@BeforeClass(alwaysRun = true)
public void setUpGlobal() throws Exception {
    // HTTP server
    httpServer = new Server();
    ServerConnector connector1 = addHttpsConnector(httpServer);
    httpServer.setHandler(new EchoHandler());
    httpServer.start();
    httpPort = connector1.getLocalPort();
    // proxy
    proxy = new Server();
    ServerConnector connector2 = addHttpConnector(proxy);
    ConnectHandler connectHandler = new ConnectHandler() {

        @Override
        protected boolean handleAuthentication(HttpServletRequest request, HttpServletResponse response, String address) {
            String authorization = request.getHeader(PROXY_AUTHORIZATION.toString());
            if (authorization == null) {
                response.setStatus(HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED);
                response.setHeader(PROXY_AUTHENTICATE.toString(), "Basic realm=\"Fake Realm\"");
                return false;
            } else if (authorization.equals("Basic am9obmRvZTpwYXNz")) {
                return true;
            }
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            return false;
        }
    };
    proxy.setHandler(connectHandler);
    proxy.start();
    proxyPort = connector2.getLocalPort();
    LOGGER.info("Local HTTP Server (" + httpPort + "), Proxy (" + proxyPort + ") started successfully");
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpServletRequest(javax.servlet.http.HttpServletRequest) ConnectHandler(org.eclipse.jetty.proxy.ConnectHandler) Server(org.eclipse.jetty.server.Server) HttpServletResponse(javax.servlet.http.HttpServletResponse) EchoHandler(org.asynchttpclient.test.EchoHandler) BeforeClass(org.testng.annotations.BeforeClass)

Example 3 with ConnectHandler

use of org.eclipse.jetty.proxy.ConnectHandler in project camel by apache.

the class HttpProxyMojoIntegrationTest method setupServer.

@BeforeClass
public static void setupServer() throws Exception {
    // start a local HTTP proxy using Jetty server
    server = new Server();
    /*
        final SSLContextParameters contextParameters = new SSLContextParameters();
        final SslContextFactory sslContextFactory = new SslContextFactory();
        sslContextFactory.setSslContext(contextParameters.createSSLContext());
        ServerConnector connector = new ServerConnector(server, sslContextFactory);
*/
    ServerConnector connector = new ServerConnector(server);
    connector.setHost(HTTP_PROXY_HOST);
    server.addConnector(connector);
    final String authenticationString = "Basic " + B64Code.encode(HTTP_PROXY_USER_NAME + ":" + HTTP_PROXY_PASSWORD, StringUtil.__ISO_8859_1);
    ConnectHandler connectHandler = new ConnectHandler() {

        @Override
        protected boolean handleAuthentication(HttpServletRequest request, HttpServletResponse response, String address) {
            // validate proxy-authentication header
            final String header = request.getHeader(PROXY_AUTHORIZATION.toString());
            if (!authenticationString.equals(header)) {
                LOG.warn("Missing header " + PROXY_AUTHORIZATION);
                // ask for authentication header
                response.setHeader(PROXY_AUTHENTICATE.toString(), String.format("Basic realm=\"%s\"", HTTP_PROXY_REALM));
                return false;
            }
            LOG.info("Request contains required header " + PROXY_AUTHORIZATION);
            return true;
        }
    };
    server.setHandler(connectHandler);
    LOG.info("Starting proxy server...");
    server.start();
    httpProxyPort = connector.getLocalPort();
    LOG.info("Started proxy server on port {}", httpProxyPort);
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpServletRequest(javax.servlet.http.HttpServletRequest) ConnectHandler(org.eclipse.jetty.proxy.ConnectHandler) Server(org.eclipse.jetty.server.Server) HttpServletResponse(javax.servlet.http.HttpServletResponse) BeforeClass(org.junit.BeforeClass)

Example 4 with ConnectHandler

use of org.eclipse.jetty.proxy.ConnectHandler in project camel by apache.

the class HttpProxyIntegrationTest method setupServer.

@BeforeClass
public static void setupServer() throws Exception {
    // start a local HTTP proxy using Jetty server
    server = new Server();
    /*
        final SSLContextParameters contextParameters = new SSLContextParameters();
        final SslContextFactory sslContextFactory = new SslContextFactory();
        sslContextFactory.setSslContext(contextParameters.createSSLContext());
        ServerConnector connector = new ServerConnector(server, sslContextFactory);
*/
    ServerConnector connector = new ServerConnector(server);
    connector.setHost(HTTP_PROXY_HOST);
    server.addConnector(connector);
    final String authenticationString = "Basic " + B64Code.encode(HTTP_PROXY_USER_NAME + ":" + HTTP_PROXY_PASSWORD, StringUtil.__ISO_8859_1);
    ConnectHandler connectHandler = new ConnectHandler() {

        @Override
        protected boolean handleAuthentication(HttpServletRequest request, HttpServletResponse response, String address) {
            // validate proxy-authentication header
            final String header = request.getHeader(PROXY_AUTHORIZATION.toString());
            if (!authenticationString.equals(header)) {
                LOG.warn("Missing header " + PROXY_AUTHORIZATION);
                // ask for authentication header
                response.setHeader(PROXY_AUTHENTICATE.toString(), String.format("Basic realm=\"%s\"", HTTP_PROXY_REALM));
                return false;
            }
            LOG.info("Request contains required header " + PROXY_AUTHORIZATION);
            return true;
        }
    };
    server.setHandler(connectHandler);
    LOG.info("Starting proxy server...");
    server.start();
    httpProxyPort = connector.getLocalPort();
    LOG.info("Started proxy server on port {}", httpProxyPort);
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpServletRequest(javax.servlet.http.HttpServletRequest) ConnectHandler(org.eclipse.jetty.proxy.ConnectHandler) Server(org.eclipse.jetty.server.Server) HttpServletResponse(javax.servlet.http.HttpServletResponse) BeforeClass(org.junit.BeforeClass)

Example 5 with ConnectHandler

use of org.eclipse.jetty.proxy.ConnectHandler in project async-http-client by AsyncHttpClient.

the class ProxyTunnellingTest method setUpServers.

private void setUpServers(boolean targetHttps) throws Exception {
    server = new Server();
    ServerConnector connector = addHttpConnector(server);
    server.setHandler(new ConnectHandler());
    server.start();
    port1 = connector.getLocalPort();
    server2 = new Server();
    @SuppressWarnings("resource") ServerConnector connector2 = targetHttps ? addHttpsConnector(server2) : addHttpConnector(server2);
    server2.setHandler(configureHandler());
    server2.start();
    port2 = connector2.getLocalPort();
    logger.info("Local HTTP server started successfully");
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) ConnectHandler(org.eclipse.jetty.proxy.ConnectHandler) ProxyServer(org.asynchttpclient.proxy.ProxyServer) Server(org.eclipse.jetty.server.Server)

Aggregations

ConnectHandler (org.eclipse.jetty.proxy.ConnectHandler)5 Server (org.eclipse.jetty.server.Server)5 ServerConnector (org.eclipse.jetty.server.ServerConnector)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 BeforeClass (org.junit.BeforeClass)2 ProxyServer (org.asynchttpclient.proxy.ProxyServer)1 EchoHandler (org.asynchttpclient.test.EchoHandler)1 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)1 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)1 BeforeClass (org.testng.annotations.BeforeClass)1