Search in sources :

Example 61 with OMText

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

the class OMElementTest method testAddDOOMTextAsChild.

public void testAddDOOMTextAsChild() throws XMLStreamException {
    OMFactory doomFactory = OMAbstractFactory.getMetaFactory(OMAbstractFactory.FEATURE_DOM).getOMFactory();
    OMFactory llomFactory = OMAbstractFactory.getOMFactory();
    String text = "This was a DOOM Text";
    OMElement llomRoot = llomFactory.createOMElement("root", null);
    OMText doomText = doomFactory.createOMText(text);
    llomRoot.addChild(doomText);
    OMElement newElement = (OMXMLBuilderFactory.createStAXOMBuilder(this.factory, llomRoot.getXMLStreamReader())).getDocumentElement();
    newElement.build();
    assertEquals(newElement.getText(), text);
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement)

Example 62 with OMText

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

the class XOPRoundtripTest method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    DataHandler dh = new DataHandler(new TestDataSource('x', Runtime.getRuntime().maxMemory()));
    OMElement element1 = factory.createOMElement(new QName("test"));
    element1.addChild(factory.createOMText(dh, true));
    XOPEncoded<XMLStreamReader> xopEncodedStream = element1.getXOPEncodedStreamReader(true);
    OMElement element2 = OMXMLBuilderFactory.createOMBuilder(factory, new StAXSource(xopEncodedStream.getRootPart()), xopEncodedStream.getAttachmentAccessor()).getDocumentElement();
    OMText child = (OMText) element2.getFirstOMChild();
    assertNotNull(child);
    assertTrue(child.isBinary());
    assertTrue(child.isOptimized());
    assertSame(dh, child.getDataHandler());
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) XMLStreamReader(javax.xml.stream.XMLStreamReader) QName(javax.xml.namespace.QName) TestDataSource(org.apache.axiom.testutils.activation.TestDataSource) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement) DataHandler(javax.activation.DataHandler) StAXSource(javax.xml.transform.stax.StAXSource)

Example 63 with OMText

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

the class TestCreateOMTextCDATASection method runTest.

@Override
protected void runTest() throws Throwable {
    OMText text = metaFactory.getOMFactory().createOMText("cdata section content", OMNode.CDATA_SECTION_NODE);
    assertTrue(text instanceof CDATASection);
}
Also used : CDATASection(org.w3c.dom.CDATASection) OMText(org.apache.axiom.om.OMText)

Example 64 with OMText

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

the class Echo method echoMTOMtoBase64.

public OMElement echoMTOMtoBase64(OMElement omEle) {
    OMText omText = (OMText) (omEle.getFirstElement()).getFirstOMChild();
    omText.setOptimize(false);
    return omEle;
}
Also used : OMText(org.apache.axiom.om.OMText)

Example 65 with OMText

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

the class JMSSender method createJMSMessage.

/**
 * Create a JMS Message from the given MessageContext and using the given
 * session
 *
 * @param msgContext the MessageContext
 * @param session    the JMS session
 * @param contentTypeProperty the message property to be used to store the
 *                            content type
 * @return a JMS message from the context and session
 * @throws JMSException on exception
 * @throws AxisFault on exception
 */
private Message createJMSMessage(MessageContext msgContext, Session session, String contentTypeProperty) throws JMSException, AxisFault {
    Message message = null;
    String msgType = getProperty(msgContext, JMSConstants.JMS_MESSAGE_TYPE);
    // check the first element of the SOAP body, do we have content wrapped using the
    // default wrapper elements for binary (BaseConstants.DEFAULT_BINARY_WRAPPER) or
    // text (BaseConstants.DEFAULT_TEXT_WRAPPER) ? If so, do not create SOAP messages
    // for JMS but just get the payload in its native format
    String jmsPayloadType = guessMessageType(msgContext);
    if (jmsPayloadType == null) {
        OMOutputFormat format = BaseUtils.getOMOutputFormat(msgContext);
        MessageFormatter messageFormatter = null;
        try {
            messageFormatter = MessageProcessorSelector.getMessageFormatter(msgContext);
        } catch (AxisFault axisFault) {
            throw new JMSException("Unable to get the message formatter to use");
        }
        String contentType = messageFormatter.getContentType(msgContext, format, msgContext.getSoapAction());
        boolean useBytesMessage = msgType != null && JMSConstants.JMS_BYTE_MESSAGE.equals(msgType) || contentType.indexOf(HTTPConstants.HEADER_ACCEPT_MULTIPART_RELATED) > -1;
        OutputStream out;
        StringWriter sw;
        if (useBytesMessage) {
            BytesMessage bytesMsg = session.createBytesMessage();
            sw = null;
            out = new BytesMessageOutputStream(bytesMsg);
            message = bytesMsg;
        } else {
            sw = new StringWriter();
            try {
                out = new WriterOutputStream(sw, format.getCharSetEncoding());
            } catch (UnsupportedCharsetException ex) {
                handleException("Unsupported encoding " + format.getCharSetEncoding(), ex);
                return null;
            }
        }
        try {
            messageFormatter.writeTo(msgContext, format, out, true);
            out.close();
        } catch (IOException e) {
            Transaction transaction = null;
            try {
                transaction = ((TransactionManager) msgContext.getProperty(JMSConstants.JMS_XA_TRANSACTION_MANAGER)).getTransaction();
                rollbackXATransaction(transaction);
            } catch (SystemException e1) {
                handleException("Error occurred during obtaining  transaction", e1);
            }
            handleException("IO Error while creating BytesMessage", e);
        }
        if (!useBytesMessage) {
            TextMessage txtMsg = session.createTextMessage();
            txtMsg.setText(sw.toString());
            message = txtMsg;
        }
        if (contentTypeProperty != null) {
            message.setStringProperty(contentTypeProperty, contentType);
        }
    } else if (JMSConstants.JMS_BYTE_MESSAGE.equals(jmsPayloadType)) {
        message = session.createBytesMessage();
        BytesMessage bytesMsg = (BytesMessage) message;
        OMElement wrapper = msgContext.getEnvelope().getBody().getFirstChildWithName(BaseConstants.DEFAULT_BINARY_WRAPPER);
        OMNode omNode = wrapper.getFirstOMChild();
        if (omNode != null && omNode instanceof OMText) {
            Object dh = ((OMText) omNode).getDataHandler();
            if (dh != null && dh instanceof DataHandler) {
                try {
                    ((DataHandler) dh).writeTo(new BytesMessageOutputStream(bytesMsg));
                } catch (IOException e) {
                    Transaction transaction = null;
                    try {
                        transaction = ((TransactionManager) msgContext.getProperty(JMSConstants.JMS_XA_TRANSACTION_MANAGER)).getTransaction();
                        rollbackXATransaction(transaction);
                    } catch (SystemException e1) {
                        handleException("Error occurred during obtaining  transaction", e1);
                    }
                    handleException("Error serializing binary content of element : " + BaseConstants.DEFAULT_BINARY_WRAPPER, e);
                }
            }
        }
    } else if (JMSConstants.JMS_TEXT_MESSAGE.equals(jmsPayloadType)) {
        message = session.createTextMessage();
        TextMessage txtMsg = (TextMessage) message;
        txtMsg.setText(msgContext.getEnvelope().getBody().getFirstChildWithName(BaseConstants.DEFAULT_TEXT_WRAPPER).getText());
    } else if (JMSConstants.JMS_MAP_MESSAGE.equalsIgnoreCase(jmsPayloadType)) {
        message = session.createMapMessage();
        JMSUtils.convertXMLtoJMSMap(msgContext.getEnvelope().getBody().getFirstChildWithName(JMSConstants.JMS_MAP_QNAME), (MapMessage) message);
    }
    // set the JMS correlation ID if specified
    String correlationId = getProperty(msgContext, JMSConstants.JMS_COORELATION_ID);
    if (correlationId == null && msgContext.getRelatesTo() != null) {
        correlationId = msgContext.getRelatesTo().getValue();
    }
    if (correlationId != null) {
        message.setJMSCorrelationID(correlationId);
    }
    if (msgContext.isServerSide()) {
        // set SOAP Action as a property on the JMS message
        setProperty(message, msgContext, BaseConstants.SOAPACTION);
    } else {
        String action = msgContext.getOptions().getAction();
        if (action != null) {
            message.setStringProperty(BaseConstants.SOAPACTION, action);
        }
    }
    JMSUtils.setTransportHeaders(msgContext, message);
    return message;
}
Also used : AxisFault(org.apache.axis2.AxisFault) BytesMessageOutputStream(org.apache.axis2.transport.jms.iowrappers.BytesMessageOutputStream) OutputStream(java.io.OutputStream) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) BytesMessageOutputStream(org.apache.axis2.transport.jms.iowrappers.BytesMessageOutputStream) OMElement(org.apache.axiom.om.OMElement) MessageFormatter(org.apache.axis2.transport.MessageFormatter) IOException(java.io.IOException) DataHandler(javax.activation.DataHandler) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) OMNode(org.apache.axiom.om.OMNode) StringWriter(java.io.StringWriter) Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) TransactionManager(javax.transaction.TransactionManager) OMText(org.apache.axiom.om.OMText) OMOutputFormat(org.apache.axiom.om.OMOutputFormat)

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