Search in sources :

Example 1 with AttachmentResultCallback

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");
        }
    }
}
Also used : AttachmentRequestCallback(org.apache.wss4j.common.ext.AttachmentRequestCallback) AttachmentDataSource(org.apache.cxf.attachment.AttachmentDataSource) ArrayList(java.util.ArrayList) Attachment(org.apache.cxf.message.Attachment) DataHandler(javax.activation.DataHandler) AttachmentRemovalCallback(org.apache.wss4j.common.ext.AttachmentRemovalCallback) Attachment(org.apache.cxf.message.Attachment) AttachmentResultCallback(org.apache.wss4j.common.ext.AttachmentResultCallback) AttachmentRemovalCallback(org.apache.wss4j.common.ext.AttachmentRemovalCallback) Callback(javax.security.auth.callback.Callback) AttachmentRequestCallback(org.apache.wss4j.common.ext.AttachmentRequestCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) AttachmentResultCallback(org.apache.wss4j.common.ext.AttachmentResultCallback) Map(java.util.Map)

Aggregations

ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 DataHandler (javax.activation.DataHandler)1 Callback (javax.security.auth.callback.Callback)1 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)1 AttachmentDataSource (org.apache.cxf.attachment.AttachmentDataSource)1 Attachment (org.apache.cxf.message.Attachment)1 AttachmentRemovalCallback (org.apache.wss4j.common.ext.AttachmentRemovalCallback)1 AttachmentRequestCallback (org.apache.wss4j.common.ext.AttachmentRequestCallback)1 AttachmentResultCallback (org.apache.wss4j.common.ext.AttachmentResultCallback)1