cancel
Showing results for 
Search instead for 
Did you mean: 

Java code to add message to existing payload

Former Member
0 Kudos

Hello experts,

<br><br>

I have build an J2ee adapter module. To help me build this I let inspire throw document:

<br><br>

href="https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/docs/library/uuid/02706f11-0d01-0010-e5ae-ac25e74c4c81">How

to create modules for the j2ee adapter <span class=SpellE>engine.pdf</span>

<br><br>

The document describes an example of building an adapter module that creates a new payload in XI and send it with the mailadapter to an e-mailadres.

<br><br>

I would like to create an adapter module that adds a new element to an existing payload.

<br><br>

for example:

<br><br>

I have a data structure like:

<br><br>

<mt_example>

<Element1></Element1>

<Element2></Element2>

<Element3></Element3>

</mt_example>

<br><br>

XI processes the file. Now the adapter module has to add the element <signature>Luc Hermans</signature> to the XI Payload so the XML file finally looks like

<br><br>

<mt_example>

<Element1></Element1>

<Element2></Element2>

<Element3></Element3>

<signature>Luc Hermans</signature>

</mt_example>

<br><br>

I know how to build the J2EE adapter, but I don’t know the code to realise this. Anyone an idea? It had to be something like under need but this only creates an new message with the value Luc Hermans into the payload.

<br><br>

package sample;<br>

import javax.ejb.CreateException;<br>

import javax.ejb.SessionBean;<br>

import javax.ejb.SessionContext;<br>

import com.sap.aii.af.mp.module.*;<br>

import com.sap.aii.af.ra.ms.api.*;<br>

/**<br>

  • @ejbHome <{com.sap.aii.af.mp.module.ModuleHome}><br>

  • @ejbLocal <{com.sap.aii.af.mp.module.ModuleLocal}><br>

  • @ejbLocalHome <{com.sap.aii.af.mp.module.ModuleLocalHome}><br>

  • @ejbRemote <{com.sap.aii.af.mp.module.ModuleRemote}><br>

  • @stateless<br>

*/<br>

public class CreateAttachment implements SessionBean, Module{<br>

private SessionContext myContext;<br>

public void ejbRemove() {<br>

}<br>

public void ejbActivate() {<br>

}<br>

public void ejbPassivate() {<br>

}<br>

public void setSessionContext(SessionContext context) {<br>

myContext = context;<br>

}<br>

public void ejbCreate() throws CreateException {<br>

}<br>

public ModuleData process(ModuleContext moduleContext,<br>

ModuleData inputModuleData)<br>

throws ModuleException {<br>

<br><br>

// create a second attachment for the receiver mail adapter<br>

try {<br>

// get the XI message from the environment<br>

Message msg = (Message)<br>

inputModuleData.getPrincipalData();<br>

// create a new payload<br>

TextPayload attachment = msg.createTextPayload();<br>

// provide attributes and content for the new payload<br>

attachment.setName("Attachment");<br>

attachment.setContentType("text/plain");<br>

attachment.setText("Luc Hermans");<br>

// add the new payload as attachment to the message<br>

msg.addAttachment(attachment);<br>

// provide the XI message for returning<br>

inputModuleData.setPrincipalData(msg);<br>

} catch (Exception e) {<br>

// raise exception, when an error occurred<br>

ModuleException me = new ModuleException(e);<br>

throw me;<br>

}<br>

// return XI message<br>

return inputModuleData;<br>

}<br>

<br><br>

I don't know if it's possible to write an User Defiend function that can add the message from the payload and put it into the xml by an message mapping?<br><br>

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

try this...

XMLPayload xmlPay = message.getDocument();
String xmlText = xmlPay.getText();
StringBuffer rootCloseExcluded =
				new StringBuffer(
					xmlText.substring(0, xmlText.lastIndexOf("</")));
String rootClose = xmlText.substring(xmlText.lastIndexOf("</"));

rootCloseExcluded.append("<signature>Luc Hermans</signature>");
String resultPayloadText = rootCloseExcluded.append(rootClose).toString();
xmlPay.setText(resultPayloadText);
message.setDocument(xmlPay);
moduleData.setPrincipalData(message);

Answers (1)

Answers (1)

Former Member
0 Kudos

The String manipulation trick with the call to String.lastIndexOf("</") is a quick (and IMHO the most sensible) solution in this particular case, but in general, whenever you want to manipulate XML, then you should consider using XSLT for this; you are several orders of magnitude more flexible that way!

In your case, the following stylesheet would do the job:


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:output method="xml"/>

  <xsl:template match="*">
    <xsl:copy-of select="."/>
  </xsl:template>

  <xsl:template match="/*">
    <xsl:copy>
      <xsl:apply-templates/>
      <signature>Luc Hermans</signature>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

A small sample Java file could make use of this in the following way:


import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class TransformerTest {

  public static void main(String[] args) throws TransformerException {

      String xml =
              "<mt_example><Element1></Element1><Element2></Element2><Element3></Element3></mt_example>";
      String xsl =
              "<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">n" +
              "  <xsl:output method="xml"/>n" +
              "  <xsl:template match="*">n" +
              "    <xsl:copy-of select="."/>n" +
              "  </xsl:template>n" +
              "  <xsl:template match="/*">n" +
              "    <xsl:copy>n" +
              "      <xsl:apply-templates/>n" +
              "      <signature>Luc Hermans</signature>n" +
              "    </xsl:copy>n" +
              "  </xsl:template>n" +
              "</xsl:stylesheet>";

      Source xmlSource = new StreamSource(new StringReader(xml));
      Source xslSource = new StreamSource(new StringReader(xsl));

      StringWriter out = new StringWriter();
      Result result = new StreamResult(out);

      TransformerFactory.newInstance().newTransformer(xslSource).transform(xmlSource, result);
      System.out.println(out.getBuffer().toString());

  }

}

Regards,

Jens

Former Member
0 Kudos

Thanx guys!

I guess I have two very good answers to start solving my issue. I will try them both.