// Check for local contentEncoding override
final String contentEncoding = getContentEncoding();
boolean haveContentEncoding = (contentEncoding != null && contentEncoding.trim().length() > 0);
HttpParams putParams = put.getParams();
HTTPFileArg files[] = getHTTPFiles();
// If there are no arguments, we can send a file as the body of the request
if(!hasArguments() && getSendFileAsPostBody()) {
hasPutBody = true;
// If getSendFileAsPostBody returned true, it's sure that file is not null
FileEntity fileRequestEntity = new FileEntity(new File(files[0].getPath()), (String) null); // TODO is null correct?
put.setEntity(fileRequestEntity);
// We just add placeholder text for file content
putBody.append("<actual file content, not shown here>");
}
// If none of the arguments have a name specified, we
// just send all the values as the put body
else if(getSendParameterValuesAsPostBody()) {
hasPutBody = true;
// If a content encoding is specified, we set it as http parameter, so that
// the post body will be encoded in the specified content encoding
if(haveContentEncoding) {
putParams.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET,contentEncoding);
}
// Just append all the parameter values, and use that as the post body
StringBuilder putBodyContent = new StringBuilder();
PropertyIterator args = getArguments().iterator();
while (args.hasNext()) {
HTTPArgument arg = (HTTPArgument) args.next().getObjectValue();
String value = null;
if (haveContentEncoding){
value = arg.getEncodedValue(contentEncoding);
} else {
value = arg.getEncodedValue();
}
putBodyContent.append(value);
}
String contentTypeValue = null;
if(hasContentTypeHeader) {
contentTypeValue = put.getFirstHeader(HEADER_CONTENT_TYPE).getValue();
}
StringEntity requestEntity = new StringEntity(putBodyContent.toString(), contentTypeValue,
(String) putParams.getParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET));
put.setEntity(requestEntity);
}
// Check if we have any content to send for body
if(hasPutBody) {
// If the request entity is repeatable, we can send it first to
// our own stream, so we can return it
if(put.getEntity().isRepeatable()) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
put.getEntity().writeTo(bos);
bos.flush();
// We get the posted bytes using the charset that was used to create them
putBody.append(new String(bos.toByteArray(),
(String) putParams.getParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET)));
bos.close();
}
else {
putBody.append("<RequestEntity was not repeatable, cannot view what was sent>");
}