I have a problem with camel route using IMAP.
from("imap://mail.server?username=aaa&password=zzz©To=out&delete=true&unseen=false&folderName=in&mapMailMessage=false")
.tracing()
.setHeader("copyTo", simple("works"))
.bean(MyTest.class, "setBodyWithHardcodedJson")
.unmarshal(new JacksonDataFormat(Reply.class))
.log("copyTo=${header[copyTo]}")
The call to setBodyWithHardcodedJson override the body with a valid json, the unmarshall is done correctly.
The last instruction prints me null value. (My header is lost between the unmarshall and the log steps)
I tried the same route, but replacing the from(imap) with a from(direct) and send something from unit test. The last instruction prints me "works" value, as expected.
Please, can somebody explain me why the exchange loose the headers i set by code when i start the route with a mail ?
EDIT : Here is a reduced version of my code to show the problem.
public class MyTest extends CamelTestSupport {
@Test
public void testInjectRequest() throws Exception {
//just here to start the route and wait result...
String response = "x";
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
template.sendBody("direct:start", response);
assertMockEndpointsSatisfied();
}
@Override
protected RoutesBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
// activate next line to start with direct -> works
//from("direct:start")
// activate next line to start with mail : test fails (not important)
// BUT PROBLEM : different result of .log("copyTo=${header[copyTo]}")
from("imap://server?username=aaa&password=bbb©To=out&delete=true&unseen=false&folderName=in&mapMailMessage=false")
.tracing()
.onException(Exception.class)
.log("exception here ${exception.message}")
.end()
.setHeader("copyTo", simple("works"))
.bean(MyTest.class, "hardcodeJson")
.unmarshal(new JacksonDataFormat(Reply.class))
.log("body=${body.id}")
.log("copyTo=${header[copyTo]}")
;
}
};
}
protected void hardcodeJson(Exchange exchange) {
String jsonInputString = "{\n" +
" \"id\": \"Corresp_Rest:3e9a525d-fc64-\"\n" +
"}";
exchange.getIn().setBody(jsonInputString);
}
}
public class Reply {
String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
Info following questions from @kladderradatsch
in the traces i find :
INFO route1 - body=Corresp_Rest:3e9a525d-fc64-
INFO route1 - copyTo=
if i do it with from(direct) i have
INFO route1 - body=Corresp_Rest:3e9a525d-fc64-
INFO route1 - copyTo=works
I hardcoded the json body in the bean to be sure it should unmarshal the same way, independent of the from().
After the unmarshal, the body is a Reply object with the content OK, but the headers modified are resetted only on from("imap"), not on from("direct").
Maybe i'm wrong, but i imagine the unmarshall provoques something in camel-mail to recreate the exchange from original message, losing the changes in headers made by my code...
Rename your header
copyTo
to any other name becausecopyTo
is an option of the imap: component so this clashes.