Search in sources :

Example 6 with TypeConversionException

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

the class SpringTypeConverter method convertTo.

@Override
public <T> T convertTo(Class<T> type, Exchange exchange, Object value) throws TypeConversionException {
    // do not attempt to convert Camel types
    if (type.getCanonicalName().startsWith("org.apache")) {
        return null;
    }
    // do not attempt to convert List -> Map. Ognl expression may use this converter as a fallback expecting null
    if (type.isAssignableFrom(Map.class) && (value.getClass().isArray() || value instanceof Collection)) {
        return null;
    }
    TypeDescriptor sourceType = types.computeIfAbsent(value.getClass(), TypeDescriptor::valueOf);
    TypeDescriptor targetType = types.computeIfAbsent(type, TypeDescriptor::valueOf);
    for (ConversionService conversionService : conversionServices) {
        if (conversionService.canConvert(sourceType, targetType)) {
            try {
                return (T) conversionService.convert(value, sourceType, targetType);
            } catch (ConversionFailedException e) {
                //
                if (e.getCause() instanceof ConverterNotFoundException && isArrayOrCollection(value)) {
                    return null;
                } else {
                    throw new TypeConversionException(value, type, e);
                }
            }
        }
    }
    return null;
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ConversionFailedException(org.springframework.core.convert.ConversionFailedException) TypeConversionException(org.apache.camel.TypeConversionException) ConversionService(org.springframework.core.convert.ConversionService) ConverterNotFoundException(org.springframework.core.convert.ConverterNotFoundException) Collection(java.util.Collection) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 7 with TypeConversionException

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

the class ConvertingPublisher method subscribe.

@Override
public void subscribe(Subscriber<? super R> subscriber) {
    delegate.subscribe(new Subscriber<Exchange>() {

        private AtomicBoolean active = new AtomicBoolean(true);

        private Subscription subscription;

        @Override
        public void onSubscribe(Subscription newSubscription) {
            if (newSubscription == null) {
                throw new NullPointerException("subscription is null");
            } else if (newSubscription == this.subscription) {
                throw new IllegalArgumentException("already subscribed to the subscription: " + newSubscription);
            }
            if (this.subscription != null) {
                newSubscription.cancel();
            } else {
                this.subscription = newSubscription;
                subscriber.onSubscribe(newSubscription);
            }
        }

        @Override
        public void onNext(Exchange ex) {
            if (!active.get()) {
                return;
            }
            R r;
            try {
                if (ex.hasOut()) {
                    r = ex.getOut().getBody(type);
                } else {
                    r = ex.getIn().getBody(type);
                }
            } catch (TypeConversionException e) {
                LOG.warn("Unable to convert body to the specified type: " + type.getName(), e);
                r = null;
            }
            if (r == null && ex.getIn().getBody() != null) {
                this.onError(new ClassCastException("Unable to convert body to the specified type: " + type.getName()));
                active.set(false);
                subscription.cancel();
            } else {
                subscriber.onNext(r);
            }
        }

        @Override
        public void onError(Throwable throwable) {
            if (!active.get()) {
                return;
            }
            subscriber.onError(throwable);
        }

        @Override
        public void onComplete() {
            if (!active.get()) {
                return;
            }
            subscriber.onComplete();
        }
    });
}
Also used : Exchange(org.apache.camel.Exchange) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TypeConversionException(org.apache.camel.TypeConversionException) Subscription(org.reactivestreams.Subscription)

Example 8 with TypeConversionException

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

the class CamelJaxbFallbackConverterTest method testFallbackConverterUnmarshalWithNonJAXBComplaintValue.

@Test
public void testFallbackConverterUnmarshalWithNonJAXBComplaintValue() throws Exception {
    TypeConverter converter = context.getTypeConverter();
    try {
        converter.convertTo(Foo.class, "Not every String is XML");
        fail("Should have thrown exception");
    } catch (TypeConversionException e) {
    // expected
    }
    try {
        converter.convertTo(Bar.class, "<bar></bar");
        fail("Should have thrown exception");
    } catch (TypeConversionException e) {
    // expected
    }
}
Also used : TypeConverter(org.apache.camel.TypeConverter) TypeConversionException(org.apache.camel.TypeConversionException) Test(org.junit.Test)

Example 9 with TypeConversionException

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

the class CamelJaxbFallbackConverterTest method testConverter.

@Test
public void testConverter() throws Exception {
    TypeConverter converter = context.getTypeConverter();
    PersonType person = converter.convertTo(PersonType.class, "<Person><firstName>FOO</firstName><lastName>BAR</lastName></Person>");
    assertNotNull("Person should not be null ", person);
    assertEquals("Get the wrong first name ", "FOO", person.getFirstName());
    assertEquals("Get the wrong second name ", "BAR", person.getLastName());
    Exchange exchange = new DefaultExchange(context);
    exchange.setProperty(Exchange.CHARSET_NAME, "UTF-8");
    String value = converter.convertTo(String.class, exchange, person);
    assertTrue("Should get a right marshalled string", value.indexOf("<lastName>BAR</lastName>") > 0);
    byte[] buffers = "<Person><firstName>FOO</firstName><lastName>BAR</lastName></Person>".getBytes("UTF-8");
    InputStream is = new ByteArrayInputStream(buffers);
    try {
        converter.convertTo(PersonType.class, exchange, is);
        fail("Should have thrown exception");
    } catch (TypeConversionException e) {
    // expected
    }
}
Also used : TypeConverter(org.apache.camel.TypeConverter) DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) ByteArrayInputStream(java.io.ByteArrayInputStream) TypeConversionException(org.apache.camel.TypeConversionException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PersonType(org.apache.camel.foo.bar.PersonType) Test(org.junit.Test)

Example 10 with TypeConversionException

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

the class FallbackTypeConverter method convertTo.

public <T> T convertTo(Class<T> type, Exchange exchange, Object value) {
    if (BeanInvocation.class.isAssignableFrom(type) || Processor.class.isAssignableFrom(type)) {
        // to avoid Camel trying to do this when using beans with JAXB payloads
        return null;
    }
    try {
        if (isJaxbType(type)) {
            return unmarshall(type, exchange, value);
        }
        if (value != null && isNotStreamCacheType(type)) {
            if (hasXmlRootElement(value.getClass())) {
                return marshall(type, exchange, value, null);
            }
            if (isObjectFactory()) {
                CamelContext context = exchange != null ? exchange.getContext() : camelContext;
                Method objectFactoryMethod = JaxbHelper.getJaxbElementFactoryMethod(context, value.getClass());
                if (objectFactoryMethod != null) {
                    return marshall(type, exchange, value, objectFactoryMethod);
                }
            }
        }
    } catch (Exception e) {
        throw new TypeConversionException(value, type, e);
    }
    // should return null if didn't even try to convert at all or for whatever reason the conversion is failed
    return null;
}
Also used : CamelContext(org.apache.camel.CamelContext) Processor(org.apache.camel.Processor) TypeConversionException(org.apache.camel.TypeConversionException) BeanInvocation(org.apache.camel.component.bean.BeanInvocation) Method(java.lang.reflect.Method) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) XMLStreamException(javax.xml.stream.XMLStreamException) JAXBException(javax.xml.bind.JAXBException) TypeConversionException(org.apache.camel.TypeConversionException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

TypeConversionException (org.apache.camel.TypeConversionException)13 JAXBException (javax.xml.bind.JAXBException)4 Exchange (org.apache.camel.Exchange)4 Test (org.junit.Test)4 InputStream (java.io.InputStream)3 JAXBContext (javax.xml.bind.JAXBContext)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 StringWriter (java.io.StringWriter)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 LinkedHashMap (java.util.LinkedHashMap)2 Marshaller (javax.xml.bind.Marshaller)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 TransformerException (javax.xml.transform.TransformerException)2 NoTypeConversionAvailableException (org.apache.camel.NoTypeConversionAvailableException)2 Processor (org.apache.camel.Processor)2 TypeConverter (org.apache.camel.TypeConverter)2 XmlConverter (org.apache.camel.converter.jaxp.XmlConverter)2 Document (org.w3c.dom.Document)2 Metacard (ddf.catalog.data.Metacard)1 Writer (java.io.Writer)1