// to track if we got a response (any response) or an exception.
boolean gotUrl = false;
boolean isTimeout = false;
String fName = backingFileBase + "-" + Thread.currentThread().getId();
Recorder recorder = new Recorder(recorderCacheDir,fName,
outBufferSize, inBufferSize);
ExtendedGetMethod getMethod = null;
// TWO STEPS:
// first do the GET, using a Recorder to get the response.
// then, if that worked, save the recorded value into an ARC
// and return it's region
// if we didn't get a response, forge a fake record and return that.
try {
Recorder.setHttpRecorder(recorder);
LaxURI lURI = new LaxURI(url,true);
getMethod = new ExtendedGetMethod(url,recorder);
getMethod.setURI(lURI);
HttpClient client = getHttpClient();
getMethod.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
getMethod.setFollowRedirects(false);
getMethod.setRequestHeader("User-Agent", userAgent);
int code = client.executeMethod(getMethod);
LOGGER.info("URL(" + url + ") HTTP:" + code);
InputStream responseIS = getMethod.getResponseBodyAsStream();
if(responseIS != null) {
ByteOp.discardStream(responseIS);
responseIS.close();
}
gotUrl = true;
} catch (URIException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
LOGGER.warning("Unknown host for " + url);
} catch (ConnectTimeoutException e) {
// TODO: should we act like it's a full block?
LOGGER.warning("Timeout out connecting to " + url);
isTimeout = true;
} catch(SocketTimeoutException e) {
LOGGER.warning("Timeout out socket for " + url);
isTimeout = true;
} catch (ConnectException e) {
LOGGER.warning("ConnectionRefused to " + url);
} catch (NoRouteToHostException e) {
LOGGER.warning("NoRouteToHost for " + url);
} catch (SocketException e) {
// should only be things like "Connection Reset", etc..
LOGGER.warning("SocketException for " + url);
} catch (HttpException e) {
e.printStackTrace();
// we have to let IOExceptions out, problems caused by local disk
// NEED to return errors, indicating that there is not an
// authoritative answer, and thus... NOTHING can be shown.
// } catch (IOException e) {
// e.printStackTrace();
} finally {
recorder.closeRecorders();
Recorder.setHttpRecorder(null);
if(getMethod != null) {
getMethod.releaseConnection();
}
}
// now write the content, or a fake record:
ARCWriter writer = null;
ReplayInputStream replayIS = null;
try {
writer = cache.getWriter();
if(gotUrl) {
RecordingInputStream ris = recorder.getRecordedInput();
replayIS = ris.getReplayInputStream();
region = storeInputStreamARCRecord(writer, url,
getMethod.getMime(), getMethod.getRemoteIP(),
getMethod.getCaptureDate(),
replayIS, (int) ris.getSize());
} else if(isTimeout) {
region = storeTimeout(writer,url);
} else {
region = storeNotAvailable(writer, url);
}
} finally {
IOUtils.closeQuietly(replayIS);
if(writer != null) {
cache.returnWriter(writer);
}
}
recorder.close();
return region;
}