Search in sources :

Example 1 with Attachments

use of org.apache.axiom.attachments.Attachments in project webservices-axiom by apache.

the class MTOMStAXSOAPModelBuilderTest method testDeferredLoadingOfAttachments.

/**
     * Test that MIME parts are not loaded before requesting the DataHandlers from the corresponding
     * OMText nodes.
     *  
     * @throws Exception
     */
public void testDeferredLoadingOfAttachments() throws Exception {
    Attachments attachments = createAttachmentsForTestMTOMMessage();
    SOAPModelBuilder builder = OMXMLBuilderFactory.createSOAPModelBuilder(attachments);
    OMDocument doc = builder.getDocument();
    // Find all the binary nodes
    List<OMText> binaryNodes = new ArrayList<>();
    for (Iterator<OMSerializable> it = doc.getDescendants(false); it.hasNext(); ) {
        OMSerializable node = it.next();
        if (node instanceof OMText) {
            OMText text = (OMText) node;
            if (text.isBinary()) {
                binaryNodes.add(text);
            }
        }
    }
    assertFalse(binaryNodes.isEmpty());
    // At this moment only the SOAP part should have been loaded
    assertEquals(1, attachments.getContentIDList().size());
    for (OMText node : binaryNodes) {
        // Request the DataHandler and do something with it to make sure
        // the part is loaded
        node.getDataHandler().getInputStream().close();
    }
    assertEquals(binaryNodes.size() + 1, attachments.getContentIDList().size());
}
Also used : ArrayList(java.util.ArrayList) OMText(org.apache.axiom.om.OMText) OMSerializable(org.apache.axiom.om.OMSerializable) SOAPModelBuilder(org.apache.axiom.soap.SOAPModelBuilder) Attachments(org.apache.axiom.attachments.Attachments) OMDocument(org.apache.axiom.om.OMDocument)

Example 2 with Attachments

use of org.apache.axiom.attachments.Attachments in project webservices-axiom by apache.

the class XOPEncodingStreamReaderTest method test.

public void test() throws Exception {
    Attachments[] attachments = new Attachments[2];
    XMLStreamReader[] soapPartReader = new XMLStreamReader[2];
    for (int i = 0; i < 2; i++) {
        attachments[i] = new Attachments(MTOMSample.SAMPLE1.getInputStream(), MTOMSample.SAMPLE1.getContentType());
        soapPartReader[i] = StAXUtils.createXMLStreamReader(attachments[i].getRootPartInputStream());
    }
    XMLStreamReader actual = new XOPEncodingStreamReader(new XOPDecodingStreamReader(soapPartReader[1], new AttachmentsMimePartProvider(attachments[1])), contentIDGenerator, OptimizationPolicy.DEFAULT);
    new XMLStreamReaderComparator(soapPartReader[0], actual).compare();
    for (int i = 0; i < 2; i++) {
        soapPartReader[i].close();
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamReaderComparator(org.apache.axiom.testutils.stax.XMLStreamReaderComparator) AttachmentsMimePartProvider(org.apache.axiom.om.impl.builder.AttachmentsMimePartProvider) Attachments(org.apache.axiom.attachments.Attachments)

Example 3 with Attachments

use of org.apache.axiom.attachments.Attachments in project webservices-axiom by apache.

the class MTOMStAXSOAPModelBuilderTest method testUTF16MTOMMessage.

public void testUTF16MTOMMessage() throws Exception {
    String contentTypeString = "multipart/Related; charset=\"UTF-8\"; type=\"application/xop+xml\"; boundary=\"----=_AxIs2_Def_boundary_=42214532\"; start=\"SOAPPart\"";
    String cid = "1.urn:uuid:A3ADBAEE51A1A87B2A11443668160994@apache.org";
    String xmlPlusMime1 = "------=_AxIs2_Def_boundary_=42214532\r\n" + "Content-Type: application/xop+xml; charset=UTF-16; type=\"application/soap+xml\"\r\n" + "Content-Transfer-Encoding: 8bit\r\n" + "Content-ID: SOAPPart\r\n" + "\r\n";
    String xmlPlusMime2 = "<soapenv:Envelope xmlns:soapenv=\"http://www.w3.org/2003/05/soap-envelope\"><soapenv:Body><m:data xmlns:m=\"http://www.example.org/stuff\"><m:name m:contentType=\"text/plain\"><xop:Include xmlns:xop=\"http://www.w3.org/2004/08/xop/include\" href=\"cid:" + cid + "\"></xop:Include></m:name></m:data></soapenv:Body></soapenv:Envelope>\r\n";
    String xmlPlusMime3 = "\r\n------=_AxIs2_Def_boundary_=42214532\r\n" + "Content-Transfer-Encoding: binary\r\n" + "Content-ID: " + cid + "\r\n" + "\r\n" + "Foo Bar\r\n" + "------=_AxIs2_Def_boundary_=42214532--\r\n";
    byte[] bytes1 = xmlPlusMime1.getBytes();
    byte[] bytes2 = xmlPlusMime2.getBytes("UTF-16");
    byte[] bytes3 = xmlPlusMime3.getBytes();
    byte[] full = append(bytes1, bytes2);
    full = append(full, bytes3);
    InputStream inStream = new BufferedInputStream(new ByteArrayInputStream(full));
    Attachments attachments = new Attachments(inStream, contentTypeString);
    SOAPModelBuilder builder = OMXMLBuilderFactory.createSOAPModelBuilder(attachments);
    OMElement root = builder.getDocumentElement();
    root.build();
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OMElement(org.apache.axiom.om.OMElement) SOAPModelBuilder(org.apache.axiom.soap.SOAPModelBuilder) Attachments(org.apache.axiom.attachments.Attachments)

Aggregations

Attachments (org.apache.axiom.attachments.Attachments)3 SOAPModelBuilder (org.apache.axiom.soap.SOAPModelBuilder)2 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 OMDocument (org.apache.axiom.om.OMDocument)1 OMElement (org.apache.axiom.om.OMElement)1 OMSerializable (org.apache.axiom.om.OMSerializable)1 OMText (org.apache.axiom.om.OMText)1 AttachmentsMimePartProvider (org.apache.axiom.om.impl.builder.AttachmentsMimePartProvider)1 XMLStreamReaderComparator (org.apache.axiom.testutils.stax.XMLStreamReaderComparator)1