Search in sources :

Example 16 with AttachmentImpl

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

the class ContextPropertiesMappingTest method testCreateWebServiceContextWithInAttachments.

@Test
public void testCreateWebServiceContextWithInAttachments() {
    Exchange exchange = new ExchangeImpl();
    Message inMessage = new MessageImpl();
    Collection<Attachment> attachments = new LinkedList<>();
    DataSource source = new ByteDataSource(new byte[0], "text/xml");
    DataHandler handler1 = new DataHandler(source);
    attachments.add(new AttachmentImpl("part1", handler1));
    DataHandler handler2 = new DataHandler(source);
    attachments.add(new AttachmentImpl("part2", handler2));
    inMessage.setAttachments(attachments);
    inMessage.putAll(message);
    exchange.setInMessage(inMessage);
    exchange.setOutMessage(new MessageImpl());
    MessageContext ctx = new WrappedMessageContext(exchange.getInMessage(), Scope.APPLICATION);
    Object inAttachments = ctx.get(MessageContext.INBOUND_MESSAGE_ATTACHMENTS);
    assertNotNull("inbound attachments object must be initialized", inAttachments);
    assertTrue("inbound attachments must be in a Map", inAttachments instanceof Map);
    Map<String, DataHandler> dataHandlers = CastUtils.cast((Map<?, ?>) inAttachments);
    assertEquals("two inbound attachments expected", 2, dataHandlers.size());
    assertTrue("part1 attachment is missing", dataHandlers.containsKey("part1"));
    // should do as it's the same instance
    assertTrue("part1 handler is missing", dataHandlers.get("part1") == handler1);
    assertTrue("part2 attachment is missing", dataHandlers.containsKey("part2"));
    assertTrue("part2 handler is missing", dataHandlers.get("part2") == handler2);
}
Also used : Message(org.apache.cxf.message.Message) Attachment(org.apache.cxf.message.Attachment) DataHandler(javax.activation.DataHandler) LinkedList(java.util.LinkedList) ByteDataSource(org.apache.cxf.attachment.ByteDataSource) DataSource(javax.activation.DataSource) Exchange(org.apache.cxf.message.Exchange) ByteDataSource(org.apache.cxf.attachment.ByteDataSource) WrappedMessageContext(org.apache.cxf.jaxws.context.WrappedMessageContext) AttachmentImpl(org.apache.cxf.attachment.AttachmentImpl) WrappedMessageContext(org.apache.cxf.jaxws.context.WrappedMessageContext) MessageContext(javax.xml.ws.handler.MessageContext) MessageImpl(org.apache.cxf.message.MessageImpl) HashMap(java.util.HashMap) Map(java.util.Map) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 17 with AttachmentImpl

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

the class TestUtil method createSoapMessage.

public static SoapMessage createSoapMessage(SoapVersion soapVersion, InterceptorChain chain, Class<?> clazz) throws IOException {
    SoapMessage soapMessage = createEmptySoapMessage(soapVersion, chain);
    // setup the message result with attachment.class
    ByteArrayDataSource bads = new ByteArrayDataSource(clazz.getResourceAsStream("primarySoapPart.xml"), "Application/xop+xml");
    String cid = AttachmentUtil.createContentID("http://cxf.apache.org");
    soapMessage.setContent(Attachment.class, new AttachmentImpl(cid, new DataHandler(bads)));
    // setup the message attachments
    Collection<Attachment> attachments = new ArrayList<>();
    soapMessage.setAttachments(attachments);
    // String cidAtt1 = "cid:http://cxf.apache.org/me.bmp";
    // bads = new ByteArrayDataSource(clazz.getResourceAsStream("me.bmp"), "image/bmp");
    // AttachmentImpl att1 = new AttachmentImpl(cidAtt1, new DataHandler(bads));
    // att1.setXOP(true);
    // attachments.add(att1);
    String cidAtt2 = "cid:http://cxf.apache.org/my.wav";
    bads = new ByteArrayDataSource(clazz.getResourceAsStream("my.wav"), "Application/octet-stream");
    AttachmentImpl att2 = new AttachmentImpl(cidAtt2, new DataHandler(bads));
    att2.setXOP(true);
    attachments.add(att2);
    return soapMessage;
}
Also used : ArrayList(java.util.ArrayList) AttachmentImpl(org.apache.cxf.attachment.AttachmentImpl) Attachment(org.apache.cxf.message.Attachment) DataHandler(javax.activation.DataHandler) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

Example 18 with AttachmentImpl

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

the class DataSourceType method createAttachment.

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

Example 19 with AttachmentImpl

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

the class JAXBAttachmentMarshaller method addSwaRefAttachment.

@Override
public String addSwaRefAttachment(DataHandler handler) {
    String id = UUID.randomUUID() + "@apache.org";
    AttachmentImpl att = new AttachmentImpl(id, handler);
    att.setXOP(false);
    atts.add(att);
    return id;
}
Also used : AttachmentImpl(org.apache.cxf.attachment.AttachmentImpl)

Example 20 with AttachmentImpl

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

the class WrappedAttachmentsTest method testCreateAndModify.

@Test
public void testCreateAndModify() {
    Map<String, DataHandler> content = new HashMap<>();
    content.put("att-1", new DataHandler(new ByteArrayDataSource("Hello world!".getBytes(), "text/plain")));
    content.put("att-2", new DataHandler(new ByteArrayDataSource("Hola mundo!".getBytes(), "text/plain")));
    WrappedAttachments attachments = new WrappedAttachments(content);
    Attachment att3 = new AttachmentImpl("att-3", new DataHandler(new ByteArrayDataSource("Bonjour tout le monde!".getBytes(), "text/plain")));
    assertEquals(2, attachments.size());
    assertFalse(attachments.isEmpty());
    assertTrue(attachments.containsAll(attachments));
    List<String> testCollection = new ArrayList<>();
    testCollection.add("Some value");
    assertFalse(attachments.containsAll(testCollection));
    attachments.add(att3);
    assertEquals(3, attachments.size());
    attachments.add(att3);
    assertEquals(3, attachments.size());
    attachments.remove(att3);
    assertEquals(2, attachments.size());
    Attachment attx = attachments.iterator().next();
    attachments.remove(attx);
    assertEquals(1, attachments.size());
    // NOPMD - explicitly test this
    Attachment[] atts = attachments.toArray(new Attachment[0]);
    assertEquals(1, atts.length);
    assertEquals("att-1".equals(attx.getId()) ? "att-2" : "att-1", atts[0].getId());
    // NOPMD - explicitly test this
    atts = attachments.toArray(new Attachment[attachments.size()]);
    assertEquals(1, atts.length);
    assertEquals("att-1".equals(attx.getId()) ? "att-2" : "att-1", atts[0].getId());
    // NOPMD - explicitly test this
    Object[] o = attachments.toArray();
    assertEquals(1, o.length);
    Attachment a = (Attachment) o[0];
    assertEquals("att-1".equals(attx.getId()) ? "att-2" : "att-1", a.getId());
    attachments.clear();
    assertTrue(attachments.isEmpty());
    assertTrue(content.isEmpty());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Attachment(org.apache.cxf.message.Attachment) DataHandler(javax.activation.DataHandler) AttachmentImpl(org.apache.cxf.attachment.AttachmentImpl) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) Test(org.junit.Test)

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