Nerd Notes

/dev/brain: no space left on device

Archive for the ‘struts’ Category

Struttin’ from the CLI

leave a comment »

Suppose you want to write a shell script that calls every action in your struts-config.xml in order to, say, stress test every callable url in your application.

Assume you have the following struts-config.xml:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<action-mappings>
<action
path="/page1"
forward="/WEB-INF/page/page1.jsp"/>
<action
path="/page2"
forward="/WEB-INF/page/page2.jsp"/>
</action-mappings>
</struts-config>

The point here is that you want to extract the path attribute value for each action and complete it with a full blown URI. You could do that with a lot of grep and/or awk or you could just use xmlstarlet. So, go now, download a copy and install it, or fire up your favourite package manager (be it apt-get, synaptic, portage or whatever) and let it do the job for you.

Once you have xmlstarlet installed, just do:

$ xmlstarlet sel -t -m "/struts-config/action-mappings/action" \
-o http://localhost/ctx -v "@path" -o .do -n struts-config.xml

and you should get the following output:

http://localhost/ctx/page1.do
http://localhost/ctx/page2.do

This basically means that you can, for instance, use wget to grab all those URIs:

$ xmlstarlet sel -t \
-m "/struts-config/action-mappings/action" \
-o "wget http://localhost/ctx" \
-v "@path" \
-o .do -n \
struts-config.xml | sh

This will fire up wget for each action path in the struts-config.xml. Neat, uh?

Under the hood, xmlstarlet is simply using some xslt transformation on the xpath expressions given in the above template to do its work. So, in case you want to sharpen your xml tools, you’ll need to get a better understanding of xpath at least, since it has to be used in the select templates.

Written by Mirko Caserta

November 15, 2006 at 3:34 pm

Posted in CLI, struts, XML