KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > protocol > http > sampler > PostWriter


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/sampler/PostWriter.java,v 1.12.2.2 2004/06/12 20:28:50 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.protocol.http.sampler;
20
21 import java.io.BufferedInputStream JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.io.PrintWriter JavaDoc;
27 import java.io.UnsupportedEncodingException JavaDoc;
28 import java.net.HttpURLConnection JavaDoc;
29 import java.net.URLConnection JavaDoc;
30
31 import org.apache.jmeter.config.Argument;
32 import org.apache.jmeter.testelement.property.PropertyIterator;
33
34 /**
35  * @version $Revision: 1.12.2.2 $
36  */

37
38 public class PostWriter
39 {
40     protected final static String JavaDoc BOUNDARY =
41         "---------------------------7d159c1302d0y0";
42     private final static byte[] CRLF = { 0x0d, 0x0A };
43     //protected static int fudge = -20;
44
protected static final String JavaDoc encoding = "iso-8859-1";
45
46     /**
47      * Send POST data from Entry to the open connection.
48      */

49     public void sendPostData(URLConnection JavaDoc connection, HTTPSampler sampler)
50         throws IOException JavaDoc
51     {
52         // If filename was specified then send the post using multipart syntax
53
String JavaDoc filename = sampler.getFilename();
54         if ((filename != null) && (filename.trim().length() > 0))
55         {
56             OutputStream JavaDoc out = connection.getOutputStream();
57             //new FileOutputStream("c:\\data\\experiment.txt");
58
//new ByteArrayOutputStream();
59
writeln(out, "--" + BOUNDARY);
60             PropertyIterator args = sampler.getArguments().iterator();
61             while (args.hasNext())
62             {
63                 Argument arg = (Argument) args.next().getObjectValue();
64                 writeFormMultipartStyle(
65                     out,
66                     arg.getName(),
67                     (String JavaDoc) arg.getValue());
68                 writeln(out, "--" + BOUNDARY);
69             }
70             writeFileToURL(
71                 out,
72                 filename,
73                 sampler.getFileField(),
74                 getFileStream(filename),
75                 sampler.getMimetype());
76
77             writeln(out, "--" + BOUNDARY + "--");
78             out.flush();
79             out.close();
80         }
81
82         // No filename specified, so send the post using normal syntax
83
else
84         {
85             String JavaDoc postData = sampler.getQueryString();
86             PrintWriter JavaDoc out = new PrintWriter JavaDoc(connection.getOutputStream());
87             out.print(postData);
88             out.flush();
89         }
90     }
91
92     public void setHeaders(URLConnection JavaDoc connection, HTTPSampler sampler)
93         throws IOException JavaDoc
94     {
95         ((HttpURLConnection JavaDoc) connection).setRequestMethod("POST");
96
97         // If filename was specified then send the post using multipart syntax
98
String JavaDoc filename = sampler.getFileField();
99         if ((filename != null) && (filename.trim().length() > 0))
100         {
101             connection.setRequestProperty(
102                 "Content-Type",
103                 "multipart/form-data; boundary=" + BOUNDARY);
104             connection.setDoOutput(true);
105             connection.setDoInput(true);
106         }
107
108         // No filename specified, so send the post using normal syntax
109
else
110         {
111             String JavaDoc postData = sampler.getQueryString();
112             connection.setRequestProperty(
113                 "Content-Length",
114                 "" + postData.length());
115             connection.setRequestProperty(
116                 "Content-Type",
117                 "application/x-www-form-urlencoded");
118             connection.setDoOutput(true);
119         }
120     }
121
122     private InputStream JavaDoc getFileStream(String JavaDoc filename) throws IOException JavaDoc
123     {
124         return new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(filename));
125     }
126
127     /* NOTUSED
128     private String getContentLength(MultipartUrlConfig config)
129     {
130         long size = 0;
131         size += BOUNDARY.length() + 2;
132         PropertyIterator iter = config.getArguments().iterator();
133         while (iter.hasNext())
134         {
135             Argument item = (Argument) iter.next().getObjectValue();
136             size += item.getName().length()
137                 + item.getValue().toString().length();
138             size += CRLF.length * 4;
139             size += BOUNDARY.length() + 2;
140             size += 39;
141         }
142         size += new File(config.getFilename()).length();
143         size += CRLF.length * 5;
144         size += BOUNDARY.length() + 2;
145         size += encode(config.getFileFieldName()).length();
146         size += encode(config.getFilename()).length();
147         size += config.getMimeType().length();
148         size += 66;
149         size += 2 + (CRLF.length * 1);
150         return Long.toString(size);
151     }
152     */

153     
154     /**
155      * Writes out the contents of a file in correct multipart format.
156      */

157     private void writeFileToURL(
158         OutputStream JavaDoc out,
159         String JavaDoc filename,
160         String JavaDoc fieldname,
161         InputStream JavaDoc in,
162         String JavaDoc mimetype)
163         throws IOException JavaDoc
164     {
165         writeln(
166             out,
167             "Content-Disposition: form-data; name=\""
168                 + encode(fieldname)
169                 + "\"; filename=\""
170                 + encode(filename)
171                 + "\"");
172         writeln(out, "Content-Type: " + mimetype);
173         out.write(CRLF);
174
175         byte[] buf = new byte[1024];
176             //1k - the previous 100k made no sense (there's tons of buffers
177
// elsewhere in the chain) and it caused OOM when many concurrent
178
// uploads were being done. Could be fixed by increasing the evacuation
179
// ratio in bin/jmeter[.bat], but this is better.
180
int read;
181         while ((read = in.read(buf)) > 0)
182         {
183             out.write(buf, 0, read);
184         }
185         out.write(CRLF);
186         in.close();
187     }
188
189     /**
190      * Writes form data in multipart format.
191      */

192     private void writeFormMultipartStyle(
193         OutputStream JavaDoc out,
194         String JavaDoc name,
195         String JavaDoc value)
196         throws IOException JavaDoc
197     {
198         writeln(out, "Content-Disposition: form-data; name=\"" + name + "\"");
199         out.write(CRLF);
200         writeln(out, value);
201     }
202
203     private String JavaDoc encode(String JavaDoc value)
204     {
205         StringBuffer JavaDoc newValue = new StringBuffer JavaDoc();
206         char[] chars = value.toCharArray();
207         for (int i = 0; i < chars.length; i++)
208         {
209             if (chars[i] == '\\')
210             {
211                 newValue.append("\\\\");
212             }
213             else
214             {
215                 newValue.append(chars[i]);
216             }
217         }
218         return newValue.toString();
219     }
220
221     /* NOTUSED
222     private void write(OutputStream out, String value)
223         throws UnsupportedEncodingException, IOException
224     {
225         out.write(value.getBytes(encoding));
226     }
227     */

228
229     private void writeln(OutputStream JavaDoc out, String JavaDoc value)
230         throws UnsupportedEncodingException JavaDoc, IOException JavaDoc
231     {
232         out.write(value.getBytes(encoding));
233         out.write(CRLF);
234     }
235 }
236
Popular Tags