Author: sebb
Date: Tue Dec 7 13:26:08 2010
New Revision: 1043037
URL: http://svn.apache.org/viewvc?rev=1043037&view=rev
Log:
Re-order methods slightly
Modified:
jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC3Impl.java
Modified:
jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC3Impl.java
URL:
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC3Impl.java?rev=1043037&r1=1043036&r2=1043037&view=diff
==============================================================================
---
jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC3Impl.java
(original)
+++
jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC3Impl.java
Tue Dec 7 13:26:08 2010
@@ -160,241 +160,6 @@ public class HTTPHC3Impl extends HTTPHCA
super(base);
}
- /*
- * Send POST data from <code>Entry</code> to the open connection.
- *
- * @param connection
- * <code>URLConnection</code> where POST data should be sent
- * @return a String show what was posted. Will not contain actual file
upload content
- * @exception IOException
- * if an I/O exception occurs
- */
- private String sendPostData(PostMethod post) throws IOException {
- // Buffer to hold the post body, except file content
- StringBuilder postedBody = new StringBuilder(1000);
- HTTPFileArg files[] = getHTTPFiles();
- // Check if we should do a multipart/form-data or an
- // application/x-www-form-urlencoded post request
- if(getUseMultipartForPost()) {
- // If a content encoding is specified, we use that as the
- // encoding of any parameter values
- String contentEncoding = getContentEncoding();
- if(contentEncoding != null && contentEncoding.length() == 0) {
- contentEncoding = null;
- }
-
- // We don't know how many entries will be skipped
- ArrayList<PartBase> partlist = new ArrayList<PartBase>();
- // Create the parts
- // Add any parameters
- PropertyIterator args = getArguments().iterator();
- while (args.hasNext()) {
- HTTPArgument arg = (HTTPArgument) args.next().getObjectValue();
- String parameterName = arg.getName();
- if (arg.isSkippable(parameterName)){
- continue;
- }
- partlist.add(new StringPart(arg.getName(), arg.getValue(),
contentEncoding));
- }
-
- // Add any files
- for (int i=0; i < files.length; i++) {
- HTTPFileArg file = files[i];
- File inputFile = new File(file.getPath());
- // We do not know the char set of the file to be uploaded, so
we set it to null
- ViewableFilePart filePart = new
ViewableFilePart(file.getParamName(), inputFile, file.getMimeType(), null);
- filePart.setCharSet(null); // We do not know what the char set
of the file is
- partlist.add(filePart);
- }
-
- // Set the multipart for the post
- int partNo = partlist.size();
- Part[] parts = partlist.toArray(new Part[partNo]);
- MultipartRequestEntity multiPart = new
MultipartRequestEntity(parts, post.getParams());
- post.setRequestEntity(multiPart);
-
- // Set the content type
- String multiPartContentType = multiPart.getContentType();
- post.setRequestHeader(HEADER_CONTENT_TYPE, multiPartContentType);
-
- // If the Multipart is repeatable, we can send it first to
- // our own stream, without the actual file content, so we can
return it
- if(multiPart.isRepeatable()) {
- // For all the file multiparts, we must tell it to not include
- // the actual file content
- for(int i = 0; i < partNo; i++) {
- if(parts[i] instanceof ViewableFilePart) {
- ((ViewableFilePart) parts[i]).setHideFileData(true);
// .sendMultipartWithoutFileContent(bos);
- }
- }
- // Write the request to our own stream
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- multiPart.writeRequest(bos);
- bos.flush();
- // We get the posted bytes using the encoding used to create it
- postedBody.append(new String(bos.toByteArray(),
- contentEncoding == null ? "US-ASCII" // $NON-NLS-1$
this is the default used by HttpClient
- : contentEncoding));
- bos.close();
-
- // For all the file multiparts, we must revert the hiding of
- // the actual file content
- for(int i = 0; i < partNo; i++) {
- if(parts[i] instanceof ViewableFilePart) {
- ((ViewableFilePart) parts[i]).setHideFileData(false);
- }
- }
- }
- else {
- postedBody.append("<Multipart was not repeatable, cannot view
what was sent>"); // $NON-NLS-1$
- }
- }
- else {
- // Check if the header manager had a content type header
- // This allows the user to specify his own content-type for a POST
request
- Header contentTypeHeader =
post.getRequestHeader(HEADER_CONTENT_TYPE);
- boolean hasContentTypeHeader = contentTypeHeader != null &&
contentTypeHeader.getValue() != null && contentTypeHeader.getValue().length() >
0;
- // If there are no arguments, we can send a file as the body of
the request
- // TODO: needs a multiple file upload scenerio
- if(!hasArguments() && getSendFileAsPostBody()) {
- // If getSendFileAsPostBody returned true, it's sure that file
is not null
- HTTPFileArg file = files[0];
- if(!hasContentTypeHeader) {
- // Allow the mimetype of the file to control the content
type
- if(file.getMimeType() != null &&
file.getMimeType().length() > 0) {
- post.setRequestHeader(HEADER_CONTENT_TYPE,
file.getMimeType());
- }
- else {
- post.setRequestHeader(HEADER_CONTENT_TYPE,
APPLICATION_X_WWW_FORM_URLENCODED);
- }
- }
-
- FileRequestEntity fileRequestEntity = new
FileRequestEntity(new File(file.getPath()),null);
- post.setRequestEntity(fileRequestEntity);
-
- // We just add placeholder text for file content
- postedBody.append("<actual file content, not shown here>");
- }
- else {
- // In a post request which is not multipart, we only support
- // parameters, no file upload is allowed
-
- // 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
- String contentEncoding = getContentEncoding();
- boolean haveContentEncoding = false;
- if(contentEncoding != null && contentEncoding.trim().length()
> 0) {
- post.getParams().setContentCharset(contentEncoding);
- haveContentEncoding = true;
- } else if (contentEncoding != null &&
contentEncoding.trim().length() == 0){
- contentEncoding=null;
- }
-
- // If none of the arguments have a name specified, we
- // just send all the values as the post body
- if(getSendParameterValuesAsPostBody()) {
- // Allow the mimetype of the file to control the content
type
- // This is not obvious in GUI if you are not uploading any
files,
- // but just sending the content of nameless parameters
- // TODO: needs a multiple file upload scenerio
- if(!hasContentTypeHeader) {
- HTTPFileArg file = files.length > 0? files[0] : null;
- if(file != null && file.getMimeType() != null &&
file.getMimeType().length() > 0) {
- post.setRequestHeader(HEADER_CONTENT_TYPE,
file.getMimeType());
- }
- else {
- // TODO - is this the correct default?
- post.setRequestHeader(HEADER_CONTENT_TYPE,
APPLICATION_X_WWW_FORM_URLENCODED);
- }
- }
-
- // Just append all the parameter values, and use that as
the post body
- StringBuilder postBody = new StringBuilder();
- PropertyIterator args = getArguments().iterator();
- while (args.hasNext()) {
- HTTPArgument arg = (HTTPArgument)
args.next().getObjectValue();
- String value;
- if (haveContentEncoding){
- value = arg.getEncodedValue(contentEncoding);
- } else {
- value = arg.getEncodedValue();
- }
- postBody.append(value);
- }
- StringRequestEntity requestEntity = new
StringRequestEntity(postBody.toString(),
post.getRequestHeader(HEADER_CONTENT_TYPE).getValue(), contentEncoding);
- post.setRequestEntity(requestEntity);
- }
- else {
- // It is a normal post request, with parameter names and
values
-
- // Set the content type
- if(!hasContentTypeHeader) {
- post.setRequestHeader(HEADER_CONTENT_TYPE,
APPLICATION_X_WWW_FORM_URLENCODED);
- }
- // Add the parameters
- PropertyIterator args = getArguments().iterator();
- while (args.hasNext()) {
- HTTPArgument arg = (HTTPArgument)
args.next().getObjectValue();
- // The HTTPClient always urlencodes both name and
value,
- // so if the argument is already encoded, we have to
decode
- // it before adding it to the post request
- String parameterName = arg.getName();
- if (arg.isSkippable(parameterName)){
- continue;
- }
- String parameterValue = arg.getValue();
- if(!arg.isAlwaysEncoded()) {
- // The value is already encoded by the user
- // Must decode the value now, so that when the
- // httpclient encodes it, we end up with the same
value
- // as the user had entered.
- String urlContentEncoding = contentEncoding;
- if(urlContentEncoding == null ||
urlContentEncoding.length() == 0) {
- // Use the default encoding for urls
- urlContentEncoding =
EncoderCache.URL_ARGUMENT_ENCODING;
- }
- parameterName = URLDecoder.decode(parameterName,
urlContentEncoding);
- parameterValue = URLDecoder.decode(parameterValue,
urlContentEncoding);
- }
- // Add the parameter, httpclient will urlencode it
- post.addParameter(parameterName, parameterValue);
- }
-
-/*
-// // Alternative implementation, to make sure that
HTTPSampler and HTTPSampler2
-// // sends the same post body.
-//
-// // Only include the content char set in the content-type
header if it is not
-// // an APPLICATION_X_WWW_FORM_URLENCODED content type
-// String contentCharSet = null;
-//
if(!post.getRequestHeader(HEADER_CONTENT_TYPE).getValue().equals(APPLICATION_X_WWW_FORM_URLENCODED))
{
-// contentCharSet = post.getRequestCharSet();
-// }
-// StringRequestEntity requestEntity = new
StringRequestEntity(getQueryString(contentEncoding),
post.getRequestHeader(HEADER_CONTENT_TYPE).getValue(), contentCharSet);
-// post.setRequestEntity(requestEntity);
-*/
- }
-
- // If the request entity is repeatable, we can send it first to
- // our own stream, so we can return it
- if(post.getRequestEntity().isRepeatable()) {
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- post.getRequestEntity().writeRequest(bos);
- bos.flush();
- // We get the posted bytes using the encoding used to
create it
- postedBody.append(new
String(bos.toByteArray(),post.getRequestCharSet()));
- bos.close();
- }
- else {
- postedBody.append("<RequestEntity was not repeatable,
cannot view what was sent>");
- }
- }
- }
- // Set the content length
- post.setRequestHeader(HEADER_CONTENT_LENGTH,
Long.toString(post.getRequestEntity().getContentLength()));
-
- return postedBody.toString();
- }
/**
* Returns an <code>HttpConnection</code> fully ready to attempt
@@ -893,6 +658,242 @@ public class HTTPHC3Impl extends HTTPHCA
}
}
+ /*
+ * Send POST data from <code>Entry</code> to the open connection.
+ *
+ * @param connection
+ * <code>URLConnection</code> where POST data should be sent
+ * @return a String show what was posted. Will not contain actual file
upload content
+ * @exception IOException
+ * if an I/O exception occurs
+ */
+ private String sendPostData(PostMethod post) throws IOException {
+ // Buffer to hold the post body, except file content
+ StringBuilder postedBody = new StringBuilder(1000);
+ HTTPFileArg files[] = getHTTPFiles();
+ // Check if we should do a multipart/form-data or an
+ // application/x-www-form-urlencoded post request
+ if(getUseMultipartForPost()) {
+ // If a content encoding is specified, we use that as the
+ // encoding of any parameter values
+ String contentEncoding = getContentEncoding();
+ if(contentEncoding != null && contentEncoding.length() == 0) {
+ contentEncoding = null;
+ }
+
+ // We don't know how many entries will be skipped
+ ArrayList<PartBase> partlist = new ArrayList<PartBase>();
+ // Create the parts
+ // Add any parameters
+ PropertyIterator args = getArguments().iterator();
+ while (args.hasNext()) {
+ HTTPArgument arg = (HTTPArgument) args.next().getObjectValue();
+ String parameterName = arg.getName();
+ if (arg.isSkippable(parameterName)){
+ continue;
+ }
+ partlist.add(new StringPart(arg.getName(), arg.getValue(),
contentEncoding));
+ }
+
+ // Add any files
+ for (int i=0; i < files.length; i++) {
+ HTTPFileArg file = files[i];
+ File inputFile = new File(file.getPath());
+ // We do not know the char set of the file to be uploaded, so
we set it to null
+ ViewableFilePart filePart = new
ViewableFilePart(file.getParamName(), inputFile, file.getMimeType(), null);
+ filePart.setCharSet(null); // We do not know what the char set
of the file is
+ partlist.add(filePart);
+ }
+
+ // Set the multipart for the post
+ int partNo = partlist.size();
+ Part[] parts = partlist.toArray(new Part[partNo]);
+ MultipartRequestEntity multiPart = new
MultipartRequestEntity(parts, post.getParams());
+ post.setRequestEntity(multiPart);
+
+ // Set the content type
+ String multiPartContentType = multiPart.getContentType();
+ post.setRequestHeader(HEADER_CONTENT_TYPE, multiPartContentType);
+
+ // If the Multipart is repeatable, we can send it first to
+ // our own stream, without the actual file content, so we can
return it
+ if(multiPart.isRepeatable()) {
+ // For all the file multiparts, we must tell it to not include
+ // the actual file content
+ for(int i = 0; i < partNo; i++) {
+ if(parts[i] instanceof ViewableFilePart) {
+ ((ViewableFilePart) parts[i]).setHideFileData(true);
// .sendMultipartWithoutFileContent(bos);
+ }
+ }
+ // Write the request to our own stream
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ multiPart.writeRequest(bos);
+ bos.flush();
+ // We get the posted bytes using the encoding used to create it
+ postedBody.append(new String(bos.toByteArray(),
+ contentEncoding == null ? "US-ASCII" // $NON-NLS-1$
this is the default used by HttpClient
+ : contentEncoding));
+ bos.close();
+
+ // For all the file multiparts, we must revert the hiding of
+ // the actual file content
+ for(int i = 0; i < partNo; i++) {
+ if(parts[i] instanceof ViewableFilePart) {
+ ((ViewableFilePart) parts[i]).setHideFileData(false);
+ }
+ }
+ }
+ else {
+ postedBody.append("<Multipart was not repeatable, cannot view
what was sent>"); // $NON-NLS-1$
+ }
+ }
+ else {
+ // Check if the header manager had a content type header
+ // This allows the user to specify his own content-type for a POST
request
+ Header contentTypeHeader =
post.getRequestHeader(HEADER_CONTENT_TYPE);
+ boolean hasContentTypeHeader = contentTypeHeader != null &&
contentTypeHeader.getValue() != null && contentTypeHeader.getValue().length() >
0;
+ // If there are no arguments, we can send a file as the body of
the request
+ // TODO: needs a multiple file upload scenerio
+ if(!hasArguments() && getSendFileAsPostBody()) {
+ // If getSendFileAsPostBody returned true, it's sure that file
is not null
+ HTTPFileArg file = files[0];
+ if(!hasContentTypeHeader) {
+ // Allow the mimetype of the file to control the content
type
+ if(file.getMimeType() != null &&
file.getMimeType().length() > 0) {
+ post.setRequestHeader(HEADER_CONTENT_TYPE,
file.getMimeType());
+ }
+ else {
+ post.setRequestHeader(HEADER_CONTENT_TYPE,
APPLICATION_X_WWW_FORM_URLENCODED);
+ }
+ }
+
+ FileRequestEntity fileRequestEntity = new
FileRequestEntity(new File(file.getPath()),null);
+ post.setRequestEntity(fileRequestEntity);
+
+ // We just add placeholder text for file content
+ postedBody.append("<actual file content, not shown here>");
+ }
+ else {
+ // In a post request which is not multipart, we only support
+ // parameters, no file upload is allowed
+
+ // 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
+ String contentEncoding = getContentEncoding();
+ boolean haveContentEncoding = false;
+ if(contentEncoding != null && contentEncoding.trim().length()
> 0) {
+ post.getParams().setContentCharset(contentEncoding);
+ haveContentEncoding = true;
+ } else if (contentEncoding != null &&
contentEncoding.trim().length() == 0){
+ contentEncoding=null;
+ }
+
+ // If none of the arguments have a name specified, we
+ // just send all the values as the post body
+ if(getSendParameterValuesAsPostBody()) {
+ // Allow the mimetype of the file to control the content
type
+ // This is not obvious in GUI if you are not uploading any
files,
+ // but just sending the content of nameless parameters
+ // TODO: needs a multiple file upload scenerio
+ if(!hasContentTypeHeader) {
+ HTTPFileArg file = files.length > 0? files[0] : null;
+ if(file != null && file.getMimeType() != null &&
file.getMimeType().length() > 0) {
+ post.setRequestHeader(HEADER_CONTENT_TYPE,
file.getMimeType());
+ }
+ else {
+ // TODO - is this the correct default?
+ post.setRequestHeader(HEADER_CONTENT_TYPE,
APPLICATION_X_WWW_FORM_URLENCODED);
+ }
+ }
+
+ // Just append all the parameter values, and use that as
the post body
+ StringBuilder postBody = new StringBuilder();
+ PropertyIterator args = getArguments().iterator();
+ while (args.hasNext()) {
+ HTTPArgument arg = (HTTPArgument)
args.next().getObjectValue();
+ String value;
+ if (haveContentEncoding){
+ value = arg.getEncodedValue(contentEncoding);
+ } else {
+ value = arg.getEncodedValue();
+ }
+ postBody.append(value);
+ }
+ StringRequestEntity requestEntity = new
StringRequestEntity(postBody.toString(),
post.getRequestHeader(HEADER_CONTENT_TYPE).getValue(), contentEncoding);
+ post.setRequestEntity(requestEntity);
+ }
+ else {
+ // It is a normal post request, with parameter names and
values
+
+ // Set the content type
+ if(!hasContentTypeHeader) {
+ post.setRequestHeader(HEADER_CONTENT_TYPE,
APPLICATION_X_WWW_FORM_URLENCODED);
+ }
+ // Add the parameters
+ PropertyIterator args = getArguments().iterator();
+ while (args.hasNext()) {
+ HTTPArgument arg = (HTTPArgument)
args.next().getObjectValue();
+ // The HTTPClient always urlencodes both name and
value,
+ // so if the argument is already encoded, we have to
decode
+ // it before adding it to the post request
+ String parameterName = arg.getName();
+ if (arg.isSkippable(parameterName)){
+ continue;
+ }
+ String parameterValue = arg.getValue();
+ if(!arg.isAlwaysEncoded()) {
+ // The value is already encoded by the user
+ // Must decode the value now, so that when the
+ // httpclient encodes it, we end up with the same
value
+ // as the user had entered.
+ String urlContentEncoding = contentEncoding;
+ if(urlContentEncoding == null ||
urlContentEncoding.length() == 0) {
+ // Use the default encoding for urls
+ urlContentEncoding =
EncoderCache.URL_ARGUMENT_ENCODING;
+ }
+ parameterName = URLDecoder.decode(parameterName,
urlContentEncoding);
+ parameterValue = URLDecoder.decode(parameterValue,
urlContentEncoding);
+ }
+ // Add the parameter, httpclient will urlencode it
+ post.addParameter(parameterName, parameterValue);
+ }
+
+/*
+// // Alternative implementation, to make sure that
HTTPSampler and HTTPSampler2
+// // sends the same post body.
+//
+// // Only include the content char set in the content-type
header if it is not
+// // an APPLICATION_X_WWW_FORM_URLENCODED content type
+// String contentCharSet = null;
+//
if(!post.getRequestHeader(HEADER_CONTENT_TYPE).getValue().equals(APPLICATION_X_WWW_FORM_URLENCODED))
{
+// contentCharSet = post.getRequestCharSet();
+// }
+// StringRequestEntity requestEntity = new
StringRequestEntity(getQueryString(contentEncoding),
post.getRequestHeader(HEADER_CONTENT_TYPE).getValue(), contentCharSet);
+// post.setRequestEntity(requestEntity);
+*/
+ }
+
+ // If the request entity is repeatable, we can send it first to
+ // our own stream, so we can return it
+ if(post.getRequestEntity().isRepeatable()) {
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ post.getRequestEntity().writeRequest(bos);
+ bos.flush();
+ // We get the posted bytes using the encoding used to
create it
+ postedBody.append(new
String(bos.toByteArray(),post.getRequestCharSet()));
+ bos.close();
+ }
+ else {
+ postedBody.append("<RequestEntity was not repeatable,
cannot view what was sent>");
+ }
+ }
+ }
+ // Set the content length
+ post.setRequestHeader(HEADER_CONTENT_LENGTH,
Long.toString(post.getRequestEntity().getContentLength()));
+
+ return postedBody.toString();
+ }
+
/**
* Set up the PUT data
*/
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]