Smooks In a Servlet Container (Two Minute)

The following is a very simple tutorial demonstrating how to plug Smooks into a Servlet Filter chain, and get it to transform the Servlet Response based on the type of requesting browser.

So, what we're going to do is very simple:

  1. For MS Pocket IE browsers, we will convert <code> elements to <pre> elements.
  2. For all other browser types, we will leave <code> elements unchanged.

Install Smooks in the Servlet Container

Your WEB-INF folder should contain the following file and directory structure:

WEB-INF-|
        cdr-|
        lib-|
        | device-ident.xml
        | device-profile.xml
        | smooks-cdr.lst

Download the Smooks Core and Smooks Servlet Cartridge binaries (plus dependencies) and install them in the lib folder in the usual manor.

For more information on Smooks deployment in a Servlet Container, see the Smooks Servlet Filter docs.

Configure Device Identification

Modify the WEB-INF/device-ident.xml file to configure browser identification - so we know when it's a PocketIE browser making the request.

Note how we assign the name "PocketIE" to the Pocket IE browser.

<device-ident>
    <device name="Firefox">
        <http-req-header name="User-Agent" value=".*Firefox\/.*" />
    </device>
    <device name="PocketIE">
        <http-req-header name="User-Agent" value=".*Windows CE; PPC.*" />
    </device>
    <device name="Unknown-HTML" match="*">
        <http-req-header name="Accept" value=".*text\/html.*" />
        <http-req-header name="Accept" value=".*\*\/\*.*" />
    </device>
    <device name="text">
        <http-req-header name="Accept" value=".*text\/plain.*" />
    </device>
</device-ident>

For more information on device recognition, see the device recognition docs.

Write the "Transformation Unit"

So, the transformation code to perform this task is very simple:

public class RenameElementTrans extends AbstractProcessingUnit {

	// cache the new element name.
	private String newElementName;

	public RenameElementTrans(SmooksResourceConfiguration config) {
		super(config);
		// Capture the new name for the element from the configuration...
		newElementName = config.getStringParameter("new-name");
	}

	public void visit(Element element, ContainerRequest containerRequest) {
		// Rename the element to the configured new name.
		DomUtils.renameElement(element, newElementName, true, true);
	}

	public boolean visitBefore() {
		// We want this transformation code to visit the target element after all
		// child elements have been iterated over.
		return false;
	}
}

The constructor and "visit" method code is of most interest. In the constructor we capture a parameter from the transformation configuration (talked about below). In the "visit" method, we use this parameter value to perform the rename. So as you can see, we've actually implemented the transformation unit such that it can be reused for renaming any element.

Classes of interest here are:

"Target" the Transformation Unit at the Pocket IE Browser

To target the new "RenameElementTrans" Transformation Unit at <code> elements on the Pocket IE browser, we need to either add a configuration to an existing .cdrl config file, or create a new .cdrl file. For the purposes of this tutorial, we will create a new .cdrl.

We'll call the new .cdrl config "pocket-ie.cdrl" and place it in the root of the WEB-INF/cdr folder. It's contents are as follows:

<smooks-resource-list> 
        <!-- On "PocketIE" browser, visit <code> elements with the RenameElementTrans Transformation Unit. -->
	<smooks-resource useragent="PocketIE" selector="code" path="com.acme.trans.RenameElementTrans">
                <!-- Specify the "rename" name using a <param> definition. -->
		<param name="new-name">pre</param>
	</smooks-resource>
</smooks-resource-list>

The last thing to be done is to tell Smooks to load the new .cdrl config pocket-ie.cdrl. We do this by adding very simple entry in the WEB-INF/smooks-cdr.lst file. This is a simple flat text file i.e. not XML. So, the entry for pocket-ie.cdrl is added as follows:

/WEB-INF/cdr/pocket-ie.cdrl

And getting more complex...

To see more complex transformations, configurations etc see the "what to look at" section of the Chiba Integration Notes.

Labels

 
(None)