Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Note

Content is work in progress!

 

Apart from In addition to using the default message builders and formatters that are shipped with  in WSO2 ESB, you have the flexibility to use can create your own custom message builder builders and formatterformatters

Table of Contents

Custom

...

message builder

Let's look at how to create a custom message builder using an example a sample scenario where you need to Base64 encode an XML entry field. In this sample, you retrieve the text content from the payload and then Base64 encode the text. This is then converted to SOAP, and the content is then processed in the ESB mediation flow.

  1. You will first need to write a class implementing the org.apache.axis2.builder.Builder interface in the Axis2 Kernel module

...

  1. and then override the processDocument

...

  1.  method. Within the processDocument

...

  1.  method, you can define your specific logic to process the payload content as required and

...

  1. then convert it to

...

  1. SOAP format.

...

  1. Code Block

...

 

  1. languagexml
    package org.test.builder;
    
    import org.apache.axiom.om.OMAbstractFactory;
    import org.apache.axiom.om.OMElement;
    import org.apache.axiom.om.impl.OMNodeEx;
    import org.apache.axiom.om.impl.builder.StAXBuilder;
    import org.apache.axiom.om.impl.builder.StAXOMBuilder;
    import org.apache.axiom.om.util.StAXParserConfiguration;
    import org.apache.axiom.om.util.StAXUtils;
    import org.apache.axiom.soap.SOAPBody;
    import org.apache.axiom.soap.SOAPEnvelope;
    import org.apache.axiom.soap.SOAPFactory;
    import org.apache.axis2.AxisFault;
    import org.apache.axis2.Constants;
    import org.apache.axis2.builder.Builder;
    import org.apache.axis2.context.MessageContext;
    import org.apache.commons.codec.binary.Base64;
    
    import javax.xml.stream.XMLStreamException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.PushbackInputStream;
    
    public class CustomBuilderForTextXml implements Builder{
        public OMElement processDocument(InputStream inputStream, String s, MessageContext messageContext) throws AxisFault {
            SOAPFactory soapFactory = OMAbstractFactory.getSOAP11Factory();
            SOAPEnvelope soapEnvelope = soapFactory.getDefaultEnvelope();
    
            PushbackInputStream pushbackInputStream = new PushbackInputStream(inputStream);
    
            try {
                int byteVal = pushbackInputStream.read();
                if (byteVal != -1) {
                    pushbackInputStream.unread(byteVal);
    
                    javax.xml.stream.XMLStreamReader xmlReader = StAXUtils.createXMLStreamReader(StAXParserConfiguration.SOAP,
                            pushbackInputStream, (String) messageContext.getProperty(Constants.Configuration.CHARACTER_SET_ENCODING));
    
                    StAXBuilder builder = new StAXOMBuilder(xmlReader);
                    OMNodeEx documentElement = (OMNodeEx) builder.getDocumentElement();
                    documentElement.setParent(null);
                    String elementVal = ((OMElement) documentElement).getText();
                    byte[]   bytesEncoded = Base64.encodeBase64(elementVal.getBytes());
                    ((OMElement) documentElement).setText(new String(bytesEncoded ));
                    SOAPBody body = soapEnvelope.getBody();
                    body.addChild(documentElement);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (XMLStreamException e) {
                e.printStackTrace();
            }
    
            return soapEnvelope;
        }
    }

 

...

  1. Create a JAR file of this class and add it into the classpath of the Axis2 installation, i.e., the <ESB_HOME>/repository/components/lib folder.
  2. To enable

...

  1. your custom message builder for content type text/xml, add the following line in the

...

  1. Message Buillders section in the <ESB_HOME/repository/conf/axis2/axis2.xml> file (or <ESB_HOME>/repository/conf/axis2/tenant-axis2.xml if you are working in a multi-tenant environment):
Code Block
languagexml
<messageBuilder contentType="text/xml" class="org.apachetest.axis2.transportbuilder.http.CustomBuilderForTextXml"/>

Custom

...

message formatter

Similarly, you can write your own Message Formatters as well, in order message formatter to manipulate the outgoing payload from the ESB.

When creating a custom Message Formattermessage formatter, you will need to create a class implementing the org.apache.axis2.transport.MessageFormatter Interface interface and then override the writeTo method. You can implement your logic within the writeTo method.

Ensure to add Add the following line in the Message Formatters section in the <ESB_HOME/repository/conf/axis2/axis2.xml> file (or <ESB_HOME>/repository/conf/axis2/tenant-axis2.xml if you are working in a multi-tenant environment):

Code Block
languagexml
<messageFormatter contentType= "text/xml" class= "org.apache.axis2.transport.http.SOAPMessageFormatter" /> 

...

Note

The class name used in the above line should be the name used for the class when writing the formatter.