Thursday, January 8, 2009

How to write vrd-config.xml file?

It's very important to understand how we should write vrd-config.xml. There is a file named vrd-config-1.0.xsd which is available with the API and can be used to validate vrd-config.xml however it is not required. Below is a quick guide:

The root element of the configuration file is vrd-config. It may have 0 or many request elements. E.g.

<vrd-config>
<request ...>
...
</request>
</vrd-config>
The request elements take two attributes, pattern (mandatory) and exclude. Both accepts java regular expressions, e.g.

<request pattern=".*" exclude="/scripts/.*">
...
</request>
The above definition is to intercept all URIs except those followed by /script/ .

Each request element can have either response or invocation as child element. The response element takes two attributes, url (mandatory) and redirect (optional with default value false), e.g.

<request pattern=".*">
<response url="http://www.google.com" redirect="true"/>
</request>
Normal if the response is a forward, the redirect attribute is not specified. To do a response redirect on the same web application, you need to specify #{contextPath} as prefix in the path attribute, e.g.

<request pattern="/|/index.jsp">
<response path="#{contextPath}/welcome.jsp" redirect="true"/>
</request>
The invocation element of the response object is used to invoke java method. There are two attributes, method-name (mandatory) and bean-class. There are two ways to specify the method to invoke:

1. Using method-name and bean-class attributes:
<request pattern=".*">
<invocation bean-class="Foo" method-name="bar"/>
</request>
2. Using only the method-name attribute:
<request pattern=".*">
<invocation method-name="Foo.bar"/>
</request>

The above mentioned methods are only applicable to public static methods of a public class. If the method is public but not static, there are two additional (optional) attributes, var and scope. The var attribute is used to specify a variable name to refer instance of bean-class. The scope is the scope of the instance. The possible values of scope are application, session, and request. If not specified, the scope is local. E.g.

<request pattern=".*">
<invocation bean-class="Foo" method-name="bar" var="foo" scope="request"/>
</request>

Note: The method that can be invoked must satisfy one of the following method signatures:

public (static) void|Object methodName();
public (static) void|Object methodName(javax.servlet.http.HttpServletRequest);
public (static) void|Object methodName(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse);
The Object can be an instance of any java class, but if it is a String, it can be used to define navigation. For navigation, you must use result element of invocation element. The result element takes two attributes, pattern and exclude and allows either response or invocation element just like request element.

<invocation method-name="java.util.Date.toString" var="dt" scope="application">
<result pattern="(Sat|Sun).*">
<response url="#{contextPath}/access-denied.html" redirect="true"/>
</result>
</invocation>
Or
<invocation method-name="A.m1">
<result pattern="value1">
<invocation method-name="B.m2">
<result pattern="value2">
<response url="http://www.someurl.com" redirect="true"/>
</result>
</invocation>
</result>
</invocation>
This is all about writing vrd-config.xml file.

No comments:

Post a Comment