use of org.apache.wss4j.common.ext.AttachmentRequestCallback in project cxf by apache.
the class AttachmentCallbackHandlerTest method parseAttachment.
private void parseAttachment(String attachmentId) throws Exception {
Attachment attachment = new AttachmentImpl(attachmentId);
// Mock up a DataHandler for the Attachment
DataHandler dataHandler = EasyMock.mock(DataHandler.class);
dataHandler.setCommandMap(anyObject(CommandMap.class));
EasyMock.expectLastCall();
EasyMock.expect(dataHandler.getInputStream()).andReturn(null);
EasyMock.expect(dataHandler.getContentType()).andReturn(null);
EasyMock.replay(dataHandler);
((AttachmentImpl) attachment).setDataHandler(dataHandler);
AttachmentCallbackHandler callbackHandler = new AttachmentCallbackHandler(Collections.singletonList(attachment));
AttachmentRequestCallback attachmentRequestCallback = new AttachmentRequestCallback();
attachmentRequestCallback.setAttachmentId(AttachmentUtils.getAttachmentId("cid:" + attachmentId));
attachmentRequestCallback.setRemoveAttachments(false);
callbackHandler.handle(new Callback[] { attachmentRequestCallback });
List<org.apache.wss4j.common.ext.Attachment> attachments = attachmentRequestCallback.getAttachments();
assertNotNull(attachments);
assertEquals(1, attachments.size());
EasyMock.verify(dataHandler);
}
use of org.apache.wss4j.common.ext.AttachmentRequestCallback in project cxf by apache.
the class AttachmentCallbackHandler method handle.
@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (Callback callback : callbacks) {
if (callback instanceof AttachmentRequestCallback) {
AttachmentRequestCallback attachmentRequestCallback = (AttachmentRequestCallback) callback;
List<org.apache.wss4j.common.ext.Attachment> attachmentList = new ArrayList<>();
attachmentRequestCallback.setAttachments(attachmentList);
String attachmentId = attachmentRequestCallback.getAttachmentId();
if ("Attachments".equals(attachmentId)) {
// Load all attachments
attachmentId = null;
}
loadAttachments(attachmentList, attachmentId, attachmentRequestCallback.isRemoveAttachments());
} else if (callback instanceof AttachmentResultCallback) {
AttachmentResultCallback attachmentResultCallback = (AttachmentResultCallback) callback;
org.apache.cxf.attachment.AttachmentImpl securedAttachment = new org.apache.cxf.attachment.AttachmentImpl(attachmentResultCallback.getAttachmentId(), new DataHandler(new AttachmentDataSource(attachmentResultCallback.getAttachment().getMimeType(), attachmentResultCallback.getAttachment().getSourceStream())));
Map<String, String> headers = attachmentResultCallback.getAttachment().getHeaders();
for (Map.Entry<String, String> entry : headers.entrySet()) {
securedAttachment.setHeader(entry.getKey(), entry.getValue());
}
attachments.add(securedAttachment);
} else if (callback instanceof AttachmentRemovalCallback) {
AttachmentRemovalCallback attachmentRemovalCallback = (AttachmentRemovalCallback) callback;
String attachmentId = attachmentRemovalCallback.getAttachmentId();
if (attachmentId != null) {
// Calling LazyAttachmentCollection.size() here to force it to load the attachments
if (attachments != null && attachments.size() > 0) {
// NOPMD
for (Iterator<org.apache.cxf.message.Attachment> iterator = attachments.iterator(); iterator.hasNext(); ) {
org.apache.cxf.message.Attachment attachment = iterator.next();
if (attachmentId.equals(attachment.getId())) {
iterator.remove();
break;
}
}
}
}
} else {
throw new UnsupportedCallbackException(callback, "Unsupported callback");
}
}
}
Aggregations