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

io.automatiko.engine.addons.services.receiveemail.EmailAttachmentInputConverter Maven / Gradle / Ivy

There is a newer version: 0.36.0
Show newest version
package io.automatiko.engine.addons.services.receiveemail;

import java.io.IOException;
import java.util.Map;

import org.apache.camel.TypeConversionException;
import org.apache.camel.attachment.AttachmentMessage;
import org.apache.camel.component.mail.MailMessage;

import io.automatiko.engine.api.io.InputConverter;
import io.automatiko.engine.workflow.file.ByteArrayFile;
import jakarta.activation.DataHandler;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.mail.internet.MimeUtility;

@ApplicationScoped
public class EmailAttachmentInputConverter implements InputConverter {

    @Override
    public ByteArrayFile convert(Object input) {
        if (input instanceof MailMessage) {

            MailMessage mailMessage = (MailMessage) input;

            AttachmentMessage attachmentMessage = mailMessage.getExchange().getMessage(AttachmentMessage.class);
            if (attachmentMessage != null) {
                Map attachments = attachmentMessage.getAttachments();
                if (attachments != null && attachments.size() > 0) {
                    for (String name : attachments.keySet()) {
                        DataHandler dh = attachments.get(name);
                        // get the file name
                        String filename = dh.getName();

                        // get the content and convert it to byte[]

                        try {
                            filename = MimeUtility.decodeText(filename);
                            byte[] data = mailMessage.getExchange().getContext().getTypeConverter()
                                    .convertTo(byte[].class, dh.getInputStream());

                            return new io.automatiko.engine.addons.services.receiveemail.Attachment(filename, data);
                        } catch (TypeConversionException | IOException e) {
                            e.printStackTrace();
                        }

                    }
                }
            }
        }
        return null;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy