HTTPSampleResult res = createSampleResult(url, method);
HttpClient httpClient = setupClient(url);
HttpRequestBase httpRequest = null;
try {
URI uri = url.toURI();
if (method.equals(HTTPConstants.POST)) {
httpRequest = new HttpPost(uri);
} else if (method.equals(HTTPConstants.PUT)) {
httpRequest = new HttpPut(uri);
} else if (method.equals(HTTPConstants.HEAD)) {
httpRequest = new HttpHead(uri);
} else if (method.equals(HTTPConstants.TRACE)) {
httpRequest = new HttpTrace(uri);
} else if (method.equals(HTTPConstants.OPTIONS)) {
httpRequest = new HttpOptions(uri);
} else if (method.equals(HTTPConstants.DELETE)) {
httpRequest = new HttpDelete(uri);
} else if (method.equals(HTTPConstants.GET)) {
httpRequest = new HttpGet(uri);
} else if (method.equals(HTTPConstants.PATCH)) {
httpRequest = new HttpPatch(uri);
} else {
throw new IllegalArgumentException("Unexpected method: '"+method+"'");
}
setupRequest(url, httpRequest, res); // can throw IOException
} catch (Exception e) {
res.sampleStart();
res.sampleEnd();
errorResult(e, res);
return res;
}
HttpContext localContext = new BasicHttpContext();
res.sampleStart();
final CacheManager cacheManager = getCacheManager();
if (cacheManager != null && HTTPConstants.GET.equalsIgnoreCase(method)) {
if (cacheManager.inCache(url)) {
res.sampleEnd();
res.setResponseNoContent();
res.setSuccessful(true);
return res;
}
}
try {
currentRequest = httpRequest;
handleMethod(method, res, httpRequest, localContext);
// perform the sample
HttpResponse httpResponse =
executeRequest(httpClient, httpRequest, localContext, url);
// Needs to be done after execute to pick up all the headers
final HttpRequest request = (HttpRequest) localContext.getAttribute(ExecutionContext.HTTP_REQUEST);
// We've finished with the request, so we can add the LocalAddress to it for display
final InetAddress localAddr = (InetAddress) httpRequest.getParams().getParameter(ConnRoutePNames.LOCAL_ADDRESS);
if (localAddr != null) {
request.addHeader(HEADER_LOCAL_ADDRESS, localAddr.toString());
}
res.setRequestHeaders(getConnectionHeaders(request));
Header contentType = httpResponse.getLastHeader(HTTPConstants.HEADER_CONTENT_TYPE);
if (contentType != null){
String ct = contentType.getValue();
res.setContentType(ct);
res.setEncodingAndType(ct);
}
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
res.setResponseData(readResponse(res, instream, (int) entity.getContentLength()));
}
res.sampleEnd(); // Done with the sampling proper.
currentRequest = null;
// Now collect the results into the HTTPSampleResult:
StatusLine statusLine = httpResponse.getStatusLine();
int statusCode = statusLine.getStatusCode();
res.setResponseCode(Integer.toString(statusCode));
res.setResponseMessage(statusLine.getReasonPhrase());
res.setSuccessful(isSuccessCode(statusCode));
res.setResponseHeaders(getResponseHeaders(httpResponse));
if (res.isRedirect()) {
final Header headerLocation = httpResponse.getLastHeader(HTTPConstants.HEADER_LOCATION);
if (headerLocation == null) { // HTTP protocol violation, but avoids NPE
throw new IllegalArgumentException("Missing location header in redirect for " + httpRequest.getRequestLine());
}
final String redirectLocation = headerLocation.getValue();
try {
final URL redirectUrl = new URL(redirectLocation);
res.setRedirectLocation(ConversionUtils.sanitizeUrl(redirectUrl).toString());
} catch (Exception e) {
log.error("Error in redirect URL for " + httpRequest.getRequestLine()
+"\n\tCould not sanitize URL: " + redirectLocation + "\n\t", e);
}
}
// record some sizes to allow HTTPSampleResult.getBytes() with different options