Search in sources :

Example 21 with NoTypeConversionAvailableException

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 + ")";
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Matcher(java.util.regex.Matcher) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) ArrayList(java.util.ArrayList) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) ExpressionAdapter(org.apache.camel.support.ExpressionAdapter)

Example 22 with NoTypeConversionAvailableException

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();
}
Also used : CamelExecutionException(org.apache.camel.CamelExecutionException) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) Document(org.w3c.dom.Document)

Example 23 with NoTypeConversionAvailableException

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());
    }
}
Also used : CamelExecutionException(org.apache.camel.CamelExecutionException) RouteBuilder(org.apache.camel.builder.RouteBuilder) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) CamelExecutionException(org.apache.camel.CamelExecutionException)

Aggregations

NoTypeConversionAvailableException (org.apache.camel.NoTypeConversionAvailableException)23 Exchange (org.apache.camel.Exchange)7 Map (java.util.Map)4 CamelExecutionException (org.apache.camel.CamelExecutionException)4 DefaultExchange (org.apache.camel.impl.DefaultExchange)4 Method (java.lang.reflect.Method)3 TypeConverter (org.apache.camel.TypeConverter)3 InputStream (java.io.InputStream)2 Serializable (java.io.Serializable)2 URI (java.net.URI)2 Timestamp (java.sql.Timestamp)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 BytesMessage (javax.jms.BytesMessage)2 JMSException (javax.jms.JMSException)2 MapMessage (javax.jms.MapMessage)2 MessageFormatException (javax.jms.MessageFormatException)2 ObjectMessage (javax.jms.ObjectMessage)2 TextMessage (javax.jms.TextMessage)2 CamelContext (org.apache.camel.CamelContext)2