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;
}
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();
}
});
}
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
}
}
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
}
}
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;
}
Aggregations