Search in sources :

Example 1 with ContentType

use of org.apache.axiom.mime.ContentType in project webservices-axiom by apache.

the class AttachmentsTest method testGetRootPartContentID.

private void testGetRootPartContentID(String contentTypeStartParam, String contentId) throws Exception {
    MimeMessage message = new MimeMessage((Session) null);
    MimeMultipart mp = new MimeMultipart("related");
    MimeBodyPart rootPart = new MimeBodyPart();
    rootPart.setText("<root/>", "utf-8", "xml");
    rootPart.addHeader("Content-Transfer-Encoding", "binary");
    rootPart.addHeader("Content-ID", "<" + contentId + ">");
    mp.addBodyPart(rootPart);
    message.setContent(mp);
    message.saveChanges();
    MemoryBlob blob = Blobs.createMemoryBlob();
    OutputStream out = blob.getOutputStream();
    mp.writeTo(out);
    out.close();
    Attachments attachments = new Attachments(blob.getInputStream(), new ContentType(message.getContentType()).toBuilder().setParameter("start", contentTypeStartParam).build().toString());
    assertEquals("Did not obtain correct content ID", contentId, attachments.getRootPartContentID());
}
Also used : ContentType(org.apache.axiom.mime.ContentType) 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) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 2 with ContentType

use of org.apache.axiom.mime.ContentType 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 3 with ContentType

use of org.apache.axiom.mime.ContentType in project webservices-axiom by apache.

the class Attachments method getAttachmentSpecType.

/**
     * Identify the type of message (MTOM or SOAP with attachments) represented by this object. Note
     * that this method is only meaningful if the instance was created from a stream.
     * 
     * @return One of the {@link MTOMConstants#MTOM_TYPE}, {@link MTOMConstants#SWA_TYPE} or
     *         {@link MTOMConstants#SWA_TYPE_12} constants.
     * @throws OMException
     *             if the message doesn't have one of the supported types (i.e. is neither MTOM nor
     *             SOAP with attachments) or if the instance was not created from a stream
     */
public String getAttachmentSpecType() {
    if (this.applicationType == null) {
        ContentType contentType = delegate.getContentType();
        if (contentType == null) {
            throw new OMException("Unable to determine the attachment spec type because the " + "Attachments object doesn't have a known content type");
        }
        applicationType = contentType.getParameter("type");
        if ((MTOMConstants.MTOM_TYPE).equalsIgnoreCase(applicationType)) {
            this.applicationType = MTOMConstants.MTOM_TYPE;
        } else if ((MTOMConstants.SWA_TYPE).equalsIgnoreCase(applicationType)) {
            this.applicationType = MTOMConstants.SWA_TYPE;
        } else if ((MTOMConstants.SWA_TYPE_12).equalsIgnoreCase(applicationType)) {
            this.applicationType = MTOMConstants.SWA_TYPE_12;
        } else {
            throw new OMException("Invalid Application type. Support available for MTOM & SwA only.");
        }
    }
    return this.applicationType;
}
Also used : ContentType(org.apache.axiom.mime.ContentType) OMException(org.apache.axiom.om.OMException)

Aggregations

ContentType (org.apache.axiom.mime.ContentType)3 OutputStream (java.io.OutputStream)2 MimeBodyPart (javax.mail.internet.MimeBodyPart)2 MimeMessage (javax.mail.internet.MimeMessage)2 MimeMultipart (javax.mail.internet.MimeMultipart)2 MemoryBlob (org.apache.axiom.blob.MemoryBlob)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 DataHandler (javax.activation.DataHandler)1 DataSource (javax.activation.DataSource)1 OMException (org.apache.axiom.om.OMException)1 SOAPProcessingException (org.apache.axiom.soap.SOAPProcessingException)1 SOAPSample (org.apache.axiom.ts.soap.SOAPSample)1 NullOutputStream (org.apache.commons.io.output.NullOutputStream)1