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