final GetMethod method = new GetMethod(url.toExternalForm());
method.setFollowRedirects(true);
// create cache information object
SystemClock clock = SystemClock.getDefaultInstance();
CachedHttpContentStateBuilder cacheBuilder = new CachedHttpContentStateBuilder();
cacheBuilder.setMethodAccessor(
ResponseHeaderAccessorFactory.getDefaultInstance().
createHttpClientResponseHeaderAccessor(method));
cacheBuilder.setRequestTime(clock.getCurrentTime());
// save request headers to request method
setRequestHeaders(method, headers);
DefaultRepresentation responseInfo = null;
// execute request
try {
httpClient.executeMethod(method);
} catch (IOException e) {
throw new ResourceRetrieverException(exceptionLocalizer.format(
"connection-refused", url.toString()), e);
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Requested resource at: " + url.toString());
}
// If the get failed then return immediately.
if (method.getStatusCode() == 200) {
// get response caching information
cacheBuilder.setResponseTime(clock.getCurrentTime());
// read resource stream
InputStream stream = method.getResponseBodyAsStream();
// a custom closer to ensure the method releases its connection
CloseListener closer = new CloseListener() {
public void close() {
method.releaseConnection();
}
};
SeekableInputStream seekableStream =
new DefaultSeekableInputStream(closer, stream, true);
if(stream != null) {
// the mimeDiscoverer should not effect the Seekable stream
// and should restore its position but we mark it just in case.
String mimeType = null;
try {
seekableStream.mark();
mimeType = mimeDiscoverer.discoverMimeType(seekableStream);
} finally {
seekableStream.reset();
}
responseInfo = new DefaultRepresentation(
method,
mimeType,
cacheBuilder.build(),
seekableStream);
}
} else {
LOGGER.error("request-failed",
new String[]{ url.toString(),