Search in sources :

Example 6 with TimeoutConstraint

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);
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TimeoutConstraint(org.apache.ivy.core.settings.TimeoutConstraint) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) URL(java.net.URL)

Example 7 with TimeoutConstraint

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);
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) TimeoutConstraint(org.apache.ivy.core.settings.TimeoutConstraint) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) URL(java.net.URL)

Example 8 with TimeoutConstraint

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));
    }
}
Also used : Header(org.apache.http.Header) InputStream(java.io.InputStream) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) TimeoutConstraint(org.apache.ivy.core.settings.TimeoutConstraint)

Example 9 with TimeoutConstraint

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());
}
Also used : Header(org.apache.http.Header) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) TimeoutConstraint(org.apache.ivy.core.settings.TimeoutConstraint)

Aggregations

TimeoutConstraint (org.apache.ivy.core.settings.TimeoutConstraint)9 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 HttpURLConnection (java.net.HttpURLConnection)4 URL (java.net.URL)4 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 FileInputStream (java.io.FileInputStream)3 URLConnection (java.net.URLConnection)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Header (org.apache.http.Header)2 File (java.io.File)1 OutputStream (java.io.OutputStream)1 InetSocketAddress (java.net.InetSocketAddress)1 UnknownHostException (java.net.UnknownHostException)1 Charset (java.nio.charset.Charset)1 Path (java.nio.file.Path)1 ExecutorService (java.util.concurrent.ExecutorService)1 HttpEntity (org.apache.http.HttpEntity)1 RequestConfig (org.apache.http.client.config.RequestConfig)1