Go to new doc!

+49 228 5552576-0


info@predic8.com

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)));
That's it. Your proxy is up and running, and listens for requests on port 3128. Just tell your other applications to use this port, and you're set.

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)); 
In this example, the Router will forward any incoming HTTP POST requests on port 4000 to thomas-bayer.com:80. The regular expressions path and hostname can be used to forward only requests with matching HTTP paths or Host:-headers.

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());
Yes, that's all. From now on, every request and response will be intercepted by your implementation.