Search in sources :

Example 6 with MemoryBlob

use of org.apache.axiom.blob.MemoryBlob in project webservices-axiom by apache.

the class TestRegisterCustomBuilderForPayloadJAXBWithXOP method runTest.

@Override
protected void runTest() throws Throwable {
    DataHandler dh = new DataHandler(new RandomDataSource(10000));
    MemoryBlob blob = Blobs.createMemoryBlob();
    OutputStream out = blob.getOutputStream();
    OMOutputFormat format = new OMOutputFormat();
    format.setDoOptimize(true);
    createTestDocument(dh).serialize(out, format);
    out.close();
    MultipartBody mb = MultipartBody.builder().setInputStream(blob.getInputStream()).setContentType(format.getContentType()).build();
    test(dh, OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), StAXParserConfiguration.DEFAULT, mb), false);
}
Also used : RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) MemoryBlob(org.apache.axiom.blob.MemoryBlob) MultipartBody(org.apache.axiom.mime.MultipartBody) OutputStream(java.io.OutputStream) DataHandler(javax.activation.DataHandler) OMOutputFormat(org.apache.axiom.om.OMOutputFormat)

Example 7 with MemoryBlob

use of org.apache.axiom.blob.MemoryBlob in project webservices-axiom by apache.

the class TestCreateSOAPModelBuilderMTOMContentTypeMismatch method runTest.

@Override
protected void runTest() throws Throwable {
    final SOAPSample sample = SOAPSampleSet.NO_HEADER.getMessage(spec);
    // Generate an MTOM message with the wrong content type
    MimeMessage message = new MimeMessage((Session) null);
    MimeMultipart mp = new MimeMultipart("related");
    MimeBodyPart bp = new MimeBodyPart();
    String contentID = "<" + UIDGenerator.generateContentId() + ">";
    bp.setDataHandler(new DataHandler(new DataSource() {

        @Override
        public String getContentType() {
            return "application/xop+xml; charset=\"" + sample.getEncoding() + "\"; type=\"" + spec.getAltSpec().getContentType() + "\"";
        }

        @Override
        public InputStream getInputStream() throws IOException {
            return sample.getInputStream();
        }

        @Override
        public String getName() {
            return null;
        }

        @Override
        public OutputStream getOutputStream() {
            throw new UnsupportedOperationException();
        }
    }));
    bp.addHeader("Content-Transfer-Encoding", "binary");
    bp.addHeader("Content-ID", contentID);
    mp.addBodyPart(bp);
    message.setContent(mp);
    message.saveChanges();
    ContentType contentType = new ContentType(message.getContentType()).toBuilder().setParameter("type", "application/xop+xml").setParameter("start", contentID).setParameter("start-info", spec.getAltSpec().getContentType()).build();
    MemoryBlob blob = Blobs.createMemoryBlob();
    OutputStream out = blob.getOutputStream();
    mp.writeTo(out);
    out.close();
    // Now attempt to create an Axiom builder
    try {
        OMXMLBuilderFactory.createSOAPModelBuilder(metaFactory, MultipartBody.builder().setInputStream(blob.getInputStream()).setContentType(contentType).build());
        fail("Expected SOAPProcessingException");
    } catch (SOAPProcessingException ex) {
    // Expected
    }
}
Also used : ContentType(org.apache.axiom.mime.ContentType) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MemoryBlob(org.apache.axiom.blob.MemoryBlob) OutputStream(java.io.OutputStream) SOAPProcessingException(org.apache.axiom.soap.SOAPProcessingException) DataHandler(javax.activation.DataHandler) SOAPSample(org.apache.axiom.ts.soap.SOAPSample) MimeBodyPart(javax.mail.internet.MimeBodyPart) DataSource(javax.activation.DataSource)

Example 8 with MemoryBlob

use of org.apache.axiom.blob.MemoryBlob in project webservices-axiom by apache.

the class DetachableInputStream method detach.

@Override
public void detach() throws OMException {
    MemoryBlob blob = Blobs.createMemoryBlob();
    try {
        blob.readFrom(target);
    } catch (StreamCopyException ex) {
        throw new OMException(ex.getCause());
    }
    if (closeOnDetach) {
        try {
            target.close();
        } catch (IOException ex) {
            throw new OMException(ex);
        }
    }
    target = blob.readOnce();
}
Also used : MemoryBlob(org.apache.axiom.blob.MemoryBlob) StreamCopyException(org.apache.axiom.ext.io.StreamCopyException) IOException(java.io.IOException) OMException(org.apache.axiom.om.OMException)

Example 9 with MemoryBlob

use of org.apache.axiom.blob.MemoryBlob in project webservices-axiom by apache.

the class DetachableReader method detach.

@Override
public void detach() {
    MemoryBlob blob = Blobs.createMemoryBlob();
    Writer out = new OutputStreamWriter(blob.getOutputStream(), UTF8);
    char[] buffer = new char[2048];
    int c;
    try {
        while ((c = target.read(buffer)) != -1) {
            out.write(buffer, 0, c);
        }
        out.close();
    } catch (IOException ex) {
        throw new OMException(ex);
    }
    target = new InputStreamReader(blob.readOnce(), UTF8);
}
Also used : InputStreamReader(java.io.InputStreamReader) MemoryBlob(org.apache.axiom.blob.MemoryBlob) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) OMException(org.apache.axiom.om.OMException) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 10 with MemoryBlob

use of org.apache.axiom.blob.MemoryBlob in project webservices-axiom by apache.

the class AttachmentsTest method testFakeRootPartContentID.

/**
     * Tests access to a root part that doesn't have a content ID. In this case, the
     * {@link Attachments} API generates a fake content ID.
     * 
     * @throws Exception
     */
public void testFakeRootPartContentID() throws Exception {
    MimeMessage message = new MimeMessage((Session) null);
    MimeMultipart mp = new MimeMultipart("related");
    MimeBodyPart bp1 = new MimeBodyPart();
    bp1.setText("<root/>", "utf-8", "xml");
    bp1.addHeader("Content-Transfer-Encoding", "binary");
    mp.addBodyPart(bp1);
    MimeBodyPart bp2 = new MimeBodyPart();
    bp2.setDataHandler(new DataHandler("Test", "text/plain"));
    bp2.addHeader("Content-Transfer-Encoding", "binary");
    mp.addBodyPart(bp2);
    message.setContent(mp);
    message.saveChanges();
    MemoryBlob blob = Blobs.createMemoryBlob();
    OutputStream out = blob.getOutputStream();
    mp.writeTo(out);
    out.close();
    String contentType = message.getContentType();
    Attachments attachments = new Attachments(blob.getInputStream(), contentType);
    String rootPartContentID = attachments.getRootPartContentID();
    assertThat(rootPartContentID).isNotNull();
    DataHandler rootPart = attachments.getDataHandler(rootPartContentID);
    assertThat(rootPart.getContent()).isEqualTo("<root/>");
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MemoryBlob(org.apache.axiom.blob.MemoryBlob) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) PipedOutputStream(java.io.PipedOutputStream) NullOutputStream(org.apache.commons.io.output.NullOutputStream) DataHandler(javax.activation.DataHandler) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Aggregations

MemoryBlob (org.apache.axiom.blob.MemoryBlob)12 OutputStream (java.io.OutputStream)9 DataHandler (javax.activation.DataHandler)7 IOException (java.io.IOException)3 MimeBodyPart (javax.mail.internet.MimeBodyPart)3 MimeMessage (javax.mail.internet.MimeMessage)3 MimeMultipart (javax.mail.internet.MimeMultipart)3 OMElement (org.apache.axiom.om.OMElement)3 OMOutputFormat (org.apache.axiom.om.OMOutputFormat)3 RandomDataSource (org.apache.axiom.testutils.activation.RandomDataSource)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 PipedOutputStream (java.io.PipedOutputStream)2 Writer (java.io.Writer)2 DataSource (javax.activation.DataSource)2 QName (javax.xml.namespace.QName)2 ContentType (org.apache.axiom.mime.ContentType)2 MultipartBody (org.apache.axiom.mime.MultipartBody)2 OMException (org.apache.axiom.om.OMException)2 OMText (org.apache.axiom.om.OMText)2 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)2