下面列出了怎么用org.apache.commons.httpclient.NoHttpResponseException的API类实例代码及写法,或者点击链接到github查看源代码。
private HttpMethodRetryHandler createRetryTwiceHandler() {
return new HttpMethodRetryHandler() {
@Override
public boolean retryMethod(final HttpMethod method, final IOException exception, int executionCount) {
if (executionCount >= 2) {
// Do not retry if over max retry count
return false;
}
if (exception instanceof NoHttpResponseException) {
// Retry if the server dropped connection on us
return true;
}
if (!method.isRequestSent()) {
// Retry if the request has not been sent fully or
// if it's OK to retry methods that have been sent
return true;
}
// otherwise do not retry
return false;
}
};
}
protected HttpMethodRetryHandler createRetryTwiceHandler() {
return new HttpMethodRetryHandler() {
@Override
public boolean retryMethod(final HttpMethod method, final IOException exception, int executionCount) {
if (executionCount >= 2) {
// Do not retry if over max retry count
return false;
}
if (exception instanceof NoHttpResponseException) {
// Retry if the server dropped connection on us
return true;
}
if (!method.isRequestSent()) {
// Retry if the request has not been sent fully or
// if it's OK to retry methods that have been sent
return true;
}
// otherwise do not retry
return false;
}
};
}
/**
* @return A HttpMethodRetryHandler with given number of retries.
*/
public static HttpMethodRetryHandler getHttpMethodRetryHandler(final int retryCount) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Initializing new HttpMethodRetryHandler with retry count " + retryCount);
}
return new HttpMethodRetryHandler() {
@Override
public boolean retryMethod(final HttpMethod method, final IOException exception, final int executionCount) {
if (executionCount >= retryCount) {
// Do not retry if over max retry count
return false;
}
if (exception instanceof NoHttpResponseException) {
// Retry if the server dropped connection on us
return true;
}
if (!method.isRequestSent()) {
// Retry if the request has not been sent fully or
// if it's OK to retry methods that have been sent
return true;
}
// otherwise do not retry
return false;
}
};
}
/**
* @return A HttpMethodRetryHandler with given number of retries.
*/
public static HttpMethodRetryHandler getHttpMethodRetryHandler(final int retryCount) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Initializing new HttpMethodRetryHandler with retry count " + retryCount);
}
return new HttpMethodRetryHandler() {
@Override
public boolean retryMethod(final HttpMethod method, final IOException exception, int executionCount) {
if (executionCount >= retryCount) {
// Do not retry if over max retry count
return false;
}
if (exception instanceof NoHttpResponseException) {
// Retry if the server dropped connection on us
return true;
}
if (!method.isRequestSent()) {
// Retry if the request has not been sent fully or
// if it's OK to retry methods that have been sent
return true;
}
// otherwise do not retry
return false;
}
};
}
public HttpTemplateDownloader(final StorageLayer storageLayer, final String downloadUrl, final String toDir, final DownloadCompleteCallback callback,
final long maxTemplateSizeInBytes, final String user, final String password, final Proxy proxy, final ResourceType resourceType) {
this._storage = storageLayer;
this.downloadUrl = downloadUrl;
setToDir(toDir);
this.status = TemplateDownloadStatus.NOT_STARTED;
this.resourceType = resourceType;
this.maxTemplateSizeInBytes = maxTemplateSizeInBytes;
this.totalBytes = 0;
this.client = new HttpClient(s_httpClientManager);
this.myretryhandler = (method, exception, executionCount) -> {
if (executionCount >= 2) {
// Do not retry if over max retry count
return false;
}
if (exception instanceof NoHttpResponseException) {
// Retry if the server dropped connection on us
return true;
}
if (!method.isRequestSent()) {
// Retry if the request has not been sent fully or
// if it's OK to retry methods that have been sent
return true;
}
// otherwise do not retry
return false;
};
try {
this.request = new GetMethod(downloadUrl);
this.request.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, this.myretryhandler);
this.completionCallback = callback;
this.request.setFollowRedirects(true);
final File f = File.createTempFile("dnld", "tmp_", new File(toDir));
if (this._storage != null) {
this._storage.setWorldReadableAndWriteable(f);
}
this.toFile = f.getAbsolutePath();
final Pair<String, Integer> hostAndPort = UriUtils.validateUrl(downloadUrl);
if (proxy != null) {
this.client.getHostConfiguration().setProxy(proxy.getHost(), proxy.getPort());
if (proxy.getUserName() != null) {
final Credentials proxyCreds = new UsernamePasswordCredentials(proxy.getUserName(), proxy.getPassword());
this.client.getState().setProxyCredentials(AuthScope.ANY, proxyCreds);
}
}
if (user != null && password != null) {
this.client.getParams().setAuthenticationPreemptive(true);
final Credentials defaultcreds = new UsernamePasswordCredentials(user, password);
this.client.getState().setCredentials(new AuthScope(hostAndPort.first(), hostAndPort.second(), AuthScope.ANY_REALM), defaultcreds);
s_logger.info("Added username=" + user + ", password=" + password + "for host " + hostAndPort.first() + ":" + hostAndPort.second());
} else {
s_logger.info("No credentials configured for host=" + hostAndPort.first() + ":" + hostAndPort.second());
}
} catch (final IllegalArgumentException iae) {
this.errorString = iae.getMessage();
this.status = TemplateDownloadStatus.UNRECOVERABLE_ERROR;
this.inited = false;
} catch (final Exception ex) {
this.errorString = "Unable to start download -- check url? ";
this.status = TemplateDownloadStatus.UNRECOVERABLE_ERROR;
s_logger.warn("Exception in constructor -- " + ex.toString());
}
}