org.osesb.msgrouting
Class DefaultXMLMessageTypeHelper

java.lang.Object
  extended by org.osesb.msgrouting.AbstractXMLMessageTypeHelper
      extended by org.osesb.msgrouting.DefaultXMLMessageTypeHelper
Direct Known Subclasses:
CustomTestXMLMessageTypeHelper_Test2, DroolsXMLMessageTypeHelper

public class DefaultXMLMessageTypeHelper
extends AbstractXMLMessageTypeHelper

The default implementation of XMLMessageTypeHelper, which applies a List of XPath expressions to a XML message in order to extract the message type.

A messageType is typically represented as a QName where the namespaceURI is extracted from the XML message and where the localPart is determined by the message content either directly (see DefaultXMLMessageTypeHelper) or indirectly (see DroolsXMLMessageTypeHelper).

Transport handlers and ClientGateway(s) use a MessageRouter to route messages to a org.osesb.messagepipeline.MessagePipeline for processing. The MessageRouter selects the MessagePipeline based on the message's messageType. The MessageRouter uses the <messagetype-to-messagepipeline-map> configuration element of a MessageRouterConfiguration to map message types to MessagePipelines.

See the org.osesb.messagepipeline package. See org.osesb.msgrouting.ClientGateway. See the org.osesb.transporthandlers package in the transport-handlers module.

There are two ways that the message type is determined:

  1. Transport handlers and ClientGateway(s) can be configured with a static messageType. In this case all messages arriving at a transport handler or ClientGateway will be assigned the same message type .
  2. If a transport handler or ClientGateway is not configured with a static message type, then the configured MessageRouter uses a XMLMessageTypeHelper to extract a message type from the XML message.

DefaultXMLMessageTypeHelper is the default XMLMessageTypeHelper class and is constructed with a XMLMessageTypeXPathExpressionListConfiguration. DefaultXMLMessageTypeHelper applies the configured XPath expressions to a XML message until a messageType is determined. DefaultXMLMessageTypeHelper#getMessagetype() returns the messageType as a QName, where the namespaceURI is the XML document namespace (if any) and the localPart is the value for the element or attribute that is given by a XPath expression.

XPath expressions can be namespace-qualified or not. DefaultXMLMessageTypeHelper uses org.osesb.utilities.xml.DocumentNamespaceHelper to extract namespaces from the root element of the incoming XML message. The following namespaces are considered, in order, when searching the <xmlmessage-messagetype-xpathexpression-list> configuration element for a namespace-qualified XPath expression:

  1. Namespaces declared using the 'xmlns' attribute of the root element.
  2. The 'targetNamespace' attribute of the root element.
  3. The namespaceURI portion of the 'schemaLocation' attribute of the root element.

If the incoming XML message is associated with a namespace, then DefaultXMLMessageTypeHelper applies all matching namespace-qualified XPath expressions in the order that they are configured by XMLMessageTypeXPathExpressionListConfiguration until a message type is extracted from the XML message. If the namespace-qualified XPath expressions do not yield a message type, DefaultXMLMessageTypeHelper applies all unqualified XPath expressions in the order that they are configured until a message type is extracted from the XML message.

This Class is parser pool-aware and uses SAX to extract the message type. A org.osesb.utilities.xml.SAXXPathElementHandler is used as the SAX handler that stops processing when it finds the target element based on a XPath expression, which in this case is a XPath expression for the message type.

getMessageType() returns the message type for the input XML based on the following ordered lookup:

  1. If the xpathExpression resolves to an Attribute and the Attribute has a value, then the attribute's value is returned as the message type.
  2. If the xpathExpression resolves to an Element and the element has a value, then the element's value is returned as the message type.
  3. If the xpathExpression resolves to an Element and the element DOES NOT have a value (e.g. the root element), then the element's name is returned as the message type.

To configure a MessageRouter with a different XMLMessageTypeHelper, create a class that extends AbstractXMLMessageTypeHelper and use the <xmlMessagetypeHelperConfiguration> content model element of the MessageRouterConfiguration.


Field Summary
protected  DocumentNamespaceHelper documentNamespaceHelper
          DocumentNamespaceHelper extracts namespaces from the target XML.
protected static org.apache.commons.logging.Log log
          The logging implementation for this class.
protected  SAXXPathElementHandler saxXPathElementHandler
          SAX handler that stops processing when it finds the first target element based on a XPath expression.
protected  java.lang.String thisClassName
          The full name of this class.
protected  XMLMessageTypeXPathExpressionListConfiguration xmlMessageTypeXPathExpressionListConfiguration
          The configuration of XPath expressions that evaluate to a message type.
 
Fields inherited from class org.osesb.msgrouting.AbstractXMLMessageTypeHelper
debugFlag
 
Constructor Summary
DefaultXMLMessageTypeHelper(XMLMessageTypeXPathExpressionListConfiguration xmlMessageTypeXPathExpressionListConfiguration)
          Creates a new instance of DefaultXMLMessageTypeHelper.
 
Method Summary
 javax.xml.namespace.QName getMessageType(java.lang.String xmlText)
          Get a message type from a XML message using the following ordered lookup:

Apply namespace-qualified XPath expressions until a messageType is found. Apply uqualified XPath expressions until a messageType is found.

protected  javax.xml.namespace.QName getNamespaceQualifiedMessageType(java.lang.String xmlText)
          Extract the message type from a XML message using namespace-qualified xpathExpressions.
protected  javax.xml.namespace.QName getUnqualifiedMessageType(java.lang.String xmlText)
          Extract the message type from a XML message using unqualified namespace message type locations.
static void main(java.lang.String[] args)
          Main method for testing.
protected  javax.xml.namespace.QName parseForMessageType(java.lang.String xmlText, java.lang.String namespaceURI, java.lang.String xpathExpression)
          Invoked by getNamespaceQualifiedMessageType() and getUnqualifiedMessageType() to extract the message type from xmlText using a XPath expression.
 void setDebugFlag(boolean debugFlag)
          Set the debugFlag for this object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected static org.apache.commons.logging.Log log
The logging implementation for this class.


thisClassName

protected java.lang.String thisClassName
The full name of this class.


xmlMessageTypeXPathExpressionListConfiguration

protected XMLMessageTypeXPathExpressionListConfiguration xmlMessageTypeXPathExpressionListConfiguration
The configuration of XPath expressions that evaluate to a message type.


documentNamespaceHelper

protected DocumentNamespaceHelper documentNamespaceHelper
DocumentNamespaceHelper extracts namespaces from the target XML.


saxXPathElementHandler

protected SAXXPathElementHandler saxXPathElementHandler
SAX handler that stops processing when it finds the first target element based on a XPath expression. In this case the XPath expressions extract the message type from the target XML. The XPath expressions are configured by xmlMessageTypeXPathExpressionListConfiguration.

Constructor Detail

DefaultXMLMessageTypeHelper

public DefaultXMLMessageTypeHelper(XMLMessageTypeXPathExpressionListConfiguration xmlMessageTypeXPathExpressionListConfiguration)
                            throws java.lang.Exception
Creates a new instance of DefaultXMLMessageTypeHelper.

Parameters:
xmlMessageTypeXPathExpressionListConfiguration - that configures message type locations in the population of XML messages as xpathExpressions.
Throws:
java.lang.Exception - if constructed with a null xmlMessageTypeXPathExpressionListConfiguration.
Method Detail

getMessageType

public javax.xml.namespace.QName getMessageType(java.lang.String xmlText)
                                         throws java.lang.Exception,
                                                MessageTypeNotFoundException
Get a message type from a XML message using the following ordered lookup:

  1. Apply namespace-qualified XPath expressions until a messageType is found.
  2. Apply uqualified XPath expressions until a messageType is found.

Specified by:
getMessageType in class AbstractXMLMessageTypeHelper
Parameters:
xmlText - The XML text to extract the message type from.
Returns:
A QName that represents the message type.
Throws:
java.lang.Exception - if there was a general error extracting the message type.
MessageTypeNotFoundException - if the message type was not found.

getNamespaceQualifiedMessageType

protected javax.xml.namespace.QName getNamespaceQualifiedMessageType(java.lang.String xmlText)
                                                              throws java.lang.Exception
Extract the message type from a XML message using namespace-qualified xpathExpressions. Namespace-qualified message type locations are configured by xmlMessageTypeXPathExpressionListConfiguration as xpathExpressions associated with the namespace of the target XML. This is the fastest way to extract a message type from a XML message.

The following namespaces are considered, in order, when searching the <xmlmessage-messagetype-xpathexpression-list> configuration element for a namespace-qualified XPath expression:

  1. Namespaces declared using the 'xmlns' attribute of the root element.
  2. The 'targetNamespace' attribute of the root element.
  3. The namespaceURI portion of the 'schemaLocation' attribute of the root element.

getNamespaceQualifiedMessageType() returns the message type for the input XML based on the following ordered lookup:

  1. If the xpathXpression resolves to an Attribute and the Attribute has a value, then the attribute's value is returned as local part of the message type.
  2. If the xpathXpression resolves to an Element and the element has a value, then the element's value is returned as the local part of the message type.
  3. If the xpathXpression resolves to an Element and the element DOES NOT have a value (e.g. the root element), then the element's name is returned as the local part of the message type.
In all cases, the message type QName is returned with the configured namespace as the namespaceURI for the message type.

Parameters:
xmlText - The XML text to extract the message type from.
Returns:
A QName that represents a namespace qualified message type.
Throws:
java.lang.Exception - if an error is encountered while searching the XML for a namespace-qualified message type.

getUnqualifiedMessageType

protected javax.xml.namespace.QName getUnqualifiedMessageType(java.lang.String xmlText)
                                                       throws java.lang.Exception
Extract the message type from a XML message using unqualified namespace message type locations. Unqualified message type locations are configured by xmlMessageTypeXPathExpressionListConfiguration as xpathExpressions that are not associated with any namespace. Each unqualified xpathExpression is applied to the XML until a message type is found.

getUnqualifiedMessageType() returns the message type for the input XML based on the following ordered lookup:

  1. If the xpathXpression resolves to an Attribute and the Attribute has a value, then the attribute's value is returned as local part of the message type.
  2. If the xpathXpression resolves to an Element and the element has a value, then the element's value is returned as the local part of the message type.
  3. If the xpathXpression resolves to an Element and the element DOES NOT have a value (e.g. the root element), then the element's name is returned as the local part of the message type.

Parameters:
xmlText - The XML text to extract the message type from.
Returns:
A QName that represents a namespace qualified message type.
Throws:
java.lang.Exception - if an error is encountered while searching the XML for a unqualified message type.

parseForMessageType

protected javax.xml.namespace.QName parseForMessageType(java.lang.String xmlText,
                                                        java.lang.String namespaceURI,
                                                        java.lang.String xpathExpression)
                                                 throws java.lang.Exception
Invoked by getNamespaceQualifiedMessageType() and getUnqualifiedMessageType() to extract the message type from xmlText using a XPath expression.

This method is parser pool-aware and uses SAX to extract the message type. A org.osesb.utilities.xml.SAXXPathElementHandler is used as the SAX handler that stops processing when it finds the target element based on a XPath expression, which in this case is a XPath expression for the message type.

parseForMessageType() returns the message type for the input XML based on the following ordered lookup:

  1. If the xpathXpression resolves to an Attribute and the Attribute has a value, then the attribute's value is returned as local part of the message type.
  2. If the xpathXpression resolves to an Element and the element has a value, then the element's value is returned as the local part of the message type.
  3. If the xpathXpression resolves to an Element and the element DOES NOT have a value (e.g. the root element), then the element's name is returned as the local part of the message type.

In all cases, the message type QName is returned with the given namespaceURI as the namespaceURI for the message type.

Parameters:
xmlText - The XML text to extract the message type from.
namespaceURI - The URI for the namespace that the message type belongs to.
xpathExpression - The xpathExpression that resolves to the element or attribute that defines the message type.
Returns:
A QName that represents a message type.
Throws:
java.lang.Exception

setDebugFlag

public void setDebugFlag(boolean debugFlag)
Set the debugFlag for this object.

Overrides:
setDebugFlag in class AbstractXMLMessageTypeHelper
Parameters:
debugFlag - If true, debug messages are logged.

main

public static void main(java.lang.String[] args)
Main method for testing.

Parameters:
args - The test data directory.