use of org.apache.wss4j.common.ext.AttachmentResultCallback 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