3
votes

I'm using Camel 2.9.x for integration purposes in our current project. One of the routes consists of two endpoints - file polling endpoint and smtp mail endpoint. Files produced by the first endpoint must be sent through smtp endpoint as attachments.

For Camel configuration we're using Spring DSL (this actually is a requirement). Spring version is 3.1.1. Unfortunately, I've found only java dsl examples and documentation of attaching a file to a e-mail message in camel routes.

<endpoint uri="file:///path/to" id="file-source"/>
<endpoint uri="smtp://mail.example.com:25/[email protected]&amp;password=secret&amp;[email protected]" id="mail-dest"/>
<route id="simplified-for-readability">
  <from ref="file-source"/>
  <to ref="mail-dest"/>
</route>

This config sends files as plain/text body, not as attachments (even binary files). Is there a way to send files as attachments without using Java dsl?

4
Czar, have you got what you have asked. If yes, please give some direction to achieve it. I am working on same stuff, still cannot able to figure it out.Lucifer
@Lucifer It was 4 years ago, with spring and camel versions which are today obsolete, plus I don't really remember details, except that the accepted answer worked for me.Czar
You have used java dsl instead spring to achieve it. Am i correct.Lucifer
No, there was no Java DSL then :) It was still spring xml config, I've just implemented an Attacher bean and registered it with spring xml config, like is suggested by Petter's answer.Czar
Thanks a lot for your time.:)Lucifer

4 Answers

12
votes

This could be done with Spring config, but you might have to code a simple java bean or so, although that does not have to do with spring or java DSL.

First create a class similar to this one (you might need to fix stuff here):

// Note: Content Type - might need treatment!
public class AttachmentAttacher{
   public void process(Exchange exchange){
      Message in = exchange.getIn();
      byte[] file = in.getBody(byte[].class);
      String fileId = in.getHeader("CamelFileName",String.class);
      in.addAttachment(fileId, new DataHandler(file,"plain/text"));
    }
}

Then just wire up a spring bean and use it in your route. Should do the trick.

<bean id="attacher" class="foo.bar.AttachmentAttacher"/>

<route>
  <from ref="file-source"/>
  <bean ref="attacher"/>
  <to ref="mail-dest"/>
</route>
5
votes

This worked for me, a slight variation on what's above.

import javax.activation.DataHandler;
import javax.mail.util.ByteArrayDataSource;

import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;

public class AttachmentAttacher implements Processor {

  private final String mimetype;

  public AttachmentAttacher(String mimetype) {
    this.mimetype = mimetype;
  }

  @Override
  public void process(Exchange exchange){
    Message in = exchange.getIn();
    byte[] file = in.getBody(byte[].class);
    String fileId = in.getHeader("CamelFileName",String.class);
    in.addAttachment(fileId, new DataHandler(new     ByteArrayDataSource(file, mimetype)));
  }
}
0
votes

For me, the following code snippet works without setting a MIME-type:

(Of course, you have it to adapt it for your needs)

public class MyProcesor implements org.apache.camel.Processor {
    @Override
    public void process(Exchange exchange) throws Exception {
        String tempDir = System.getProperty("java.io.tmpdir");
        File file = new File(tempDir + "/filename.ext" ); //or wherever you file is located
        exchange.getIn().addAttachment("id", new DataHandler(new FileDataSource(file))); //no explicit MIME-type needed
    }    
}

But, I dont have this code for Spring DSL...

-1
votes

You might be able to do this with an expression such as simple. Simple is nice because it comes with Camel but I don't think it's powerful enough to do what you want. I haven't tried it but I'm sure that a groovy expression could do this. A groovy expression can be specified in Spring.