khammond 01/10/07 08:34:26
Modified: src/org/apache/jmeter/protocol/http/sampler
MultipartFormSampler.java
Log:
Added logic. If no file specified, then send request using normal syntax (not
multipart form syntax).
Revision Changes Path
1.6 +106 -29
jakarta-jmeter/src/org/apache/jmeter/protocol/http/sampler/MultipartFormSampler.java
Index: MultipartFormSampler.java
===================================================================
RCS file:
/home/cvs/jakarta-jmeter/src/org/apache/jmeter/protocol/http/sampler/MultipartFormSampler.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- MultipartFormSampler.java 2001/08/01 13:38:45 1.5
+++ MultipartFormSampler.java 2001/10/07 15:34:26 1.6
@@ -1,3 +1,58 @@
+/*
+ * ====================================================================
+ * The Apache Software License, Version 1.1
+ *
+ * Copyright (c) 2001 The Apache Software Foundation. All rights
+ * reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ * if any, must include the following acknowledgment:
+ * "This product includes software developed by the
+ * Apache Software Foundation (http://www.apache.org/)."
+ * Alternately, this acknowledgment may appear in the software itself,
+ * if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. The names "Apache" and "Apache Software Foundation" and
+ * "Apache JMeter" must not be used to endorse or promote products
+ * derived from this software without prior written permission. For
+ * written permission, please contact [EMAIL PROTECTED]
+ *
+ * 5. Products derived from this software may not be called "Apache",
+ * "Apache JMeter", nor may "Apache" appear in their name, without
+ * prior written permission of the Apache Software Foundation.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ */
+
package org.apache.jmeter.protocol.http.sampler;
import java.io.*;
@@ -21,9 +76,9 @@
public class MultipartFormSampler extends HTTPSampler
{
- private final static String BOUNDARY =
"---------------------------7d159c1302d0y0";
- private final static byte[] CRLF = {0x0d,0x0A};
- private static int fudge = -20;
+ protected final static String BOUNDARY =
"---------------------------7d159c1302d0y0";
+ protected final static byte[] CRLF = {0x0d,0x0A};
+ protected static int fudge = -20;
public MultipartFormSampler()
{
@@ -44,6 +99,9 @@
{
sendPostData(conn, url);
}
+ else
+ {
+ }
}
/************************************************************
@@ -58,27 +116,46 @@
{
MultipartUrlConfig config = (MultipartUrlConfig)url;
((HttpURLConnection)connection).setRequestMethod("POST");
- connection.setRequestProperty("Content-type", "multipart/form-data;
boundary=" + BOUNDARY);
- connection.setDoOutput(true);
- connection.setDoInput(true);
- OutputStream out = connection.getOutputStream();//new
FileOutputStream("c:\\data\\experiment.txt");//new ByteArrayOutputStream();//
- writeln(out,"--"+BOUNDARY);
- Iterator args = config.getArguments().iterator();
- while (args.hasNext())
- {
- Argument arg = (Argument)args.next();
- writeFormMultipartStyle(out, arg.getName(),
(String)arg.getValue());
- writeln(out,"--" + BOUNDARY);
- }
- writeFileToURL(out,config.getFilename(),config.getFileFieldName(),
-
config.getFileStream(),config.getMimeType());
-
- writeln(out,"--" + BOUNDARY+"--");
- out.flush();
- out.close();
+
+ // If filename was specified then send the post using multipart syntax
+ String filename = config.getFilename();
+ if ((filename != null) && (filename.trim().length() > 0))
+ {
+ connection.setRequestProperty("Content-type",
"multipart/form-data; boundary=" + BOUNDARY);
+ connection.setDoOutput(true);
+ connection.setDoInput(true);
+ OutputStream out = connection.getOutputStream();//new
FileOutputStream("c:\\data\\experiment.txt");//new ByteArrayOutputStream();//
+ writeln(out,"--"+BOUNDARY);
+ Iterator args = config.getArguments().iterator();
+ while (args.hasNext())
+ {
+ Argument arg = (Argument)args.next();
+ writeFormMultipartStyle(out, arg.getName(),
(String)arg.getValue());
+ writeln(out,"--" + BOUNDARY);
+ }
+ writeFileToURL(out, filename, config.getFileFieldName(),
+ config.getFileStream(),config.getMimeType());
+
+ writeln(out,"--" + BOUNDARY+"--");
+ out.flush();
+ out.close();
+ }
+
+ // No filename specified, so send the post using normal syntax
+ else
+ {
+ String postData = url.getQueryString();
+ connection.setRequestProperty("Content-length", "" +
postData.length());
+ connection.setRequestProperty("Content-type",
"application/x-www-form-urlencoded");
+ connection.setDoOutput(true);
+
+ PrintWriter out = new
PrintWriter(connection.getOutputStream());
+ out.print(postData);
+ out.close();
+ }
}
- private String getContentLength(MultipartUrlConfig config)
+ protected String getContentLength(MultipartUrlConfig config)
{
long size = 0;
size += BOUNDARY.length()+2;
@@ -102,7 +179,7 @@
return Long.toString(size);
}
- /************************************************************
+ /************************************************************
* Writes out the contents of a file in correct multipart format.
*
*@param o Description of Parameter
@@ -112,7 +189,7 @@
*@param mimetype Description of Parameter
*@exception IOException Description of Exception
***********************************************************/
- private void writeFileToURL(OutputStream out, String filename, String
fieldname,
+ protected void writeFileToURL(OutputStream out, String filename, String
fieldname,
InputStream in, String mimetype) throws IOException
{
writeln(out,"Content-Disposition: form-data; name=\"" +
encode(fieldname) + "\"; filename=\"" +
@@ -137,14 +214,14 @@
*@param name Description of Parameter
*@param value Description of Parameter
***********************************************************/
- private void writeFormMultipartStyle(OutputStream out, String name, String
value) throws IOException
+ protected void writeFormMultipartStyle(OutputStream out, String name, String
value) throws IOException
{
writeln(out,"Content-Disposition: form-data; name=\"" + name + "\"");
out.write(CRLF);
writeln(out,value);
}
- private String encode(String value)
+ protected String encode(String value)
{
StringBuffer newValue = new StringBuffer();
char[] chars = value.toCharArray();
@@ -162,14 +239,14 @@
return newValue.toString();
}
- private void write(OutputStream out,String value) throws
UnsupportedEncodingException,IOException
+ protected void write(OutputStream out,String value) throws
UnsupportedEncodingException,IOException
{
out.write(value.getBytes(encoding));
}
- private void writeln(OutputStream out,String value) throws
UnsupportedEncodingException, IOException
+ protected void writeln(OutputStream out,String value) throws
UnsupportedEncodingException, IOException
{
out.write(value.getBytes(encoding));
out.write(CRLF);
}
-}
\ No newline at end of file
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]