Search in sources :

Example 11 with Attachment

use of org.apache.cxf.message.Attachment in project cxf by apache.

the class WrappedAttachments method addAll.

public boolean addAll(Collection<? extends Attachment> c) {
    boolean b = false;
    for (Iterator<? extends Attachment> it = c.iterator(); it.hasNext(); ) {
        Attachment o = it.next();
        if (!attachments.containsKey(o.getId())) {
            b = true;
            attachments.put(o.getId(), o.getDataHandler());
            cache.put(o.getId(), o);
        }
    }
    return b;
}
Also used : Attachment(org.apache.cxf.message.Attachment)

Example 12 with Attachment

use of org.apache.cxf.message.Attachment in project cxf by apache.

the class AbstractXOPType method writeObject.

@Override
public void writeObject(Object object, MessageWriter writer, Context context) throws DatabindingException {
    // add the content type attribute even if we are going to fall back.
    String contentType = getContentType(object, context);
    if (contentType != null && useXmimeBinaryType) {
        MessageWriter ctWriter = writer.getAttributeWriter(XML_MIME_CONTENT_TYPE);
        ctWriter.writeValue(contentType);
    }
    if (!context.isMtomEnabled()) {
        fallbackDelegate.writeObject(getBytes(object), writer, context);
        return;
    }
    Collection<Attachment> attachments = context.getAttachments();
    if (attachments == null) {
        attachments = new ArrayList<>();
        context.setAttachments(attachments);
    }
    String id = AttachmentUtil.createContentID(getSchemaType().getNamespaceURI());
    Attachment att = createAttachment(object, id);
    attachments.add(att);
    MessageWriter include = writer.getElementWriter(XOP_INCLUDE);
    MessageWriter href = include.getAttributeWriter(XOP_HREF);
    href.writeValue("cid:" + id);
    include.close();
}
Also used : MessageWriter(org.apache.cxf.aegis.xml.MessageWriter) Attachment(org.apache.cxf.message.Attachment)

Example 13 with Attachment

use of org.apache.cxf.message.Attachment in project cxf by apache.

the class LazyAttachmentCollection method loadAll.

private void loadAll() {
    try {
        Attachment a = deserializer.readNext();
        int count = 0;
        while (a != null) {
            attachments.add(a);
            count++;
            if (count > maxAttachmentCount) {
                throw new IOException("The message contains more attachments than are permitted");
            }
            a = deserializer.readNext();
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : Attachment(org.apache.cxf.message.Attachment) IOException(java.io.IOException)

Example 14 with Attachment

use of org.apache.cxf.message.Attachment in project cxf by apache.

the class LazyAttachmentCollection method iterator.

public Iterator<Attachment> iterator() {
    return new Iterator<Attachment>() {

        int current;

        boolean removed;

        public boolean hasNext() {
            if (attachments.size() > current) {
                return true;
            }
            // check if there is another attachment
            try {
                Attachment a = deserializer.readNext();
                if (a == null) {
                    return false;
                }
                attachments.add(a);
                return true;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public Attachment next() {
            Attachment a = attachments.get(current);
            current++;
            removed = false;
            return a;
        }

        @Override
        public void remove() {
            if (removed) {
                throw new IllegalStateException();
            }
            attachments.remove(--current);
            removed = true;
        }
    };
}
Also used : Iterator(java.util.Iterator) Attachment(org.apache.cxf.message.Attachment) IOException(java.io.IOException)

Example 15 with Attachment

use of org.apache.cxf.message.Attachment in project cxf by apache.

the class AttachmentDeserializerTest method testDeserializerMtomWithAxis2StyleBoundaries.

@Test
public void testDeserializerMtomWithAxis2StyleBoundaries() throws Exception {
    InputStream is = getClass().getResourceAsStream("axis2_mimedata");
    String ct = "multipart/related; type=\"application/xop+xml\"; " + "start=\"<soap.xml@xfire.codehaus.org>\"; " + "start-info=\"text/xml; charset=utf-8\"; " + "boundary=MIMEBoundaryurn_uuid_6BC4984D5D38EB283C1177616488109";
    msg.put(Message.CONTENT_TYPE, ct);
    msg.setContent(InputStream.class, is);
    AttachmentDeserializer deserializer = new AttachmentDeserializer(msg);
    deserializer.initializeAttachments();
    InputStream attBody = msg.getContent(InputStream.class);
    assertTrue(attBody != is);
    assertTrue(attBody instanceof DelegatingInputStream);
    Collection<Attachment> atts = msg.getAttachments();
    assertNotNull(atts);
    Iterator<Attachment> itr = atts.iterator();
    assertTrue(itr.hasNext());
    Attachment a = itr.next();
    assertNotNull(a);
    InputStream attIs = a.getDataHandler().getInputStream();
    // check the cached output stream
    try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        IOUtils.copy(attBody, out);
        assertTrue(out.toString().startsWith("<env:Envelope"));
    }
    // try streaming a character off the wire
    assertEquals(255, attIs.read());
    assertEquals(216, attIs.read());
    // Attachment invalid = atts.get("INVALID");
    // assertNull(invalid.getDataHandler().getInputStream());
    // 
    // assertTrue(attIs instanceof ByteArrayInputStream);
    is.close();
}
Also used : PushbackInputStream(java.io.PushbackInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Attachment(org.apache.cxf.message.Attachment) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

Attachment (org.apache.cxf.message.Attachment)51 DataHandler (javax.activation.DataHandler)18 Test (org.junit.Test)18 InputStream (java.io.InputStream)14 Message (org.apache.cxf.message.Message)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 AttachmentImpl (org.apache.cxf.attachment.AttachmentImpl)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)11 MessageImpl (org.apache.cxf.message.MessageImpl)11 HashMap (java.util.HashMap)10 List (java.util.List)9 PushbackInputStream (java.io.PushbackInputStream)7 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)6 Fault (org.apache.cxf.interceptor.Fault)6 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)6 Map (java.util.Map)5 OutputStream (java.io.OutputStream)4 TreeMap (java.util.TreeMap)4