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);
}
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;
}
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;
}
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;
}
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());
}
Aggregations