use of org.apache.axiom.attachments.ByteArrayDataSource in project webservices-axiom by apache.
the class DataHandlerUtils method getDataHandlerFromText.
public static Object getDataHandlerFromText(String value, String mimeType) {
ByteArrayDataSource dataSource;
byte[] data = Base64Utils.decode(value);
if (mimeType != null) {
dataSource = new ByteArrayDataSource(data, mimeType);
} else {
// Assumes type as application/octet-stream
dataSource = new ByteArrayDataSource(data);
}
return new DataHandler(dataSource);
}
use of org.apache.axiom.attachments.ByteArrayDataSource in project webservices-axiom by apache.
the class OMOutputTest method setUp.
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
DataHandler dataHandler;
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace soap = fac.createOMNamespace("http://schemas.xmlsoap.org/soap/envelope/", "soap");
envelope = fac.createOMElement("Envelope", soap);
OMElement body = fac.createOMElement("Body", soap);
OMNamespace dataName = fac.createOMNamespace("http://www.example.org/stuff", "m");
OMElement data = fac.createOMElement("data", dataName);
OMNamespace mime = fac.createOMNamespace("http://www.w3.org/2003/06/xmlmime", "mime");
OMElement text = fac.createOMElement("name", dataName);
OMAttribute cType1 = fac.createOMAttribute("contentType", mime, "text/plain");
text.addAttribute(cType1);
byte[] byteArray = new byte[] { 13, 56, 65, 32, 12, 12, 7, -3, -2, -1, 98 };
dataHandler = new DataHandler(new ByteArrayDataSource(byteArray));
OMText textData = fac.createOMText(dataHandler, false);
envelope.addChild(body);
body.addChild(data);
data.addChild(text);
text.addChild(textData);
}
use of org.apache.axiom.attachments.ByteArrayDataSource in project webservices-axiom by apache.
the class AbstractMultipartWriterTest method test.
private void test(String contentTransferEncoding) throws Exception {
Random random = new Random();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
MultipartWriter mpw = factory.createMultipartWriter(baos, UIDGenerator.generateMimeBoundary());
byte[] content = new byte[8192];
random.nextBytes(content);
OutputStream partOutputStream = mpw.writePart("application/octet-stream", contentTransferEncoding, UIDGenerator.generateContentId());
partOutputStream.write(content);
partOutputStream.close();
mpw.complete();
MimeMultipart mp = new MimeMultipart(new ByteArrayDataSource(baos.toByteArray()));
assertEquals(1, mp.getCount());
MimeBodyPart bp = (MimeBodyPart) mp.getBodyPart(0);
assertEquals(contentTransferEncoding, bp.getHeader("Content-Transfer-Encoding")[0]);
baos.reset();
bp.getDataHandler().writeTo(baos);
assertTrue(Arrays.equals(content, baos.toByteArray()));
}
use of org.apache.axiom.attachments.ByteArrayDataSource in project wso2-synapse by wso2.
the class SimpleMapImpl method getOMElement.
public OMElement getOMElement(OMFactory fac) {
OMElement mapElement = fac.createOMElement(PayloadHelper.MAPELT);
for (Object entryObj : this.entrySet()) {
Object key = ((Map.Entry) entryObj).getKey();
Object o = ((Map.Entry) entryObj).getValue();
if (key instanceof String) {
OMElement entry = fac.createOMElement(new QName(PayloadHelper.AXIOMPAYLOADNS, ENTRY), mapElement);
entry.addAttribute(NAME, (String) key, attrNS);
if (o instanceof Character) {
entry.addAttribute(TYPE, CHAR, attrNS);
entry.setText(o.toString());
} else if (o instanceof Boolean) {
entry.addAttribute(TYPE, BOOLEAN, attrNS);
entry.setText(o.toString());
} else if (o instanceof String) {
entry.addAttribute(TYPE, STRING, attrNS);
entry.setText(o.toString());
} else if (o instanceof Byte) {
entry.addAttribute(TYPE, BYTE, attrNS);
entry.setText(o.toString());
} else if (o instanceof byte[]) {
entry.addAttribute(TYPE, BYTEARRAY, attrNS);
OMText text = fac.createOMText(new DataHandler(new ByteArrayDataSource((byte[]) o)), true);
entry.addChild(text);
} else if (o instanceof Float) {
entry.addAttribute(TYPE, FLOAT, attrNS);
entry.setText(o.toString());
} else if (o instanceof Double) {
entry.addAttribute(TYPE, DOUBLE, attrNS);
entry.setText(o.toString());
} else if (o instanceof Long) {
entry.addAttribute(TYPE, LONG, attrNS);
entry.setText(o.toString());
} else if (o instanceof Short) {
entry.addAttribute(TYPE, SHORT, attrNS);
entry.setText(o.toString());
} else if (o instanceof Integer) {
entry.addAttribute(TYPE, INTEGER, attrNS);
entry.setText(o.toString());
}
} else {
// shouldn't be any non-string keys. Ignore!
}
}
return mapElement;
}
use of org.apache.axiom.attachments.ByteArrayDataSource in project wso2-synapse by wso2.
the class FIXMessageBuilder method processDocument.
public OMElement processDocument(InputStream inputStream, String contentType, MessageContext messageContext) throws AxisFault {
Reader reader = null;
StringBuilder messageString = new StringBuilder();
quickfix.Message message = null;
try {
String charSetEncoding = (String) messageContext.getProperty(Constants.Configuration.CHARACTER_SET_ENCODING);
if (charSetEncoding == null) {
charSetEncoding = MessageContext.DEFAULT_CHAR_SET_ENCODING;
}
reader = new InputStreamReader(inputStream, charSetEncoding);
try {
int data = reader.read();
while (data != -1) {
char dataChar = (char) data;
data = reader.read();
messageString.append(dataChar);
}
} catch (Exception e) {
// TODO Auto-generated catch block
log.error("Error In creating FIX SOAP envelope ...", e);
throw new AxisFault(e.getMessage());
}
} catch (Exception e) {
// TODO Auto-generated catch block
log.error("Error In creating FIX SOAP envelope ...", e);
throw new AxisFault(e.getMessage());
}
try {
DefaultDataDictionaryProvider dataDictionary = new DefaultDataDictionaryProvider();
String beginString = MessageUtils.getStringField(messageString.toString(), BeginString.FIELD);
DataDictionary dataDic = dataDictionary.getSessionDataDictionary(beginString);
message = new quickfix.Message(messageString.toString(), null, false);
} catch (InvalidMessage e) {
// TODO Auto-generated catch block
log.error("Error In creating FIX SOAP envelope ...", e);
throw new AxisFault(e.getMessage());
}
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, ""));
msg.addAttribute(soapFactory.createOMAttribute(FIXConstants.FIX_MESSAGE_COUNTER, null, String.valueOf("-1")));
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<quickfix.Field<?>> iter = message.getHeader().iterator();
if (iter != null) {
while (iter.hasNext()) {
quickfix.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 = messageContext.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 {
soapFactory.createOMText(msgField, value.toString(), OMElement.CDATA_SECTION_NODE);
}
header.addChild(msgField);
}
}
// process FIX body
convertFIXBodyToXML(message, body, soapFactory, messageContext);
// process FIX trailer
iter = message.getTrailer().iterator();
if (iter != null) {
while (iter.hasNext()) {
quickfix.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 = messageContext.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 {
soapFactory.createOMText(msgField, value.toString(), OMElement.CDATA_SECTION_NODE);
}
trailer.addChild(msgField);
}
}
msg.addChild(header);
msg.addChild(body);
msg.addChild(trailer);
SOAPEnvelope envelope = soapFactory.getDefaultEnvelope();
envelope.getBody().addChild(msg);
messageContext.setEnvelope(envelope);
return msg;
}
Aggregations