生产者模板。骆驼。如何添加附件

ProducerTemplate. Camel. How to add attachment

谁能告诉我如何使用 ProducerTemplate 添加附件? 我一直在搜索,但找不到我的案例的答案。

我正在使用 Camen 2.1,我有这三个类:

MailSender2.java


import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
import org.apache.camel.ProducerTemplate;

public class MailSender2 extends TypeMail{

    private static final ResourceBundle RES = ResourceBundle.getBundle("mail");
    protected static final String MAIL_NOTIFICATION_ENDPOINT=RES.getString("mail.host.location").trim()+":"+RES.getString("mail.port").trim();

    private Map<String, Object> header;

    public MailSender2() {
        this.header=new HashMap<>();
    }

    public void send(ProducerTemplate template) {
        this.header.put("From", this.getT_from());
        this.header.put("To", this.getT_to());
        this.header.put("Subject", this.getT_subject());
        this.header.put(Exchange.CONTENT_TYPE, "text/html; charset=UTF-8");

        //this.getF_ficher() <-- I have here the file to attach
        //this.getT_ficnon() <-- I have here the name ot the file
        //this.getT_ficext() <-- I have here the extension ot the file

        template.sendBodyAndHeaders(MAIL_NOTIFICATION_ENDPOINT, this.getT_mensaje(), header);
    }

}

TypeMail.java:


public class TypeMail {


    private String t_id;
    private String t_from;
    private String t_to;
    private String t_subject;
    private String t_mensaje;
    private byte[] f_ficher;
    private String t_ficnon;
    private String t_ficext;

    public String getT_id() {
        return t_id;
    }

    public void setT_id(String t_id) {
        this.t_id = t_id;
    }

    public String getT_from() {
        return t_from;
    }

    public void setT_from(String t_from) {
        this.t_from = t_from;
    }

    public String getT_to() {
        return t_to;
    }

    public void setT_to(String t_to) {
        this.t_to = t_to;
    }

    public String getT_subject() {
        return t_subject;
    }

    public void setT_subject(String t_subject) {
        this.t_subject = t_subject;
    }

    public String getT_mensaje() {
        return t_mensaje;
    }

    public void setT_mensaje(String t_mensaje) {
        this.t_mensaje = t_mensaje;
    }

    public byte[] getF_ficher() {
        return f_ficher;
    }

    public void setF_ficher(byte[] f_ficher) {
        this.f_ficher = f_ficher;
    }

    public String getT_ficnon() {
        return t_ficnon;
    }

    public void setT_ficnon(String t_ficnon) {
        this.t_ficnon = t_ficnon;
    }

    public String getT_ficext() {
        return t_ficext;
    }

    public void setT_ficext(String t_ficext) {
        this.t_ficext = t_ficext;
    }
}

MailCommunicationTransformer.java:


import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ws.soap.client.SoapFaultClientException;

public class MailCommunicationTransformer {

    MailSender2 mailSender = null;

    static Logger logger = LoggerFactory.getLogger(MailCommunicationTransformer.class);


    public MailCommunicationTransformer()
    {
    }

    public MailLog transform(Object actualMessage, Exchange exchange, CamelContext context)
    {

        mailSender = exchange.getIn().getBody(MailSender2.class);
        try {
            MailSenderDAO mailSenderDAO = (MailSenderDAO)context.getRegistry().lookup("MailSenderDAO");
            mailSenderDAO.validarInput(mailSender);

            if (mailSender!=null) {
                ProducerTemplate template=exchange.getContext().createProducerTemplate();
                try {
                    mailSender.send(template);
                }
                catch (Throwable ex) {
                    ex.printStackTrace();
                    exchange.setProperty(Exchange.EXCEPTION_CAUGHT,ex);
                }
            }
        }catch (MailException me) {
            me.printStackTrace();
            exchange.setProperty(Exchange.EXCEPTION_CAUGHT,me);
        }

        Throwable e = exchange.getProperty(Exchange.EXCEPTION_CAUGHT,
                Throwable.class);
        String response = "OK";
        if (e != null) {
            StringBuffer mensaje = new StringBuffer();
            if (e instanceof SoapFaultClientException) {
                mensaje.append("MAIL fault exception: CLIENT. ");
            } else {
                mensaje.append("MAIL fault exception: MAIL. ");
            }
            logger.info("MailCommunicationTransformer",e);
            while (e != null) {
                e.printStackTrace();
                mensaje.append(e.getMessage());
                e = e.getCause();
            }
            response = mensaje.toString();
        }

        MailLog log = new MailLog(mailSender, response); //, protocolo
        return log;
    }
}

在 TypeMail 中,我有 f_ficher 中的文件以及文件名 (t_ficnon) 和扩展名 (t_ficext),但我找不到如何在 MailSender2 中附加此文件在 template.sendBodyAndHeaders(.......)

之前

如有任何帮助,我们将不胜感激。 问候。

也许我没有完全理解你的问题,但是 ProducerTemplate 不知道消息类型。

您只需发送一个 body 也可能 headers 到一个端点。

因此 body 只需要是完整构建的 MimeMessage object as documented in the Camel Mail 文档。

您可以简单地 construct the mail message with Java 然后将 object 与 ProducerTemplate 一起使用(您已经这样做了)。

template.sendBodyAndHeaders("your-smtp-endpoint", yourMimeMessageInstance, yourHeaderMap);

感谢您的回答!

但是,最后,我可以这样做:

新 class EmailProcessor.java

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.util.Objects;
import java.util.ResourceBundle;

import javax.activation.DataHandler;

import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.apache.commons.codec.binary.Base64;

public class EmailProcessor implements Processor {

    // Atributos de la clase
    private TypeMail typeMail;

    public EmailProcessor(TypeMail typeMail) {
        this.typeMail = typeMail;

    }

    @Override
    public void process(Exchange exchange) throws Exception {

        Message ms = exchange.getIn();
        ms.setHeader("From", this.typeMail.getT_from());
        ms.setHeader("To", this.typeMail.getT_to());
        ms.setHeader("Subject", this.typeMail.getT_subject());
        ms.setHeader(Exchange.CONTENT_TYPE, "text/html; charset=UTF-8");

        ms.setBody("<p style='font-family: Calibri;'>" + this.typeMail.getT_mensaje() + "</p>");

        if (this.typeMail.getF_ficher() != null) {
            String mimeType = "application/pdf";
            if ("zip".equals(typeMail.getT_ficext())) {
                mimeType = "application/zip";
            }
            ms.addAttachment(typeMail.getT_ficnom() + "." + typeMail.getT_ficext(), new DataHandler(typeMail.getF_ficher(), mimeType));
        }
    }

}

MailSender.java:

import java.util.ResourceBundle;

import org.apache.camel.ExchangePattern;
import org.apache.camel.ProducerTemplate;

public class MailSender extends TypeMail{

    private static final ResourceBundle RES = ResourceBundle.getBundle("mail");
    protected static final String MAIL_NOTIFICATION_ENDPOINT=RES.getString("mail.host.location").trim()+":"+RES.getString("mail.port").trim();

    public MailSender() {
    }

    public void send(ProducerTemplate template) {

        template.send(MAIL_NOTIFICATION_ENDPOINT, ExchangePattern.InOnly, new EmailProcessor(this));
    }

}