REST Router Quickstart Tutorial
Help needed?
Do you need any help for this tutorial. Then contact us using the Membrane Google Group or send an email to membrane@predic8.com.
In this tutorial you will create a service proxy for a REST API and add additional functionality like URL rewriting, logging and transformation.
To run the example:
- download Membrane Service Proxy version 4.0.18 or above and unzip it.
- open a command prompt and change to the installation folder of the unzipped archive.
- change to the subfolder examples/quickstart-rest
- execute service-proxy.bat or service-proxy.sh. Make sure you are in the examples/quickstart-rest folder.
- Now take a look at the REST resource at the following URL.
The script service-proxy.bat will start the router and load the proxies.xml file.
<spring:beans xmlns="http://membrane-soa.org/proxies/1/" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://membrane-soa.org/proxies/1/ http://membrane-soa.org/schemas/proxies-1.xsd"> <router> <serviceProxy name="names" port="2000"> <target host="thomas-bayer.com" port="80" /> </serviceProxy> </router> </spring:beans>
That's all to setup a reverse proxy. Pretty easy isn't it? The serviceProxy will listen at port 2000 for HTTP messages. If it receives a message it will be forwarded 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 rewriter to the serviceProxy.
- Save the proxies.xml file and Membrane will detect the modification and automatically reload.
- Open the following URL in your browser: http://localhost:2000/names/Pia
[...] <serviceProxy name="names" port="2000"> <path isRegExp="true">/(rest)?names.*</path> <rewriter> <map from="^/names/(.*)" to="/restnames/name\.groovy\?name=$1" /> </rewriter> <target host="thomas-bayer.com" port="80" /> </serviceProxy> [...]
Logging
- Add a statisticsCSV interceptor to the serviceProxy and specify a location for the logfile.
- Save the proxies.xml file to trigger a reload of Membrane.
- 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.
<serviceProxy name="names" port="2000"> <path isRegExp="true">/(rest)?names.*</path> <rewriter> <map from="^/names/(.*)" to="/restnames/name\.groovy\?name=$1" /> </rewriter> <statisticsCSV file="log.csv" /> <target host="thomas-bayer.com" port="80" /> </serviceProxy>
Applying A XSLT Transformation
- 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 response element. By using the response element the transformation will only be applied to the response.
- Save the proxies.xml file to trigger a reload.
- Open the following URL in your browser: http://localhost:2000/names/Pia
<serviceProxy name="names" port="2000"> <path isRegExp="true">/(rest)?names.*</path> <rewriter> <map from="/names/(.*)" to="/restnames/name\.groovy\?name=$1" /> </rewriter> <statisticsCSV file="log.csv" /> <response> <transform xslt="restnames.xsl" /> </response> <target host="thomas-bayer.com" port="80" /> </serviceProxy>
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 Regular 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.
- Save the proxies.xml file.
- Open the following URL in your browser: http://localhost:2000/names/Pia
<serviceProxy name="names" port="2000"> <path isRegExp="true">/(rest)?names.*</path> <rewriter> <map from="^/names/(.*)" to="/restnames/name\.groovy\?name=$1" /> </rewriter> <statisticsCSV file="log.csv" /> <response> <regExReplacer regex="\s*,\s*<" replace="<" /> <transform xslt="restnames.xsl" /> </response> <target host="thomas-bayer.com" port="80" /> </serviceProxy>
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.
- Save the proxies.xml file.
- 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.
<spring:beans xmlns="http://membrane-soa.org/proxies/1/" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://membrane-soa.org/proxies/1/ http://membrane-soa.org/schemas/proxies-1.xsd"> <router> <serviceProxy name="names" port="2000"> <path isRegExp="true">/(rest)?names.*</path> <rewriter> <map from="^/names/(.*)" to="/restnames/name\.groovy\?name=$1" /> </rewriter> <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> </router> </spring:beans>
Figure1:
You will see all interceptors you have registered before.
Figure2:
Securing with HTTP Basic Authentication
- Add a basicAuthentication interceptor to the serviceProxy named Console.
- Save the proxies.xml file.
- Open the following URL in your browser: http://localhost:9000/admin
<spring:beans xmlns="http://membrane-soa.org/proxies/1/" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://membrane-soa.org/proxies/1/ http://membrane-soa.org/schemas/proxies-1.xsd"> <router> <serviceProxy name="names" port="2000"> <path isRegExp="true">/(rest)?names.*</path> <rewriter> <map from="/names/(.*)" to="/restnames/name\.groovy\?name=$1" /> </rewriter> <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> </router> </spring:beans>
Login with the username alice and the password membrane.