Search in sources :

Example 16 with NoTypeConversionAvailableException

use of org.apache.camel.NoTypeConversionAvailableException in project camel by apache.

the class JaxbDataFormat method marshal.

void marshal(Exchange exchange, Object graph, OutputStream stream, Marshaller marshaller) throws XMLStreamException, JAXBException, NoTypeConversionAvailableException, IOException, InvalidPayloadException {
    Object element = graph;
    if (partialClass != null && getPartNamespace() != null) {
        element = new JAXBElement<Object>(getPartNamespace(), partialClass, graph);
    }
    // only marshal if its possible
    if (introspector.isElement(element)) {
        if (asXmlStreamWriter(exchange)) {
            XMLStreamWriter writer = typeConverter.convertTo(XMLStreamWriter.class, stream);
            if (needFiltering(exchange)) {
                writer = new FilteringXmlStreamWriter(writer);
            }
            if (xmlStreamWriterWrapper != null) {
                writer = xmlStreamWriterWrapper.wrapWriter(writer);
            }
            marshaller.marshal(element, writer);
        } else {
            marshaller.marshal(element, stream);
        }
        return;
    } else if (objectFactory && element != null) {
        Method objectFactoryMethod = JaxbHelper.getJaxbElementFactoryMethod(camelContext, element.getClass());
        if (objectFactoryMethod != null) {
            try {
                Object instance = objectFactoryMethod.getDeclaringClass().newInstance();
                if (instance != null) {
                    Object toMarshall = objectFactoryMethod.invoke(instance, element);
                    if (asXmlStreamWriter(exchange)) {
                        XMLStreamWriter writer = typeConverter.convertTo(XMLStreamWriter.class, stream);
                        if (needFiltering(exchange)) {
                            writer = new FilteringXmlStreamWriter(writer);
                        }
                        if (xmlStreamWriterWrapper != null) {
                            writer = xmlStreamWriterWrapper.wrapWriter(writer);
                        }
                        marshaller.marshal(toMarshall, writer);
                    } else {
                        marshaller.marshal(toMarshall, stream);
                    }
                    return;
                }
            } catch (Exception e) {
                LOG.debug("Unable to create JAXBElement object for type " + element.getClass() + " due to " + e.getMessage(), e);
            }
        }
    }
    // cannot marshal
    if (!mustBeJAXBElement) {
        // write the graph as is to the output stream
        if (LOG.isDebugEnabled()) {
            LOG.debug("Attempt to marshalling non JAXBElement with type {} as InputStream", ObjectHelper.classCanonicalName(graph));
        }
        InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, exchange, graph);
        IOHelper.copyAndCloseInput(is, stream);
    } else {
        throw new InvalidPayloadException(exchange, JAXBElement.class);
    }
}
Also used : XMLStreamWriter(javax.xml.stream.XMLStreamWriter) InputStream(java.io.InputStream) Method(java.lang.reflect.Method) InvalidPayloadException(org.apache.camel.InvalidPayloadException) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) XMLStreamException(javax.xml.stream.XMLStreamException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) InvalidPayloadException(org.apache.camel.InvalidPayloadException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 17 with NoTypeConversionAvailableException

use of org.apache.camel.NoTypeConversionAvailableException in project camel by apache.

the class JmsBinding method createJmsMessageForType.

/**
     * 
     * Create the {@link Message} 
     * 
     * @return jmsMessage or null if the mapping was not successfully
     */
protected Message createJmsMessageForType(Exchange exchange, Object body, Map<String, Object> headers, Session session, CamelContext context, JmsMessageType type) throws JMSException {
    switch(type) {
        case Text:
            {
                TextMessage message = session.createTextMessage();
                if (body != null) {
                    String payload = context.getTypeConverter().convertTo(String.class, exchange, body);
                    message.setText(payload);
                }
                return message;
            }
        case Bytes:
            {
                BytesMessage message = session.createBytesMessage();
                if (body != null) {
                    byte[] payload = context.getTypeConverter().convertTo(byte[].class, exchange, body);
                    message.writeBytes(payload);
                }
                return message;
            }
        case Map:
            {
                MapMessage message = session.createMapMessage();
                if (body != null) {
                    Map<?, ?> payload = context.getTypeConverter().convertTo(Map.class, exchange, body);
                    populateMapMessage(message, payload, context);
                }
                return message;
            }
        case Object:
            ObjectMessage message = session.createObjectMessage();
            if (body != null) {
                try {
                    Serializable payload = context.getTypeConverter().mandatoryConvertTo(Serializable.class, exchange, body);
                    message.setObject(payload);
                } catch (NoTypeConversionAvailableException e) {
                    // cannot convert to serializable then thrown an exception to avoid sending a null message
                    JMSException cause = new MessageFormatException(e.getMessage());
                    cause.initCause(e);
                    throw cause;
                }
            }
            return message;
        default:
            break;
    }
    return null;
}
Also used : MessageFormatException(javax.jms.MessageFormatException) Serializable(java.io.Serializable) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) ObjectMessage(javax.jms.ObjectMessage) MapMessage(javax.jms.MapMessage) BytesMessage(javax.jms.BytesMessage) JMSException(javax.jms.JMSException) HashMap(java.util.HashMap) Map(org.apache.camel.component.jms.JmsMessageType.Map) Map(java.util.Map) TextMessage(javax.jms.TextMessage)

Example 18 with NoTypeConversionAvailableException

use of org.apache.camel.NoTypeConversionAvailableException in project camel by apache.

the class UnaryExpression method createIncExpression.

private Expression createIncExpression(final Expression leftExp) {
    return new Expression() {

        @Override
        public <T> T evaluate(Exchange exchange, Class<T> type) {
            Number num = leftExp.evaluate(exchange, Number.class);
            if (num != null) {
                long val = num.longValue();
                val++;
                // convert value back to same type as input as we want to preserve type
                Object left = leftExp.evaluate(exchange, Object.class);
                try {
                    left = exchange.getContext().getTypeConverter().mandatoryConvertTo(left.getClass(), exchange, val);
                } catch (NoTypeConversionAvailableException e) {
                    throw ObjectHelper.wrapRuntimeCamelException(e);
                }
                // and return the result
                return exchange.getContext().getTypeConverter().convertTo(type, left);
            }
            // cannot convert the expression as a number
            Exception cause = new CamelExchangeException("Cannot evaluate " + leftExp + " as a number", exchange);
            throw ObjectHelper.wrapRuntimeCamelException(cause);
        }

        @Override
        public String toString() {
            return left + operator.toString();
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) CamelExchangeException(org.apache.camel.CamelExchangeException) Expression(org.apache.camel.Expression) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) SimpleParserException(org.apache.camel.language.simple.types.SimpleParserException) CamelExchangeException(org.apache.camel.CamelExchangeException)

Example 19 with NoTypeConversionAvailableException

use of org.apache.camel.NoTypeConversionAvailableException in project camel by apache.

the class InstanceFallbackConverterTest method testInstanceFallbackMandatoryFailed.

public void testInstanceFallbackMandatoryFailed() throws Exception {
    Exchange exchange = new DefaultExchange(context);
    try {
        context.getTypeConverter().mandatoryConvertTo(Date.class, exchange, new Timestamp(0));
        fail("Should have thrown an exception");
    } catch (NoTypeConversionAvailableException e) {
    // expected
    }
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) Timestamp(java.sql.Timestamp)

Example 20 with NoTypeConversionAvailableException

use of org.apache.camel.NoTypeConversionAvailableException in project camel by apache.

the class TypeConverterConcurrencyIssueTest method testTypeConverter.

public void testTypeConverter() throws Exception {
    // add as type converter
    Method method = TypeConverterConcurrencyIssueTest.class.getMethod("toMyCamelBean", String.class);
    assertNotNull(method);
    context.getTypeConverterRegistry().addTypeConverter(MyCamelBean.class, String.class, new StaticMethodTypeConverter(method));
    ExecutorService pool = context.getExecutorServiceManager().newThreadPool(this, "test", 50, 50);
    final CountDownLatch latch = new CountDownLatch(size);
    StopWatch watch = new StopWatch();
    for (int i = 0; i < size; i++) {
        pool.submit(new Runnable() {

            @Override
            public void run() {
                try {
                    context.getTypeConverter().mandatoryConvertTo(MyCamelBean.class, "1;MyCamel");
                    latch.countDown();
                } catch (NoTypeConversionAvailableException e) {
                // ignore, as the latch will not be decremented anymore so that the assert below
                // will fail after the one minute timeout anyway
                }
            }
        });
    }
    assertTrue("The expected mandatory conversions failed!", latch.await(1, TimeUnit.MINUTES));
    log.info("Took " + watch.stop() + " millis to convert " + size + " objects");
}
Also used : NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) ExecutorService(java.util.concurrent.ExecutorService) Method(java.lang.reflect.Method) CountDownLatch(java.util.concurrent.CountDownLatch) StaticMethodTypeConverter(org.apache.camel.impl.converter.StaticMethodTypeConverter) StopWatch(org.apache.camel.util.StopWatch)

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