Skip to content

Commit

Permalink
CAMEL-13146: Allow JdbcCamelCodec to be used without requiring byte a…
Browse files Browse the repository at this point in the history
…rrays
  • Loading branch information
Marc Carter authored and oscerd committed Feb 6, 2019
1 parent 694a4d8 commit f7b3db9
Showing 1 changed file with 19 additions and 12 deletions.
Expand Up @@ -19,8 +19,10 @@
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.io.OutputStream;


import org.apache.camel.CamelContext; import org.apache.camel.CamelContext;
import org.apache.camel.Endpoint; import org.apache.camel.Endpoint;
Expand All @@ -35,6 +37,12 @@
public class JdbcCamelCodec { public class JdbcCamelCodec {


public byte[] marshallExchange(CamelContext camelContext, Exchange exchange, boolean allowSerializedHeaders) throws IOException { public byte[] marshallExchange(CamelContext camelContext, Exchange exchange, boolean allowSerializedHeaders) throws IOException {
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
marshallExchange(camelContext, exchange, allowSerializedHeaders, bytesOut);
return bytesOut.toByteArray();
}

public void marshallExchange(CamelContext camelContext, Exchange exchange, boolean allowSerializedHeaders, OutputStream outputStream) throws IOException {
// use DefaultExchangeHolder to marshal to a serialized object // use DefaultExchangeHolder to marshal to a serialized object
DefaultExchangeHolder pe = DefaultExchangeHolder.marshal(exchange, false, allowSerializedHeaders); DefaultExchangeHolder pe = DefaultExchangeHolder.marshal(exchange, false, allowSerializedHeaders);
// add the aggregated size and timeout property as the only properties we want to retain // add the aggregated size and timeout property as the only properties we want to retain
Expand All @@ -51,11 +59,15 @@ public byte[] marshallExchange(CamelContext camelContext, Exchange exchange, boo
if (exchange.getFromEndpoint() != null) { if (exchange.getFromEndpoint() != null) {
DefaultExchangeHolder.addProperty(pe, "CamelAggregatedFromEndpoint", exchange.getFromEndpoint().getEndpointUri()); DefaultExchangeHolder.addProperty(pe, "CamelAggregatedFromEndpoint", exchange.getFromEndpoint().getEndpointUri());
} }
return encode(pe); encode(pe, outputStream);
} }


public Exchange unmarshallExchange(CamelContext camelContext, byte[] buffer) throws IOException, ClassNotFoundException { public Exchange unmarshallExchange(CamelContext camelContext, byte[] buffer) throws IOException, ClassNotFoundException {
DefaultExchangeHolder pe = decode(camelContext, buffer); return unmarshallExchange(camelContext, new ByteArrayInputStream(buffer));
}

public Exchange unmarshallExchange(CamelContext camelContext, InputStream inputStream) throws IOException, ClassNotFoundException {
DefaultExchangeHolder pe = decode(camelContext, inputStream);
Exchange answer = new DefaultExchange(camelContext); Exchange answer = new DefaultExchange(camelContext);
DefaultExchangeHolder.unmarshal(answer, pe); DefaultExchangeHolder.unmarshal(answer, pe);
// restore the from endpoint // restore the from endpoint
Expand All @@ -69,18 +81,13 @@ public Exchange unmarshallExchange(CamelContext camelContext, byte[] buffer) thr
return answer; return answer;
} }


private byte[] encode(Object object) throws IOException { private void encode(Object object, OutputStream bytesOut) throws IOException {
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); try (ObjectOutputStream objectOut = new ObjectOutputStream(bytesOut)) {
ObjectOutputStream objectOut = new ObjectOutputStream(bytesOut); objectOut.writeObject(object);
objectOut.writeObject(object); }
objectOut.close();
byte[] data = bytesOut.toByteArray();
return data;
} }


private DefaultExchangeHolder decode(CamelContext camelContext, byte[] dataIn) throws IOException, ClassNotFoundException { private DefaultExchangeHolder decode(CamelContext camelContext, InputStream bytesIn) throws IOException, ClassNotFoundException {
ByteArrayInputStream bytesIn = new ByteArrayInputStream(dataIn);

ObjectInputStream objectIn = null; ObjectInputStream objectIn = null;
Object obj = null; Object obj = null;
try { try {
Expand Down

0 comments on commit f7b3db9

Please sign in to comment.