AdroitLogic Private Ltd.

  • Increase font size
  • Default font size
  • Decrease font size

UltraESB as a Reverse Proxy or Web Proxy

Although the UltraESB is mainly targetted towards service interactions, sometimes services return HTML content, which is not typically well handled by an ESB. This example shows the ability of the UltraESB to proxy HTML responses, acting as a Reverse Proxy

 

To try out this sample, start the ToolBox and the sample Jetty server within it. The sample server hosts a servlet that outputs HTML content. One can issue a GET request to the URL http://localhost:9000/service/HtmlServlet from a browser to get this response directly from the servlet as shown below. Any optional parameters passed to the servlet will be displayed within the response body as shown below.

Now, start the sample configuration 102 of the UltraESB through the ToolBox, or the command line as follows

asankha@asankha:~/java/ultraesb-1.0-beta-1/bin$ ./ultraesb.sh -sample 102

Now issue the same request, after replacing the port from 9000 to 8280 - which is the port use by UltraESB. Thus the full URL would be something like http://localhost:8280/service/HtmlServlet?a=1&b=2 and will yield the following result.

Let us now evaluate the proxy service configuration, which allowed the UltraESB to act as a Reverse Proxy in this example. The proxy is accepting any request over the transport and forwarding it to the target server. The transport property "url" specifies a URL pattern over which the proxy service will accept messages, and by default the URL of a service will be : "/service/<proxy-id>".

    <u:proxy id="web-proxy-1">
<u:transport id="http-8280">
<u:property name="url" value="*"/>
</u:transport>
<u:target>
<u:inDestination>
<u:address type="prefix">http://localhost:9000</u:address>
<u:property name="switchLocationHeadersTo" value="http://localhost:8280/"/>
</u:inDestination>
<u:outDestination>
<u:address type="response"/>
</u:outDestination>
</u:target>
</u:proxy>

Next we will see how the UltraESB can even mediate such requests through it with the "web-proxy-2" in the same configuration file. This proxy service is hosted over HTTP port 8281, and shows how multiple transport instances can be configured with the UltraESB. This allows an UltraESB instance for example to show one SSL certificate to one set of users, and another for another set of users! It also makes the implementation of HTTP Basic or Digest authentication simpler across multiple services.

Getting back to the example, a request to the URL http://localhost:8281/vpath?x=1&y=2&z=3 will now be handled by the "web-proxy-2" which states that it should use "/vpath" as its URL. In addition, its advised to drop this same prefix, so that the request could be forwarded to the actual service using the prefix URL http://localhost:9000/service/HtmlServlet. However, responses returned to the client maybe advised to re-write certain headers such as the "Location" header - so that clients will always be exposed only to the proxy service.

The "inSequence" of the proxy will substitue parameters such as "?x=1&y=2&z=3" to "?a=1&b=2&c=3" using a Javascript snippet. Javascript is the default JSR 233 scripting language bundled with the JDK. You may select ANY scripting language [e.g. Groovy, Ruby, FreeMarker, Python, Velocity erc - See https://scripting.dev.java.net/ for a complete list] support by the JDK 6 after placing the necessary library JAR files into the UltraESB library directories.

The configuration of this proxy service is as shown below

    <u:proxy id="web-proxy-2">
<u:transport id="http-8281">
<u:property name="url" value="/vpath"/>
<u:property name="stripURLPrefix" value="/vpath"/>
</u:transport>
<u:target>
<u:inSequence>
<u:script ><![CDATA[
var val = message.getMessageProperty("QUERY_STRING");
val = val.replace('x', 'a');
val = val.replace('y', 'b');
val = val.replace('z', 'c');
message.addMessageProperty("QUERY_STRING", val);
]]></u:script>
</u:inSequence>
<u:inDestination>
<u:address type="prefix">http://localhost:9000/service/HtmlServlet</u:address>
<u:property name="switchLocationHeadersTo" value="http://localhost:8280/vpath"/>
</u:inDestination>
<u:outDestination>
<u:address type="response"/>
</u:outDestination>
</u:target>
</u:proxy>

If you would like to try out further examples, try commenting the "web-proxy-1" and uncommenting the "web-proxy" and restarting the UltraESB. Now directing your browser to URL http://localhost:8280/ will instead take you to the MIT home page as shown below. Notice that the links within the web page body have been re-written to point back at the proxy instead of the MIT pages!

The configuration driving this is the proxy service given below.Note that the response is read into a String, and a simple 'replaceAll' call has been made in this example to perform the mediation - although much more advanced and useful mediation is available for use.

<u:proxy id="web-proxy">
<u:transport id="http-8280">
<u:property name="url" value="*"/>
</u:transport>
<u:target>
<u:inDestination>
<u:address type="prefix">http://web.mit.edu</u:address>
<u:property name="switchLocationHeadersTo" value="http://localhost:8280/"/>
</u:inDestination>
<u:outSequence>
<u:java><![CDATA[
String original = Mediation.readPayloadAsString(msg);
if (original != null) {
Mediation.setPayloadFromString(msg, original.replaceAll("web.mit.edu", "localhost:8280"));
}
]]></u:java>
</u:outSequence>
<u:outDestination>
<u:address type="response"/>
</u:outDestination>
</u:target>
</u:proxy>