use of org.apache.camel.impl.DefaultExchangeHolder in project camel by apache.
the class MyObject method deserialize.
@Test
public void deserialize() throws IOException, ClassNotFoundException {
CamelContext context = new DefaultCamelContext();
final DefaultExchange exchange = new DefaultExchange(context);
final List<MyObject> objects = new ArrayList<>();
final MyObject o = new MyObject("leb", "hello".getBytes());
objects.add(o);
exchange.getIn().setBody(objects);
final DefaultExchangeHolder deh = DefaultExchangeHolder.marshal(exchange);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(deh);
oos.flush();
final byte[] serialized = baos.toByteArray();
final ObjectInputStream bis = new ClassLoadingAwareObjectInputStream(context, new ByteArrayInputStream(serialized));
final DefaultExchangeHolder deserialized = (DefaultExchangeHolder) bis.readObject();
final DefaultExchange exchange2 = new DefaultExchange(context);
DefaultExchangeHolder.unmarshal(exchange2, deserialized);
List<MyObject> receivedObjects = exchange2.getIn().getBody(List.class);
assertEquals(1, receivedObjects.size());
assertEquals(o, receivedObjects.get(0));
}
use of org.apache.camel.impl.DefaultExchangeHolder in project camel by apache.
the class JmsBinding method extractBodyFromJms.
/**
* Extracts the body from the JMS message
*
* @param exchange the exchange
* @param message the message to extract its body
* @return the body, can be <tt>null</tt>
*/
public Object extractBodyFromJms(Exchange exchange, Message message) {
try {
// if we are configured to not map the jms message then return it as body
if (!mapJmsMessage) {
LOG.trace("Option map JMS message is false so using JMS message as body: {}", message);
return message;
}
if (message instanceof ObjectMessage) {
LOG.trace("Extracting body as a ObjectMessage from JMS message: {}", message);
ObjectMessage objectMessage = (ObjectMessage) message;
Object payload = objectMessage.getObject();
if (payload instanceof DefaultExchangeHolder) {
DefaultExchangeHolder holder = (DefaultExchangeHolder) payload;
DefaultExchangeHolder.unmarshal(exchange, holder);
return exchange.getIn().getBody();
} else {
return objectMessage.getObject();
}
} else if (message instanceof TextMessage) {
LOG.trace("Extracting body as a TextMessage from JMS message: {}", message);
TextMessage textMessage = (TextMessage) message;
return textMessage.getText();
} else if (message instanceof MapMessage) {
LOG.trace("Extracting body as a MapMessage from JMS message: {}", message);
return createMapFromMapMessage((MapMessage) message);
} else if (message instanceof BytesMessage) {
LOG.trace("Extracting body as a BytesMessage from JMS message: {}", message);
return createByteArrayFromBytesMessage((BytesMessage) message);
} else if (message instanceof StreamMessage) {
LOG.trace("Extracting body as a StreamMessage from JMS message: {}", message);
return message;
} else {
return null;
}
} catch (JMSException e) {
throw new RuntimeCamelException("Failed to extract body due to: " + e + ". Message: " + message, e);
}
}
use of org.apache.camel.impl.DefaultExchangeHolder in project camel by apache.
the class JCacheAggregationRepository method add.
@Override
public Exchange add(CamelContext camelContext, String key, Exchange exchange) {
if (optimistic) {
throw new UnsupportedOperationException();
}
LOG.trace("Adding an Exchange with ID {} for key {} in a thread-safe manner.", exchange.getExchangeId(), key);
DefaultExchangeHolder newHolder = DefaultExchangeHolder.marshal(exchange, true, allowSerializedHeaders);
DefaultExchangeHolder oldHolder = cache.getAndPut(key, newHolder);
return unmarshallExchange(camelContext, oldHolder);
}
use of org.apache.camel.impl.DefaultExchangeHolder in project camel by apache.
the class JdbcCamelCodec method marshallExchange.
public byte[] marshallExchange(CamelContext camelContext, Exchange exchange, boolean allowSerializedHeaders) throws IOException {
// use DefaultExchangeHolder to marshal to a serialized object
DefaultExchangeHolder pe = DefaultExchangeHolder.marshal(exchange, false, allowSerializedHeaders);
// add the aggregated size and timeout property as the only properties we want to retain
DefaultExchangeHolder.addProperty(pe, Exchange.AGGREGATED_SIZE, exchange.getProperty(Exchange.AGGREGATED_SIZE, Integer.class));
DefaultExchangeHolder.addProperty(pe, Exchange.AGGREGATED_TIMEOUT, exchange.getProperty(Exchange.AGGREGATED_TIMEOUT, Long.class));
// add the aggregated completed by property to retain
DefaultExchangeHolder.addProperty(pe, Exchange.AGGREGATED_COMPLETED_BY, exchange.getProperty(Exchange.AGGREGATED_COMPLETED_BY, String.class));
// add the aggregated correlation key property to retain
DefaultExchangeHolder.addProperty(pe, Exchange.AGGREGATED_CORRELATION_KEY, exchange.getProperty(Exchange.AGGREGATED_CORRELATION_KEY, String.class));
DefaultExchangeHolder.addProperty(pe, Exchange.AGGREGATED_CORRELATION_KEY, exchange.getProperty(Exchange.AGGREGATED_CORRELATION_KEY, String.class));
// and a guard property if using the flexible toolbox aggregator
DefaultExchangeHolder.addProperty(pe, Exchange.AGGREGATED_COLLECTION_GUARD, exchange.getProperty(Exchange.AGGREGATED_COLLECTION_GUARD, String.class));
// persist the from endpoint as well
if (exchange.getFromEndpoint() != null) {
DefaultExchangeHolder.addProperty(pe, "CamelAggregatedFromEndpoint", exchange.getFromEndpoint().getEndpointUri());
}
return encode(pe);
}
use of org.apache.camel.impl.DefaultExchangeHolder in project camel by apache.
the class JdbcCamelCodec method decode.
private DefaultExchangeHolder decode(CamelContext camelContext, byte[] dataIn) throws IOException, ClassNotFoundException {
ByteArrayInputStream bytesIn = new ByteArrayInputStream(dataIn);
ObjectInputStream objectIn = null;
Object obj = null;
try {
objectIn = new ClassLoadingAwareObjectInputStream(camelContext, bytesIn);
obj = objectIn.readObject();
} finally {
IOHelper.close(objectIn);
}
return (DefaultExchangeHolder) obj;
}
Aggregations