Example usage for org.apache.http.client.utils URIUtils resolve

List of usage examples for org.apache.http.client.utils URIUtils resolve

Introduction

In this page you can find the example usage for org.apache.http.client.utils URIUtils resolve.

Prototype

public static URI resolve(final URI originalURI, final HttpHost target, final List<URI> redirects)
        throws URISyntaxException 

Source Link

Document

Derives the interpreted (absolute) URI that was used to generate the last request.

Usage

From source file:io.cloudslang.content.httpclient.consume.FinalLocationConsumer.java

public void consume(Map<String, String> returnResult) {
    URI location;//from w  w w  .  j  a v a 2s  .  c om
    try {
        location = URIUtils.resolve(uri, targetHost, redirectLocations);
    } catch (URISyntaxException e) {
        //this is not a fatal error
        throw new IllegalArgumentException(
                "could not determine '" + CSHttpClient.FINAL_LOCATION + "': " + e.getMessage(), e);
    }
    returnResult.put(CSHttpClient.FINAL_LOCATION, location.toASCIIString());
}

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

private URI getSystemId(HttpClientContext ctx) {
    HttpUriRequest request = (HttpUriRequest) ctx.getRequest();
    try {/*w w w .jav a 2  s.  c  om*/
        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:webfx.URLVerifier.java

private void discoverThroughHeaders() throws IOException, URISyntaxException {
    // relax redirections
    HttpGet httpGet = new HttpGet(location.toURI());
    HttpClientContext httpcontext = HttpClientContext.create();
    try (CloseableHttpResponse response = httpclient.execute(httpGet, httpcontext)) {
        // get mimetype via Content-Type http header
        Arrays.stream(response.getHeaders("Content-Type")).findFirst()
                .ifPresent(h -> this.contentType = h.getValue());
        if (!Objects.isNull(contentType)) {
            contentType = contentType.contains(";") ? contentType.substring(0, contentType.indexOf(";")).trim()
                    : contentType;//  w w w .j a v  a2 s.  co  m
            LOGGER.log(Level.INFO, "Final Content-Type: {0}", contentType);
        } else {
            LOGGER.log(Level.INFO, "Content-Type Header is Empty: {0}",
                    Arrays.toString(response.getHeaders("Content-Type")));
            // clear field b/c it was used inside lambda as temp var
            contentType = null;
        }

        // get filename via Content-Disposition http header
        Arrays.stream(response.getHeaders("Content-Disposition")).findFirst()
                .ifPresent(h -> this.pageName = h.getValue());
        if (!Objects.isNull(pageName) && pageName.contains("filename=")) {
            pageName = pageName.substring(pageName.lastIndexOf("filename=") + 9);
            LOGGER.log(Level.INFO, "temporary page name: {0}", pageName);
            if (pageName.indexOf('.') > -1) {
                fileExtension = pageName.substring(pageName.indexOf('.') + 1).trim();
                LOGGER.log(Level.INFO, "Final file extension: {0}", fileExtension);
            }
            pageName = pageName.substring(0, pageName.indexOf('.')).trim();
            LOGGER.log(Level.INFO, "Final page name: {0}", pageName);
        } else {
            // clear field b/c it was used inside lambda as temp var
            pageName = null;
        }

        HttpHost target = httpcontext.getTargetHost();
        List<URI> redirectLocations = httpcontext.getRedirectLocations();
        URI _loc = URIUtils.resolve(httpGet.getURI(), target, redirectLocations);
        this.location = _loc.toURL();
        LOGGER.log(Level.INFO, "Final HTTP location: {0}", _loc.toURL());
    }
}

From source file:sachin.spider.SpiderConfig.java

private String handleRedirect(String url) {
    try {//w  ww  . j  ava  2  s .com
        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:sachin.spider.WebSpider.java

private void handleRedirectedLink(WebURL curUrl) {
    String url = curUrl.getUrl();
    HttpGet httpget = new HttpGet(url);
    RequestConfig requestConfig = getRequestConfigWithRedirectEnabled();
    httpget.setConfig(requestConfig);//www  .  jav  a 2 s.  c  o m
    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.callimachusproject.client.HttpClientRedirectTest.java

/**
 * The interpreted (absolute) URI that was used to generate the last
 * request./*from   www  . j av a 2  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;
    }
}