Search in sources :

Example 1 with AttachmentRequestCallback

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);
}
Also used : AttachmentRequestCallback(org.apache.wss4j.common.ext.AttachmentRequestCallback) Attachment(org.apache.cxf.message.Attachment) AttachmentImpl(org.apache.cxf.attachment.AttachmentImpl) DataHandler(javax.activation.DataHandler) CommandMap(javax.activation.CommandMap)

Example 2 with AttachmentRequestCallback

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

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