if (answer == null) {
try {
Object data = in.getBody();
if (data != null) {
String contentTypeString = ExchangeHelper.getContentType(exchange);
ContentType contentType = null;
//Check the contentType is valid or not, If not it throws an exception.
//When ContentType.parse parse method parse "multipart/form-data;boundary=---------------------------j2radvtrk",
//it removes "boundary" from Content-Type; I have to use contentType.create method.
if (contentTypeString != null) {
contentType = ContentType.create(contentTypeString);
}
if (contentTypeString != null && HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT.equals(contentTypeString)) {
// serialized java object
Serializable obj = in.getMandatoryBody(Serializable.class);
// write object to output stream
ByteArrayOutputStream bos = new ByteArrayOutputStream();
HttpHelper.writeObjectToStream(bos, obj);
ByteArrayEntity entity = new ByteArrayEntity(bos.toByteArray());
entity.setContentType(HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT);
IOHelper.close(bos);
answer = entity;
} else if (data instanceof File || data instanceof GenericFile) {
// file based (could potentially also be a FTP file etc)
File file = in.getBody(File.class);
if (file != null) {
if (contentType != null) {
answer = new FileEntity(file, contentType);
} else {
answer = new FileEntity(file);
}
}
} else if (data instanceof String) {
// 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 && contentType != null) {
// okay try to get the charset from the content-type
Charset cs = contentType.getCharset();
if (cs != null) {
charset = cs.name();
}
}
StringEntity entity = new StringEntity((String) data, charset);
if (contentType != null) {
entity.setContentType(contentType.toString());
}
answer = entity;
}
// fallback as input stream
if (answer == null) {
// force the body as an input stream since this is the fallback
InputStream is = in.getMandatoryBody(InputStream.class);
InputStreamEntity entity = new InputStreamEntity(is, -1);
if (contentType != null) {
entity.setContentType(contentType.toString());
}
answer = entity;
}
}
} catch (UnsupportedEncodingException e) {