Search in sources :

Example 86 with HttpClientContext

use of org.apache.http.client.protocol.HttpClientContext in project swift by luastar.

the class HttpClientUtils method getRedirectUrl.

public static String getRedirectUrl(String url, Map<String, String> headMap) {
    CloseableHttpClient httpclient = null;
    CloseableHttpResponse response = null;
    try {
        HttpGet httpGet = new HttpGet(url);
        // 超时设置
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(DEFAULT_TIMEOUT).setConnectTimeout(DEFAULT_TIMEOUT).setConnectionRequestTimeout(DEFAULT_TIMEOUT).build();
        httpGet.setConfig(requestConfig);
        // head设置
        if (headMap != null && !headMap.isEmpty()) {
            for (Map.Entry<String, String> entry : headMap.entrySet()) {
                httpGet.setHeader(entry.getKey(), entry.getValue());
            }
        }
        // 自定义重定向,不自动处理
        CustomRedirectStrategy redirectStrategy = new CustomRedirectStrategy();
        httpclient = createHttpClient(url, redirectStrategy);
        HttpClientContext context = HttpClientContext.create();
        long start = System.currentTimeMillis();
        response = httpclient.execute(httpGet, context);
        long end = System.currentTimeMillis();
        int status = response.getStatusLine().getStatusCode();
        logger.info("请求url:{},结果状态:{},耗时:{}毫秒。", url, status, ((end - start)));
        HttpEntity entity = response.getEntity();
        logger.info(EntityUtils.toString(entity));
        // 判断是否重定向
        boolean isRedirected = redirectStrategy.isRedirectedDefault(httpGet, response, context);
        if (!isRedirected) {
            return null;
        }
        return redirectStrategy.getRedirectLocation(httpGet, response, context);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        org.apache.http.client.utils.HttpClientUtils.closeQuietly(response);
        org.apache.http.client.utils.HttpClientUtils.closeQuietly(httpclient);
    }
    return null;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) CertificateException(java.security.cert.CertificateException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 87 with HttpClientContext

use of org.apache.http.client.protocol.HttpClientContext in project camel by apache.

the class HttpPollingConsumer method doReceive.

protected Exchange doReceive(int timeout) {
    Exchange exchange = endpoint.createExchange();
    HttpRequestBase method = createMethod(exchange);
    HttpClientContext httpClientContext = new HttpClientContext();
    // set optional timeout in millis
    if (timeout > 0) {
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).build();
        httpClientContext.setRequestConfig(requestConfig);
    }
    HttpEntity responeEntity = null;
    try {
        // execute request
        HttpResponse response = httpClient.execute(method, httpClientContext);
        int responseCode = response.getStatusLine().getStatusCode();
        responeEntity = response.getEntity();
        Object body = HttpHelper.readResponseBodyFromInputStream(responeEntity.getContent(), exchange);
        // lets store the result in the output message.
        Message message = exchange.getOut();
        message.setBody(body);
        // lets set the headers
        Header[] headers = response.getAllHeaders();
        HeaderFilterStrategy strategy = endpoint.getHeaderFilterStrategy();
        for (Header header : headers) {
            String name = header.getName();
            // mapping the content-type
            if (name.toLowerCase().equals("content-type")) {
                name = Exchange.CONTENT_TYPE;
            }
            String value = header.getValue();
            if (strategy != null && !strategy.applyFilterToExternalHeaders(name, value, exchange)) {
                message.setHeader(name, value);
            }
        }
        message.setHeader(Exchange.HTTP_RESPONSE_CODE, responseCode);
        if (response.getStatusLine() != null) {
            message.setHeader(Exchange.HTTP_RESPONSE_TEXT, response.getStatusLine().getReasonPhrase());
        }
        return exchange;
    } catch (IOException e) {
        throw new RuntimeCamelException(e);
    } finally {
        if (responeEntity != null) {
            try {
                EntityUtils.consume(responeEntity);
            } catch (IOException e) {
            // nothing what we can do
            }
        }
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpEntity(org.apache.http.HttpEntity) Message(org.apache.camel.Message) HttpResponse(org.apache.http.HttpResponse) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) HeaderFilterStrategy(org.apache.camel.spi.HeaderFilterStrategy) IOException(java.io.IOException) Exchange(org.apache.camel.Exchange) Header(org.apache.http.Header) RuntimeCamelException(org.apache.camel.RuntimeCamelException)

Example 88 with HttpClientContext

use of org.apache.http.client.protocol.HttpClientContext in project estatio by estatio.

the class UrlDownloaderUsingNtlmCredentials method download.

@Override
public byte[] download(final URL url) throws IOException {
    HttpHost target = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
    // Make sure the same context is used to execute logically related requests
    // (not thread-safe, so need a new one each time)
    HttpClientContext context = HttpClientContext.create();
    context.setCredentialsProvider(credsProvider);
    HttpGet httpGet = new HttpGet(url.getFile());
    try (final CloseableHttpResponse httpResponse = httpclient.execute(target, httpGet, context)) {
        final StatusLine statusLine = httpResponse.getStatusLine();
        if (statusLine == null) {
            throw new ApplicationException(String.format("Could not obtain response statusLine for %s", url.toExternalForm()));
        }
        final int statusCode = statusLine.getStatusCode();
        if (statusCode != 200) {
            // try to read content of entity, but ignore any exceptions
            // because we are simply trying to get extra data to report in the exception below
            InputStreamReader inputStreamReader = null;
            String entityContent = "";
            try {
                inputStreamReader = new InputStreamReader(httpResponse.getEntity().getContent());
                entityContent = CharStreams.toString(inputStreamReader);
            } catch (java.lang.Throwable ex) {
            // ignore
            } finally {
                if (inputStreamReader != null) {
                    try {
                        inputStreamReader.close();
                    } catch (Exception ex) {
                    // ignore
                    }
                }
            }
            throw new ApplicationException(String.format("Failed to download from '%s': %d %s\n%s", url.toExternalForm(), statusCode, statusLine.getReasonPhrase(), entityContent));
        }
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        httpResponse.getEntity().writeTo(baos);
        return baos.toByteArray();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) HttpGet(org.apache.http.client.methods.HttpGet) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ApplicationException(org.apache.isis.applib.ApplicationException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) StatusLine(org.apache.http.StatusLine) ApplicationException(org.apache.isis.applib.ApplicationException) HttpHost(org.apache.http.HttpHost) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 89 with HttpClientContext

use of org.apache.http.client.protocol.HttpClientContext in project briefcase by opendatakit.

the class WebUtils method createHttpContext.

public static HttpClientContext createHttpContext() {
    // set up one context for all HTTP requests so that authentication
    // and cookies can be retained.
    HttpClientContext localContext = HttpClientContext.create();
    // establish a local cookie store for this attempt at downloading...
    CookieStore cookieStore = new BasicCookieStore();
    localContext.setCookieStore(cookieStore);
    // and establish a credentials provider...
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    localContext.setCredentialsProvider(credsProvider);
    return localContext;
}
Also used : CookieStore(org.apache.http.client.CookieStore) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider)

Example 90 with HttpClientContext

use of org.apache.http.client.protocol.HttpClientContext in project briefcase by opendatakit.

the class AggregateUtils method commonDownloadFile.

/**
 * Common routine to download a document from the downloadUrl and save the
 * contents in the file 'f'. Shared by media file download and form file
 * download.
 *
 * @param f
 * @param downloadUrl
 * @throws URISyntaxException
 * @throws IOException
 * @throws ClientProtocolException
 * @throws TransmissionException
 */
public static final void commonDownloadFile(ServerConnectionInfo serverInfo, File f, String downloadUrl) throws URISyntaxException, IOException, TransmissionException {
    log.info("Downloading URL {} into {}", downloadUrl, f);
    // OK. We need to download it because we either:
    // (1) don't have it
    // (2) don't know if it is changed because the hash is not md5
    // (3) know it is changed
    URI u = null;
    try {
        log.info("Parsing URL {}", downloadUrl);
        URL uurl = new URL(downloadUrl);
        u = uurl.toURI();
    } catch (MalformedURLException | URISyntaxException e) {
        log.warn("bad download url", e);
        throw e;
    }
    HttpClient httpclient = WebUtils.createHttpClient();
    // get shared HttpContext so that authentication and cookies are retained.
    HttpClientContext localContext = WebUtils.getHttpContext();
    // set up request...
    HttpGet req = WebUtils.createOpenRosaHttpGet(u);
    WebUtils.setCredentials(localContext, serverInfo, u);
    HttpResponse response = null;
    // try
    {
        response = httpclient.execute(req, localContext);
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode == 401) {
            // We reset the Http context to force next request to authenticate itself
            WebUtils.resetHttpContext();
            throw new TransmissionException("Authentication failure");
        } else if (statusCode != 200) {
            String errMsg = "Fetch failed. Detailed reason: " + response.getStatusLine().getReasonPhrase() + " (" + statusCode + ")";
            log.error(errMsg);
            flushEntityBytes(response.getEntity());
            throw new TransmissionException(errMsg);
        }
        try (InputStream is = response.getEntity().getContent();
            OutputStream os = new FileOutputStream(f)) {
            byte[] buf = new byte[1024];
            int len;
            while ((len = is.read(buf)) > 0) {
                os.write(buf, 0, len);
            }
            os.flush();
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) HttpResponse(org.apache.http.HttpResponse) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL) TransmissionException(org.opendatakit.briefcase.model.TransmissionException) HttpClient(org.apache.http.client.HttpClient) FileOutputStream(java.io.FileOutputStream)

Aggregations

HttpClientContext (org.apache.http.client.protocol.HttpClientContext)160 HttpGet (org.apache.http.client.methods.HttpGet)56 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)54 IOException (java.io.IOException)48 HttpHost (org.apache.http.HttpHost)47 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)45 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)45 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)39 CredentialsProvider (org.apache.http.client.CredentialsProvider)39 URI (java.net.URI)32 HttpResponse (org.apache.http.HttpResponse)32 BasicScheme (org.apache.http.impl.auth.BasicScheme)32 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)32 AuthScope (org.apache.http.auth.AuthScope)31 AuthCache (org.apache.http.client.AuthCache)29 Test (org.junit.Test)29 HttpEntity (org.apache.http.HttpEntity)22 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)21 HttpClient (org.apache.http.client.HttpClient)18 RequestConfig (org.apache.http.client.config.RequestConfig)17