Receiving AS2 messages from a trading partner

AS2 (Applicability Statement 2) is a specification [RFC4130] about how to transport data securely and reliably over the Internet. Security is achieved by using digital certificates and encryption. This articles explains a sample configuration that demonstrates some of the AS2 support built-in with the UltraESB, specifically the AS2 receive support and the integration of the Smooks framework to transform the received EDI message into XML.

Receiving AS2 messages from a trading partner

This sample is written against the example configuration # 351, which shows how the AS2 support of the UltraESB can be used to configure message receipt from trading partners using AS2. The UltraESB server is configured using the AS2 identifier 'AdroitLogicAS2', and thus the corresponding configuration as per the 'Setting up the sample AS2 trading partner' article is assumed to be completed for the remote trading partner. The salient points of the sample configuration are discussed below.

<!--Handles all incoming AS2 messages and sends sync MDNs or the transport closure. Also acts as the service to receive async MDNs-->
<u:proxy id="AS2Receiver">
<u:transport id="http-8280"/>
<u:target>
<u:inSequence>
<u:java import="org.adroitlogic.as2.manager.*; org.milyn.*; org.milyn.container.*;
javax.xml.transform.dom.DOMResult; javax.xml.transform.stream.StreamSource;
org.w3c.dom.*;"><![CDATA[
AS2Manager as2Manager = (AS2Manager) mediation.getSpringBean("as2Manager");
as2Manager.processIncomingAS2Message(msg);

Smooks smooks = new Smooks("samples/resources/smooks-config.xml");
try {
ExecutionContext executionContext = smooks.createExecutionContext();
DOMResult domResult = new DOMResult();

// Filter the input message to the outputWriter, using the execution context...
smooks.filterSource(executionContext,
new StreamSource(msg.getCurrentPayload().getInputStream()), domResult);

Node node = domResult.getNode();
if (node instanceof Document) {
msg.setCurrentPayload(new DOMMessage((Document) node));
} else {
msg.setCurrentPayload(new StringMessage(node.getTextContent()));
}
} finally {
smooks.close();
}
]]></u:java>
</u:inSequence>
<u:inDestination>
<u:address>file:///tmp/AS2/received</u:address>
<u:property name="fileName" value="received.xml"/>
<u:property name="fileTimestampFormat" value="yyyy_MM_dd_'T'HH_mm_ss.SSSSZ"/>
</u:inDestination>
</u:target>
</u:proxy>

The UltraESB can be configured to offer any number of AS2 service endpoints with varying levels of security etc for different partners. Please refer to the HTTP/S configuration options for proxy services for more information. An AS2 proxy service only needs to call into the AS2Manager via the following call to process an AS2 message

                    as2Manager.processIncomingAS2Message(msg);

If you wish to simply save the received message into a file, after AS2 level processing, you may use the mediation.readPayloadToFile() convenience method, the File transport with an endpoint or any other Java method as suitable. In the above example, we pass the received EDI message for an EDI-to-XML transformation using the Smooks framework. The Smooks framework example can be found here with descriptions specific to the use of the Smooks framework. Note that Smooks offers many more options for processing both XML and non-XML data such as CSV, EDI etc.

To run the sample, first specify the UltraESB AS2 endpoint URL "http://localhost:8280/service/AS2Receiver" to the example AS2 server as shown below.

As the configuration # 351 uses the Smooks framework, the following dependency JAR files must be placed into the UltraESB classpath - to either the 'lib/optional' or 'lib/patches' subdirectories. These files can be found with the installation bundle of Smooks

asankha@asankha:~/java/ultraesb-1.0-beta-1/lib/optional$ ls -l
total 1048
-rw-r--r-- 1 asankha asankha 120328 2010-01-12 02:15 milyn-commons-1.2.4.jar
-rw-r--r-- 1 asankha asankha 359414 2010-01-12 02:22 milyn-edisax-parser-1.2.4.jar
-rw-r--r-- 1 asankha asankha 563528 2010-01-12 02:14 milyn-smooks-core-1.2.4.jar
-rw-r--r-- 1 asankha asankha 10114 2010-01-12 02:13 milyn-smooks-edi-1.2.4.jar

Now, start the UltraESB sample configuration # 351 as follows:

asankha@asankha:~/java/ultraesb-1.0-beta-1/bin$ ./ultraesb.sh -sample 351

From the Mendelson Open Source AS2 console, select "File"->"Send file to partner" and select the "samples/resources/input-message.edi" file as the input. The file will be immidiately received and processed by the UltraESB proxy, and the result can be seen as follows:

asankha@asankha:~/java/ultraesb-1.0-beta-1/samples/resources$ find /tmp/AS2/
/tmp/AS2/
/tmp/AS2/tosend
/tmp/AS2/sent
/tmp/AS2/received
/tmp/AS2/received/2010_01_12_T02_23_00.0538+0530_received.xml

The saved XML file will be the result of the EDI-to-XML transformation through Smooks and will be as follows:

<?xml version="1.0" encoding="UTF-8"?>
<Order>
<header>
<order-id>1</order-id>
<status-code>0</status-code>
<net-amount>59.97</net-amount>
<total-amount>64.92</total-amount>
<tax>4.95</tax>
<date>Wed Nov 15 13:45:28 EST 2006</date>
</header>
<customer-details>
<username>user1</username>
<name>
<firstname>Harry</firstname>
<lastname>Fletcher</lastname>
</name>
<state>SD</state>
</customer-details>
<order-item>
<position>1</position>
<quantity>1</quantity>
<product-id>364</product-id>
<title>The 40-Year-Old Virgin</title>
<price>29.98</price>
</order-item>
<order-item>
<position>2</position>
<quantity>1</quantity>
<product-id>299</product-id>
<title>Pulp Fiction</title>
<price>29.99</price>
</order-item>
</Order>