Search in sources :

Example 1 with ByteArrayDataSource

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);
}
Also used : DataHandler(javax.activation.DataHandler) ByteArrayDataSource(org.apache.axiom.attachments.ByteArrayDataSource)

Example 2 with ByteArrayDataSource

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);
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMNamespace(org.apache.axiom.om.OMNamespace) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement) DataHandler(javax.activation.DataHandler) OMAttribute(org.apache.axiom.om.OMAttribute) ByteArrayDataSource(org.apache.axiom.attachments.ByteArrayDataSource)

Example 3 with ByteArrayDataSource

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()));
}
Also used : Random(java.util.Random) MimeMultipart(javax.mail.internet.MimeMultipart) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MimeBodyPart(javax.mail.internet.MimeBodyPart) ByteArrayDataSource(org.apache.axiom.attachments.ByteArrayDataSource)

Example 4 with ByteArrayDataSource

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;
}
Also used : QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement) DataHandler(javax.activation.DataHandler) OMText(org.apache.axiom.om.OMText) ByteArrayDataSource(org.apache.axiom.attachments.ByteArrayDataSource)

Example 5 with ByteArrayDataSource

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;
}
Also used : AxisFault(org.apache.axis2.AxisFault) InputStreamReader(java.io.InputStreamReader) DefaultDataDictionaryProvider(quickfix.DefaultDataDictionaryProvider) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) OMElement(org.apache.axiom.om.OMElement) BeginString(quickfix.field.BeginString) InvalidMessage(quickfix.InvalidMessage) DataHandler(javax.activation.DataHandler) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) DataDictionary(quickfix.DataDictionary) SOAPFactory(org.apache.axiom.soap.SOAPFactory) ByteArrayDataSource(org.apache.axiom.attachments.ByteArrayDataSource) DataSource(javax.activation.DataSource) SOAP11Factory(org.apache.axiom.soap.impl.llom.soap11.SOAP11Factory) ByteArrayDataSource(org.apache.axiom.attachments.ByteArrayDataSource)

Aggregations

ByteArrayDataSource (org.apache.axiom.attachments.ByteArrayDataSource)15 DataHandler (javax.activation.DataHandler)11 OMElement (org.apache.axiom.om.OMElement)8 DataSource (javax.activation.DataSource)6 OMText (org.apache.axiom.om.OMText)5 MimeBodyPart (javax.mail.internet.MimeBodyPart)3 QName (javax.xml.namespace.QName)3 OMFactory (org.apache.axiom.om.OMFactory)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 Random (java.util.Random)2 MimeMultipart (javax.mail.internet.MimeMultipart)2 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)2 SOAPFactory (org.apache.axiom.soap.SOAPFactory)2 SOAP11Factory (org.apache.axiom.soap.impl.llom.soap11.SOAP11Factory)2 MessageContext (org.apache.synapse.MessageContext)2 Entry (org.apache.synapse.config.Entry)2 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)2 Value (org.apache.synapse.mediators.Value)2