Serializable obj = exchange.getIn().getMandatoryBody(Serializable.class);
// write object to output stream
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
HttpHelper.writeObjectToStream(bos, obj);
httpExchange.setRequestContent(new ByteArrayBuffer(bos.toByteArray()));
} finally {
IOHelper.close(bos, "body", LOG);
}
} else {
Object body = exchange.getIn().getBody();
if (body instanceof String) {
String data = (String) body;
// be a bit careful with String as any type can most likely be converted to String
// so we only do an instanceof check and accept String if the body is really a String
// do not fallback to use the default charset as it can influence the request
// (for example application/x-www-form-urlencoded forms being sent)
String charset = IOHelper.getCharsetName(exchange, false);
if (charset != null) {
httpExchange.setRequestContent(new ByteArrayBuffer(data, charset));
} else {
httpExchange.setRequestContent(new ByteArrayBuffer(data));
}
} else {
// then fallback to input stream
InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, exchange, exchange.getIn().getBody());
httpExchange.setRequestContentSource(is);