Note |
---|
Content is work in progress! |
...
In the below sample code snippet, you retrieve the text content from the payload and then Base64 encode the text. This is then converted to SOAP and then the content is processed in the ESB mediation flow.
Code Block |
---|
|
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
public OMElement processDocument(InputStream inputStream, String s, MessageContext messageContext) throws AxisFault |
{
SOAPFactory soapFactory = {
SOAPFactory soapFactory = OMAbstractFactory.getSOAP11Factory(); |
SOAPEnvelope soapEnvelope =
SOAPEnvelope soapEnvelope = soapFactory.getDefaultEnvelope(); |
PushbackInputStream pushbackInputStream = new
PushbackInputStream pushbackInputStream = new PushbackInputStream(inputStream); |
try {
int byteVal =
try {
int byteVal = pushbackInputStream.read(); |
if {
pushbackInputStream {
pushbackInputStream.unread(byteVal); |
javax
javax.xml.stream.XMLStreamReader xmlReader = StAXUtils.createXMLStreamReader(StAXParserConfiguration.SOAP, |
pushbackInputStream,
pushbackInputStream, (String) messageContext.getProperty(Constants.Configuration.CHARACTER_SET_ENCODING)); |
StAXBuilder builder = new
StAXBuilder builder = new StAXOMBuilder(xmlReader); |
OMNodeEx documentElement =
OMNodeEx documentElement = (OMNodeEx) builder.getDocumentElement(); |
documentElement
documentElement.setParent(null); |
String elementVal =
String elementVal = ((OMElement) documentElement).getText(); |
byte[] bytesEncoded =
byte[] bytesEncoded = Base64.encodeBase64(elementVal.getBytes()); |
((OMElement) documentElement).setText(new String(bytesEncoded )); |
SOAPBody body =
SOAPBody body = soapEnvelope.getBody(); |
body
body.addChild(documentElement); |
}
} catch (IOException e) {
e
}
} catch (IOException e) {
e.printStackTrace(); |
} catch
} catch (XMLStreamException e) |
{
e
}
return soapEnvelope;
}
}
return soapEnvelope;
}
} |
Now you need to make a jar out of this class and add it into the classpath of Axis2 installation i.e. <ESB_HOME>/repository/components/lib folder.
...
<messageBuilder contentType="text/xml" class="CustomBuilderForTextXml"/>
Custom Formatters
Similarly you can write your own Message Formatters as well, in order to manipulate the outgoing payload from ESB.
When creating a custom Message Formatter, you will need to create a class implementing org.apache.axis2.transport.MessageFormatter Interface and then override the writeTo method
Ensure to add the following line in Message Formatters section in <ESB_HOME/repository/conf/axis2/axis2.xml> file:
<messageFormatter contentType="text/xml" class="org.apache.axis2.transport.http.SOAPMessageFormatter"/>