use of org.apache.axiom.attachments.ByteArrayDataSource in project webservices-axiom by apache.
the class MIMEOutputUtils method complete.
/**
* @deprecated Use {@link OMMultipartWriter} instead.
*/
public static void complete(OutputStream outStream, byte[] xmlData, LinkedList binaryNodeList, String boundary, String contentId, String charSetEncoding, String SOAPContentType, OMOutputFormat omOutputFormat) {
try {
log.debug("Start: write the SOAPPart and the attachments");
// Write out the mime boundary
startWritingMime(outStream, boundary);
javax.activation.DataHandler dh = new javax.activation.DataHandler(new ByteArrayDataSource(xmlData, "text/xml; charset=" + charSetEncoding));
MimeBodyPart rootMimeBodyPart = new MimeBodyPart();
rootMimeBodyPart.setDataHandler(dh);
rootMimeBodyPart.addHeader("Content-Type", "application/xop+xml; charset=" + charSetEncoding + "; type=\"" + SOAPContentType + "\"");
rootMimeBodyPart.addHeader("Content-Transfer-Encoding", "binary");
rootMimeBodyPart.addHeader("Content-ID", "<" + contentId + ">");
// Write out the SOAPPart
writeBodyPart(outStream, rootMimeBodyPart, boundary);
// Now write out the Attachment parts (which are represented by the
// text nodes int the binary node list)
Iterator binaryNodeIterator = binaryNodeList.iterator();
while (binaryNodeIterator.hasNext()) {
OMText binaryNode = (OMText) binaryNodeIterator.next();
writeBodyPart(outStream, createMimeBodyPart(binaryNode.getContentID(), binaryNode.getDataHandler(), omOutputFormat), boundary);
}
finishWritingMime(outStream);
outStream.flush();
log.debug("End: write the SOAPPart and the attachments");
} catch (IOException e) {
throw new OMException("Error while writing to the OutputStream.", e);
} catch (MessagingException e) {
throw new OMException("Problem writing Mime Parts.", e);
}
}
use of org.apache.axiom.attachments.ByteArrayDataSource in project wso2-synapse by wso2.
the class ScriptMediatorTest method testExternalScriptWithComments.
/**
* Test functionality of mediate with external script in js.
*
* @throws Exception
*/
public void testExternalScriptWithComments() throws Exception {
String request = "{\n" + " \"results\": [\n" + " {\n" + " \"geometry\": {\n" + " \"location\": {\n" + " \"lat\": -33.86726,\n" + " \"lng\": 151.195813\n" + " }\n" + " },\n" + " \"icon\": \"bar-71.png\",\n" + " \"id\": \"7eaf7\",\n" + " \"name\": \"Biaggio Cafe\",\n" + " \"opening_hours\": {\n" + " \"open_now\": true\n" + " },\n" + " \"photos\": [\n" + " {\n" + " \"height\": 600,\n" + " \"html_attributions\": [],\n" + " \"photo_reference\": \"CoQBegAAAI\",\n" + " \"width\": 900\n" + " }\n" + " ],\n" + " \"price_level\": 1,\n" + " \"reference\": \"CnRqAAAAtz\",\n" + " \"types\": [\n" + " \"bar\",\n" + " \"restaurant\",\n" + " \"food\",\n" + " \"establishment\"\n" + " ],\n" + " \"vicinity\": \"48 Pirrama Road, Pyrmont\"\n" + " },\n" + " {\n" + " \"geometry\": {\n" + " \"location\": {\n" + " \"lat\": -33.866804,\n" + " \"lng\": 151.195579\n" + " }\n" + " },\n" + " \"icon\": \"generic_business-71.png\",\n" + " \"id\": \"3ef98\",\n" + " \"name\": \"Doltone House\",\n" + " \"photos\": [\n" + " {\n" + " \"height\": 600,\n" + " \"html_attributions\": [],\n" + " \"photo_reference\": \"CqQBmgAAAL\",\n" + " \"width\": 900\n" + " }\n" + " ],\n" + " \"reference\": \"CnRrAAAAV\",\n" + " \"types\": [\n" + " \"food\",\n" + " \"establishment\"\n" + " ],\n" + " \"vicinity\": \"48 Pirrama Road, Pyrmont\"\n" + " }\n" + " ],\n" + " \"status\": \"OK\"\n" + "}";
MessageContext mc = TestUtils.getTestContextJson(request, null);
String scriptSrc = "function transform(mc) {\n" + " payload = mc.getPayloadJSON();\n" + " results = payload.results;\n" + " var response = new Array();\n" + " for (i = 0; i < results.length; ++i) {\n" + " // this is a comment\n" + " location_object = results[i];\n" + " l = new Object();\n" + " l.name = location_object.name;\n" + " l.tags = location_object.types;\n" + " l.id = \"ID:\" + (location_object.id);\n" + " response[i] = l;\n" + " }\n" + " mc.setPayloadJSON(response);\n" + "}";
String scriptSrcKey = "conf:/repository/esb/transform.js";
Entry e = new Entry();
DataSource dataSource = new ByteArrayDataSource(scriptSrc.getBytes());
DataHandler dataHandler = new DataHandler(dataSource);
OMText text = OMAbstractFactory.getOMFactory().createOMText(dataHandler, true);
e.setKey(scriptSrcKey);
e.setValue(text);
mc.getConfiguration().addEntry(scriptSrcKey, e);
Value v = new Value(scriptSrcKey);
ScriptMediator mediator = new ScriptMediator("js", new LinkedHashMap<Value, Object>(), v, "transform", null);
boolean result = mediator.mediate(mc);
String response = JsonUtil.jsonPayloadToString(((Axis2MessageContext) mc).getAxis2MessageContext());
String expectedResponse = "[{\"name\":\"Biaggio Cafe\", \"tags\":[\"bar\", \"restaurant\", \"food\"," + " \"establishment\"], \"id\":\"ID:7eaf7\"}, {\"name\":\"Doltone House\", \"tags\":[\"food\"," + " \"establishment\"], \"id\":\"ID:3ef98\"}]";
assertEquals(expectedResponse, response);
assertEquals(true, result);
}
use of org.apache.axiom.attachments.ByteArrayDataSource in project wso2-synapse by wso2.
the class StreamingOnRequestDataSource method getInputStream.
public InputStream getInputStream() throws IOException {
if (cachedData != null) {
return cachedData.getInputStream();
} else {
if (lastUse && in != null) {
InputStream returnStram = in;
this.in = null;
return returnStram;
} else if (in != null) {
byte[] data = BinaryRelayBuilder.readAllFromInputSteam(in);
cachedData = new ByteArrayDataSource(data);
return cachedData.getInputStream();
} else {
throw new IOException("Input stream has been already consumed");
}
}
}
use of org.apache.axiom.attachments.ByteArrayDataSource in project wso2-synapse by wso2.
the class FIXUtils method setSOAPEnvelope.
/**
* FIX messages are non-XML. So convert them into XML using the AXIOM API.
* Put the FIX message into an Axis2 MessageContext.The basic format of the
* generated SOAP envelope;
* <p/>
* <soapEnvelope>
* <soapBody>
* <message>
* <header> ....</header>
* <body> .... </body>
* <trailer> .... </trailer>
* </message>
* </soapBody>
* </soapEnvelope>
*
* @param message the FIX message
* @param counter application level sequence number of the message
* @param sessionID the incoming session
* @param msgCtx the Axis2 MessageContext to hold the FIX message
* @throws AxisFault the exception thrown when invalid soap envelopes are set to the msgCtx
*/
public void setSOAPEnvelope(Message message, int counter, String sessionID, MessageContext msgCtx) throws AxisFault {
if (log.isDebugEnabled()) {
log.debug("Creating SOAP envelope for FIX message...");
}
SOAPFactory soapFactory = new SOAP11Factory();
OMElement msg = soapFactory.createOMElement(FIXConstants.FIX_MESSAGE, null);
msg.addAttribute(soapFactory.createOMAttribute(FIXConstants.FIX_MESSAGE_INCOMING_SESSION, null, sessionID));
msg.addAttribute(soapFactory.createOMAttribute(FIXConstants.FIX_MESSAGE_COUNTER, null, String.valueOf(counter)));
OMElement header = soapFactory.createOMElement(FIXConstants.FIX_HEADER, null);
OMElement body = soapFactory.createOMElement(FIXConstants.FIX_BODY, null);
OMElement trailer = soapFactory.createOMElement(FIXConstants.FIX_TRAILER, null);
// process FIX header
Iterator<Field<?>> iter = message.getHeader().iterator();
if (iter != null) {
while (iter.hasNext()) {
Field<?> field = iter.next();
OMElement msgField = soapFactory.createOMElement(FIXConstants.FIX_FIELD, null);
msgField.addAttribute(soapFactory.createOMAttribute(FIXConstants.FIX_FIELD_ID, null, String.valueOf(field.getTag())));
Object value = field.getObject();
if (value instanceof byte[]) {
DataSource dataSource = new ByteArrayDataSource((byte[]) value);
DataHandler dataHandler = new DataHandler(dataSource);
String contentID = msgCtx.addAttachment(dataHandler);
OMElement binaryData = soapFactory.createOMElement(FIXConstants.FIX_BINARY_FIELD, null);
String binaryCID = "cid:" + contentID;
binaryData.addAttribute(FIXConstants.FIX_MESSAGE_REFERENCE, binaryCID, null);
msgField.addChild(binaryData);
} else {
createOMText(soapFactory, msgField, value.toString());
}
header.addChild(msgField);
}
}
// process FIX body
convertFIXBodyToXML(message, body, soapFactory, msgCtx);
// process FIX trailer
iter = message.getTrailer().iterator();
if (iter != null) {
while (iter.hasNext()) {
Field<?> field = iter.next();
OMElement msgField = soapFactory.createOMElement(FIXConstants.FIX_FIELD, null);
msgField.addAttribute(soapFactory.createOMAttribute(FIXConstants.FIX_FIELD_ID, null, String.valueOf(field.getTag())));
Object value = field.getObject();
if (value instanceof byte[]) {
DataSource dataSource = new ByteArrayDataSource((byte[]) value);
DataHandler dataHandler = new DataHandler(dataSource);
String contentID = msgCtx.addAttachment(dataHandler);
OMElement binaryData = soapFactory.createOMElement(FIXConstants.FIX_BINARY_FIELD, null);
String binaryCID = "cid:" + contentID;
binaryData.addAttribute(FIXConstants.FIX_MESSAGE_REFERENCE, binaryCID, null);
msgField.addChild(binaryData);
} else {
createOMText(soapFactory, msgField, value.toString());
}
trailer.addChild(msgField);
}
}
msg.addChild(header);
msg.addChild(body);
msg.addChild(trailer);
SOAPEnvelope envelope = soapFactory.getDefaultEnvelope();
envelope.getBody().addChild(msg);
msgCtx.setEnvelope(envelope);
}
use of org.apache.axiom.attachments.ByteArrayDataSource in project wso2-synapse by wso2.
the class FIXUtils method convertFIXBodyToXML.
/**
* Constructs the XML infoset for the FIX message body
*
* @param message the FIX message
* @param body the body element of the XML infoset
* @param soapFactory the SOAP factory to create XML elements
* @param msgCtx the Axis2 Message context
* @throws AxisFault on error
*/
private void convertFIXBodyToXML(FieldMap message, OMElement body, SOAPFactory soapFactory, MessageContext msgCtx) throws AxisFault {
if (log.isDebugEnabled()) {
log.debug("Generating FIX message body (Message ID: " + msgCtx.getMessageID() + ")");
}
Iterator<Field<?>> iter = message.iterator();
if (iter != null) {
while (iter.hasNext()) {
Field<?> field = iter.next();
OMElement msgField = soapFactory.createOMElement(FIXConstants.FIX_FIELD, null);
msgField.addAttribute(soapFactory.createOMAttribute(FIXConstants.FIX_FIELD_ID, null, String.valueOf(field.getTag())));
Object value = field.getObject();
if (value instanceof byte[]) {
DataSource dataSource = new ByteArrayDataSource((byte[]) value);
DataHandler dataHandler = new DataHandler(dataSource);
String contentID = msgCtx.addAttachment(dataHandler);
OMElement binaryData = soapFactory.createOMElement(FIXConstants.FIX_BINARY_FIELD, null);
String binaryCID = "cid:" + contentID;
binaryData.addAttribute(FIXConstants.FIX_MESSAGE_REFERENCE, binaryCID, null);
msgField.addChild(binaryData);
} else {
createOMText(soapFactory, msgField, value.toString());
}
body.addChild(msgField);
}
}
// process FIX repeating groups
Iterator<Integer> groupKeyItr = message.groupKeyIterator();
if (groupKeyItr != null) {
while (groupKeyItr.hasNext()) {
int groupKey = groupKeyItr.next();
OMElement groupsField = soapFactory.createOMElement(FIXConstants.FIX_GROUPS, null);
groupsField.addAttribute(FIXConstants.FIX_FIELD_ID, String.valueOf(groupKey), null);
List<Group> groupList = message.getGroups(groupKey);
Iterator<Group> groupIterator = groupList.iterator();
while (groupIterator.hasNext()) {
Group msgGroup = groupIterator.next();
OMElement groupField = soapFactory.createOMElement(FIXConstants.FIX_GROUP, null);
// rec. call the method to process the repeating groups
convertFIXBodyToXML(msgGroup, groupField, soapFactory, msgCtx);
groupsField.addChild(groupField);
}
body.addChild(groupsField);
}
}
}
Aggregations