use of org.apache.camel.NoTypeConversionAvailableException in project camel by apache.
the class ExpressionBuilder method dateExpression.
public static Expression dateExpression(final String commandWithOffsets, final String timezone, final String pattern) {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
// Capture optional time offsets
String command = commandWithOffsets.split("[+-]", 2)[0].trim();
List<Long> offsets = new ArrayList<>();
Matcher offsetMatcher = OFFSET_PATTERN.matcher(commandWithOffsets);
while (offsetMatcher.find()) {
try {
long value = exchange.getContext().getTypeConverter().mandatoryConvertTo(long.class, exchange, offsetMatcher.group(2).trim());
offsets.add(offsetMatcher.group(1).equals("+") ? value : -value);
} catch (NoTypeConversionAvailableException e) {
throw ObjectHelper.wrapCamelExecutionException(exchange, e);
}
}
Date date;
if ("now".equals(command)) {
date = new Date();
} else if (command.startsWith("header.") || command.startsWith("in.header.")) {
String key = command.substring(command.lastIndexOf('.') + 1);
date = exchange.getIn().getHeader(key, Date.class);
if (date == null) {
throw new IllegalArgumentException("Cannot find java.util.Date object at command: " + command);
}
} else if (command.startsWith("out.header.")) {
String key = command.substring(command.lastIndexOf('.') + 1);
date = exchange.getOut().getHeader(key, Date.class);
if (date == null) {
throw new IllegalArgumentException("Cannot find java.util.Date object at command: " + command);
}
} else if ("file".equals(command)) {
Long num = exchange.getIn().getHeader(Exchange.FILE_LAST_MODIFIED, Long.class);
if (num != null && num > 0) {
date = new Date(num);
} else {
date = exchange.getIn().getHeader(Exchange.FILE_LAST_MODIFIED, Date.class);
if (date == null) {
throw new IllegalArgumentException("Cannot find " + Exchange.FILE_LAST_MODIFIED + " header at command: " + command);
}
}
} else {
throw new IllegalArgumentException("Command not supported for dateExpression: " + command);
}
// Apply offsets
long dateAsLong = date.getTime();
for (long offset : offsets) {
dateAsLong += offset;
}
date = new Date(dateAsLong);
if (pattern != null && !pattern.isEmpty()) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
if (timezone != null && !timezone.isEmpty()) {
df.setTimeZone(TimeZone.getTimeZone(timezone));
}
return df.format(date);
} else {
return date;
}
}
@Override
public String toString() {
return "date(" + commandWithOffsets + ":" + pattern + ":" + timezone + ")";
}
};
}
use of org.apache.camel.NoTypeConversionAvailableException in project camel by apache.
the class BeanNoTypeConvertionPossibleWhenHeaderTest method testBeanHeaderNoTypeConvertionPossibleFail.
public void testBeanHeaderNoTypeConvertionPossibleFail() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(0);
// we send in a bar string as header which cannot be converted to a number so it should fail
try {
template.requestBodyAndHeader("direct:start", "Hello World", "foo", 555);
fail("Should have thrown an exception");
} catch (CamelExecutionException e) {
ParameterBindingException pbe = assertIsInstanceOf(ParameterBindingException.class, e.getCause());
assertEquals(1, pbe.getIndex());
assertTrue(pbe.getMethod().getName().contains("hello"));
assertEquals(555, pbe.getParameterValue());
NoTypeConversionAvailableException ntae = assertIsInstanceOf(NoTypeConversionAvailableException.class, e.getCause().getCause());
assertEquals(Integer.class, ntae.getFromType());
assertEquals(Document.class, ntae.getToType());
assertEquals(555, ntae.getValue());
assertNotNull(ntae.getMessage());
}
assertMockEndpointsSatisfied();
}
use of org.apache.camel.NoTypeConversionAvailableException in project camel by apache.
the class BeanOverloadedMethodFQNTest method testOrderNoFQNUnknown.
public void testOrderNoFQNUnknown() throws Exception {
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start").bean(MyBean.class, "order(Unknown)").to("mock:result");
}
});
context.start();
try {
template.sendBody("direct:start", new MyOrder());
fail("Should have thrown an exception");
} catch (CamelExecutionException e) {
NoTypeConversionAvailableException cause = assertIsInstanceOf(NoTypeConversionAvailableException.class, e.getCause().getCause());
assertEquals("Unknown", cause.getValue());
}
}
Aggregations