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

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

Introduction

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

Prototype

public HttpHost getTargetHost() 

Source Link

Usage

From source file:org.callimachusproject.client.HttpUriClient.java

private URI getSystemId(HttpClientContext ctx) {
    HttpUriRequest request = (HttpUriRequest) ctx.getRequest();
    try {//from   ww w.j av a2  s  .c  o  m
        URI original = request.getURI();
        HttpHost target = ctx.getTargetHost();
        List<URI> list = ctx.getRedirectLocations();
        URI absolute = URIUtils.resolve(original, target, list);
        return new URI(TermFactory.newInstance(absolute.toASCIIString()).getSystemId());
    } catch (URISyntaxException e) {
        return request.getURI();
    }
}

From source file:org.sonatype.nexus.httpclient.PreemptiveAuthHttpRequestInterceptor.java

@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    HttpClientContext clientContext = HttpClientContext.adapt(context);
    AuthState authState = clientContext.getTargetAuthState();
    if (authState.getAuthScheme() == null) {
        CredentialsProvider credsProvider = clientContext.getCredentialsProvider();
        HttpHost targetHost = clientContext.getTargetHost();
        Credentials creds = credsProvider
                .getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
        if (creds != null) {
            authState.update(new BasicScheme(), creds);
        }/*from   ww  w  . j  ava  2 s  .  co m*/
    }
}

From source file:org.sonatype.nexus.apachehttpclient.PreemptiveAuthHttpRequestInterceptor.java

@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    final HttpClientContext clientContext = HttpClientContext.adapt(context);
    final AuthState authState = clientContext.getTargetAuthState();
    if (authState.getAuthScheme() == null) {
        final CredentialsProvider credsProvider = clientContext.getCredentialsProvider();
        final HttpHost targetHost = clientContext.getTargetHost();
        final Credentials creds = credsProvider
                .getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
        if (creds != null) {
            authState.update(new BasicScheme(), creds);
        }//w  ww  . j ava2  s.c  om
    }
}

From source file:org.callimachusproject.client.HttpClientRedirectTest.java

/**
 * The interpreted (absolute) URI that was used to generate the last
 * request./*from   ww w  . ja v  a2 s. com*/
 */
private URI getHttpLocation(HttpUriRequest originalRequest, HttpClientContext ctx) {
    try {
        URI original = originalRequest.getURI();
        HttpHost target = ctx.getTargetHost();
        List<URI> redirects = ctx.getRedirectLocations();
        URI absolute = URIUtils.resolve(original, target, redirects);
        return new URI(TermFactory.newInstance(absolute.toASCIIString()).getSystemId());
    } catch (URISyntaxException e) {
        return null;
    }
}

From source file:org.artifactory.util.PreemptiveAuthInterceptor.java

@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    HttpClientContext clientContext = HttpClientContext.adapt(context);
    AuthState authState = clientContext.getTargetAuthState();

    // If there's no auth scheme available yet, try to initialize it preemptively
    if (authState.getAuthScheme() == null) {
        CredentialsProvider credsProvider = clientContext.getCredentialsProvider();
        HttpHost targetHost = clientContext.getTargetHost();
        Credentials creds = credsProvider
                .getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
        if (creds == null) {
            log.debug("No credentials found for host " + targetHost);
        } else {//from   ww w. j  a  v a  2s.c  o  m
            log.debug("Updating credentials for host " + targetHost);
            authState.update(new BasicScheme(), creds);
        }
    }
}

From source file:org.sonatype.nexus.internal.httpclient.HttpClientManagerImpl.java

/**
 * Creates absolute request URI with full path from passed in context.
 *//*from   www .  j  a va  2s .c  o  m*/
@Nonnull
private URI getRequestURI(final HttpContext context) {
    final HttpClientContext clientContext = HttpClientContext.adapt(context);
    final HttpRequest httpRequest = clientContext.getRequest();
    final HttpHost target = clientContext.getTargetHost();
    try {
        URI uri;
        if (httpRequest instanceof HttpUriRequest) {
            uri = ((HttpUriRequest) httpRequest).getURI();
        } else {
            uri = URI.create(httpRequest.getRequestLine().getUri());
        }
        return uri.isAbsolute() ? uri : URIUtils.resolve(URI.create(target.toURI()), uri);
    } catch (Exception e) {
        log.warn("Could not create absolute request URI", e);
        return URI.create(target.toURI());
    }
}

From source file:sachin.spider.SpiderConfig.java

private String handleRedirect(String url) {
    try {/*from   w w w. j  a v a  2  s  .  c o  m*/
        HttpGet httpget = new HttpGet(url);
        RequestConfig requestConfig = RequestConfig.custom().setRedirectsEnabled(true)
                .setCircularRedirectsAllowed(true).setRelativeRedirectsAllowed(true)
                .setConnectionRequestTimeout(getConnectionRequestTimeout()).setSocketTimeout(getSocketTimeout())
                .setConnectTimeout(getConnectionTimeout()).build();
        httpget.setConfig(requestConfig);
        HttpClientBuilder builder = HttpClientBuilder.create();
        builder.setUserAgent(getUserAgentString());
        if (isAuthenticate()) {
            CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY,
                    new UsernamePasswordCredentials(getUsername(), getPassword()));
            builder.setDefaultCredentialsProvider(credentialsProvider);
        }
        CloseableHttpClient httpclient = builder.build();
        HttpClientContext context = HttpClientContext.create();
        CloseableHttpResponse response = httpclient.execute(httpget, context);
        HttpHost target = context.getTargetHost();
        List<URI> redirectLocations = context.getRedirectLocations();
        URI location = URIUtils.resolve(httpget.getURI(), target, redirectLocations);
        url = location.toString();
        EntityUtils.consumeQuietly(response.getEntity());
        HttpClientUtils.closeQuietly(response);
    } catch (IOException | URISyntaxException ex) {
        Logger.getLogger(SpiderConfig.class.getName()).log(Level.SEVERE, null, ex);
        System.err.println(url);
    }
    return url;
}

From source file:org.artifactory.util.ProxyPreemptiveAuthInterceptor.java

@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    HttpClientContext clientContext = HttpClientContext.adapt(context);
    AuthState proxyAuthState = clientContext.getProxyAuthState();

    // If there's no auth scheme available yet, try to initialize it preemptively
    if (proxyAuthState.getAuthScheme() == null) {
        CredentialsProvider credsProvider = clientContext.getCredentialsProvider();
        RouteInfo route = clientContext.getHttpRoute();
        if (route == null) {
            log.debug("No route found for {}", clientContext.getTargetHost());
            return;
        }// w ww .j  av  a 2 s.  c o m

        HttpHost proxyHost = route.getProxyHost();
        if (proxyHost == null) {
            log.warn("No proxy host found in route {} for host {}", route, clientContext.getTargetHost());
            return;
        }

        Credentials creds = credsProvider
                .getCredentials(new AuthScope(proxyHost.getHostName(), proxyHost.getPort()));
        if (creds == null) {
            log.info("No credentials found for proxy: " + proxyHost);
            return;
        }
        proxyAuthState.update(new BasicScheme(ChallengeState.PROXY), creds);
    }
}

From source file:sachin.spider.WebSpider.java

private void handleRedirectedLink(WebURL curUrl) {
    String url = curUrl.getUrl();
    HttpGet httpget = new HttpGet(url);
    RequestConfig requestConfig = getRequestConfigWithRedirectEnabled();
    httpget.setConfig(requestConfig);//from   w w  w .j av  a  2  s. c om
    try {
        HttpClientContext context = HttpClientContext.create();
        long startingTime = System.currentTimeMillis();
        try (CloseableHttpResponse response = httpclient.execute(httpget, context)) {
            long endingTime = System.currentTimeMillis();
            HttpHost target = context.getTargetHost();
            List<URI> redirectLocations = context.getRedirectLocations();
            URI location = URIUtils.resolve(httpget.getURI(), target, redirectLocations);
            String redirectUrl = location.toString();
            curUrl.setRedirectTo(redirectUrl);
            curUrl.setResposneTime(((int) (endingTime - startingTime)) / 1000);
            redirectUrl = URLCanonicalizer.getCanonicalURL(redirectUrl);
            WebURL weburl = new WebURL(redirectUrl);
            weburl.addParent(curUrl);
            if (!config.links.contains(weburl)) {
                config.links.add(weburl);
            }
            try {
                if (redirectLocations != null) {
                    for (URI s : redirectLocations) {
                        String urls = URLCanonicalizer.getCanonicalURL(s.toString());
                        WebURL url1 = new WebURL(urls);
                        if (!config.links.contains(url1)) {
                            config.links.add(url1);
                        }
                    }
                }
            } catch (Exception ex) {
                Logger.getLogger(WebSpider.class.getName()).log(Level.SEVERE, null, ex);
            }
            EntityUtils.consumeQuietly(response.getEntity());
            HttpClientUtils.closeQuietly(response);
        }
    } catch (IOException | URISyntaxException ex) {
        System.out.println(curUrl.getUrl());
        curUrl.setErrorMsg(ex.toString());
        Logger.getLogger(WebSpider.class.getName()).log(Level.SEVERE, null, ex);
    } catch (Exception ex) {
        System.out.println(curUrl.getUrl());
        curUrl.setErrorMsg(ex.toString());
        Logger.getLogger(WebSpider.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        httpget.releaseConnection();
    }
}

From source file:org.everit.osgi.authentication.cas.tests.SampleApp.java

public void sessionLogout(final SecureHttpClient secureHttpClient) throws Exception {

    CloseableHttpClient httpClient = secureHttpClient.getHttpClient();
    HttpClientContext httpClientContext = secureHttpClient.getHttpClientContext();

    HttpGet httpGet = new HttpGet(sessionLogoutUrl);
    HttpResponse httpResponse = httpClient.execute(httpGet, httpClientContext);
    Assert.assertEquals("URL should not be accessed [" + sessionLogoutUrl + "]",
            HttpServletResponse.SC_NOT_FOUND, httpResponse.getStatusLine().getStatusCode());

    HttpUriRequest currentReq = (HttpUriRequest) httpClientContext.getRequest();
    HttpHost currentHost = httpClientContext.getTargetHost();
    String currentUrl = (currentReq.getURI().isAbsolute()) ? currentReq.getURI().toString()
            : (currentHost.toURI() + currentReq.getURI());
    Assert.assertEquals(loggedOutUrl, currentUrl);
    EntityUtils.consume(httpResponse.getEntity());
}