Smooks Mediator

Smooks is a powerful framework for processing, manipulating and transforming XML. More information about Smooks can be obtained from the official Smooks website. Smooks mediator can be used to execute following scenarios.

  1. Lightweight transformations on messages in an efficient manner
  2. Process huge messages
  3. Apply scripting on messages
  4. Output a java bean result from messages
  5. Routing messages to endpoints (files, databases)
  6. Entity Persistence Framework Support (JPA)

Configurations

The Smooks mediator requires the transformation definition to be passed in as an external resource. This transformation can be specified as a local entry or be stored in the registry. The mediator UI enables you to point the mediator to an existing local entry or a registry resource.

  • Smooks configuartion : Smooks mediator requires the transformation definition to be passed in as an external resource.
  • Input : User can define an 'input' as a 'TEXT' or 'XML' and can provide a 'xpath' expression to pick exact message block. If not 'xpath' is not provided, by default whole 'message body' will be selected.
  • Output : User can define how his output should be either as 'TEXT' or 'XML' or 'JAVA' . User can define 'Property' attribute for the output where output will be saved in the messagecontext for the future uses with the given Property name.

Syntax

		<smooks [config-key="string"] [persistence-unit="string"]>
			<input [type="|text|xml"]  [regex="regex"]/>
			<output [type="|text|xml|java"]  [regex="regex"] [property="string"] [action="string"]/>
		</smooks>
	

UI Configuration

Smooks Mediator

Smooks Mediator Configuartion

Configurations

  • Configuration Key:
  • Smooks transformation definition can be specified as a local entry or be stored in the registry.

  • persistence-unit:
  • User can define the persistence unit name as defined in persistence.xml, when Smooks transformation is using JPA.

  • Input :
  • User can define an 'input' as a 'Text' or 'XML' and can provide a 'xpath' expression to pick exact message block. If not 'xpath' is not provided, by default whole 'message body' will be selected.

  • Output :
  • User can define how his output should be either as 'Text' or 'XML' or 'JAVA' .
    When user defines an 'output' expression there are additional operations can be performed.

    • Add : Selected node will be added as a child to the message.
    • Replace : Selected node will be replaced in the message.
    • Sibling : Selected node will be added as a sibling.

    When user defines an 'output' as a 'Property', property will be saved in the messagecontext for the future uses.

Examples

1. Smooks Mediator to output a java bean result from messages

Input message can be,

<order>
		  <order-items>
		    <order-item>
		       <product>111</product>
		       <quantity>2</quantity>
		       <price>8.90</price>
		    </order-item>
		  </order-items>
		</order>
	

ESB Smooks Mediator Configuration is as follows.

<smooks config-key="smooks-key">
                    <input type="xml"/>
                    <output type="java" property="order"/>
                </smooks>
	

The Smooks transformation definition guide for xml to java can be found from here.

As the above configuration the input message will be transformed to java object 'order'. It will be saved in the messagecontext. Any mediator followed by the smooks mediator can retrieve that java object, and use its values inside the mediator.

Ex :

JavaResult result = (JavaResult)synCtx.getProperty("order");
Order order = (Order)result.getBean("order");
order.getOrderItems();

2. Smooks Mediator to use Entity Persistence Framework Support (JPA)

Input message can be,

<order>
	    <ordernumber>1</ordernumber>
	    <customer>123456</customer>
	    <order-items>
		<order-item>
		    <product>11</product>
		    <quantity>2</quantity>
		</order-item>
		<order-item>
		    <product>22</product>
		    <quantity>7</quantity>
		</order-item>
	    </order-items>
	</order>

ESB Smooks Mediator Configuration is as follows. The Smooks transformation definition guide for Entity Persistence Framework (JPA) support can be found from here.

<smooks config-key="smooks-key" persistence-unit="db">
		<input type="xml" />
		<output type="xml"/>
	</smooks> 
	

User need to write the persistence.xml with the database connection properties. The name of persistence-unit definition inside persistence.xml should come to above smooks configuration.

With the JPA support Smooks will populate beans from the data in the database according to the input message. Also from the input message Smmoks can persist beans to the database as well.

Ex : For the order-items Smooks queries the database and populate product bean according to given product ids (11 and 22). Also order bean is populated with the given values in the input message and the previously populated product bean. Then order bean will be inserted to the database.