Search in sources :

Example 71 with OMText

use of org.apache.axiom.om.OMText in project wso2-synapse by wso2.

the class MTOMSwASampleService method uploadFileUsingMTOM.

public OMElement uploadFileUsingMTOM(OMElement request) throws Exception {
    OMText binaryNode = (OMText) request.getFirstChildWithName(new QName("http://services.samples", "request")).getFirstChildWithName(new QName("http://services.samples", "image")).getFirstOMChild();
    DataHandler dataHandler = (DataHandler) binaryNode.getDataHandler();
    InputStream is = dataHandler.getInputStream();
    File tempFile = File.createTempFile("mtom-", ".gif");
    FileOutputStream fos = new FileOutputStream(tempFile);
    BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
    byte[] data = new byte[BUFFER];
    int count;
    while ((count = is.read(data, 0, BUFFER)) != -1) {
        dest.write(data, 0, count);
    }
    dest.flush();
    dest.close();
    System.out.println("Wrote MTOM content to temp file : " + tempFile.getAbsolutePath());
    OMFactory factory = request.getOMFactory();
    OMNamespace ns = factory.createOMNamespace("http://services.samples", "m0");
    OMElement payload = factory.createOMElement("uploadFileUsingMTOMResponse", ns);
    OMElement response = factory.createOMElement("response", ns);
    OMElement image = factory.createOMElement("image", ns);
    FileDataSource fileDataSource = new FileDataSource(tempFile);
    dataHandler = new DataHandler(fileDataSource);
    OMText textData = factory.createOMText(dataHandler, true);
    image.addChild(textData);
    response.addChild(image);
    payload.addChild(response);
    MessageContext outMsgCtx = MessageContext.getCurrentMessageContext().getOperationContext().getMessageContext(WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
    outMsgCtx.setProperty(org.apache.axis2.Constants.Configuration.ENABLE_MTOM, org.apache.axis2.Constants.VALUE_TRUE);
    return payload;
}
Also used : OMNamespace(org.apache.axiom.om.OMNamespace) QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement) DataHandler(javax.activation.DataHandler) OMFactory(org.apache.axiom.om.OMFactory) OMText(org.apache.axiom.om.OMText) FileDataSource(javax.activation.FileDataSource) MessageContext(org.apache.axis2.context.MessageContext)

Example 72 with OMText

use of org.apache.axiom.om.OMText in project wso2-synapse by wso2.

the class MessageInjector method execute.

/**
 * This will be invoked by the scheduler to inject the message
 * in to the SynapseEnvironment
 */
public void execute() {
    if (log.isDebugEnabled()) {
        log.debug("execute");
    }
    if (synapseEnvironment == null) {
        handleError("Synapse Environment not set");
        return;
    }
    if (synapseEnvironment.getTaskManager() != null && !synapseEnvironment.getTaskManager().isInitialized()) {
        log.warn("Task Manager not initialized. Not executing the cycle");
        return;
    }
    if (message == null && registryKey == null) {
        handleError("message or registry-key not set");
        return;
    }
    if (INJECT_TO_PROXY.equalsIgnoreCase(injectTo)) {
        if (proxyName == null || proxyName.equals("")) {
            handleError("Proxy service name not specified");
        }
        // Prepare axis2 message context
        org.apache.axis2.context.MessageContext axis2MsgCtx = new org.apache.axis2.context.MessageContext();
        ConfigurationContext configurationContext = ((Axis2SynapseEnvironment) synapseEnvironment).getAxis2ConfigurationContext();
        axis2MsgCtx.setConfigurationContext(configurationContext);
        axis2MsgCtx.setIncomingTransportName(Constants.TRANSPORT_LOCAL);
        axis2MsgCtx.setServerSide(true);
        axis2MsgCtx.setMessageID(UIDGenerator.generateURNString());
        try {
            AxisService axisService = configurationContext.getAxisConfiguration().getService(proxyName);
            if (axisService == null) {
                handleError("Proxy Service: " + proxyName + " not found");
            }
            axis2MsgCtx.setAxisService(axisService);
        } catch (AxisFault axisFault) {
            handleError("Error occurred while attempting to find the Proxy Service");
        }
        if (to != null) {
            axis2MsgCtx.setTo(new EndpointReference(to));
        }
        SOAPEnvelope envelope = null;
        if (format == null) {
            envelope = OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope();
        } else if (SOAP11_FORMAT.equalsIgnoreCase(format)) {
            envelope = OMAbstractFactory.getSOAP11Factory().createSOAPEnvelope();
        } else if (SOAP12_FORMAT.equalsIgnoreCase(format)) {
            envelope = OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope();
        } else if (POX_FORMAT.equalsIgnoreCase(format)) {
            envelope = OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope();
            axis2MsgCtx.setDoingREST(true);
        } else if (GET_FORMAT.equalsIgnoreCase(format)) {
            envelope = OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope();
            axis2MsgCtx.setDoingREST(true);
            axis2MsgCtx.setProperty(Constants.Configuration.HTTP_METHOD, Constants.Configuration.HTTP_METHOD_GET);
        } else {
            handleError("incorrect format specified");
        }
        try {
            PayloadHelper.setXMLPayload(envelope, message.cloneOMElement());
            axis2MsgCtx.setEnvelope(envelope);
        } catch (AxisFault axisFault) {
            handleError("Error in setting the message payload : " + message);
        }
        if (soapAction != null) {
            axis2MsgCtx.setSoapAction(soapAction);
        }
        try {
            if (log.isDebugEnabled()) {
                log.debug("injecting message to proxy service : " + proxyName);
            }
            AxisEngine.receive(axis2MsgCtx);
        } catch (AxisFault axisFault) {
            handleError("Error occurred while invoking proxy service : " + proxyName);
        }
    } else {
        MessageContext mc = synapseEnvironment.createMessageContext();
        mc.setMessageID(UIDGenerator.generateURNString());
        mc.pushFaultHandler(new MediatorFaultHandler(mc.getFaultSequence()));
        if (to != null) {
            mc.setTo(new EndpointReference(to));
        }
        if (registryKey == null) {
            if (format == null) {
                PayloadHelper.setXMLPayload(mc, message.cloneOMElement());
            } else {
                try {
                    if (SOAP11_FORMAT.equalsIgnoreCase(format)) {
                        mc.setEnvelope(OMAbstractFactory.getSOAP11Factory().createSOAPEnvelope());
                    } else if (SOAP12_FORMAT.equalsIgnoreCase(format)) {
                        mc.setEnvelope(OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope());
                    } else if (POX_FORMAT.equalsIgnoreCase(format)) {
                        mc.setDoingPOX(true);
                    } else if (GET_FORMAT.equalsIgnoreCase(format)) {
                        mc.setDoingGET(true);
                    }
                    PayloadHelper.setXMLPayload(mc, message.cloneOMElement());
                } catch (AxisFault axisFault) {
                    handleError("Error in setting the message payload : " + message);
                }
            }
        } else {
            Object entry = mc.getEntry(registryKey);
            if (entry == null) {
                handleError("Key " + registryKey + " not found ");
            }
            String text = "";
            if (entry instanceof OMElement) {
                OMElement e = (OMElement) entry;
                removeIndentations(e);
                text = e.toString();
            } else if (entry instanceof OMText) {
                text = ((OMText) entry).getText();
            } else if (entry instanceof String) {
                text = (String) entry;
            }
            OMElement omXML = null;
            try {
                omXML = AXIOMUtil.stringToOM(text);
                if (format == null) {
                    PayloadHelper.setXMLPayload(mc, omXML);
                } else {
                    if (SOAP11_FORMAT.equalsIgnoreCase(format)) {
                        mc.setEnvelope(OMAbstractFactory.getSOAP11Factory().createSOAPEnvelope());
                    } else if (SOAP12_FORMAT.equalsIgnoreCase(format)) {
                        mc.setEnvelope(OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope());
                    } else if (POX_FORMAT.equalsIgnoreCase(format)) {
                        mc.setDoingPOX(true);
                    } else if (GET_FORMAT.equalsIgnoreCase(format)) {
                        mc.setDoingGET(true);
                    }
                    PayloadHelper.setXMLPayload(mc, omXML);
                }
            } catch (XMLStreamException e) {
                handleError("Error parsing XML for JSON conversion, please check your property values return valid XML");
            } catch (AxisFault axisFault) {
                handleError("Error in setting the message payload : " + omXML);
            }
        }
        if (soapAction != null) {
            mc.setSoapAction(soapAction);
        }
        // Adding runtime properties to SynapseMessageContext, if exists
        if (runtimeProperties != null && runtimeProperties.size() > 0) {
            for (Map.Entry<String, Object> entry : runtimeProperties.entrySet()) {
                mc.setProperty(entry.getKey(), entry.getValue());
            }
        }
        if (INJECT_TO_SEQUENCE.equalsIgnoreCase(injectTo)) {
            if (sequenceName == null || sequenceName.equals("")) {
                handleError("Sequence name not specified");
            }
            SequenceMediator seq = (SequenceMediator) synapseEnvironment.getSynapseConfiguration().getSequence(sequenceName);
            if (seq != null) {
                if (log.isDebugEnabled()) {
                    log.debug("injecting message to sequence : " + sequenceName);
                }
                mc.pushFaultHandler(new MediatorFaultHandler(mc.getFaultSequence()));
                synapseEnvironment.injectAsync(mc, seq);
            } else {
                handleError("Sequence: " + sequenceName + " not found");
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("injecting message to main sequence");
            }
            synapseEnvironment.injectMessage(mc);
        }
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) MediatorFaultHandler(org.apache.synapse.mediators.MediatorFaultHandler) AxisService(org.apache.axis2.description.AxisService) OMElement(org.apache.axiom.om.OMElement) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) EndpointReference(org.apache.axis2.addressing.EndpointReference) Axis2SynapseEnvironment(org.apache.synapse.core.axis2.Axis2SynapseEnvironment) XMLStreamException(javax.xml.stream.XMLStreamException) OMText(org.apache.axiom.om.OMText) MessageContext(org.apache.synapse.MessageContext) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) HashMap(java.util.HashMap) Map(java.util.Map)

Example 73 with OMText

use of org.apache.axiom.om.OMText in project wso2-synapse by wso2.

the class PayloadHelper method getBinaryPayload.

// Binary Payload is carried in a wrapper element with QName BINARYELT
public static DataHandler getBinaryPayload(SOAPEnvelope envelope) {
    OMElement el = getXMLPayload(envelope);
    if (el == null)
        return null;
    if (!el.getQName().equals(BINARYELT)) {
        log.error("Wrong QName" + el.getQName());
        return null;
    }
    OMNode textNode = el.getFirstOMChild();
    if (textNode.getType() != OMNode.TEXT_NODE) {
        log.error("Text Node not found");
        return null;
    }
    OMText text = (OMText) textNode;
    try {
        return (DataHandler) text.getDataHandler();
    } catch (ClassCastException ce) {
        log.error("cannot get DataHandler" + ce.getMessage());
        return null;
    }
}
Also used : OMNode(org.apache.axiom.om.OMNode) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement) DataHandler(javax.activation.DataHandler)

Example 74 with OMText

use of org.apache.axiom.om.OMText in project wso2-synapse by wso2.

the class PayloadHelper method setTextPayload.

public static void setTextPayload(SOAPEnvelope envelope, String text) {
    OMFactory fac = envelope.getOMFactory();
    OMElement textElt = envelope.getOMFactory().createOMElement(TEXTELT);
    OMText textNode = fac.createOMText(text);
    textElt.addChild(textNode);
    setXMLPayload(envelope, textElt);
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement)

Example 75 with OMText

use of org.apache.axiom.om.OMText in project wso2-synapse by wso2.

the class PayloadHelper method setBinaryPayload.

public static void setBinaryPayload(SOAPEnvelope envelope, DataHandler dh) {
    OMFactory fac = envelope.getOMFactory();
    OMElement binaryElt = envelope.getOMFactory().createOMElement(BINARYELT);
    OMText text = fac.createOMText(dh, true);
    binaryElt.addChild(text);
    setXMLPayload(envelope, binaryElt);
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement)

Aggregations

OMText (org.apache.axiom.om.OMText)92 OMElement (org.apache.axiom.om.OMElement)62 OMFactory (org.apache.axiom.om.OMFactory)39 DataHandler (javax.activation.DataHandler)26 OMNode (org.apache.axiom.om.OMNode)21 OMNamespace (org.apache.axiom.om.OMNamespace)10 QName (javax.xml.namespace.QName)8 OMException (org.apache.axiom.om.OMException)8 IOException (java.io.IOException)7 Iterator (java.util.Iterator)7 InputStream (java.io.InputStream)6 OMAttribute (org.apache.axiom.om.OMAttribute)6 Entry (org.apache.synapse.config.Entry)6 ArrayList (java.util.ArrayList)5 DataSource (javax.activation.DataSource)5 ByteArrayDataSource (org.apache.axiom.attachments.ByteArrayDataSource)5 OMOutputFormat (org.apache.axiom.om.OMOutputFormat)5 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)5 OutputStream (java.io.OutputStream)4 StringReader (java.io.StringReader)4