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

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

Introduction

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

Prototype

public HttpClientContext() 

Source Link

Usage

From source file:com.yaauie.unfurl.UrlExpander.java

public URI expand(URI uri) throws IOException {
    final HttpHead request = new HttpHead(uri);
    final HttpClientContext context = new HttpClientContext();

    httpClient.execute(request, context);

    final List<URI> redirectLocations = context.getRedirectLocations();
    if (redirectLocations == null || redirectLocations.isEmpty()) {
        return uri;
    } else {//ww  w .j  a  va 2s  .  c  o  m
        return redirectLocations.get(redirectLocations.size() - 1);
    }
}

From source file:goofyhts.torrentkinesis.test.HttpTest.java

@Test
public void testGet() {
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        HttpClientContext context = new HttpClientContext();
        CredentialsProvider credProv = new BasicCredentialsProvider();
        credProv.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("root", "Whcinhry21#"));
        context.setCredentialsProvider(credProv);
        HttpGet httpGet = new HttpGet("http://localhost:8150/gui/token.html");

        CloseableHttpResponse response = client.execute(httpGet, context);
        String responseBody = IOUtils.toString(response.getEntity().getContent());
        System.out.println(responseBody);
        Document doc = Jsoup.parse(responseBody);
        System.out.println(doc.getElementById("token").text());
    } catch (ClientProtocolException e) {
        e.printStackTrace();// w  w  w  .jav a2s  .  c o m
    } catch (IOException e) {
        e.printStackTrace();
    }
    //Assert.assertTrue(true);
}

From source file:com.ksc.http.apache.utils.ApacheUtils.java

/**
 * Returns a new HttpClientContext used for request execution.
 *//*w  w  w  . j  a  v  a2 s.co  m*/
public static HttpClientContext newClientContext(HttpClientSettings settings,
        Map<String, ? extends Object> attributes) {
    final HttpClientContext clientContext = new HttpClientContext();

    if (attributes != null && !attributes.isEmpty()) {
        for (Map.Entry<String, ?> entry : attributes.entrySet()) {
            clientContext.setAttribute(entry.getKey(), entry.getValue());
        }
    }

    addPreemptiveAuthenticationProxy(clientContext, settings);
    return clientContext;

}

From source file:org.opensaml.security.httpclient.impl.SecurityEnhancedTLSSocketFactoryTest.java

@BeforeMethod
public void buildHttpContext() {
    httpContext = new HttpClientContext();
}

From source file:goofyhts.torrentkinesis.http.client.DefaultHttpClient.java

@Override
public void open() {
    client = HttpClients.createDefault();
    context = new HttpClientContext();
    CredentialsProvider credProv = new BasicCredentialsProvider();
    credProv.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
    context.setCredentialsProvider(credProv);
}

From source file:org.apache.solr.client.solrj.impl.SolrHttpClientContextBuilder.java

public HttpClientContext createContext(Object userToken) {
    HttpClientContext context = new HttpClientContext();
    if (getCredentialsProviderProvider() != null) {
        context.setCredentialsProvider(getCredentialsProviderProvider().getCredentialsProvider());
    }/*from  ww  w .  j  a va  2 s . c  om*/
    if (getAuthSchemeRegistryProvider() != null) {
        context.setAuthSchemeRegistry(getAuthSchemeRegistryProvider().getAuthSchemeRegistry());
    }

    if (getCookieSpecRegistryProvider() != null) {
        context.setCookieSpecRegistry(getCookieSpecRegistryProvider().getCookieSpecRegistry());
    }

    context.setUserToken(userToken);

    return context;
}

From source file:org.sonatype.spice.zapper.client.hc4.Hc4Client.java

@Override
public State upload(final Payload payload, final Hc4Track track) throws IOException {
    final String url = getRemoteUrl() + payload.getPath().stringValue();
    final HttpPut put = new HttpPut(url);
    if (payload instanceof SegmentPayload) {
        put.setEntity(new ZapperEntity(payload, getParameters().getCodecSelector()
                .selectCodecs(SegmentPayload.class.cast(payload).getSegment().getZFile())));
    } else {//from  ww w. j a va  2  s.com
        put.setEntity(new ZapperEntity(payload));
    }
    put.addHeader("X-Zapper-Transfer-ID", payload.getTransferIdentifier().stringValue());
    if (track != null) {
        put.addHeader("X-Zapper-Track-ID", track.getIdentifier().stringValue());
    }
    final HttpClientContext context = new HttpClientContext();
    if (preemptiveCredentialsProvider != null) {
        context.setCredentialsProvider(preemptiveCredentialsProvider);
        context.setAuthCache(new BasicAuthCache());
        context.getAuthCache().put(
                new HttpHost(put.getURI().getHost(), put.getURI().getPort(), put.getURI().getScheme()),
                new BasicScheme());
    }
    final HttpResponse response = httpClient.execute(put, context);
    final StatusLine statusLine = response.getStatusLine();
    EntityUtils.consume(response.getEntity());
    if (!(statusLine.getStatusCode() > 199 && statusLine.getStatusCode() < 299)) {
        throw new IOException(String.format("Unexpected server response: %s %s", statusLine.getStatusCode(),
                statusLine.getReasonPhrase()));
    }
    return State.SUCCESS;
}

From source file:net.officefloor.plugin.web.http.security.integrate.AbstractHttpSecurityIntegrateTestCase.java

/**
 * Use credentials.//from  w  w w .  ja v  a  2 s. co  m
 * 
 * @param realm
 *            Security realm.
 * @param scheme
 *            Security scheme.
 * @param username
 *            User name.
 * @param password
 *            Password.
 * @return {@link CredentialsProvider}.
 * @throws IOException
 *             If fails to use credentials.
 */
protected CredentialsProvider useCredentials(String realm, String scheme, String username, String password)
        throws IOException {

    // Close the existing client
    this.client.close();

    // Use client with credentials
    HttpClientBuilder builder = HttpClientBuilder.create();
    CredentialsProvider provider = HttpTestUtil.configureCredentials(builder, realm, scheme, username,
            password);
    this.client = builder.build();

    // Reset the client context
    this.context = new HttpClientContext();

    // FIXME: determine if HttpClient cookie authentication fix
    if ("Digest".equalsIgnoreCase(scheme)) {
        isDigestHttpClientCookieBug = true;
    }

    // Return the credentials provider
    return provider;
}

From source file:com.gargoylesoftware.htmlunit.HttpWebConnection.java

/**
 * Creates a new HTTP web connection instance.
 * @param webClient the WebClient that is using this connection
 *//*from  w  ww  .  j  a v  a2s  .  c  o m*/
public HttpWebConnection(final WebClient webClient) {
    webClient_ = webClient;
    htmlUnitCookieSpecProvider_ = new HtmlUnitCookieSpecProvider(webClient.getBrowserVersion());
    httpContext_ = new HttpClientContext();
    usedOptions_ = new WebClientOptions();
}

From source file:com.jaeksoft.searchlib.crawler.web.spider.HttpAbstract.java

protected void execute(HttpRequestBase httpBaseRequest, CredentialItem credentialItem, List<CookieItem> cookies)
        throws ClientProtocolException, IOException, URISyntaxException {

    if (!CollectionUtils.isEmpty(cookies)) {
        List<Cookie> cookieList = cookieStore.getCookies();
        for (CookieItem cookie : cookies) {
            Cookie newCookie = cookie.getCookie();
            if (!cookieList.contains(newCookie))
                cookieStore.addCookie(newCookie);
        }/*from www .  j ava2  s .c o  m*/
    }

    this.httpBaseRequest = httpBaseRequest;

    // No more than one 1 minute to establish the connection
    // No more than 10 minutes to establish the socket
    // Enable stales connection checking
    // Cookies uses browser compatibility
    RequestConfig.Builder configBuilber = RequestConfig.custom().setSocketTimeout(1000 * 60 * 10)
            .setConnectTimeout(1000 * 60).setStaleConnectionCheckEnabled(true)
            .setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY);

    if (credentialItem == null)
        credentialsProvider.clear();
    else
        credentialItem.setUpCredentials(credentialsProvider);

    URI uri = httpBaseRequest.getURI();
    if (proxyHandler != null)
        proxyHandler.check(configBuilber, uri, credentialsProvider);

    httpBaseRequest.setConfig(configBuilber.build());

    httpClientContext = new HttpClientContext();

    httpResponse = httpClient.execute(httpBaseRequest, httpClientContext);
    if (httpResponse == null)
        return;
    statusLine = httpResponse.getStatusLine();
    httpEntity = httpResponse.getEntity();
}