Skip to content

Instantly share code, notes, and snippets.

@dwelch2344
Created November 29, 2012 18:51
Show Gist options
  • Save dwelch2344/4171088 to your computer and use it in GitHub Desktop.
Save dwelch2344/4171088 to your computer and use it in GitHub Desktop.
A simple proxy test
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
public class EndpointServer{
public static void main(String[] args) throws Exception {
Server server = new Server();
SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(9999);
server.addConnector(connector);
HandlerCollection handlers = new HandlerCollection();
server.setHandler(handlers);
// Setup proxy servlet
ServletContextHandler context = new ServletContextHandler(handlers, "/", ServletContextHandler.SESSIONS);
ServletHolder servlet = new ServletHolder(DemoServlet.class);
context.addServlet(servlet, "/*");
server.start();
}
public static class DemoServlet implements Servlet{
private ServletConfig config;
public void init(ServletConfig config) throws ServletException {
this.config = config;
}
public ServletConfig getServletConfig() {
return config;
}
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
HttpServletRequest request = (HttpServletRequest) req;
PrintWriter writer = res.getWriter();
writer.write("Hello world from Demo. You requested " + request.getRequestURI());
writer.flush();
writer.close();
}
public String getServletInfo() {
return "DemoServlet Info";
}
public void destroy() {
}
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!-- Omitted project details -->
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>example-jetty-embedded</artifactId>
<version>7.4.5.v20110725</version>
</dependency>
</dependencies>
</project>
import java.net.MalformedURLException;
import org.eclipse.jetty.http.HttpURI;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ConnectHandler;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.servlets.ProxyServlet;
public class ProxyServer {
public static void main(String[] args) throws Exception {
Server server = new Server();
SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(8888);
server.addConnector(connector);
HandlerCollection handlers = new HandlerCollection();
server.setHandler(handlers);
// Setup proxy servlet
ServletContextHandler context = new ServletContextHandler(handlers, "/", ServletContextHandler.SESSIONS);
ServletHolder proxyServlet = new ServletHolder(MyProxyServlet.class);
proxyServlet.setInitParameter("whiteList", "google.com, www.eclipse.org, localhost");
proxyServlet.setInitParameter("blackList", "google.com/calendar/*, www.eclipse.org/committers/");
context.addServlet(proxyServlet, "/*");
// Setup proxy handler to handle CONNECT methods
ConnectHandler proxy = new ConnectHandler();
proxy.setWhite(new String[]{"mail.google.com"});
proxy.addWhite("www.google.com");
handlers.addHandler(proxy);
server.start();
}
public static class MyProxyServlet extends ProxyServlet{
@Override
protected HttpURI proxyHttpURI(String scheme, String serverName,
int serverPort, String uri) throws MalformedURLException {
// TODO Auto-generated method stub
System.out.println("Requested " + scheme + "://" + serverName + ":" + serverPort + uri);
return new HttpURI("http://localhost:9999" + uri);
}
}
}
@dwelch2344
Copy link
Author

Related SO quesiton about handling Connect (and SSL) http://stackoverflow.com/questions/9852056/jetty-proxyservlet-with-ssl-support

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment