Search in sources :

Example 1 with SynapseBinaryDataSource

use of org.apache.synapse.util.SynapseBinaryDataSource in project wso2-synapse by wso2.

the class SynapseConfigUtils method readNonXML.

/**
 * Helper method to handle non-XMl resources
 *
 * @param url The resource url
 * @return The content as an OMNode
 */
public static OMNode readNonXML(URL url) {
    try {
        // Open a new connection
        URLConnection newConnection = getURLConnection(url);
        if (newConnection == null) {
            if (log.isDebugEnabled()) {
                log.debug("Cannot create a URLConnection for given URL : " + url);
            }
            return null;
        }
        BufferedInputStream newInputStream = new BufferedInputStream(newConnection.getInputStream());
        OMFactory omFactory = OMAbstractFactory.getOMFactory();
        return omFactory.createOMText(new DataHandler(new SynapseBinaryDataSource(newInputStream, newConnection.getContentType())), true);
    } catch (IOException e) {
        handleException("Error when getting a stream from resource's content", e);
    }
    return null;
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) BufferedInputStream(java.io.BufferedInputStream) SynapseBinaryDataSource(org.apache.synapse.util.SynapseBinaryDataSource) DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 2 with SynapseBinaryDataSource

use of org.apache.synapse.util.SynapseBinaryDataSource in project wso2-synapse by wso2.

the class HessianMessageFormatter method writeHessianMessage.

/**
 * Writes the Hessian message contained in the message context to the provided output stream.
 *
 * @param msgCtxt the message context containing the Hessian message
 * @param out the provided output stream to which the message shall be written
 *
 * @throws AxisFault if an error occurs writing to the output stream
 */
private void writeHessianMessage(MessageContext msgCtxt, OutputStream out) throws AxisFault {
    OMElement omElement = msgCtxt.getEnvelope().getBody().getFirstElement();
    SynapseBinaryDataSource synapseBinaryDataSource = extractSynapseBinaryDataSource(omElement);
    if (synapseBinaryDataSource != null) {
        InputStream inputStream = null;
        try {
            inputStream = synapseBinaryDataSource.getInputStream();
            IOUtils.copy(inputStream, out);
        } catch (IOException e) {
            handleException("Couldn't get the bytes from the HessianDataSource", e);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException ignore) {
                    log.warn("Error closing input stream.", ignore);
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException ignore) {
                    log.warn("Error closing output stream.", ignore);
                }
            }
        }
    } else {
        handleException("Unable to find the Hessian content in the payload");
    }
}
Also used : InputStream(java.io.InputStream) SynapseBinaryDataSource(org.apache.synapse.util.SynapseBinaryDataSource) OMElement(org.apache.axiom.om.OMElement) IOException(java.io.IOException)

Example 3 with SynapseBinaryDataSource

use of org.apache.synapse.util.SynapseBinaryDataSource in project wso2-synapse by wso2.

the class HessianMessageBuilder method processDocument.

/**
 * Returns an OMElement from a Hessian encoded message
 *
 * @param inputStream stream containing the Hessian message to be built
 * @param contentType content type of the message
 * @param messageContext message to which the hessian message has to be attached
 * @return OMElement containing Hessian data handler keeping the message
 * @throws AxisFault in case of a failure in building the hessian message
 *
 * @see org.apache.axis2.builder.Builder#processDocument(java.io.InputStream,
 * String, org.apache.axis2.context.MessageContext)
 */
public OMElement processDocument(final InputStream inputStream, final String contentType, final MessageContext messageContext) throws AxisFault {
    if (log.isDebugEnabled()) {
        log.debug("Start building the hessian message in to a HessianDataSource");
    }
    OMFactory factory = OMAbstractFactory.getOMFactory();
    OMNamespace ns = factory.createOMNamespace(HessianConstants.HESSIAN_NAMESPACE_URI, HessianConstants.HESSIAN_NS_PREFIX);
    OMElement element = factory.createOMElement(HessianConstants.HESSIAN_ELEMENT_LOCAL_NAME, ns);
    try {
        Parameter synEnv = messageContext.getConfigurationContext().getAxisConfiguration().getParameter(SynapseConstants.SYNAPSE_ENV);
        PushbackInputStream pis = detectAndMarkMessageFault(messageContext, inputStream);
        DataHandler dataHandler;
        if (synEnv != null && synEnv.getValue() != null) {
            dataHandler = new DataHandler(new SynapseBinaryDataSource(pis, contentType, (SynapseEnvironment) synEnv.getValue()));
        } else {
            // add Hessian data inside a data handler
            dataHandler = new DataHandler(new SynapseBinaryDataSource(pis, contentType));
        }
        OMText textData = factory.createOMText(dataHandler, true);
        element.addChild(textData);
        // indicate that message faults shall be handled as http 200
        messageContext.setProperty(NhttpConstants.FAULTS_AS_HTTP_200, NhttpConstants.TRUE);
    } catch (IOException e) {
        String msg = "Unable to create the HessianDataSource";
        log.error(msg, e);
        throw new AxisFault(msg, e);
    }
    if (log.isDebugEnabled()) {
        log.debug("Building the hessian message using HessianDataSource is successful");
    }
    return element;
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) AxisFault(org.apache.axis2.AxisFault) OMNamespace(org.apache.axiom.om.OMNamespace) PushbackInputStream(java.io.PushbackInputStream) SynapseBinaryDataSource(org.apache.synapse.util.SynapseBinaryDataSource) OMText(org.apache.axiom.om.OMText) Parameter(org.apache.axis2.description.Parameter) OMElement(org.apache.axiom.om.OMElement) DataHandler(javax.activation.DataHandler) IOException(java.io.IOException)

Example 4 with SynapseBinaryDataSource

use of org.apache.synapse.util.SynapseBinaryDataSource in project wso2-synapse by wso2.

the class HessianMessageBuilderTest method test.

private MessageContext test(String testMessageName, SynapseEnvironment synEnv) throws IOException {
    HessianTestHelper hessianTestHelper = new HessianTestHelper();
    MessageContext msgContext = hessianTestHelper.createAxis2MessageContext(synEnv);
    OMElement element = hessianTestHelper.buildHessianTestMessage(testMessageName, msgContext);
    OMNode hessianNode = element.getFirstOMChild();
    OMText hessianTextNode = (OMText) hessianNode;
    SynapseBinaryDataSource synapseBinaryDataSource = (SynapseBinaryDataSource) ((DataHandler) hessianTextNode.getDataHandler()).getDataSource();
    InputStream inputStream = synapseBinaryDataSource.getInputStream();
    byte[] originalByteArray = IOUtils.toByteArray(getClass().getResourceAsStream(testMessageName));
    byte[] builderByteArray = IOUtils.toByteArray(inputStream);
    assertTrue(Arrays.equals(originalByteArray, builderByteArray));
    return msgContext;
}
Also used : OMNode(org.apache.axiom.om.OMNode) InputStream(java.io.InputStream) OMText(org.apache.axiom.om.OMText) SynapseBinaryDataSource(org.apache.synapse.util.SynapseBinaryDataSource) OMElement(org.apache.axiom.om.OMElement) MessageContext(org.apache.axis2.context.MessageContext)

Example 5 with SynapseBinaryDataSource

use of org.apache.synapse.util.SynapseBinaryDataSource in project wso2-synapse by wso2.

the class HessianMessageFormatter method extractSynapseBinaryDataSource.

/**
 * Tries to extract the binary data source containing the Hessian message.
 *
 * @param omElement
 *
 * @return the binary data source containing the Hessian message or null, if the OMElement
 *         does not contain a binary datasource.
 */
private SynapseBinaryDataSource extractSynapseBinaryDataSource(OMElement omElement) {
    SynapseBinaryDataSource synapseBinaryDataSource = null;
    Iterator it = omElement.getChildren();
    while (it.hasNext() && synapseBinaryDataSource == null) {
        OMNode hessianElement = (OMNode) it.next();
        if (hessianElement instanceof OMText) {
            OMText tempNode = (OMText) hessianElement;
            if (tempNode.getDataHandler() != null && ((DataHandler) tempNode.getDataHandler()).getDataSource() instanceof SynapseBinaryDataSource) {
                synapseBinaryDataSource = (SynapseBinaryDataSource) ((DataHandler) tempNode.getDataHandler()).getDataSource();
            }
        }
    }
    return synapseBinaryDataSource;
}
Also used : OMNode(org.apache.axiom.om.OMNode) SynapseBinaryDataSource(org.apache.synapse.util.SynapseBinaryDataSource) Iterator(java.util.Iterator) OMText(org.apache.axiom.om.OMText) DataHandler(javax.activation.DataHandler)

Aggregations

SynapseBinaryDataSource (org.apache.synapse.util.SynapseBinaryDataSource)5 IOException (java.io.IOException)3 DataHandler (javax.activation.DataHandler)3 OMElement (org.apache.axiom.om.OMElement)3 OMText (org.apache.axiom.om.OMText)3 InputStream (java.io.InputStream)2 OMFactory (org.apache.axiom.om.OMFactory)2 OMNode (org.apache.axiom.om.OMNode)2 BufferedInputStream (java.io.BufferedInputStream)1 PushbackInputStream (java.io.PushbackInputStream)1 URLConnection (java.net.URLConnection)1 Iterator (java.util.Iterator)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1 OMNamespace (org.apache.axiom.om.OMNamespace)1 AxisFault (org.apache.axis2.AxisFault)1 MessageContext (org.apache.axis2.context.MessageContext)1 Parameter (org.apache.axis2.description.Parameter)1