Search in sources :

Example 6 with AttachmentImpl

use of org.apache.cxf.attachment.AttachmentImpl in project cxf by apache.

the class WrappedMessageContextTest method testPutAndGetJaxwsAttachments.

@Test
public void testPutAndGetJaxwsAttachments() throws Exception {
    WrappedMessageContext context = new WrappedMessageContext(new HashMap<String, Object>(), null, Scope.APPLICATION);
    DataHandler dh1 = new DataHandler(new ByteArrayDataSource("Hello world!".getBytes(), "text/plain"));
    DataHandler dh2 = new DataHandler(new ByteArrayDataSource("Hola mundo!".getBytes(), "text/plain"));
    DataHandler dh3 = new DataHandler(new ByteArrayDataSource("Bonjour tout le monde!".getBytes(), "text/plain"));
    Map<String, DataHandler> jattachments = new HashMap<>();
    context.put(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS, jattachments);
    jattachments.put("attachment-1", dh1);
    Set<Attachment> cattachments = CastUtils.cast((Set<?>) context.get(Message.ATTACHMENTS));
    assertNotNull(cattachments);
    assertEquals(1, cattachments.size());
    jattachments.put("attachment-2", dh2);
    assertEquals(2, cattachments.size());
    AttachmentImpl ca = new AttachmentImpl("attachment-3", dh3);
    ca.setHeader("X-test", "true");
    cattachments.add(ca);
    assertEquals(3, jattachments.size());
    assertEquals(3, cattachments.size());
    for (Attachment a : cattachments) {
        if ("attachment-1".equals(a.getId())) {
            assertEquals("Hello world!", a.getDataHandler().getContent());
        } else if ("attachment-2".equals(a.getId())) {
            assertEquals("Hola mundo!", a.getDataHandler().getContent());
        } else if ("attachment-3".equals(a.getId())) {
            assertEquals("Bonjour tout le monde!", a.getDataHandler().getContent());
            assertEquals("true", a.getHeader("X-test"));
        } else {
            fail("unknown attachment");
        }
    }
}
Also used : HashMap(java.util.HashMap) Attachment(org.apache.cxf.message.Attachment) AttachmentImpl(org.apache.cxf.attachment.AttachmentImpl) DataHandler(javax.activation.DataHandler) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) Test(org.junit.Test)

Example 7 with AttachmentImpl

use of org.apache.cxf.attachment.AttachmentImpl in project cxf by apache.

the class ByteArrayType method createAttachment.

@Override
protected Attachment createAttachment(Object object, String id) {
    byte[] data = (byte[]) object;
    ByteDataSource source = new ByteDataSource(data);
    source.setContentType(getContentType(object, null));
    AttachmentImpl att = new AttachmentImpl(id, new DataHandler(source));
    att.setXOP(true);
    return att;
}
Also used : ByteDataSource(org.apache.cxf.attachment.ByteDataSource) AttachmentImpl(org.apache.cxf.attachment.AttachmentImpl) DataHandler(javax.activation.DataHandler)

Example 8 with AttachmentImpl

use of org.apache.cxf.attachment.AttachmentImpl in project cxf by apache.

the class DataHandlerType method createAttachment.

@Override
protected Attachment createAttachment(Object object, String id) {
    DataHandler handler = (DataHandler) object;
    AttachmentImpl att = new AttachmentImpl(id, handler);
    att.setXOP(true);
    return att;
}
Also used : AttachmentImpl(org.apache.cxf.attachment.AttachmentImpl) DataHandler(javax.activation.DataHandler)

Example 9 with AttachmentImpl

use of org.apache.cxf.attachment.AttachmentImpl in project cxf by apache.

the class WrappedAttachments method toArray.

@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
    T[] copy = a.length == attachments.size() ? a : (T[]) Array.newInstance(a.getClass().getComponentType(), attachments.size());
    int i = 0;
    for (Map.Entry<String, DataHandler> entry : attachments.entrySet()) {
        Attachment o = cache.get(entry.getKey());
        if (o == null) {
            o = new AttachmentImpl(entry.getKey(), entry.getValue());
            cache.put(entry.getKey(), o);
        }
        copy[i++] = (T) o;
    }
    return copy;
}
Also used : Attachment(org.apache.cxf.message.Attachment) AttachmentImpl(org.apache.cxf.attachment.AttachmentImpl) DataHandler(javax.activation.DataHandler) Map(java.util.Map) HashMap(java.util.HashMap)

Example 10 with AttachmentImpl

use of org.apache.cxf.attachment.AttachmentImpl in project cxf by apache.

the class MessageContextImpl method convertToAttachments.

private void convertToAttachments(Object value) {
    List<?> handlers = (List<?>) value;
    List<org.apache.cxf.message.Attachment> atts = new ArrayList<>();
    for (int i = 1; i < handlers.size(); i++) {
        Attachment handler = (Attachment) handlers.get(i);
        AttachmentImpl att = new AttachmentImpl(handler.getContentId(), handler.getDataHandler());
        for (String key : handler.getHeaders().keySet()) {
            att.setHeader(key, handler.getHeader(key));
        }
        att.setXOP(false);
        atts.add(att);
    }
    Message outMessage = getOutMessage();
    outMessage.setAttachments(atts);
    outMessage.put(AttachmentOutInterceptor.WRITE_ATTACHMENTS, "true");
    Attachment root = (Attachment) handlers.get(0);
    String rootContentType = root.getContentType().toString();
    MultivaluedMap<String, String> rootHeaders = new MetadataMap<>(root.getHeaders(), true, false, true);
    if (!AttachmentUtil.isMtomEnabled(outMessage)) {
        rootHeaders.putSingle(Message.CONTENT_TYPE, rootContentType);
    }
    String messageContentType = outMessage.get(Message.CONTENT_TYPE).toString();
    int index = messageContentType.indexOf(";type");
    if (index > 0) {
        messageContentType = messageContentType.substring(0, index).trim();
    }
    AttachmentOutputInterceptor attInterceptor = new AttachmentOutputInterceptor(messageContentType, rootHeaders);
    outMessage.put(Message.CONTENT_TYPE, rootContentType);
    Map<String, List<String>> allHeaders = CastUtils.cast((Map<?, ?>) outMessage.get(Message.PROTOCOL_HEADERS));
    if (allHeaders != null) {
        allHeaders.remove(Message.CONTENT_TYPE);
    }
    attInterceptor.handleMessage(outMessage);
}
Also used : Message(org.apache.cxf.message.Message) ArrayList(java.util.ArrayList) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) Endpoint(org.apache.cxf.endpoint.Endpoint) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) AttachmentOutputInterceptor(org.apache.cxf.jaxrs.interceptor.AttachmentOutputInterceptor) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) AttachmentImpl(org.apache.cxf.attachment.AttachmentImpl)

Aggregations

AttachmentImpl (org.apache.cxf.attachment.AttachmentImpl)20 DataHandler (javax.activation.DataHandler)15 Attachment (org.apache.cxf.message.Attachment)12 HashMap (java.util.HashMap)7 ArrayList (java.util.ArrayList)6 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)6 List (java.util.List)5 Message (org.apache.cxf.message.Message)5 Test (org.junit.Test)5 Map (java.util.Map)4 TreeMap (java.util.TreeMap)4 DataSource (javax.activation.DataSource)3 ByteDataSource (org.apache.cxf.attachment.ByteDataSource)3 MessageImpl (org.apache.cxf.message.MessageImpl)3 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 FileDataSource (javax.activation.FileDataSource)2 Exchange (org.apache.camel.Exchange)2 DefaultAttachment (org.apache.camel.impl.DefaultAttachment)2 DefaultExchange (org.apache.camel.impl.DefaultExchange)2