String mimeType = "";
String hostAddress = null;
// Create a local instance of cookie store, and bind to local context
// Without this we get killed w/lots of threads, due to sync() on single cookie store.
HttpContext localContext = new BasicHttpContext();
CookieStore cookieStore = new BasicCookieStore();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
StringBuilder fetchTrace = null;
if (LOGGER.isTraceEnabled()) {
fetchTrace = new StringBuilder("Fetched url: " + url);
}
try {
request.setURI(new URI(url));
readStartTime = System.currentTimeMillis();
response = _httpClient.execute(request, localContext);
Header[] headers = response.getAllHeaders();
for (Header header : headers) {
headerMap.add(header.getName(), header.getValue());
}
int httpStatus = response.getStatusLine().getStatusCode();
if (LOGGER.isTraceEnabled()) {
fetchTrace.append("; status code: " + httpStatus);
if (headerMap.getFirst(HttpHeaderNames.CONTENT_LENGTH) != null) {
fetchTrace.append("; Content-Length: " + headerMap.getFirst(HttpHeaderNames.CONTENT_LENGTH));
}
if (headerMap.getFirst(HttpHeaderNames.LOCATION) != null) {
fetchTrace.append("; Location: " + headerMap.getFirst(HttpHeaderNames.LOCATION));
}
}
if ((httpStatus < 200) || (httpStatus >= 300)) {
// We can't just check against SC_OK, as some wackos return 201, 202, etc
throw new HttpFetchException(url, "Error fetching " + url, httpStatus, headerMap);
}
redirectedUrl = extractRedirectedUrl(url, localContext);
URI permRedirectUri = (URI)localContext.getAttribute(PERM_REDIRECT_CONTEXT_KEY);
if (permRedirectUri != null) {
newBaseUrl = permRedirectUri.toURL().toExternalForm();
}
Integer redirects = (Integer)localContext.getAttribute(REDIRECT_COUNT_CONTEXT_KEY);
if (redirects != null) {
numRedirects = redirects.intValue();
}
hostAddress = (String)(localContext.getAttribute(HOST_ADDRESS));
if (hostAddress == null) {
throw new UrlFetchException(url, "Host address not saved in context");
}
Header cth = response.getFirstHeader(HttpHeaderNames.CONTENT_TYPE);