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