Code with Misuse: |
class HttpMethodDirector {
/**
* Executes a method with the current hostConfiguration.
*
* @throws IOException if an I/O (transport) error occurs. Some transport exceptions
* can be recovered from.
* @throws HttpException if a protocol exception occurs. Usually protocol exceptions
* cannot be recovered from.
*/
private void executeWithRetry(final HttpMethod method)
throws IOException, HttpException {
/** How many times did this transparently handle a recoverable exception? */
int recoverableExceptionCount = 0;
int execCount = 0;
// TODO: how do we get requestSent?
boolean requestSent = false;
// loop until the method is successfully processed, the retryHandler
// returns false or a non-recoverable exception is thrown
while (true) {
execCount++;
requestSent = false;
if (LOG.isTraceEnabled()) {
LOG.trace("Attempt number " + execCount + " to process request");
}
if (!this.conn.isOpen()) {
// this connection must be opened before it can be used
// This has nothing to do with opening a secure tunnel
try {
this.conn.open();
if (this.conn.isProxied() && this.conn.isSecure()
&& !(method instanceof ConnectMethod)) {
// we need to create a secure tunnel before we can execute the real method
if (!executeConnect()) {
// abort, the connect method failed
return;
}
}
} catch (IOException e) {
releaseConnection = true;
throw e;
} catch (RuntimeException e) {
releaseConnection = true;
throw e;
}
}
try {
method.execute(state, this.conn);
break;
} catch (HttpRecoverableException httpre) {
if (LOG.isDebugEnabled()) {
LOG.debug("Closing the connection.");
}
this.conn.close();
LOG.info("Recoverable exception caught when processing request");
// update the recoverable exception count.
recoverableExceptionCount++;
// test if this method should be retried
MethodRetryHandler handler = method.getMethodRetryHandler();
if (handler == null) {
handler = new DefaultMethodRetryHandler();
}
if (!handler.retryMethod(
method,
this.conn,
httpre,
execCount,
requestSent)
) {
LOG.warn(
"Recoverable exception caught but MethodRetryHandler.retryMethod() "
+ "returned false, rethrowing exception"
);
// this connection can no longer be used, it has been closed
releaseConnection = true;
throw httpre;
}
}
}
}
}
|
Code with Pattern(s): |
class CloseConnection {
void pattern(HttpConnection conn, HttpMethod method, HttpState state) throws IOException, HttpException {
try {
while (true) {
if (!conn.isOpen()) {
conn.open();
if (conn.isProxied() && conn.isSecure() && !(method instanceof ConnectMethod)) {
// initialize connect method...
}
}
try {
method.execute(state, conn);
break;
} catch (HttpRecoverableException httpre) {
conn.close();
}
}
} catch (IOException e) {
if (conn.isOpen()) {
conn.close();
}
throw e;
} catch (RuntimeException e) {
if (conn.isOpen()) {
conn.close();
}
throw e;
}
}
}
|