Line 0
Link Here
|
|
|
1 |
/* |
2 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
3 |
* contributor license agreements. See the NOTICE file distributed with |
4 |
* this work for additional information regarding copyright ownership. |
5 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
6 |
* (the "License"); you may not use this file except in compliance with |
7 |
* the License. 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 |
|
22 |
import java.io.ByteArrayOutputStream; |
23 |
|
24 |
import java.io.IOException; |
25 |
import java.io.InputStream; |
26 |
|
27 |
import java.io.ObjectOutputStream; |
28 |
|
29 |
import java.net.MalformedURLException; |
30 |
import java.net.URL; |
31 |
import java.util.zip.GZIPInputStream; |
32 |
|
33 |
import com.thoughtworks.xstream.XStream; |
34 |
import com.thoughtworks.xstream.io.xml.DomDriver; |
35 |
|
36 |
import org.apache.commons.httpclient.HttpClient; |
37 |
import org.apache.commons.httpclient.methods.PostMethod; |
38 |
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity; |
39 |
import org.apache.jmeter.protocol.http.control.CacheManager; |
40 |
import org.apache.jmeter.protocol.http.control.Header; |
41 |
import org.apache.jmeter.protocol.http.control.HeaderManager; |
42 |
import org.apache.jmeter.protocol.http.util.HTTPConstants; |
43 |
import org.apache.jmeter.samplers.Interruptible; |
44 |
|
45 |
import org.apache.jorphan.logging.LoggingManager; |
46 |
import org.apache.jorphan.util.JOrphanUtils; |
47 |
import org.apache.log.Logger; |
48 |
|
49 |
import com.thoughtworks.xstream.XStream; |
50 |
|
51 |
/** |
52 |
* Commons HTTPClient based Java Serialization sampler |
53 |
*/ |
54 |
public class JavaSerializedSampler extends HTTPSampler2 implements |
55 |
Interruptible { // Implemented by parent class |
56 |
private static final Logger log = LoggingManager.getLoggerForClass(); |
57 |
|
58 |
private static final long serialVersionUID = 240L; |
59 |
|
60 |
public static final String URL_DATA = "JavaSerializedSampler.url";//$NON-NLS-1$ |
61 |
|
62 |
public static final String XSTREAM_DATA = "JavaSerializedSampler.xstream_data"; //$NON-NLS-1$ |
63 |
|
64 |
private static final String ENCODING = "utf-8"; //$NON-NLS-1$ |
65 |
|
66 |
public void setUrlData(String data) { |
67 |
setProperty(URL_DATA, data); |
68 |
} |
69 |
|
70 |
public String getUrlData() { |
71 |
return getPropertyAsString(URL_DATA); |
72 |
} |
73 |
|
74 |
public void setXstreamData(String data) { |
75 |
setProperty(XSTREAM_DATA, data); |
76 |
} |
77 |
|
78 |
public String getXstreamData() { |
79 |
return getPropertyAsString(XSTREAM_DATA); |
80 |
} |
81 |
|
82 |
protected void setPostHeaders(PostMethod post) { |
83 |
int length = 0;// Take length from file |
84 |
if (getHeaderManager() != null) { |
85 |
// headerManager was set, so let's set the connection |
86 |
// to use it. |
87 |
HeaderManager mngr = getHeaderManager(); |
88 |
int headerSize = mngr.size(); |
89 |
for (int idx = 0; idx < headerSize; idx++) { |
90 |
Header hd = mngr.getHeader(idx); |
91 |
if (HTTPConstants.HEADER_CONTENT_LENGTH.equalsIgnoreCase(hd |
92 |
.getName())) {// Use this to override file length |
93 |
length = Integer.parseInt(hd.getValue()); |
94 |
} |
95 |
|
96 |
} |
97 |
} |
98 |
} |
99 |
|
100 |
/** |
101 |
* Send POST data from <code>Entry</code> to the open connection. |
102 |
* |
103 |
* @param post |
104 |
* @throws IOException |
105 |
* if an I/O exception occurs |
106 |
*/ |
107 |
private String sendPostData(PostMethod post) { |
108 |
// Buffer to hold the post body, except file content |
109 |
StringBuilder postedBody = new StringBuilder(1000); |
110 |
XStream xstream = new XStream(new DomDriver()); |
111 |
Object o = xstream.fromXML(getXstreamData()); |
112 |
ByteArrayOutputStream bao = new ByteArrayOutputStream(); |
113 |
ObjectOutputStream oos = null; |
114 |
byte[] serialized_data = null; |
115 |
try { |
116 |
oos = new ObjectOutputStream(bao); |
117 |
oos.writeObject(o); |
118 |
serialized_data = bao.toByteArray(); |
119 |
postedBody.append(serialized_data); |
120 |
post.setRequestEntity(new ByteArrayRequestEntity(serialized_data)); |
121 |
} catch (IOException e) { |
122 |
|
123 |
} |
124 |
return postedBody.toString(); |
125 |
} |
126 |
|
127 |
protected HTTPSampleResult sample(URL url, String method, |
128 |
boolean areFollowingRedirect, int frameDepth) { |
129 |
String urlStr = url.toString(); |
130 |
|
131 |
log.debug("Start : sample " + urlStr); |
132 |
|
133 |
PostMethod httpMethod; |
134 |
httpMethod = new PostMethod(urlStr); |
135 |
|
136 |
HTTPSampleResult res = new HTTPSampleResult(); |
137 |
res.setMonitor(false); |
138 |
|
139 |
res.setSampleLabel(urlStr); // May be replaced later |
140 |
res.setHTTPMethod(HTTPConstants.POST); |
141 |
res.setURL(url); |
142 |
res.sampleStart(); // Count the retries as well in the time |
143 |
HttpClient client = null; |
144 |
InputStream instream = null; |
145 |
try { |
146 |
setPostHeaders(httpMethod); |
147 |
client = setupConnection(url, httpMethod, res); |
148 |
setSavedClient(client); |
149 |
res.setQueryString(sendPostData(httpMethod)); |
150 |
int statusCode = client.executeMethod(httpMethod); |
151 |
// Some headers are set by executeMethod() |
152 |
res.setRequestHeaders(getConnectionHeaders(httpMethod)); |
153 |
|
154 |
// Request sent. Now get the response: |
155 |
instream = httpMethod.getResponseBodyAsStream(); |
156 |
|
157 |
if (instream != null) {// will be null for HEAD |
158 |
|
159 |
org.apache.commons.httpclient.Header responseHeader = httpMethod |
160 |
.getResponseHeader(HTTPConstants.HEADER_CONTENT_ENCODING); |
161 |
if (responseHeader != null |
162 |
&& HTTPConstants.ENCODING_GZIP.equals(responseHeader |
163 |
.getValue())) { |
164 |
instream = new GZIPInputStream(instream); |
165 |
} |
166 |
|
167 |
// int contentLength = httpMethod.getResponseContentLength();Not |
168 |
// visible ... |
169 |
// TODO size ouststream according to actual content length |
170 |
ByteArrayOutputStream outstream = new ByteArrayOutputStream( |
171 |
4 * 1024); |
172 |
// contentLength > 0 ? contentLength : |
173 |
// DEFAULT_INITIAL_BUFFER_SIZE); |
174 |
byte[] buffer = new byte[4096]; |
175 |
int len; |
176 |
boolean first = true;// first response |
177 |
while ((len = instream.read(buffer)) > 0) { |
178 |
if (first) { // save the latency |
179 |
res.latencyEnd(); |
180 |
first = false; |
181 |
} |
182 |
outstream.write(buffer, 0, len); |
183 |
} |
184 |
|
185 |
res.setResponseData(outstream.toByteArray()); |
186 |
outstream.close(); |
187 |
|
188 |
} |
189 |
|
190 |
res.sampleEnd(); |
191 |
// Done with the sampling proper. |
192 |
|
193 |
// Now collect the results into the HTTPSampleResult: |
194 |
|
195 |
res.setSampleLabel(httpMethod.getURI().toString()); |
196 |
// Pick up Actual path (after redirects) |
197 |
|
198 |
res.setResponseCode(Integer.toString(statusCode)); |
199 |
res.setSuccessful(isSuccessCode(statusCode)); |
200 |
|
201 |
res.setResponseMessage(httpMethod.getStatusText()); |
202 |
|
203 |
// Set up the defaults (may be overridden below) |
204 |
res.setDataEncoding(ENCODING); |
205 |
String ct = null; |
206 |
org.apache.commons.httpclient.Header h = httpMethod |
207 |
.getResponseHeader(HTTPConstants.HEADER_CONTENT_TYPE); |
208 |
if (h != null)// Can be missing, e.g. on redirect |
209 |
{ |
210 |
ct = h.getValue(); |
211 |
res.setContentType(ct);// e.g. text/html; charset=ISO-8859-1 |
212 |
res.setEncodingAndType(ct); |
213 |
} |
214 |
|
215 |
res.setResponseHeaders(getResponseHeaders(httpMethod)); |
216 |
if (res.isRedirect()) { |
217 |
res.setRedirectLocation(httpMethod.getResponseHeader( |
218 |
HTTPConstants.HEADER_LOCATION).getValue()); |
219 |
} |
220 |
|
221 |
// If we redirected automatically, the URL may have changed |
222 |
if (getAutoRedirects()) { |
223 |
res.setURL(new URL(httpMethod.getURI().toString())); |
224 |
} |
225 |
|
226 |
// Store any cookies received in the cookie manager: |
227 |
saveConnectionCookies(httpMethod, res.getURL(), getCookieManager()); |
228 |
|
229 |
// Save cache information |
230 |
final CacheManager cacheManager = getCacheManager(); |
231 |
if (cacheManager != null) { |
232 |
cacheManager.saveDetails(httpMethod, res); |
233 |
} |
234 |
|
235 |
// Follow redirects and download page resources if appropriate: |
236 |
res = resultProcessing(areFollowingRedirect, frameDepth, res); |
237 |
|
238 |
log.debug("End : sample"); |
239 |
httpMethod.releaseConnection(); |
240 |
return res; |
241 |
} catch (IllegalArgumentException e)// e.g. some kinds of invalid URL |
242 |
{ |
243 |
res.sampleEnd(); |
244 |
errorResult(e, res); |
245 |
return res; |
246 |
} catch (IOException e) { |
247 |
res.sampleEnd(); |
248 |
errorResult(e, res); |
249 |
return res; |
250 |
} finally { |
251 |
JOrphanUtils.closeQuietly(instream); |
252 |
setSavedClient(null); |
253 |
httpMethod.releaseConnection(); |
254 |
} |
255 |
} |
256 |
|
257 |
@Override |
258 |
public URL getUrl() throws MalformedURLException { |
259 |
return new URL(getUrlData()); |
260 |
} |
261 |
} |
262 |
* |