REST Router Quickstart Tutorial
To run the example do the following:
- Download Membrane Router version 3.0.0 or above.
- Execute router.bat in the MEMBRANE_HOME/examples/quickstart-rest directory.
- Now take a look at the REST resource at the following URL.
The script router.bat will start the router and load the proxies.xml file.
<proxies> <serviceProxy name="names" port="2000"> <target host="thomas-bayer.com" port="80" /> </serviceProxy> </proxies>
Thats all to setup a reverse proxy. Pretty easy isn't it? The serviceProxy will listen at port 2000 at the localhost for HTTP messages. If it receives a message it will be forwarding to thomas-bayer.com at port 80.
Next we will use the RESTNames resources that offer information about firstnames and their distribution in different countries.
http://localhost:2000//restnames/name.groovy?name=Pia
<restnames> <nameinfo> <name>Pia</name> <countries> <country href="http://thomas-bayer.com:80/restnames/namesincountry.groovy?country=Italy">Italy</country> <country href="http://thomas-bayer.com:80/restnames/namesincountry.groovy?country=Spain">Spain</country> <country href="http://thomas-bayer.com:80/restnames/namesincountry.groovy?country=Belgium">Belgium</country> <country href="http://thomas-bayer.com:80/restnames/namesincountry.groovy?country=Netherlands">Netherlands</country> <country href="http://thomas-bayer.com:80/restnames/namesincountry.groovy?country=Frisia">Frisia</country> <country href="http://thomas-bayer.com:80/restnames/namesincountry.groovy?country=Germany">Germany</country> <country href="http://thomas-bayer.com:80/restnames/namesincountry.groovy?country=Austria">Austria</country> <country href="http://thomas-bayer.com:80/restnames/namesincountry.groovy?country=Swiss">Swiss</country> <country href="http://thomas-bayer.com:80/restnames/namesincountry.groovy?country=Denmark">Denmark</country> <country href="http://thomas-bayer.com:80/restnames/namesincountry.groovy?country=Norway">Norway</country> <country href="http://thomas-bayer.com:80/restnames/namesincountry.groovy?country=Sweden">Sweden</country> <country href="http://thomas-bayer.com:80/restnames/namesincountry.groovy?country=Finland">Finland</country> </countries> <gender>female first name</gender> <male>false</male> <female>true</female> </nameinfo> </restnames>
In the response you can see a list of countries where the name Pia is used often.
Rewriting URLs
- Open the proxies.xml file in an editor.
- Add a regexUrlRewriter to the serviceProxy. Wrap it with a request element. By using the request element the interceptor will only be called for the request.
- Restart the router.
- Open the following URL in your browser: http://localhost:2000/names/Pia
<proxies> <serviceProxy name="names" port="2000"> <request> <regExUrlRewriter> <mapping regex="/names/(.*)" uri="/restnames/name\.groovy\?name=$1" /> </regExUrlRewriter> </request> <target host="thomas-bayer.com" port="80" /> </serviceProxy> </proxies>
Logging
- Add a StatisticsCSV interceptor to the serviceProxy and specify a location for the logfile.
- Restart the router.
- Open the following URL in your browser: http://localhost:2000/names/Pia
- Take a look at the log.csv file generated in the rest-quickstart directory.
<proxies>
<serviceProxy name="names" port="2000">
<request>
<regExUrlRewriter>
<mapping regex="/names/(.*)" uri="/restnames/name\.groovy\?name=$1" />
</regExUrlRewriter>
</request>
<statisticsCSV file="log.csv" />
<target host="thomas-bayer.com" port="80" />
</serviceProxy>
</proxies>
Applying A XSLT Transformation to Requests and Responses
- Add a Transform interceptor to the serviceProxy and specify the XSLT file that will be applied to requests and responses. Wrap the transformer with a request element. By using the request element the transformation will only be applied to the response.
- Restart the router.
- Open the following URL in your browser: http://localhost:2000/names/Pia
<proxies>
<serviceProxy name="names" port="2000">
<request>
<regExUrlRewriter>
<mapping regex="/names/(.*)" uri="/restnames/name\.groovy\?name=$1" />
</regExUrlRewriter>
</request>
<statisticsCSV file="log.csv" />
<response>
<transform xslt="restnames.xsl" />
</response>
<target host="thomas-bayer.com" port="80" />
</serviceProxy>
</proxies>
The transformed response will look like this:
<restnames>
<name>Pia</name>
<countries>Italy, Spain, Belgium, Netherlands, Frisia, Germany, Austria, Swiss, Denmark, Norway, Sweden, Finland,</countries>
<gender>female first name</gender>
</restnames>
Using Regulare Expressions To Replace Parts Of The Content
- Add a RegExReplacer to the serviceProxy. Place it within the response element and befor the Transformer interceptor. Specify the regex to match against the content and the string that will be used to replace the matched substrings.
- Restart the router.
- Open the following URL in your browser: http://localhost:2000/names/Pia
<proxies>
<serviceProxy name="names" port="2000">
<request>
<regExUrlRewriter>
<mapping regex="/names/(.*)" uri="/restnames/name\.groovy\?name=$1" />
</regExUrlRewriter>
</request>
<statisticsCSV file="log.csv" />
<response>
<regExReplacer regex="\s*,\s*<" replace="<" />
<transform xslt="restnames.xsl" />
</response>
<target host="thomas-bayer.com" port="80" />
</serviceProxy>
</proxies>
Notice the deleted comma at the end of the list.
<restnames>
<name>Pia</name>
<countries>Italy, Spain, Belgium, Netherlands, Frisia, Germany, Austria, Swiss, Denmark, Norway, Sweden, Finland</countries>
<gender>female first name</gender>
</restnames>
Administration Console
Membrane Router provides a Web console that allows you to view and list proxy definitions.
- Open the proxies.xml file in an editor.
- Add an additional serviceProxy containing an adminConsole element.
- Restart the router.
- Open the following URL in your browser: http://localhost:9000/admin
- Now you will see a list containing the service proxies you have defined in the proxies.xml file.
- Click on the link names.
<proxies>
<serviceProxy name="names" port="2000">
<request>
<regExUrlRewriter>
<mapping regex="/names/(.*)" uri="/restnames/name\.groovy\?name=$1" />
</regExUrlRewriter>
</request>
<statisticsCSV file="log.csv" />
<response>
<regExReplacer regex="\s*,\s*<" replace="<" />
<transform xslt="restnames.xsl" />
</response>
<target host="thomas-bayer.com" port="80" />
</serviceProxy>
<serviceProxy name="Console" port="9000">
<adminConsole />
</serviceProxy>
</proxies>

Figure 1:
You will see all interceptors you have registered before.

Figure 2:
Securing with HTTP Basic Authentication
- Add a BasicAuthentication interceptor to the serviceProxy called Consol.
- Restart the router.
- Open the following URL in your browser: http://localhost:9000/admin
<proxies>
<serviceProxy name="names" port="2000">
<request>
<regExUrlRewriter>
<mapping regex="/names/(.*)" uri="/restnames/name\.groovy\?name=$1" />
</regExUrlRewriter>
</request>
<statisticsCSV file="log.csv" />
<response>
<regExReplacer regex="\s*,\s*<" replace="<" />
<transform xslt="restnames.xsl" />
</response>
<target host="thomas-bayer.com" port="80" />
</serviceProxy>
<serviceProxy name="Console" port="9000">
<basicAuthentication>
<user name="alice" password="membrane" />
</basicAuthentication>
<adminConsole />
</serviceProxy>
</proxies>
Login with the username alice and the password membrane.
