Class IfPipe

All Implemented Interfaces:
HasTransactionAttribute, IConfigurable, IConfigurationAware, IForwardTarget, INamedObject, IPipe, IScopeProvider, IWithParameters, EventThrowing, HasStatistics, org.springframework.beans.factory.Aware, org.springframework.context.ApplicationContextAware

@Forward(name="*",description="when {@literal thenForwardName} or {@literal elseForwardName} are used") @Forward(name="then",description="the configured condition is met") @Forward(name="else",description="the configured condition is not met") @EnterpriseIntegrationPattern(ROUTER) public class IfPipe extends AbstractPipe
Selects a forward based on an expression. The expression type is coupled to the mediaType:
  • XML (application/xml) uses Xpath
  • JSON (application/json) uses jsonPath
The XML mediaType is the default type, if you want to use json, you need to set this using 'mimeType' in the Message.

Expressions

Expressions are used to select nodes in the given input document. Imagine a collection of books:

 {
   "store": {
     "book": [
       {
         "category": "reference",
         "author": "Nigel Rees",
         "title": "Sayings of the Century",
         "price": 8.95
       },
       {
         "category": "fiction",
         "author": "Evelyn Waugh",
         "title": "Sword of Honour",
         "price": 12.99
       },
       {
         "category": "fiction",
         "author": "Herman Melville",
         "title": "Moby Dick",
         "isbn": "0-553-21311-3",
         "price": 8.99
       },
       {
         "category": "fiction",
         "author": "J. R. R. Tolkien",
         "title": "The Lord of the Rings",
         "isbn": "0-395-19395-8",
         "price": 22.99
       }
     ]
   }
 }
 

With both expression languages you'll be able to select one or multiple nodes from this collection.
Using this pipe there are two options. Use it only with an expression or combine it with an expressionValue. When using the expression, the pipe evaluates to thenForwardName when there is a match, even it is empty. In the given example, this might be one of:


   $.store
   $.store.book[1]
   $.store.book[?(@.price == 22.99)].author
   $.store.book[?(@.category == 'fiction')]
 

expressionValue

When using expression combined with expressionValue, the pipe evaluates to thenForwardName when the the matched value is equal to expressionValue. This needs to be an exact match.

XML/XPATH

Xpath has been around a long time, information about the syntax can be found everywhere on the internet. The XML implementation wraps the Xpath expression in an XSL. This enables us to use complex expressions which evaluate to true or false instead of being used only as a selector of nodes in the input XML. This is available to be backwards compatible with the XmlIf pipe. For instance, take the following example input:

   <results>
     <result name="test"></result>
     <result name="test"></result>
   </results>
 
Examples with complex expressions might be something like: number(count(/results/result[contains(@name , 'test')])) > 1, to test if there's more than one node found containing the string 'test'. Please check if a simpler, less error-prone expression like /results/result[contains(@name, 'test')] can suffice.

Without expression

Without an expression, the default behaviour is to assume the input is a string, the code will try to match the string to an optional regular expression or tries to match the string value to the optional expressionValue.

Resources

See Also:
  • Constructor Details

    • IfPipe

      public IfPipe()
  • Method Details

    • configure

      public void configure() throws ConfigurationException
      Description copied from class: AbstractPipe
      configure() is called after the Pipeline is registered at the Adapter. Purpose of this method is to reduce creating connections to databases etc. in the doPipe() method. As much as possible class-instantiating should take place in the configure() method, to improve performance.
      Specified by:
      configure in interface IConfigurable
      Specified by:
      configure in interface IPipe
      Overrides:
      configure in class AbstractPipe
      Throws:
      ConfigurationException
    • doPipe

      public PipeRunResult doPipe(Message message, PipeLineSession session) throws PipeRunException
      Description copied from interface: IPipe
      This is where the action takes place. Pipes may only throw a PipeRunException, to be handled by the caller of this object. Implementations must either consume the message, or pass it on to the next Pipe in the PipeRunResult. If the result of the Pipe does not depend on the input, like for the FixedResultPipe, the Pipe can schedule the input to be closed at session exit, by calling Message.closeOnCloseOf(PipeLineSession, String) This allows the previous Pipe to release any resources (e.g. connections) that it might have kept open until the message was consumed. Doing so avoids connections leaking from pools, while it enables efficient streaming processing of data while it is being read from a stream.
      Throws:
      PipeRunException
    • setThenForwardName

      @Deprecated(forRemoval=true, since="9.0") @ConfigurationWarning("Use the \'then\' forward in your configuration") public void setThenForwardName(String thenForwardName)
      Deprecated, for removal: This API element is subject to removal in a future version.
      forward returned when output is true
      Default value
      then
    • setElseForwardName

      @Deprecated(forRemoval=true, since="9.0") @ConfigurationWarning("Use the \'else\' forward in your configuration") public void setElseForwardName(String elseForwardName)
      Deprecated, for removal: This API element is subject to removal in a future version.
      forward returned when output is false
      Default value
      else
    • setExpressionValue

      public void setExpressionValue(String expressionValue)
      a string to compare the result of the xpathExpression (or the input-message itself) to. If not specified, a non-empty result leads to the 'then'-forward, an empty result to 'else'-forward
    • setXpathExpression

      public void setXpathExpression(String string)
      xpath expression to be applied to the input-message. if not set, no transformation is done when the input message is mediatype XML
    • setJsonPathExpression

      public void setJsonPathExpression(String jsonPathExpression)
      jsonPath expression to be applied to the input-message. if not set, no transformation is done when the input message is mediatype JSON
    • setRegex

      @Deprecated(forRemoval=true, since="9.0") @ConfigurationWarning("Use RegExPipe instead") public void setRegex(String regex)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Regular expression to be applied to the input-message (ignored if either xpathExpression or jsonPathExpression is specified). The input-message fully matching the given regular expression leads to the 'then'-forward
    • setXsltVersion

      public void setXsltVersion(int xsltVersion)
      If set to 2 or 3 a Saxon (net.sf.saxon) xslt processor 2.0 or 3.0 respectively will be used, otherwise xslt processor 1.0 (org.apache.xalan)
      Default value
      2
    • setNamespaceDefs

      public void setNamespaceDefs(String namespaceDefinitions)
      namespace definitions for xpathExpression. Must be in the form of a comma or space separated list of prefix=namespaceuri definitions.
    • setNamespaceAware

      public void setNamespaceAware(boolean namespaceAware)
      controls namespace-awareness of XSLT transformation
      Default value
      true
    • setDefaultMediaType

      public void setDefaultMediaType(IfPipe.SupportedMediaType defaultMediaType)
      Parameters:
      defaultMediaType - the default media type to use when the media type of the message could not be determined.
      Default value
      DefaultMediaType.XML