Example usage for org.apache.http.client.protocol HttpClientContext create

List of usage examples for org.apache.http.client.protocol HttpClientContext create

Introduction

In this page you can find the example usage for org.apache.http.client.protocol HttpClientContext create.

Prototype

public static HttpClientContext create() 

Source Link

Usage

From source file:com.cloud.utils.rest.RESTServiceConnectorTest.java

@Test
public void testCustomDeserializerForCustomLists() throws Exception {
    final CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    when(response.getStatusLine()).thenReturn(HTTP_200_STATUS_LINE);
    when(response.getEntity())//from w ww.  ja va2  s .  c  om
            .thenReturn(new StringEntity("{results: [{field : \"SomeValue\"}], results_count: 1}"));
    final CloseableHttpClient httpClient = mock(CloseableHttpClient.class);
    when(httpClient.execute(any(HttpHost.class), any(HttpRequest.class), any(HttpClientContext.class)))
            .thenReturn(response);
    final RestClient restClient = new BasicRestClient(httpClient, HttpClientContext.create(), "localhost");
    final Class<? extends CollectionType> clazzListOfTestPojo = new ObjectMapper().getTypeFactory()
            .constructCollectionType(List.class, TestPojo.class).getClass();
    final RESTServiceConnector connector = new RESTServiceConnector.Builder().client(restClient)
            .classToDeserializerEntry(clazzListOfTestPojo, new CustomListDeserializer<TestPojoDeserializer>())
            .build();

    connector.executeRetrieveObject(TestPojo.class, "/somepath");
}

From source file:org.fao.geonet.utils.AbstractHttpRequest.java

public void setCookieStore(CookieStore cookieStore) {
    this.cookieStore = cookieStore;
    HttpContext context = getHttpClientContext();
    if (context == null) {
        httpClientContext = HttpClientContext.create();
    }/*w  w w .j a v  a2 s .co  m*/
    httpClientContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
}

From source file:org.apache.maven.wagon.providers.http.AbstractHttpClientWagon.java

public void openConnectionInternal() {
    repository.setUrl(getURL(repository));

    localContext = HttpClientContext.create();
    credentialsProvider = new BasicCredentialsProvider();
    authCache = new BasicAuthCache();
    localContext.setCredentialsProvider(credentialsProvider);
    localContext.setAuthCache(authCache);

    if (authenticationInfo != null) {

        String username = authenticationInfo.getUserName();
        String password = authenticationInfo.getPassword();

        if (StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(password)) {
            Credentials creds = new UsernamePasswordCredentials(username, password);

            String host = getRepository().getHost();
            int port = getRepository().getPort();

            credentialsProvider.setCredentials(getBasicAuthScope().getScope(host, port), creds);
        }/*from w ww  . ja v a  2 s  .c  o  m*/
    }

    ProxyInfo proxyInfo = getProxyInfo(getRepository().getProtocol(), getRepository().getHost());
    if (proxyInfo != null) {
        String proxyUsername = proxyInfo.getUserName();
        String proxyPassword = proxyInfo.getPassword();
        String proxyHost = proxyInfo.getHost();
        String proxyNtlmHost = proxyInfo.getNtlmHost();
        String proxyNtlmDomain = proxyInfo.getNtlmDomain();
        if (proxyHost != null) {
            if (proxyUsername != null && proxyPassword != null) {
                Credentials creds;
                if (proxyNtlmHost != null || proxyNtlmDomain != null) {
                    creds = new NTCredentials(proxyUsername, proxyPassword, proxyNtlmHost, proxyNtlmDomain);
                } else {
                    creds = new UsernamePasswordCredentials(proxyUsername, proxyPassword);
                }

                int port = proxyInfo.getPort();

                AuthScope authScope = getProxyBasicAuthScope().getScope(proxyHost, port);
                credentialsProvider.setCredentials(authScope, creds);
            }
        }
    }
}

From source file:org.apache.sling.testing.clients.AbstractSlingClient.java

private HttpClientContext createHttpClientContextFromConfig() {
    // create context from config
    HttpClientContext context = HttpClientContext.create();

    if (config.getCookieStore() != null) {
        context.setCookieStore(config.getCookieStore());
    }/*w w w .  j a  va2 s .com*/

    if (config.getCredsProvider() != null) {
        context.setCredentialsProvider(config.getCredsProvider());
    }

    if (config.getAuthCache() != null) {
        context.setAuthCache(config.getAuthCache());
    }

    return context;
}

From source file:com.mirth.connect.connectors.ws.WebServiceDispatcher.java

/**
 * Returns the URL for the passed in String. If the URL requires authentication, then the WSDL
 * is saved as a temp file and the URL for that file is returned.
 * /*from   w  w  w.j a  va2  s .  co m*/
 * @param wsdlUrl
 * @param username
 * @param password
 * @return
 * @throws Exception
 */
private URL getWsdlUrl(DispatchContainer dispatchContainer) throws Exception {
    URI uri = new URI(dispatchContainer.getCurrentWsdlUrl());

    // If the URL points to file, just return it
    if (!uri.getScheme().equalsIgnoreCase("file")) {
        BasicHttpClientConnectionManager httpClientConnectionManager = new BasicHttpClientConnectionManager(
                socketFactoryRegistry.build());
        httpClientConnectionManager.setSocketConfig(SocketConfig.custom().setSoTimeout(timeout).build());
        HttpClientBuilder clientBuilder = HttpClients.custom()
                .setConnectionManager(httpClientConnectionManager);
        HttpUtil.configureClientBuilder(clientBuilder);
        CloseableHttpClient client = clientBuilder.build();

        try {
            clients.add(client);
            HttpClientContext context = HttpClientContext.create();

            if (dispatchContainer.getCurrentUsername() != null
                    && dispatchContainer.getCurrentPassword() != null) {
                CredentialsProvider credsProvider = new BasicCredentialsProvider();
                AuthScope authScope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT,
                        AuthScope.ANY_REALM);
                Credentials credentials = new UsernamePasswordCredentials(
                        dispatchContainer.getCurrentUsername(), dispatchContainer.getCurrentPassword());
                credsProvider.setCredentials(authScope, credentials);
                AuthCache authCache = new BasicAuthCache();
                RegistryBuilder<AuthSchemeProvider> registryBuilder = RegistryBuilder
                        .<AuthSchemeProvider>create();
                registryBuilder.register(AuthSchemes.BASIC, new BasicSchemeFactory());

                context.setCredentialsProvider(credsProvider);
                context.setAuthSchemeRegistry(registryBuilder.build());
                context.setAuthCache(authCache);
            }

            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout)
                    .setSocketTimeout(timeout).setStaleConnectionCheckEnabled(true).build();
            context.setRequestConfig(requestConfig);

            return getWsdl(client, context, dispatchContainer, new HashMap<String, File>(),
                    dispatchContainer.getCurrentWsdlUrl()).toURI().toURL();
        } finally {
            HttpClientUtils.closeQuietly(client);
            clients.remove(client);
        }
    }

    return uri.toURL();
}

From source file:de.dtag.tlabs.cbclient.CBClient.java

public void performHO() {

    try {/*from w w  w .ja  va2s .  c om*/
        System.out.println("Perform HO");
        HttpHost targetHost = new HttpHost(httpHostAddr, httpHostPort, "http");
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()),
                new UsernamePasswordCredentials("admin", "epc"));

        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate BASIC scheme object and add it to the local auth cache
        BasicScheme basicAuth = new BasicScheme();
        authCache.put(targetHost, basicAuth);

        // Add AuthCache to the execution context
        HttpClientContext context = HttpClientContext.create();
        context.setCredentialsProvider(credsProvider);

        HttpPost httpPost = new HttpPost(andsfResource);

        switch (newPolicy) {
        case NONE:
            break;

        case LTE:
            //Switch over to LTE
            sendingPolicy = toLTE;
            currentPolicy = accessModes.LTE;
            System.out.println("SWITCHING OVER TO LTE");
            break;

        case WIFI:
            //Switch over to WIFI
            sendingPolicy = toWiFi;
            currentPolicy = accessModes.WIFI;
            System.out.println("SWITCHING OVER TO WIFI");
            break;

        default:
            break;
        }

        StringEntity postEntity = new StringEntity(sendingPolicy, ContentType.APPLICATION_FORM_URLENCODED);

        httpPost.setEntity(postEntity);

        StringWriter writer = new StringWriter();

        System.out.println("httpPost: " + httpPost);

        CloseableHttpResponse response = httpClient.execute(targetHost, httpPost, context);
        try {
            HttpEntity entity = response.getEntity();
            System.out.println("Response: " + response.getStatusLine().toString());

            InputStream is = entity.getContent();
            IOUtils.copy(is, writer);
            String responseContent = writer.toString();

            if (responseContent.contains("Success")) {
                System.out.println("Response content: Success");
            } else {
                System.out.println("Response content: No Success found");
            }

        } finally {
            response.close();
        }
    } catch (IOException e) {
        System.out.println("IOException found");
        e.printStackTrace();
    } catch (IllegalStateException e) {
        System.out.println("Exception found");
        e.printStackTrace();
    }
}