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();
}
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);
}
}
}
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;
}
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;
}
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);
}
Aggregations