Embedding Membrane into Java Programs
The Membrane Router API makes it easy to embed the Router into your own programs.
Required Libraries
In order to use the Router API, you'll need to include the libraries from %MEMBRANE_HOME%\lib in your CLASSPATH.
Using Membrane Monitor as a HTTP Proxy
To set up Membrane Monitor as a normal HTTP Proxy, you just need to start a Router instance and tell it to listen to forwarding requests.
import com.predic8.membrane.core.*; import com.predic8.membrane.core.rules.*; HttpRouter router = new HttpRouter(); //set up the proxy rule int listenPort = 3128; router.getRuleManager().addRuleIfNew(new ProxyRule(new ProxyRuleKey(listenPort)));
Using Membrane Monitor as a Reverse Proxy
A Reverse proxy accepts all kinds of requests, but forwards them to one single host only. This way, the Router mimics the target host.
import com.predic8.membrane.core.*; import com.predic8.membrane.core.rules.*; HttpRouter router = new HttpRouter(); //set up the Rule Key String hostname = "*"; String method = "POST"; String path = ".*"; int listenPort = 4000; ServiceProxyKey ruleKey = new ServiceProxyKey(hostname, method, path, listenPort); //set up the service proxy String targetHostname = "thomas-bayer.com"; int targetPort = 80; router.getRuleManager().addRuleIfNew(new ServiceProxy(ruleKey, targetHostname, targetPort));
Adding Interceptors to the chain
All the work is done in Interceptors, and those are the key elements for you to interfere with the exchanged information. To chain in your own Interceptor implementation, add the following code:
//router has been set up before router.getTransport().getInterceptors().add(new MyInterceptor());