}
try {
connection.connect();
} catch (IOException e) {
String errorMessage;
ErrorType errorType = ErrorType.UNKNOWN;
if (e instanceof ConnectException) {
// This most likely just means the server is down.
errorMessage = "Failed to connect to [" + httpURL + "].";
errorType = ErrorType.CANNOT_CONNECT;
} else if (e instanceof SocketTimeoutException) {
// This probably means the server is up but not properly accepting connection requests.
long connectDuration = System.currentTimeMillis() - connectStartTime;
errorMessage = "Attempt to connect to [" + httpURL + "] timed out after " + connectDuration
+ " milliseconds.";
errorType = ErrorType.CONNECTION_TIMEOUT;
} else {
errorMessage = "An error occurred while attempting to connect to [" + httpURL + "].";
}
if (LOG.isDebugEnabled()) {
LOG.warn(errorMessage, e);
}
return availabilityIsDown(errorType, errorMessage, e);
}
int connectDuration = (int) (System.currentTimeMillis() - connectStartTime);
if (LOG.isDebugEnabled()) {
LOG.debug("Connected to [" + httpURL + "] in " + connectDuration + " milliseconds.");
}
if ((timeout > 0) && (connectDuration >= timeout)) {
String errorMessage = "Attempt to ping [" + httpURL + "] timed out after " + connectDuration
+ " milliseconds.";
if (LOG.isDebugEnabled()) {
LOG.warn(errorMessage);
}
return availabilityIsDown(ErrorType.CONNECTION_TIMEOUT, errorMessage);
}
try {
int readTimeout = (timeout > 0) ? (timeout - connectDuration) : 0;
connection.setReadTimeout(readTimeout);
if (connection.getReadTimeout() != readTimeout) {
if (LOG.isDebugEnabled()) {
LOG.warn("Failed to set read timeout on URLConnection for [" + httpURL
+ "] - this most likely means we're running in a non-standard JRE.");
}
}
// Now actually send the request and read the response.
long readStartTime = System.currentTimeMillis();
if (LOG.isDebugEnabled()) {
LOG.debug("Sending " + connection.getRequestMethod() + " request to [" + httpURL + "]...");
}
try {
// Calling getResponseCode() will cause the request to be sent.
int responseCode = connection.getResponseCode();
if (LOG.isDebugEnabled()) {
if (responseCode == -1) {
LOG.warn("Ping request to [" + httpURL + "] returned an invalid response: "
+ getResponseBody(connection));
} else if (responseCode >= 500) {
LOG.warn("Ping request to [" + httpURL + "] returned a response with server error "
+ responseCode + " (" + connection.getResponseMessage() + "): "
+ getResponseBody(connection));
} else if (responseCode >= 400) {
LOG.warn("Ping request to [" + httpURL + "] returned a response with client error "
+ responseCode + " (" + connection.getResponseMessage() + ").");
}
}
} catch (IOException e) {
String errorMessage;
ErrorType errorType = ErrorType.UNKNOWN;
if (e instanceof SocketTimeoutException) {
long readDuration = System.currentTimeMillis() - readStartTime;
errorMessage = "Attempt to read response from " + connection.getRequestMethod() + " request to ["
+ httpURL + "] timed out after " + readDuration + " milliseconds.";
errorType = ErrorType.CONNECTION_TIMEOUT;