Search in sources :

Example 1 with TimeoutConstraint

use of org.apache.ivy.core.settings.TimeoutConstraint in project ant-ivy by apache.

the class BasicURLHandler method upload.

@Override
public void upload(final File src, final URL dest, final CopyProgressListener listener, final TimeoutConstraint timeoutConstraint) throws IOException {
    if (!"http".equals(dest.getProtocol()) && !"https".equals(dest.getProtocol())) {
        throw new UnsupportedOperationException("URL repository only support HTTP PUT at the moment");
    }
    // Install the IvyAuthenticator
    IvyAuthenticator.install();
    final int connectionTimeout = (timeoutConstraint == null || timeoutConstraint.getConnectionTimeout() < 0) ? 0 : timeoutConstraint.getConnectionTimeout();
    HttpURLConnection conn = null;
    try {
        final URL normalizedDestURL = normalizeToURL(dest);
        conn = (HttpURLConnection) normalizedDestURL.openConnection();
        conn.setDoOutput(true);
        conn.setConnectTimeout(connectionTimeout);
        conn.setRequestMethod("PUT");
        conn.setRequestProperty("User-Agent", getUserAgent());
        conn.setRequestProperty("Content-type", "application/octet-stream");
        conn.setRequestProperty("Content-length", Long.toString(src.length()));
        conn.setInstanceFollowRedirects(true);
        try (final InputStream in = new FileInputStream(src)) {
            final OutputStream os = conn.getOutputStream();
            FileUtil.copy(in, os, listener);
        }
        validatePutStatusCode(normalizedDestURL, conn.getResponseCode(), conn.getResponseMessage());
    } finally {
        disconnect(conn);
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TimeoutConstraint(org.apache.ivy.core.settings.TimeoutConstraint) URL(java.net.URL) FileInputStream(java.io.FileInputStream)

Example 2 with TimeoutConstraint

use of org.apache.ivy.core.settings.TimeoutConstraint in project ant-ivy by apache.

the class BasicURLHandler method getURLInfo.

@SuppressWarnings("deprecation")
@Override
public URLInfo getURLInfo(final URL url, final TimeoutConstraint timeoutConstraint) {
    // 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 con = null;
    try {
        final URL normalizedURL = normalizeToURL(url);
        con = normalizedURL.openConnection();
        con.setConnectTimeout(connectionTimeout);
        con.setReadTimeout(readTimeout);
        con.setRequestProperty("User-Agent", getUserAgent());
        if (con instanceof HttpURLConnection) {
            HttpURLConnection httpCon = (HttpURLConnection) con;
            if (getRequestMethod() == TimeoutConstrainedURLHandler.REQUEST_METHOD_HEAD) {
                httpCon.setRequestMethod("HEAD");
            }
            if (checkStatusCode(normalizedURL, httpCon)) {
                String bodyCharset = getCharSetFromContentType(con.getContentType());
                return new URLInfo(true, httpCon.getContentLength(), con.getLastModified(), bodyCharset);
            }
        } else {
            int contentLength = con.getContentLength();
            if (contentLength <= 0) {
                return UNAVAILABLE;
            } else {
                // TODO: not HTTP... maybe we *don't* want to default to ISO-8559-1 here?
                String bodyCharset = getCharSetFromContentType(con.getContentType());
                return new URLInfo(true, contentLength, con.getLastModified(), bodyCharset);
            }
        }
    } catch (UnknownHostException e) {
        Message.warn("Host " + e.getMessage() + " not found. url=" + url);
        Message.info("You probably access the destination server through " + "a proxy server that is not well configured.");
    } catch (IOException e) {
        Message.error("Server access error at url " + url, e);
    } finally {
        disconnect(con);
    }
    return UNAVAILABLE;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) UnknownHostException(java.net.UnknownHostException) IOException(java.io.IOException) TimeoutConstraint(org.apache.ivy.core.settings.TimeoutConstraint) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) URL(java.net.URL)

Example 3 with TimeoutConstraint

use of org.apache.ivy.core.settings.TimeoutConstraint in project ant-ivy by apache.

the class HttpClientHandler method upload.

@Override
public void upload(final File src, final URL 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();
    final RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(readTimeout).setConnectTimeout(connectionTimeout).setAuthenticationEnabled(hasCredentialsConfigured(dest)).setTargetPreferredAuthSchemes(getAuthSchemePreferredOrder()).setProxyPreferredAuthSchemes(getAuthSchemePreferredOrder()).setExpectContinueEnabled(true).build();
    final HttpPut put = new HttpPut(normalizeToString(dest));
    put.setConfig(requestConfig);
    put.setEntity(new FileEntity(src));
    try (final CloseableHttpResponse response = this.httpClient.execute(put)) {
        validatePutStatusCode(dest, response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) FileEntity(org.apache.http.entity.FileEntity) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) TimeoutConstraint(org.apache.ivy.core.settings.TimeoutConstraint) HttpPut(org.apache.http.client.methods.HttpPut)

Example 4 with TimeoutConstraint

use of org.apache.ivy.core.settings.TimeoutConstraint in project ant-ivy by apache.

the class HttpClientHandler method getURLInfo.

@SuppressWarnings("deprecation")
@Override
public URLInfo getURLInfo(final URL url, final TimeoutConstraint timeoutConstraint) {
    final int connectionTimeout = (timeoutConstraint == null || timeoutConstraint.getConnectionTimeout() < 0) ? 0 : timeoutConstraint.getConnectionTimeout();
    final int readTimeout = (timeoutConstraint == null || timeoutConstraint.getReadTimeout() < 0) ? 0 : timeoutConstraint.getReadTimeout();
    CloseableHttpResponse response = null;
    try {
        final String httpMethod;
        if (getRequestMethod() == TimeoutConstrainedURLHandler.REQUEST_METHOD_HEAD) {
            httpMethod = HttpHead.METHOD_NAME;
            response = doHead(url, connectionTimeout, readTimeout);
        } else {
            httpMethod = HttpGet.METHOD_NAME;
            response = doGet(url, connectionTimeout, readTimeout);
        }
        if (checkStatusCode(httpMethod, url, response)) {
            final HttpEntity responseEntity = response.getEntity();
            final Charset charSet = ContentType.getOrDefault(responseEntity).getCharset();
            return new URLInfo(true, responseEntity == null ? 0 : responseEntity.getContentLength(), getLastModified(response), charSet.name());
        }
    } catch (IOException | IllegalArgumentException e) {
        // IllegalArgumentException is thrown by HttpClient library to indicate the URL is not valid,
        // this happens for instance when trying to download a dynamic version (cfr IVY-390)
        Message.error("HttpClientHandler: " + e.getMessage() + " url=" + url);
    } finally {
        if (response != null) {
            try {
                response.close();
            } catch (IOException e) {
            // ignore
            }
        }
    }
    return UNAVAILABLE;
}
Also used : HttpEntity(org.apache.http.HttpEntity) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Charset(java.nio.charset.Charset) IOException(java.io.IOException) TimeoutConstraint(org.apache.ivy.core.settings.TimeoutConstraint)

Example 5 with TimeoutConstraint

use of org.apache.ivy.core.settings.TimeoutConstraint in project ant-ivy by apache.

the class URLResolverTest method testTimeoutConstraint.

/**
 * Tests that the timeout constraint set on the URL resolver is used correctly by the resolver
 *
 * @throws Exception if something goes wrong
 */
@Test
public void testTimeoutConstraint() throws Exception {
    final NamedTimeoutConstraint highTimeout = new NamedTimeoutConstraint("test-high-timeout");
    highTimeout.setConnectionTimeout(60000);
    settings.addConfigured(highTimeout);
    final NamedTimeoutConstraint extremelyLowTimeout = new NamedTimeoutConstraint("test-extremely-low-timeout");
    extremelyLowTimeout.setConnectionTimeout(10);
    extremelyLowTimeout.setReadTimeout(20);
    settings.addConfigured(extremelyLowTimeout);
    // setup a HTTP backed repo
    // TODO: Right now the port is hard coded, but we need to find a "available" port to which can be bind to.
    // Else this can lead to occasional bind failures
    final InetSocketAddress fastServerBindAddr = new InetSocketAddress("localhost", 12345);
    final String contextRoot = "/testTimeouts";
    final Path repoRoot = new File("test/repositories/1").toPath();
    assertTrue(repoRoot + " is not a directory", Files.isDirectory(repoRoot));
    final DependencyDescriptor dependency = new DefaultDependencyDescriptor(ModuleRevisionId.newInstance("org1", "mod1.1", "2.0"), false);
    try (final AutoCloseable httpServer = TestHelper.createHttpServerBackedRepository(fastServerBindAddr, contextRoot, repoRoot)) {
        final String ivyPattern = "http://" + fastServerBindAddr.getHostName() + ":" + fastServerBindAddr.getPort() + "/testTimeouts/[organisation]/[module]/ivys/ivy-[revision].xml";
        final String artifactPattern = "http://" + fastServerBindAddr.getHostName() + ":" + fastServerBindAddr.getPort() + "/testTimeouts/[organisation]/[module]/[type]s/[artifact]-[revision].[type]";
        // first use a resolver with a high timeout to make sure
        // it can actually fetch the resources
        final URLResolver highTimeoutResolver = new URLResolver();
        highTimeoutResolver.setName("high-timeout-resolver");
        highTimeoutResolver.setAllownomd(false);
        highTimeoutResolver.setTimeoutConstraint("test-high-timeout");
        highTimeoutResolver.setSettings(settings);
        highTimeoutResolver.setIvyPatterns(Collections.singletonList(ivyPattern));
        highTimeoutResolver.setArtifactPatterns(Collections.singletonList(artifactPattern));
        highTimeoutResolver.validate();
        final TimeoutConstraint resolverTimeoutConstraint = highTimeoutResolver.getTimeoutConstraint();
        assertNotNull("Timeout constraint is missing on resolver " + highTimeoutResolver.getName(), resolverTimeoutConstraint);
        assertEquals("Unexpected connection timeout on resolver", 60000, resolverTimeoutConstraint.getConnectionTimeout());
        assertEquals("Unexpected read timeout on resolver", -1, resolverTimeoutConstraint.getReadTimeout());
        // do the fetch (expected to work fine)
        final ResolvedModuleRevision resolvedModule = highTimeoutResolver.getDependency(dependency, data);
        assertNotNull("Dependency wasn't resolved by resolver " + highTimeoutResolver.getName(), resolvedModule);
        assertEquals("Unexpected dependency resolved by resolver " + highTimeoutResolver.getName(), dependency.getDependencyRevisionId(), resolvedModule.getId());
    }
    // now test this whole fetch using a resolver with a very low connection timeout and
    // by starting the repo server with a delay so that the connection request can timeout
    // clean the cache before testing to ensure the resource isn't fetched from cache
    settings.getDefaultRepositoryCacheManager().clean();
    settings.getResolutionCacheManager().clean();
    final InetSocketAddress slowServerAddr = new InetSocketAddress("localhost", 23456);
    final String ivyPattern = "http://" + slowServerAddr.getHostName() + ":" + slowServerAddr.getPort() + "/testTimeouts/[organisation]/[module]/ivys/ivy-[revision].xml";
    final String artifactPattern = "http://" + slowServerAddr.getHostName() + ":" + slowServerAddr.getPort() + "/testTimeouts/[organisation]/[module]/[type]s/[artifact]-[revision].[type]";
    final URLResolver lowTimeoutResolver = new URLResolver();
    lowTimeoutResolver.setAllownomd(false);
    lowTimeoutResolver.setName("low-timeout-resolver");
    lowTimeoutResolver.setTimeoutConstraint("test-extremely-low-timeout");
    lowTimeoutResolver.setSettings(settings);
    lowTimeoutResolver.setIvyPatterns(Collections.singletonList(ivyPattern));
    lowTimeoutResolver.setArtifactPatterns(Collections.singletonList(artifactPattern));
    lowTimeoutResolver.validate();
    final TimeoutConstraint lowTimeoutConstraint = lowTimeoutResolver.getTimeoutConstraint();
    assertNotNull("Timeout constraint is missing on resolver " + lowTimeoutResolver.getName(), lowTimeoutConstraint);
    assertEquals("Unexpected connection timeout on resolver", 10, lowTimeoutConstraint.getConnectionTimeout());
    assertEquals("Unexpected read timeout on resolver", 20, lowTimeoutConstraint.getReadTimeout());
    final ExecutorService executor = Executors.newSingleThreadExecutor();
    final long serverStartupDelayInMillis = 500;
    final Future<AutoCloseable> httpServer = executor.submit(new ServerManager(slowServerAddr, contextRoot, repoRoot, serverStartupDelayInMillis));
    try {
        // do the fetch (resolution *isn't* expected to return resolved module)
        final ResolvedModuleRevision resolvedModuleFromLowTimeouts = lowTimeoutResolver.getDependency(dependency, data);
        assertNull("Dependency wasn't expected to be resolved by resolver " + lowTimeoutResolver.getName(), resolvedModuleFromLowTimeouts);
    } finally {
        try {
            // stop the server
            httpServer.get().close();
        } catch (Exception e) {
        // ignore
        // TODO: Better log it too. But I don't see usage of loggers in test cases currently. So need to get to this later
        }
        try {
            executor.shutdownNow();
        } catch (Exception e) {
        // ignore
        // TODO: Better log it too. But I don't see usage of loggers in test cases currently. So need to get to this later
        }
    }
}
Also used : Path(java.nio.file.Path) DependencyDescriptor(org.apache.ivy.core.module.descriptor.DependencyDescriptor) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor) InetSocketAddress(java.net.InetSocketAddress) NamedTimeoutConstraint(org.apache.ivy.core.settings.NamedTimeoutConstraint) TimeoutConstraint(org.apache.ivy.core.settings.TimeoutConstraint) ResolvedModuleRevision(org.apache.ivy.core.resolve.ResolvedModuleRevision) NamedTimeoutConstraint(org.apache.ivy.core.settings.NamedTimeoutConstraint) ExecutorService(java.util.concurrent.ExecutorService) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor) File(java.io.File) Test(org.junit.Test)

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