Search in sources :

Example 11 with ByteArrayDataSource

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);
    }
}
Also used : MessagingException(javax.mail.MessagingException) Iterator(java.util.Iterator) OMText(org.apache.axiom.om.OMText) DataHandler(javax.activation.DataHandler) ConfigurableDataHandler(org.apache.axiom.attachments.ConfigurableDataHandler) IOException(java.io.IOException) MimeBodyPart(javax.mail.internet.MimeBodyPart) DataHandler(javax.activation.DataHandler) ByteArrayDataSource(org.apache.axiom.attachments.ByteArrayDataSource) OMException(org.apache.axiom.om.OMException)

Example 12 with ByteArrayDataSource

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);
}
Also used : Entry(org.apache.synapse.config.Entry) OMText(org.apache.axiom.om.OMText) Value(org.apache.synapse.mediators.Value) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) DataHandler(javax.activation.DataHandler) ByteArrayDataSource(org.apache.axiom.attachments.ByteArrayDataSource) ByteArrayDataSource(org.apache.axiom.attachments.ByteArrayDataSource) DataSource(javax.activation.DataSource)

Example 13 with ByteArrayDataSource

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");
        }
    }
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) ByteArrayDataSource(org.apache.axiom.attachments.ByteArrayDataSource)

Example 14 with ByteArrayDataSource

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);
}
Also used : SOAP11Factory(org.apache.axiom.soap.impl.llom.soap11.SOAP11Factory) OMElement(org.apache.axiom.om.OMElement) DataHandler(javax.activation.DataHandler) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) ByteArrayDataSource(org.apache.axiom.attachments.ByteArrayDataSource) SOAPFactory(org.apache.axiom.soap.SOAPFactory) ByteArrayDataSource(org.apache.axiom.attachments.ByteArrayDataSource) DataSource(javax.activation.DataSource)

Example 15 with ByteArrayDataSource

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