HTTPSamplerFactory.class
package org.apache.jmeter.protocol.http.sampler;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.util.JOrphanUtils;
public final class HTTPSamplerFactory {
public static final String HTTP_SAMPLER_JAVA = "HTTPSampler";
public static final String HTTP_SAMPLER_APACHE = "HTTPSampler2";
public static final String IMPL_HTTP_CLIENT4 = "HttpClient4";
public static final String IMPL_HTTP_CLIENT3_1 = "HttpClient3.1";
public static final String IMPL_JAVA = "Java";
public static final String IMPL_APPRF = "Apprf";
public static final String DEFAULT_CLASSNAME = JMeterUtils.getPropDefault("jmeter.httpsampler", "HttpClient4");
public static HTTPSamplerBase newInstance() {
return newInstance(DEFAULT_CLASSNAME);
}
public static HTTPSamplerBase newInstance(String alias) {
if (alias == null || alias.length() == 0)
return new HTTPSamplerProxy();
if (alias.equals("HTTPSampler") || alias.equals("Java"))
return new HTTPSamplerProxy("Java");
if (alias.equals("HttpClient4") || alias.equals("HTTPSampler2") || alias.equals("HttpClient3.1"))
return new HTTPSamplerProxy("HttpClient4");
if (alias.equals("Apprf"))
return new HTTPSamplerProxy("Apprf");
throw new IllegalArgumentException("Unknown sampler type: '" + alias + "'");
}
public static String[] getImplementations() {
return new String[] { "HttpClient4", "Java", "Apprf" };
}
public static HTTPAbstractImpl getImplementation(String impl, HTTPSamplerBase base) {
if ("file".equals(base.getProtocol()))
return new HTTPFileImpl(base);
if (JOrphanUtils.isBlank(impl))
impl = DEFAULT_CLASSNAME;
if ("Java".equals(impl) || "HTTPSampler".equals(impl))
return new HTTPJavaImpl(base);
if ("HttpClient4".equals(impl) || "HttpClient3.1".equals(impl))
return new HTTPHC4Impl(base);
if ("Apprf".equals(impl))
return new HTTPappImpl(base);
throw new IllegalArgumentException("Unknown implementation type: '" + impl + "'");
}
}
HTTPappImpl.class
else if (getSendParameterValuesAsPostBody()) {
if (!hasContentTypeHeader) {
HTTPFileArg file = (files.length > 0) ? files[0] : null;
if (file != null && file.getMimeType() != null && file.getMimeType().length() > 0) {
entityEnclosingRequest.setHeader("Content-Type", file.getMimeType());
} else if (ADD_CONTENT_TYPE_TO_POST_IF_MISSING) {
entityEnclosingRequest.setHeader("Content-Type", "application/x-www-form-urlencoded");
}
}
StringBuilder postBody = new StringBuilder();
String argstr = null;
for (PropertyIterator propertyIterator = getArguments().iterator(); propertyIterator.hasNext(); ) {
JMeterProperty jMeterProperty = propertyIterator.next();
HTTPArgument arg = (HTTPArgument)jMeterProperty.getObjectValue();
if (haveContentEncoding) {
argstr = arg.getEncodedValue(contentEncoding);
} else {
argstr = arg.getEncodedValue();
}
String encstr = Base64.getEncoder().encodeToString(argstr.getBytes(StandardCharsets.UTF_8));
encstr = URLEncoder.encode(encstr);
postBody.append("data=" + encstr);
}
StringEntity requestEntity = new StringEntity(postBody.toString(), contentEncoding);
entityEnclosingRequest.setEntity((HttpEntity)requestEntity);
postedBody.append("data=" + argstr);
private CloseableHttpResponse executeRequest(CloseableHttpClient httpClient, HttpRequestBase httpRequest, HttpContext localContext, URL url) throws IOException {
AuthManager authManager = getAuthManager();
if (authManager != null) {
Subject subject = authManager.getSubjectForUrl(url);
if (subject != null)
try {
return Subject.doAs(subject, () -> httpClient.execute((HttpUriRequest)httpRequest, localContext));
} catch (PrivilegedActionException e) {
log.error("Can't execute httpRequest with subject: {}", subject, e);
throw new IllegalArgumentException("Can't execute httpRequest with subject:" + subject, e);
}
}
CloseableHttpResponse httpresp = httpClient.execute((HttpUriRequest)httpRequest, localContext);
HttpEntity entity = httpresp.getEntity();
String oresp = EntityUtils.toString(entity);
String respstr = oresp.split(""")[3];
respstr = respstr.replaceAll("\\u003d", "=");
String decstr = new String(Base64.getDecoder().decode(respstr), "utf-8");
StringEntity sEntity = new StringEntity(decstr, "utf-8");
httpresp.setEntity((HttpEntity)sEntity);
return httpresp;
}