Search in sources :

Example 1 with HttpRequestInterceptor

use of org.apache.http.HttpRequestInterceptor in project webmagic by code4craft.

the class HttpClientGenerator method generateClient.

private CloseableHttpClient generateClient(Site site, Proxy proxy) {
    CredentialsProvider credsProvider = null;
    HttpClientBuilder httpClientBuilder = HttpClients.custom();
    if (proxy != null && StringUtils.isNotBlank(proxy.getUser()) && StringUtils.isNotBlank(proxy.getPassword())) {
        credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(proxy.getHttpHost().getAddress().getHostAddress(), proxy.getHttpHost().getPort()), new UsernamePasswordCredentials(proxy.getUser(), proxy.getPassword()));
        httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
    }
    if (site != null && site.getHttpProxy() != null && site.getUsernamePasswordCredentials() != null) {
        credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(//可以访问的范围
        new AuthScope(site.getHttpProxy()), //用户名和密码
        site.getUsernamePasswordCredentials());
        httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
    }
    httpClientBuilder.setConnectionManager(connectionManager);
    if (site != null && site.getUserAgent() != null) {
        httpClientBuilder.setUserAgent(site.getUserAgent());
    } else {
        httpClientBuilder.setUserAgent("");
    }
    if (site == null || site.isUseGzip()) {
        httpClientBuilder.addInterceptorFirst(new HttpRequestInterceptor() {

            public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
                if (!request.containsHeader("Accept-Encoding")) {
                    request.addHeader("Accept-Encoding", "gzip");
                }
            }
        });
    }
    //解决post/redirect/post 302跳转问题
    httpClientBuilder.setRedirectStrategy(new CustomRedirectStrategy());
    SocketConfig.Builder socketConfigBuilder = SocketConfig.custom();
    socketConfigBuilder.setSoKeepAlive(true).setTcpNoDelay(true);
    if (site != null) {
        socketConfigBuilder.setSoTimeout(site.getTimeOut());
    }
    SocketConfig socketConfig = socketConfigBuilder.build();
    httpClientBuilder.setDefaultSocketConfig(socketConfig);
    connectionManager.setDefaultSocketConfig(socketConfig);
    if (site != null) {
        httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(site.getRetryTimes(), true));
        generateCookie(httpClientBuilder, site);
    }
    return httpClientBuilder.build();
}
Also used : HttpRequest(org.apache.http.HttpRequest) SocketConfig(org.apache.http.config.SocketConfig) HttpContext(org.apache.http.protocol.HttpContext) CredentialsProvider(org.apache.http.client.CredentialsProvider) IOException(java.io.IOException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) AuthScope(org.apache.http.auth.AuthScope) HttpException(org.apache.http.HttpException)

Example 2 with HttpRequestInterceptor

use of org.apache.http.HttpRequestInterceptor in project robovm by robovm.

the class BasicHttpProcessor method process.

// non-Javadoc, see interface HttpRequestInterceptor (via HttpProcessor)
public void process(final HttpRequest request, final HttpContext context) throws IOException, HttpException {
    if (this.requestInterceptors != null) {
        for (int i = 0; i < this.requestInterceptors.size(); i++) {
            HttpRequestInterceptor interceptor = (HttpRequestInterceptor) this.requestInterceptors.get(i);
            interceptor.process(request, context);
        }
    }
}
Also used : HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor)

Example 3 with HttpRequestInterceptor

use of org.apache.http.HttpRequestInterceptor in project opennms by OpenNMS.

the class HttpClientWrapper method getClient.

public CloseableHttpClient getClient() {
    if (m_httpClient == null) {
        final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
        if (!m_reuseConnections) {
            httpClientBuilder.setConnectionReuseStrategy(new NoConnectionReuseStrategy());
        }
        if (m_usePreemptiveAuth) {
            enablePreemptiveAuth(httpClientBuilder);
        }
        if (m_useSystemProxySettings) {
            httpClientBuilder.setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault()));
        }
        if (!isEmpty(m_cookieSpec)) {
            requestConfigBuilder.setCookieSpec(m_cookieSpec);
        }
        if (m_cookieStore != null) {
            httpClientBuilder.setDefaultCookieStore(m_cookieStore);
        }
        if (m_username != null) {
            setCredentials(httpClientBuilder, m_username, m_password);
        }
        if (m_socketTimeout != null) {
            requestConfigBuilder.setSocketTimeout(m_socketTimeout);
        }
        if (m_connectionTimeout != null) {
            requestConfigBuilder.setConnectTimeout(m_connectionTimeout);
        }
        if (m_retries != null) {
            httpClientBuilder.setRetryHandler(new HttpRequestRetryOnExceptionHandler(m_retries, false));
        }
        if (m_sslContext.size() != 0) {
            configureSSLContext(httpClientBuilder);
        }
        for (final HttpRequestInterceptor interceptor : m_requestInterceptors) {
            httpClientBuilder.addInterceptorLast(interceptor);
        }
        for (final HttpResponseInterceptor interceptor : m_responseInterceptors) {
            httpClientBuilder.addInterceptorLast(interceptor);
        }
        if (m_useLaxRedirect) {
            httpClientBuilder.setRedirectStrategy(new LaxRedirectStrategy());
        }
        httpClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
        m_httpClient = httpClientBuilder.build();
    }
    return m_httpClient;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) NoConnectionReuseStrategy(org.apache.http.impl.NoConnectionReuseStrategy) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) HttpResponseInterceptor(org.apache.http.HttpResponseInterceptor) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) LaxRedirectStrategy(org.apache.http.impl.client.LaxRedirectStrategy) SystemDefaultRoutePlanner(org.apache.http.impl.conn.SystemDefaultRoutePlanner)

Example 4 with HttpRequestInterceptor

use of org.apache.http.HttpRequestInterceptor in project opennms by OpenNMS.

the class HttpClientWrapper method duplicate.

/**
 * Create a duplicate HttpClientWrapper from this wrapper.
 * All settings are preserved, and the session/cookie store is
 * shared between duplicate wrappers and their parent.
 */
public HttpClientWrapper duplicate() {
    final HttpClientWrapper ret = HttpClientWrapper.create();
    ret.m_cookieStore = m_cookieStore;
    ret.m_reuseConnections = m_reuseConnections;
    ret.m_usePreemptiveAuth = m_usePreemptiveAuth;
    ret.m_useSystemProxySettings = m_useSystemProxySettings;
    ret.m_cookieSpec = m_cookieSpec;
    ret.m_username = m_username;
    ret.m_password = m_password;
    ret.m_socketTimeout = m_socketTimeout;
    ret.m_connectionTimeout = m_connectionTimeout;
    ret.m_retries = m_retries;
    for (final Map.Entry<String, SSLContext> entry : ret.m_sslContext.entrySet()) {
        ret.m_sslContext.put(entry.getKey(), entry.getValue());
    }
    for (final HttpRequestInterceptor interceptor : m_requestInterceptors) {
        ret.m_requestInterceptors.add(interceptor);
    }
    for (final HttpResponseInterceptor interceptor : m_responseInterceptors) {
        ret.m_responseInterceptors.add(interceptor);
    }
    ret.m_userAgent = m_userAgent;
    ret.m_virtualHost = m_virtualHost;
    ret.m_version = m_version;
    return ret;
}
Also used : HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) HttpResponseInterceptor(org.apache.http.HttpResponseInterceptor) SSLContext(javax.net.ssl.SSLContext) Map(java.util.Map) HashMap(java.util.HashMap)

Example 5 with HttpRequestInterceptor

use of org.apache.http.HttpRequestInterceptor in project opennms by OpenNMS.

the class HttpClientWrapper method enablePreemptiveAuth.

protected void enablePreemptiveAuth(final HttpClientBuilder builder) {
    /**
     * Add an HttpRequestInterceptor that will perform preemptive authentication
     * @see http://hc.apache.org/httpcomponents-client-4.0.1/tutorial/html/authentication.html
     */
    final HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() {

        @Override
        public void process(final HttpRequest request, final HttpContext context) throws IOException {
            if (context instanceof HttpClientContext) {
                final HttpClientContext clientContext = (HttpClientContext) context;
                final AuthState authState = clientContext.getTargetAuthState();
                final CredentialsProvider credsProvider = clientContext.getCredentialsProvider();
                final HttpHost targetHost = clientContext.getTargetHost();
                // If not authentication scheme has been initialized yet
                if (authState.getAuthScheme() == null) {
                    final AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
                    // Obtain credentials matching the target host
                    final Credentials creds = credsProvider.getCredentials(authScope);
                    // If found, generate BasicScheme preemptively
                    if (creds != null) {
                        authState.update(new BasicScheme(), creds);
                    }
                }
            } else {
                throw new IllegalArgumentException("Not sure how to handle a non-HttpClientContext context.");
            }
        }
    };
    builder.addInterceptorFirst(preemptiveAuth);
}
Also used : HttpRequest(org.apache.http.HttpRequest) BasicScheme(org.apache.http.impl.auth.BasicScheme) AuthState(org.apache.http.auth.AuthState) HttpHost(org.apache.http.HttpHost) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) HttpContext(org.apache.http.protocol.HttpContext) AuthScope(org.apache.http.auth.AuthScope) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Aggregations

HttpRequestInterceptor (org.apache.http.HttpRequestInterceptor)25 HttpRequest (org.apache.http.HttpRequest)11 HttpContext (org.apache.http.protocol.HttpContext)11 IOException (java.io.IOException)10 HttpResponseInterceptor (org.apache.http.HttpResponseInterceptor)8 ArrayList (java.util.ArrayList)7 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)6 Header (org.apache.http.Header)5 HttpResponse (org.apache.http.HttpResponse)5 CredentialsProvider (org.apache.http.client.CredentialsProvider)5 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)5 Test (org.junit.Test)5 Map (java.util.Map)4 AuthScope (org.apache.http.auth.AuthScope)4 HttpGet (org.apache.http.client.methods.HttpGet)4 ImmutableHttpProcessor (org.apache.http.protocol.ImmutableHttpProcessor)4 URI (java.net.URI)3 HttpException (org.apache.http.HttpException)3 HttpHost (org.apache.http.HttpHost)3 Credentials (org.apache.http.auth.Credentials)3