This sample explains how to validate the content of an XML resource uploaded to WSO2 Governance Registry using XPath and regular expressions in a few easy steps. This is useful to introduce additional validation to files such as WSDL, Schema and Policy. We will be reusing the code of the Handler Sample in this example. This sample requires Apache Maven. See Installing Apache Maven for Governance Registry on Windows or Installing Apache Maven for Governance Registry on Linux .
Instructions
1. Navigate to GREG_HOME/
samples/handler/src
to find the source code of the Handler Sample.
2. Add a new Java Class named XPathValidationHandler
at
GREG_HOME/samples/handler/src/src/main/java/org/wso2/carbon/registry/samples/handler/XPathValidationHandler.java
with the following source:
package org.wso2.carbon.registry.samples.handler; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.impl.builder.StAXOMBuilder; import org.apache.axiom.om.xpath.AXIOMXPath; import org.jaxen.JaxenException; import org.wso2.carbon.registry.core.Resource; import org.wso2.carbon.registry.core.exceptions.RegistryException; import org.wso2.carbon.registry.core.jdbc.handlers.Handler; import org.wso2.carbon.registry.core.jdbc.handlers.RequestContext; import javax.xml.namespace.QName; import java.io.ByteArrayInputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.regex.Pattern; public class XPathValidationHandler extends Handler { private AXIOMXPath xPath; private String attribute; private Pattern regEx; private Map<String, String> namespaces = new HashMap<String, String>(); public void setXPath(String xPath) { try { this.xPath = new AXIOMXPath(xPath); } catch (JaxenException e) { e.printStackTrace(); } } public void setNamespaces(String namespaces) { String[] t1 = namespaces.split(";"); for (String t2 : t1) { int t3 = t2.indexOf(":"); this.namespaces.put(t2.substring(0, t3), t2.substring(t3 + 1)); } } public void setRegEx(String regEx) { this.regEx = Pattern.compile(regEx); } public void setAttribute(String attribute) { this.attribute = attribute; } public void put(RequestContext requestContext) throws RegistryException { Resource resource = requestContext.getResource(); OMElement payload = null; Object o = resource.getContent(); if (o != null && (o instanceof byte[])) { try { payload = new StAXOMBuilder(new ByteArrayInputStream((byte[]) resource.getContent())).getDocumentElement(); } catch (Exception e) { throw new RegistryException("Unable to parse content of resource", e); } } if (payload != null && xPath != null) { try { if (xPath.getNamespaces().size() == 0) { for (Map.Entry<String, String> e : namespaces.entrySet()) { xPath.addNamespace(e.getKey(), e.getValue()); } } ArrayList result = (ArrayList) xPath.evaluate(payload); if (result.size() > 0) { OMElement element = (OMElement) result.get(0); if (!regEx.matcher(element.getAttributeValue(new QName(attribute))).matches()) { throw new RegistryException("Regular expression validation failure"); } } } catch (JaxenException e) { throw new RegistryException("Unable to evaluate XPath expression", e); } } } }
3. Compile the source code by running the following command inside GREG_HOME/
samples/handler/src
:
mvn clean install
Note
The command mvn clean install
will trigger an Apache Maven Build in your command line. This requires you having installed Apache Maven. See Installing Apache Maven for Governance Registry on Windows or Installing Apache Maven for Governance Registry on Linux.
A successful run of Apache Maven will generate a report similar to the following:
3. Copy the GREG_HOME/
samples/handler/src/target/
org.wso2.carbon.registry.samples.handler-4.5.0.jar
into GREG_HOME/repository/components/dropins
.
4. Edit the registry.xml
file which is in GREG_HOME/repository/conf
folder and add an XML snippet similar to the following. In this example, add this handler as the very first handler in the registry.xml file.
<handler class="org.wso2.carbon.registry.samples.handler.XPathValidationHandler"> <property name="xPath">//wsdl:service</property> <property name="attribute">name</property> <property name="regEx">SimpleStockQuoteService</property> <property name="namespaces">wsdl:http://schemas.xmlsoap.org/wsdl/</property> <filter class="org.wso2.carbon.registry.core.jdbc.handlers.filters.MediaTypeMatcher"> <property name="mediaType">application/wsdl+xml</property> </filter> </handler>
Note
The configuration above is used to validate a WSDL file as to whether the service defined in it has the name SimpleStockQuoteService
. All other WSDL files will fail to upload. For the validation to work before the WSDL has been added, this handler should be configured before the other handlers defined for the WSDL media type, which is why we added this handler as the very first handler in the registry.xml file.
See also Handler Sample.
5. Start the WSO2 Governance Registry. See Running the Product on Windows or Running the Product on Linux and Solaris.
XPath is a powerful language that can be used to retrieve a subset of elements from an XML document. This in combination with regular expressions can be used to validate content of XML files uploaded into the WSO2 Governance Registry