views:

2496

answers:

8

How would I go about creating an HTTP Proxy in Java for use recording and playing back HTTP Sessions? This would be for entirely legitimate performance monitoring purposes.

Are there any classes in the standard JDK or does anyone know of any examples that are available?

Thanks.

+2  A: 

jmeter.apache.org has an internal http proxy which it uses for recording page hits. It is a fairly decent proxy and has fairly wide support and thus serves as a decent example. The code although tied to JMeter is a good learning aid.

A second library that might help you is HTTPComponents again on apache (http://hc.apache.org/) which contains a client and core HTTP support set of libraries which can be used to write a reasonable proxy at least from the client side.

In Java 1.6 there is also a com.sun.net.httpserver HTTPServer class which is outside of the normal API but it can allow for quite simple servers to be written, and combined with something like HTTP Components for talking to the clients could be used to put together a simple proxy.

Paul Keeble
Good answer! Thanks.
David Taylor
+1  A: 

http://www.ericdaugherty.com/dev/sshwebproxy/ works well. Eventually you may want to write your own(at least we did) but at least you have a starting point. Also have a look at http://www.mortbay.org/jetty/jetty-6/apidocs/org/mortbay/servlet/ProxyServlet.html.

adrian.tarau
This is worthy of study. Thanks.
David Taylor
+1  A: 

An alternative that might work out better for you is to use a stand-alone HTTP debugging proxy. Ones to check out include WebScarab, Fiddler, and Charles.

If you're writing a web application in Java, and hitting it from browsers, then definitely check out Firefox's FireBug and YSlow! plugins -- they're pretty awesome for optimizing the front end aspects of your web app.

Jim Ferrans
I didn't know about Firebug. Thanks.
David Taylor
+1  A: 

Yet another option is to use The Grinder's HTTP proxy implementation:

http://grinder.svn.sourceforge.net/viewvc/grinder/trunk/source/src/net/grinder/plugin/http/tcpproxyfilter/

Vinko Vrsalovic
This is the best! Thank you very much.
David Taylor
+4  A: 

In your question you mentioned you're interested in standard JDK classes, so how about sockets? They're there since JDK 1.0.

If you're not familiar with the concept, try the relevant lesson on Sun's Java tutorial. For a sample proxy code from which you can start, see the last post of this thread (that's where I started. ;-) ). Naturally, some more reading and trial & error are necessary for making it work the way you want.

Oz
A sly answer but well worth study. Thanks.
David Taylor
+5  A: 

Hi,

You may be interested in this database of open source HTTP proxies written in Java.

Database of open source proxies written in java.

Regards,

Alan.

A: 

You can have a look at the core module of Membrane Monitor. It is a HTTP proxy (normal and reverse) that is written in Java and open source under the ASF 2.0 license. You can also just add the Jar of the router and use a proxy in your code. See the junit test how to do that ( about 5 lines of code).

baranco
+1  A: 

I've written an HTTP proxy called LittleProxy that works really well. It runs on top of Netty, the super fast NIO framework. Netty should theoretically allow it to scale to around 100,000 simultaneous connections, although LittleProxy hasn't been tested to that degree in the wild. It's extremely easy to get up and running, and it's easy to integrate programmatically. I originally wrote it as a component of a censorship circumvention tool that's in active development, and others have been using it as well. It's licensed under the Apache 2.0 license.

adamfisk