Closure cl = (Closure) bodyObj;
Object returnFromBodyClosure = cl.call(new Object[]{out});
if (returnFromBodyClosure instanceof Message) {
return (Message) returnFromBodyClosure;
} else if (returnFromBodyClosure instanceof String) {
return new Message(((String) returnFromBodyClosure).getBytes(), msgProps);
} else {
out.flush();
return new Message(out.toByteArray(), msgProps);
}
} else if (bodyObj instanceof String || bodyObj instanceof GString) {
// If it's sort of a String, toString() it
return new Message(bodyObj.toString().getBytes(), msgProps);
} else if (bodyObj instanceof byte[]) {
// If it's raw bytes, don't do anything
return new Message((byte[]) bodyObj, msgProps);
} else {
// Otherwise, write the object out using JDK serialization
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream oout = new ObjectOutputStream(bout);
oout.writeObject(bodyObj);
oout.flush();
oout.close();
bout.flush();
return new Message(bout.toByteArray(), msgProps);
}
}