Search in sources :

Example 1 with OMText

use of org.apache.axiom.om.OMText in project tdi-studio-se by Talend.

the class MSCRMClient method createCRMSecurityHeaderBlock.

private static SOAPHeaderBlock createCRMSecurityHeaderBlock(SecurityData securityData) throws XMLStreamException {
    RequestDateTimeData dateTimeData = WsdlTokenManager.getRequestDateTime();
    String currentDateTime = dateTimeData.getCreatedDateTime();
    String expireDateTime = dateTimeData.getExpiresDateTime();
    String securityHeaderTemplate = "<EncryptedData " + "    xmlns=\"http://www.w3.org/2001/04/xmlenc#\"" + "     Id=\"Assertion0\" " + "    Type=\"http://www.w3.org/2001/04/xmlenc#Element\">" + "    <EncryptionMethod " + "        Algorithm=\"http://www.w3.org/2001/04/xmlenc#tripledes-cbc\"/>" + "    <ds:KeyInfo " + "        xmlns:ds=\"http://www.w3.org/2000/09/xmldsig#\">" + "        <EncryptedKey>" + "            <EncryptionMethod " + "                Algorithm=\"http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p\"/>" + "            <ds:KeyInfo Id=\"keyinfo\">" + "                <wsse:SecurityTokenReference " + "                    xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">" + "                    <wsse:KeyIdentifier " + "                        EncodingType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary\" " + "                        ValueType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509SubjectKeyIdentifier\">%s</wsse:KeyIdentifier>" + "                </wsse:SecurityTokenReference>" + "            </ds:KeyInfo>" + "            <CipherData>" + "                <CipherValue>%s</CipherValue>" + "            </CipherData>" + "        </EncryptedKey>" + "    </ds:KeyInfo>" + "    <CipherData>" + "        <CipherValue>%s</CipherValue>" + "    </CipherData>" + "</EncryptedData>";
    String securityHeader = String.format(securityHeaderTemplate, securityData.getKeyIdentifier(), securityData.getSecurityToken0(), securityData.getSecurityToken1());
    try {
        OMFactory factory = OMAbstractFactory.getOMFactory();
        OMNamespace securityNS = factory.createOMNamespace("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "o");
        OMNamespace utitlityNS = factory.createOMNamespace("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "u");
        OMElement timeStamp = factory.createOMElement("Timestamp", utitlityNS);
        timeStamp.addAttribute("Id", "_0", utitlityNS);
        OMElement created = factory.createOMElement("Created", utitlityNS);
        OMText createdTime = factory.createOMText(currentDateTime + "Z");
        created.addChild(createdTime);
        OMElement expires = factory.createOMElement("Expires", utitlityNS);
        OMText expiresTime = factory.createOMText(expireDateTime + "Z");
        expires.addChild(expiresTime);
        timeStamp.addChild(created);
        timeStamp.addChild(expires);
        SOAPHeaderBlock wsseHeader = OMAbstractFactory.getSOAP12Factory().createSOAPHeaderBlock("Security", securityNS);
        wsseHeader.setMustUnderstand(true);
        wsseHeader.addChild(timeStamp);
        wsseHeader.addChild(AXIOMUtil.stringToOM(factory, securityHeader));
        return wsseHeader;
    } catch (XMLStreamException e) {
        logger.error(e.getMessage());
        throw e;
    }
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMNamespace(org.apache.axiom.om.OMNamespace) RequestDateTimeData(org.talend.ms.crm.sdk.RequestDateTimeData) XMLStreamException(javax.xml.stream.XMLStreamException) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement) SOAPHeaderBlock(org.apache.axiom.soap.SOAPHeaderBlock)

Example 2 with OMText

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

the class OMElementTest method testAddLLOMTextChildToDOOM.

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

Example 3 with OMText

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

the class OMElementTest method testChildDetachment.

public void testChildDetachment() {
    OMNamespace testNamespace2 = factory.createOMNamespace("ftp://anotherTest.ws.org", "ws");
    secondElement.detach();
    assertTrue("OMElement children detachment has not worked properly", !secondElement.equals(firstElement.getFirstElement()));
    assertNull("First Element should not contain elements after detaching. ", firstElement.getFirstElement());
    assertNull("First Element should not contain elements after detaching. ", firstElement.getFirstOMChild());
    assertNull(secondElement.findNamespace(testNamespace2.getNamespaceURI(), testNamespace2.getPrefix()));
    firstElement.addChild(secondElement);
    factory.createOMText(firstElement, "Some Sample Text");
    assertTrue("First added child must be the first child", secondElement.equals(firstElement.getFirstOMChild()));
    Iterator children = firstElement.getChildren();
    int childCount = 0;
    while (children.hasNext()) {
        children.next();
        childCount++;
    }
    assertEquals("Children count should be two", childCount, 2);
    secondElement.detach();
    assertTrue("First child should be the text child", firstElement.getFirstOMChild() instanceof OMText);
}
Also used : OMNamespace(org.apache.axiom.om.OMNamespace) Iterator(java.util.Iterator) OMText(org.apache.axiom.om.OMText)

Example 4 with OMText

use of org.apache.axiom.om.OMText 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 5 with OMText

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

the class MTOMSample method retrieveContent.

// START SNIPPET: retrieveContent
public void retrieveContent(URL serviceURL, String id, OutputStream result) throws Exception {
    // Build the SOAP request
    SOAPFactory soapFactory = OMAbstractFactory.getSOAP11Factory();
    SOAPEnvelope request = soapFactory.getDefaultEnvelope();
    OMElement retrieveContent = soapFactory.createOMElement(new QName("urn:test", "retrieveContent"), request.getBody());
    OMElement fileId = soapFactory.createOMElement(new QName("fileId"), retrieveContent);
    fileId.setText(id);
    // Use the java.net.URL API to connect to the service
    URLConnection connection = serviceURL.openConnection();
    connection.setDoOutput(true);
    connection.addRequestProperty("Content-Type", "text/xml; charset=UTF-8");
    OutputStream out = connection.getOutputStream();
    // Send the request
    OMOutputFormat format = new OMOutputFormat();
    format.setCharSetEncoding("UTF-8");
    request.serialize(out, format);
    out.close();
    // Get the SOAP response
    InputStream in = connection.getInputStream();
    MultipartBody multipartBody = MultipartBody.builder().setInputStream(in).setContentType(connection.getContentType()).build();
    SOAPEnvelope response = OMXMLBuilderFactory.createSOAPModelBuilder(multipartBody).getSOAPEnvelope();
    OMElement retrieveContentResponse = response.getBody().getFirstElement();
    OMElement content = retrieveContentResponse.getFirstElement();
    // Extract the DataHandler representing the optimized binary data
    DataHandler dh = ((OMText) content.getFirstOMChild()).getDataHandler();
    // Stream the content of the MIME part
    InputStream contentStream = ((PartDataHandler) dh).getPart().getInputStream(false);
    // Write the content to the result stream
    IOUtils.copy(contentStream, result);
    contentStream.close();
    in.close();
}
Also used : QName(javax.xml.namespace.QName) InputStream(java.io.InputStream) MultipartBody(org.apache.axiom.mime.MultipartBody) OutputStream(java.io.OutputStream) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) DataHandler(javax.activation.DataHandler) PartDataHandler(org.apache.axiom.mime.PartDataHandler) OMOutputFormat(org.apache.axiom.om.OMOutputFormat) SOAPFactory(org.apache.axiom.soap.SOAPFactory) URLConnection(java.net.URLConnection)

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