Skip to main content

Generate a file using the request payload and send to a backend via WSO2 ESB

Let's say WSO2 ESB receives an request with a payload, and we need to create a file using that payload content and send that file to a endpoint.

Here's the required proxy cofiguration.

<proxy name="testProxy" transports="https http" startOnLoad="true" trace="disable">
<description/>
<target inSequence="testProxy_in" outSequence="testProxy_out"/>
</proxy>
view raw WSO2_config_8 hosted with ❤ by GitHub
This sequence builds the message as a multipart/form-data and send it to the endpoint.

<sequence xmlns="http://ws.apache.org/ns/synapse" name="testProxy_in">
<property name="HTTP_METHOD" value="POST" scope="axis2" type="STRING"></property>
<property name="messageType" value="multipart/form-data" scope="axis2" type="STRING"></property>
<payloadFactory media-type="xml">
<format>
<text xmlns="http://ws.apache.org/commons/ns/payload">$1</text>
</format>
<args>
<arg xmlns:ns="http://org.apache.synapse/xsd" expression="$body/*" evaluator="xml"></arg>
</args>
</payloadFactory>
<log level="full" separator="testProxy In Sequence, "></log>
<send>
<endpoint key="fileUploadEndpoint"></endpoint>
</send>
</sequence>
view raw WSO2_config_9 hosted with ❤ by GitHub
If you want to customise MultipartFormDataFormatter, write your own class extending MultipartFormDataFormatter.

Configure it in WSO2_ESB/repository/conf/axis2/axis2.xml file as follows.

<messageFormatters>
<messageFormatter contentType="application/x-www-form-urlencoded"
class="org.apache.axis2.transport.http.XFormURLEncodedFormatter"/>
<messageFormatter contentType="multipart/form-data"
class="org.wso2.custom.CustomMultipartFormDataFormatter"/>
view raw WSO2_config_10 hosted with ❤ by GitHub
This org.wso2.custom.CustomMultipartFormDataFormatter sets the file name as filename.xml, Content-Type as text/xml and payload as the content of the file


package com.wso2.custom;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMOutputFormat;
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.transport.http.MultipartFormDataFormatter;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.FilePartSource;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.io.FileUtils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
public class CustomMultipartFormDataFormatter extends MultipartFormDataFormatter {
@Override
public byte[] getBytes(MessageContext messageContext, OMOutputFormat format) throws AxisFault {
OMElement omElement = messageContext.getEnvelope().getBody().getFirstElement();
Part[] parts = createMultipatFormDataRequest(omElement);
if (parts.length > 0) {
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
try {
// This is accessing a class of Commons-FlieUpload
Part.sendParts(bytesOut, parts, format.getMimeBoundary().getBytes());
} catch (IOException e) {
throw AxisFault.makeFault(e);
}
return bytesOut.toByteArray();
}
return new byte[0]; //To change body of implemented methods use File | Settings | File Templates.
}
private Part[] createMultipatFormDataRequest(OMElement dataOut) {
ArrayList parts = new ArrayList();
if (dataOut != null) {
Iterator iter1 = dataOut.getChildElements();
OMFactory omFactory = OMAbstractFactory.getOMFactory();
while (iter1.hasNext()) {
OMElement ele = (OMElement) iter1.next();
Iterator iter2 = ele.getChildElements();
// check whether the element is a complex type
if (iter2.hasNext()) {
OMElement omElement = ele.cloneOMElement();
try {
File file = new File("filename.xml");
FileUtils.writeStringToFile(file, omElement.toString());
parts.add(new FilePart("xmlfile", new FilePartSource(file), "text/xml", "UTF-8"));
} catch (IOException ex) {
}
} else {
parts.add(new StringPart(ele.getQName().getLocalPart(), ele.getText()));
}
}
}
Part[] partsArray = new Part[parts.size()];
return (Part[]) parts.toArray(partsArray);
}
}
view raw Java_code_1 hosted with ❤ by GitHub

Comments

Popular posts from this blog

How to generate random unique number in SOAP UI request

eg 1: ${=System.currentTimeMillis() + ((int)(Math.random()*10000))} eg 2: ${=java.util.UUID.randomUUID()} ${=java.util.UUID.randomUUID()} ${=System.currentTimeMillis() + ((int)(Math.random()*10000))} - See more at: http://tryitnw.blogspot.com/2014/03/generating-random-unique-number-in-soap.html#sthash.m2S4tUFu.dpuf ${=System.currentTimeMillis() + ((int)(Math.random()*10000))} - See more at: http://tryitnw.blogspot.com/2014/03/generating-random-unique-number-in-soap.html#sthash.m2S4tUFu.dpuf ${=System.currentTimeMillis() + ((int)(Math.random()*10000))} - See more at: http://tryitnw.blogspot.com/2014/03/generating-random-unique-number-in-soap.html#sthash.m2S4tUFu.dpuf

Tips on using environment variables in WSO2 Integration Cloud

Environment variables allow you to change an application's internal configuration without changing its source code. Let’s say you want to deploy the same application in development, testing  and production environments. Then database related configs and some other internal configurations may change from one environment to another. If we can define these configurations as an environment variables we can easily set those without changing the source code of that application. When you deploy your application in WSO2 Integration Cloud, it lets you define environment variables via the UI. Whenever you change the values of environment variables, you just need to redeploy the application for the changes to take effect. Predefined environment variables Key Concepts - Environment Variables   provides you some predefined set of environment variables which will be useful when deploying applications in WSO2 Integration Cloud. Sample on how to use environment variables ...

Invoking REST API using BPMN

You can use BPMN Service Task and configure it as following to invoke a REST API Example with text input and text output: Example with JSON input and JSON output mapping and registry based URL: You can find implementation of org.wso2.carbon.bpmn.extensions.rest.RESTTask class here https://github.com/wso2/carbon-business-process/blob/master/components/bpmn/org.wso2.carbon.bpmn/src/main/java/org/wso2/carbon/bpmn/extensions/rest/RESTTask.java Currently it supports GET and POST only. If you need support for other HTTP methods you have to implement custom java service task similar to REST Task implementation that supports rest of REST API functionalities.