Search in sources :

Example 1 with HttpClientOptions

use of com.atlassian.httpclient.api.factory.HttpClientOptions in project jira-plugin by jenkinsci.

the class ApacheAsyncHttpClientTest method simple_post.

@Test
public void simple_post() throws Exception {
    TestHandler testHandler = new TestHandler();
    prepare(testHandler);
    ApacheAsyncHttpClient httpClient = new ApacheAsyncHttpClient(null, buildApplicationProperties(), new NoOpThreadLocalContextManager(), new HttpClientOptions());
    Response response = // 
    httpClient.newRequest("http://localhost:" + connector.getLocalPort() + "/foo").setEntity(// 
    "FOO").setContentType(// 
    "text").post().get(10, TimeUnit.SECONDS);
    Assert.assertEquals(200, response.getStatusCode());
    Assert.assertEquals(CONTENT_RESPONSE, IOUtils.toString(response.getEntityStream()));
    Assert.assertEquals("FOO", testHandler.postReceived);
}
Also used : Response(com.atlassian.httpclient.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpClientOptions(com.atlassian.httpclient.api.factory.HttpClientOptions) Test(org.junit.Test)

Example 2 with HttpClientOptions

use of com.atlassian.httpclient.api.factory.HttpClientOptions in project jira-plugin by jenkinsci.

the class JiraSite method getHttpClientOptions.

private HttpClientOptions getHttpClientOptions() {
    final HttpClientOptions options = new HttpClientOptions();
    options.setRequestTimeout(readTimeout, TimeUnit.SECONDS);
    options.setSocketTimeout(timeout, TimeUnit.SECONDS);
    options.setCallbackExecutor(getExecutorService());
    options.setIoThreadCount(ioThreadCount);
    return options;
}
Also used : HttpClientOptions(com.atlassian.httpclient.api.factory.HttpClientOptions)

Example 3 with HttpClientOptions

use of com.atlassian.httpclient.api.factory.HttpClientOptions in project jira-plugin by jenkinsci.

the class ApacheAsyncHttpClient method createClientBuilder.

private HttpAsyncClientBuilder createClientBuilder() throws IOReactorException {
    final HttpClientOptions options = httpClientOptions;
    final IOReactorConfig reactorConfig = IOReactorConfig.custom().setIoThreadCount(options.getIoThreadCount()).setSelectInterval(options.getIoSelectInterval()).setInterestOpQueued(true).build();
    final DefaultConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(reactorConfig);
    ioReactor.setExceptionHandler(new IOReactorExceptionHandler() {

        @Override
        public boolean handle(final IOException e) {
            log.error("IO exception in reactor ", e);
            return false;
        }

        @Override
        public boolean handle(final RuntimeException e) {
            log.error("Fatal runtime error", e);
            return false;
        }
    });
    final PoolingNHttpClientConnectionManager connectionManager = new PoolingNHttpClientConnectionManager(ioReactor, ManagedNHttpClientConnectionFactory.INSTANCE, getRegistry(options), DefaultSchemePortResolver.INSTANCE, SystemDefaultDnsResolver.INSTANCE, options.getConnectionPoolTimeToLive(), TimeUnit.MILLISECONDS) {

        @Override
        protected void finalize() throws Throwable {
            // is still active.
            try {
                this.shutdown();
            } catch (Throwable e) {
            // ignore e.printStackTrace();
            }
        }
    };
    connectionManager.setDefaultMaxPerRoute(options.getMaxConnectionsPerHost());
    final RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout((int) options.getConnectionTimeout()).setConnectionRequestTimeout((int) options.getLeaseTimeout()).setCookieSpec(options.getIgnoreCookies() ? CookieSpecs.IGNORE_COOKIES : CookieSpecs.DEFAULT).setSocketTimeout((int) options.getSocketTimeout()).build();
    final HttpAsyncClientBuilder clientBuilder = HttpAsyncClients.custom().setThreadFactory(ThreadFactories.namedThreadFactory(options.getThreadPrefix() + "-io", ThreadFactories.Type.DAEMON)).setDefaultIOReactorConfig(reactorConfig).setConnectionManager(connectionManager).setRedirectStrategy(new RedirectStrategy()).setUserAgent(getUserAgent(options)).setDefaultRequestConfig(requestConfig);
    if (Jenkins.get() != null) {
        ProxyConfiguration proxyConfiguration = Jenkins.getInstance().proxy;
        if (proxyConfiguration != null) {
            final HttpHost proxy = new HttpHost(proxyConfiguration.name, proxyConfiguration.port);
            // clientBuilder.setProxy( proxy );
            if (StringUtils.isNotBlank(proxyConfiguration.getUserName())) {
                clientBuilder.setProxyAuthenticationStrategy(ProxyAuthenticationStrategy.INSTANCE);
                CredentialsProvider credsProvider = new BasicCredentialsProvider();
                credsProvider.setCredentials(new AuthScope(proxyConfiguration.name, proxyConfiguration.port), new UsernamePasswordCredentials(proxyConfiguration.getUserName(), proxyConfiguration.getPassword()));
                clientBuilder.setDefaultCredentialsProvider(credsProvider);
            }
            clientBuilder.setRoutePlanner(new JenkinsProxyRoutePlanner(proxy, proxyConfiguration.getNoProxyHostPatterns()));
        }
    }
    return clientBuilder;
}
Also used : IOReactorExceptionHandler(org.apache.http.nio.reactor.IOReactorExceptionHandler) RequestConfig(org.apache.http.client.config.RequestConfig) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) IOException(java.io.IOException) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) HttpClientOptions(com.atlassian.httpclient.api.factory.HttpClientOptions) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) IOReactorConfig(org.apache.http.impl.nio.reactor.IOReactorConfig) DefaultConnectingIOReactor(org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor) ProxyConfiguration(hudson.ProxyConfiguration) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) PoolingNHttpClientConnectionManager(org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager)

Example 4 with HttpClientOptions

use of com.atlassian.httpclient.api.factory.HttpClientOptions in project staf by simpleworks-gmbh.

the class AsynchronousHttpClientFactory method createClient.

@SuppressWarnings("static-method")
public DisposableHttpClient createClient(final URI serverUri, final AuthenticationHandler authenticationHandler) {
    final HttpClientOptions options = new HttpClientOptions();
    @SuppressWarnings({ "rawtypes", "unchecked" }) final DefaultHttpClientFactory defaultHttpClientFactory = new DefaultHttpClientFactory(new NoOpEventPublisher(), new RestClientApplicationProperties(serverUri), new ThreadLocalContextManager() {

        @Override
        public Object getThreadLocalContext() {
            return null;
        }

        @Override
        public void setThreadLocalContext(final Object context) {
        // nothing to do.
        }

        @Override
        public void clearThreadLocalContext() {
        // nothing to do.
        }
    });
    final HttpClient httpClient = defaultHttpClientFactory.create(options);
    return new AtlassianHttpClientDecorator(httpClient, authenticationHandler) {

        @Override
        public void destroy() throws Exception {
            defaultHttpClientFactory.dispose(httpClient);
        }
    };
}
Also used : DefaultHttpClientFactory(com.atlassian.httpclient.apache.httpcomponents.DefaultHttpClientFactory) HttpClient(com.atlassian.httpclient.api.HttpClient) ThreadLocalContextManager(com.atlassian.sal.api.executor.ThreadLocalContextManager) HttpClientOptions(com.atlassian.httpclient.api.factory.HttpClientOptions)

Example 5 with HttpClientOptions

use of com.atlassian.httpclient.api.factory.HttpClientOptions in project cx-flow by checkmarx-ltd.

the class CustomAsynchronousHttpClientFactory method createClientCustom.

@SuppressWarnings("unchecked")
public DisposableHttpClient createClientCustom(final URI serverUri, final AuthenticationHandler authenticationHandler, int socketTimeoutInMs) {
    final HttpClientOptions options = new HttpClientOptions();
    options.setSocketTimeout(socketTimeoutInMs, TimeUnit.MILLISECONDS);
    options.setRequestTimeout(socketTimeoutInMs, TimeUnit.MILLISECONDS);
    final DefaultHttpClientFactory defaultHttpClientFactory = new DefaultHttpClientFactory(new NoOpEventPublisher(), new RestClientApplicationProperties(serverUri), new ThreadLocalContextManager() {

        @Override
        public Object getThreadLocalContext() {
            return null;
        }

        @Override
        public void setThreadLocalContext(Object context) {
        }

        @Override
        public void clearThreadLocalContext() {
        }
    });
    final HttpClient httpClient = defaultHttpClientFactory.create(options);
    return new AtlassianHttpClientDecorator(httpClient, authenticationHandler) {

        @Override
        public void destroy() throws Exception {
            defaultHttpClientFactory.dispose(httpClient);
        }
    };
}
Also used : DefaultHttpClientFactory(com.atlassian.httpclient.apache.httpcomponents.DefaultHttpClientFactory) HttpClient(com.atlassian.httpclient.api.HttpClient) ThreadLocalContextManager(com.atlassian.sal.api.executor.ThreadLocalContextManager) HttpClientOptions(com.atlassian.httpclient.api.factory.HttpClientOptions)

Aggregations

HttpClientOptions (com.atlassian.httpclient.api.factory.HttpClientOptions)9 Response (com.atlassian.httpclient.api.Response)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 Test (org.junit.Test)5 ProxyConfiguration (hudson.ProxyConfiguration)4 DefaultHttpClientFactory (com.atlassian.httpclient.apache.httpcomponents.DefaultHttpClientFactory)2 HttpClient (com.atlassian.httpclient.api.HttpClient)2 ThreadLocalContextManager (com.atlassian.sal.api.executor.ThreadLocalContextManager)2 IOException (java.io.IOException)1 HttpHost (org.apache.http.HttpHost)1 AuthScope (org.apache.http.auth.AuthScope)1 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)1 CredentialsProvider (org.apache.http.client.CredentialsProvider)1 RequestConfig (org.apache.http.client.config.RequestConfig)1 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)1 HttpAsyncClientBuilder (org.apache.http.impl.nio.client.HttpAsyncClientBuilder)1 PoolingNHttpClientConnectionManager (org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager)1 DefaultConnectingIOReactor (org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor)1 IOReactorConfig (org.apache.http.impl.nio.reactor.IOReactorConfig)1 IOReactorExceptionHandler (org.apache.http.nio.reactor.IOReactorExceptionHandler)1