use of org.apache.ivy.core.settings.TimeoutConstraint in project ant-ivy by apache.
the class BasicURLHandler method openStream.
@Override
public InputStream openStream(final URL url, final TimeoutConstraint timeoutConstraint) throws IOException {
// Install the IvyAuthenticator
if ("http".equals(url.getProtocol()) || "https".equals(url.getProtocol())) {
IvyAuthenticator.install();
}
final int connectionTimeout = (timeoutConstraint == null || timeoutConstraint.getConnectionTimeout() < 0) ? 0 : timeoutConstraint.getConnectionTimeout();
final int readTimeout = (timeoutConstraint == null || timeoutConstraint.getReadTimeout() < 0) ? 0 : timeoutConstraint.getReadTimeout();
URLConnection conn = null;
try {
final URL normalizedURL = normalizeToURL(url);
conn = normalizedURL.openConnection();
conn.setConnectTimeout(connectionTimeout);
conn.setReadTimeout(readTimeout);
conn.setRequestProperty("User-Agent", getUserAgent());
conn.setRequestProperty("Accept-Encoding", "gzip,deflate");
if (conn instanceof HttpURLConnection) {
HttpURLConnection httpCon = (HttpURLConnection) conn;
if (!checkStatusCode(normalizedURL, httpCon)) {
throw new IOException("The HTTP response code for " + normalizedURL + " did not indicate a success." + " See log for more detail.");
}
}
InputStream inStream = getDecodingInputStream(conn.getContentEncoding(), conn.getInputStream());
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[BUFFER_SIZE];
int len;
while ((len = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, len);
}
return new ByteArrayInputStream(outStream.toByteArray());
} finally {
disconnect(conn);
}
}
use of org.apache.ivy.core.settings.TimeoutConstraint in project ant-ivy by apache.
the class BasicURLHandler method download.
@Override
public void download(final URL src, final File dest, final CopyProgressListener listener, final TimeoutConstraint timeoutConstraint) throws IOException {
// Install the IvyAuthenticator
if ("http".equals(src.getProtocol()) || "https".equals(src.getProtocol())) {
IvyAuthenticator.install();
}
final int connectionTimeout = (timeoutConstraint == null || timeoutConstraint.getConnectionTimeout() < 0) ? 0 : timeoutConstraint.getConnectionTimeout();
final int readTimeout = (timeoutConstraint == null || timeoutConstraint.getReadTimeout() < 0) ? 0 : timeoutConstraint.getReadTimeout();
URLConnection srcConn = null;
try {
final URL normalizedURL = normalizeToURL(src);
srcConn = normalizedURL.openConnection();
srcConn.setConnectTimeout(connectionTimeout);
srcConn.setReadTimeout(readTimeout);
srcConn.setRequestProperty("User-Agent", getUserAgent());
srcConn.setRequestProperty("Accept-Encoding", "gzip,deflate");
if (srcConn instanceof HttpURLConnection) {
HttpURLConnection httpCon = (HttpURLConnection) srcConn;
if (!checkStatusCode(normalizedURL, httpCon)) {
throw new IOException("The HTTP response code for " + normalizedURL + " did not indicate a success." + " See log for more detail.");
}
}
// do the download
InputStream inStream = getDecodingInputStream(srcConn.getContentEncoding(), srcConn.getInputStream());
FileUtil.copy(inStream, dest, listener);
// check content length only if content was not encoded
if (srcConn.getContentEncoding() == null) {
int contentLength = srcConn.getContentLength();
if (contentLength != -1 && dest.length() != contentLength) {
dest.delete();
throw new IOException("Downloaded file size doesn't match expected Content Length for " + normalizedURL + ". Please retry.");
}
}
// update modification date
long lastModified = srcConn.getLastModified();
if (lastModified > 0) {
dest.setLastModified(lastModified);
}
} finally {
disconnect(srcConn);
}
}
use of org.apache.ivy.core.settings.TimeoutConstraint in project ant-ivy by apache.
the class HttpClientHandler method download.
@Override
public void download(final URL src, final File dest, final CopyProgressListener listener, final TimeoutConstraint timeoutConstraint) throws IOException {
final int connectionTimeout = (timeoutConstraint == null || timeoutConstraint.getConnectionTimeout() < 0) ? 0 : timeoutConstraint.getConnectionTimeout();
final int readTimeout = (timeoutConstraint == null || timeoutConstraint.getReadTimeout() < 0) ? 0 : timeoutConstraint.getReadTimeout();
try (final CloseableHttpResponse response = doGet(src, connectionTimeout, readTimeout)) {
// We can only figure the content we got is want we want if the status is success.
this.requireSuccessStatus(HttpGet.METHOD_NAME, src, response);
final Header encoding = this.getContentEncoding(response);
try (final InputStream is = getDecodingInputStream(encoding == null ? null : encoding.getValue(), response.getEntity().getContent())) {
FileUtil.copy(is, dest, listener);
}
dest.setLastModified(getLastModified(response));
}
}
use of org.apache.ivy.core.settings.TimeoutConstraint in project ant-ivy by apache.
the class HttpClientHandler method openStream.
@Override
public InputStream openStream(final URL url, final TimeoutConstraint timeoutConstraint) throws IOException {
final int connectionTimeout = (timeoutConstraint == null || timeoutConstraint.getConnectionTimeout() < 0) ? 0 : timeoutConstraint.getConnectionTimeout();
final int readTimeout = (timeoutConstraint == null || timeoutConstraint.getReadTimeout() < 0) ? 0 : timeoutConstraint.getReadTimeout();
final CloseableHttpResponse response = doGet(url, connectionTimeout, readTimeout);
this.requireSuccessStatus(HttpGet.METHOD_NAME, url, response);
final Header encoding = this.getContentEncoding(response);
return getDecodingInputStream(encoding == null ? null : encoding.getValue(), response.getEntity().getContent());
}
Aggregations