0

I am using camel to transfer files from one endpoint to another. I am starting multiple routes in which some routes need to decrypt the files. How can i make the unmarshal process optional in a specific route based on a boolean condition?

from(source)
    .choice()
    .when(isEncrypted())) //Java boolean value 
    .unmarshal(decrypt(pgpEncryptionDetails)) 
    .endChoice()
to(destination);

PGPDataFormat decrypt(PGPEncryptionDetails pgpEncryptionDetails) {
    PGPDataFormat pgpDataFormat = new PGPDataFormat();
    pgpDataFormat.setKeyFileName(pgpEncryptionDetails.getPrivateKeyPath());
    pgpDataFormat.setPassword(pgpEncryptionDetails.getPassphrase());
    pgpDataFormat.setKeyUserid(pgpEncryptionDetails.getUserId());
    return pgpDataFormat;
}

I know how to do with a simple expression but here my condition not depends on exchange.

Kenster
  • 23,465
  • 21
  • 80
  • 106
Gangaraju
  • 4,406
  • 9
  • 45
  • 77
  • Why can't you do this check if decryption is needed inside `decrypt()` itself? – SubOptimal Oct 14 '16 at 05:44
  • @SubOptimal, I need to handle that before calling `unmarshal`. I have to skip `unmarshal()` itself, not just `decrypt()`. I have updated the code for better understanding. – Gangaraju Oct 14 '16 at 05:50

2 Answers2

1

You can use a method call expression to call a method on a java bean which implements the predicate

public class Foo {
  public boolean isSomething(Object body) {
    ... return true or false
  }
} 

And then use the method in the Camel route

when(method(Foo.class, "isSomething"))
Claus Ibsen
  • 56,060
  • 7
  • 50
  • 65
0

Its working for me.

from(source)
    .process(exchange -> decryptIfEncrypted(pgpEncryptionDetails))
to(destination);

ProcessDefinition decryptIfEncrypted(PGPEncryptionDetails pgpEncryptionDetails) {
    if (isPgpEncryped()) {
        PGPDataFormat pgpDataFormat = new PGPDataFormat();
        pgpDataFormat.setKeyFileName(pgpEncryptionDetails.getPrivateKeyPath());
        pgpDataFormat.setPassword(pgpEncryptionDetails.getPassphrase());
        pgpDataFormat.setKeyUserid(pgpEncryptionDetails.getUserId());
        return new ProcessDefinition().unmarshal(pgpDataFormat);
    }
    return new ProcessDefinition();
}
Gangaraju
  • 4,406
  • 9
  • 45
  • 77