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

network.oxalis.as4.inbound.AttachmentCleanupInterceptor Maven / Gradle / Ivy

There is a newer version: 6.7.0
Show newest version
package network.oxalis.as4.inbound;

import lombok.SneakyThrows;
import org.apache.cxf.attachment.As4AttachmentDataSource;
import org.apache.cxf.attachment.As4AttachmentDeserializer;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Attachment;
import org.apache.cxf.message.Exchange;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;

import javax.activation.DataSource;

public class AttachmentCleanupInterceptor extends AbstractPhaseInterceptor {

    public AttachmentCleanupInterceptor() {
        super(Phase.POST_INVOKE);
    }

    public void handleMessage(Message message) throws Fault {
        Exchange exchange = message.getExchange();
        cleanRequestAttachment(exchange);
    }

    private void cleanRequestAttachment(Exchange exchange) {
        As4AttachmentDeserializer ad = exchange.getInMessage().get(As4AttachmentDeserializer.class);
        ad.getRemoved().forEach(this::close);
    }

    @SneakyThrows
    private void close(Attachment attachment) {
        DataSource dataSource = attachment.getDataHandler().getDataSource();

        if (dataSource instanceof As4AttachmentDataSource) {
            As4AttachmentDataSource ads = (As4AttachmentDataSource) dataSource;
            ads.closeAll();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy