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.
This sequence builds the message as a multipart/form-data and send it to the endpoint.
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.
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
Here's the required proxy cofiguration.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<proxy name="testProxy" transports="https http" startOnLoad="true" trace="disable"> | |
<description/> | |
<target inSequence="testProxy_in" outSequence="testProxy_out"/> | |
</proxy> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
Configure it in WSO2_ESB/repository/conf/axis2/axis2.xml file as follows.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Comments