All Downloads are FREE. Search and download functionalities are using the official Maven repository.

no.digipost.api.interceptors.AttachmentCallbackHandler Maven / Gradle / Ivy

The newest version!
package no.digipost.api.interceptors;

import org.apache.commons.lang3.StringUtils;
import org.apache.wss4j.common.ext.Attachment;
import org.apache.wss4j.common.ext.AttachmentRequestCallback;
import org.springframework.ws.soap.SoapMessage;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class AttachmentCallbackHandler implements CallbackHandler {
    private final SoapMessage message;

    public AttachmentCallbackHandler(final SoapMessage message) {
        this.message = message;

    }

    protected static Attachment convert(final org.springframework.ws.mime.Attachment n) throws IOException {
        Attachment e = new Attachment();
        e.setId(n.getContentId().replaceFirst("<", "").replace(">", ""));
        e.setMimeType(n.getContentType());
        e.setSourceStream(n.getInputStream());
        return e;
    }

    @Override
    public void handle(final Callback[] callbacks) throws IOException, UnsupportedCallbackException {
        for (Callback c : callbacks) {
            if (c instanceof AttachmentRequestCallback) {
                AttachmentRequestCallback arg = (AttachmentRequestCallback) c;
                List attList = new ArrayList();
                if (StringUtils.isBlank(arg.getAttachmentId()) || arg.getAttachmentId().equals("Attachments")) {
                    Iterator attz = message.getAttachments();
                    while (attz.hasNext()) {
                        attList.add(convert(attz.next()));
                    }
                } else {
                    org.springframework.ws.mime.Attachment attachment = message.getAttachment("<" + arg.getAttachmentId() + ">");
                    if (attachment == null) {
                        throw new IllegalArgumentException("No such attachment: " + arg.getAttachmentId());
                    }
                    attList.add(convert(attachment));
                }
                arg.setAttachments(attList);
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy