use of org.apache.http.protocol.HttpContext in project tutorials by eugenp.
the class HttpClientConnectionManagementLiveTest method whenCustomizingKeepAliveStrategy_thenNoExceptions.
// 5
@Test
public final // 5.1
void whenCustomizingKeepAliveStrategy_thenNoExceptions() throws ClientProtocolException, IOException {
final ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() {
@Override
public long getKeepAliveDuration(final HttpResponse myResponse, final HttpContext myContext) {
final HeaderElementIterator it = new BasicHeaderElementIterator(myResponse.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
final HeaderElement he = it.nextElement();
final String param = he.getName();
final String value = he.getValue();
if ((value != null) && param.equalsIgnoreCase("timeout")) {
return Long.parseLong(value) * 1000;
}
}
final HttpHost target = (HttpHost) myContext.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
if ("localhost".equalsIgnoreCase(target.getHostName())) {
return 10 * 1000;
} else {
return 5 * 1000;
}
}
};
client = HttpClients.custom().setKeepAliveStrategy(myStrategy).setConnectionManager(poolingConnManager).build();
client.execute(get1);
client.execute(get2);
}
use of org.apache.http.protocol.HttpContext in project tutorials by eugenp.
the class HttpClientCookieLiveTest method whenSettingCookiesOnTheRequest_thenCookieSentCorrectly.
@Test
public final void whenSettingCookiesOnTheRequest_thenCookieSentCorrectly() throws IOException {
final BasicCookieStore cookieStore = new BasicCookieStore();
final BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
cookie.setDomain(".github.com");
cookie.setPath("/");
cookieStore.addCookie(cookie);
instance = HttpClientBuilder.create().build();
final HttpGet request = new HttpGet("http://www.github.com");
final HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
// localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); // before 4.3
response = instance.execute(request, localContext);
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}
use of org.apache.http.protocol.HttpContext in project connect-sdk-java by Ingenico-ePayments.
the class DefaultConnection method createHttpClient.
private CloseableHttpClient createHttpClient(ProxyConfiguration proxyConfiguration) {
HttpClientBuilder builder = HttpClients.custom().setConnectionManager(connectionManager);
HttpRoutePlanner routePlanner;
CredentialsProvider credentialsProvider;
if (proxyConfiguration != null) {
HttpHost proxy = new HttpHost(proxyConfiguration.getHost(), proxyConfiguration.getPort(), proxyConfiguration.getScheme());
routePlanner = new DefaultProxyRoutePlanner(proxy, DefaultSchemePortResolver.INSTANCE);
credentialsProvider = new BasicCredentialsProvider();
if (proxyConfiguration.getUsername() != null) {
AuthScope authscope = new AuthScope(proxyConfiguration.getHost(), proxyConfiguration.getPort());
final Credentials credentials = new UsernamePasswordCredentials(proxyConfiguration.getUsername(), proxyConfiguration.getPassword());
credentialsProvider.setCredentials(authscope, credentials);
// enable preemptive authentication
HttpRequestInterceptor proxyAuthenticationInterceptor = new HttpRequestInterceptor() {
@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
Header header = request.getFirstHeader(AUTH.PROXY_AUTH_RESP);
if (header == null) {
header = new BasicScheme((Charset) null).authenticate(credentials, request, context);
if (!AUTH.PROXY_AUTH_RESP.equals(header.getName())) {
header = new BasicHeader(AUTH.PROXY_AUTH_RESP, header.getValue());
}
request.setHeader(header);
}
}
};
builder = builder.addInterceptorLast(proxyAuthenticationInterceptor);
}
} else {
// add support for system properties
routePlanner = new SystemDefaultRoutePlanner(DefaultSchemePortResolver.INSTANCE, ProxySelector.getDefault());
credentialsProvider = new SystemDefaultCredentialsProvider();
}
// add logging - last for requests, first for responses
LoggingInterceptor loggingInterceptor = new LoggingInterceptor();
builder = builder.addInterceptorLast((HttpRequestInterceptor) loggingInterceptor);
builder = builder.addInterceptorFirst((HttpResponseInterceptor) loggingInterceptor);
return builder.setRoutePlanner(routePlanner).setDefaultCredentialsProvider(credentialsProvider).build();
}
use of org.apache.http.protocol.HttpContext in project ecf by eclipse.
the class SNIAwareHttpClient method createClientConnectionManager.
@Override
protected ClientConnectionManager createClientConnectionManager() {
SSLSocketFactory factory = new SSLSocketFactory(SSLContexts.createSystemDefault(), SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER) {
@Override
public Socket connectSocket(int connectTimeout, Socket socket, HttpHost host, InetSocketAddress remoteAddress, InetSocketAddress localAddress, HttpContext context) throws IOException, ConnectTimeoutException {
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=478655
if (socket instanceof SSLSocket) {
try {
final Method mSetHost = socket.getClass().getMethod("setHost", String.class);
mSetHost.setAccessible(true);
mSetHost.invoke(socket, host.getHostName());
} catch (NoSuchMethodException ex) {
} catch (IllegalAccessException ex) {
} catch (InvocationTargetException ex) {
} catch (RuntimeException ex) {
}
}
return super.connectSocket(connectTimeout, socket, host, remoteAddress, localAddress, context);
}
};
final SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
registry.register(new Scheme("https", 443, factory));
return new BasicClientConnectionManager(registry);
}
use of org.apache.http.protocol.HttpContext in project coastal-hazards by USGS-CIDA.
the class HttpComponentsWFSClient method getFeatureCollection.
@Override
public SimpleFeatureCollection getFeatureCollection(String typeName, Filter filter) throws IOException {
SimpleFeatureCollection collection = null;
String filterXml = null;
InputStream is = null;
OutputStream os = null;
DefaultHttpClient httpClient = new DefaultHttpClient();
if (filter != null) {
try {
FilterTransformer transformer = new FilterTransformer();
transformer.setOmitXMLDeclaration(true);
transformer.setNamespaceDeclarationEnabled(false);
filterXml = "<ogc:Filter>" + transformer.transform(filter) + "</ogc:Filter>";
} catch (TransformerException ex) {
throw new RuntimeException("Specified filter cannot be transformed", ex);
}
} else {
filterXml = "";
}
try {
HttpPost post = new HttpPost(wfsEndpoint);
StringEntity stringEntity = new StringEntity(fillInTemplate(typeName, filterXml), ContentType.APPLICATION_XML);
post.setEntity(stringEntity);
HttpContext localContext = new BasicHttpContext();
httpClient.setReuseStrategy(new NoConnectionReuseStrategy());
HttpResponse methodResponse = httpClient.execute(post, localContext);
if (methodResponse.getStatusLine().getStatusCode() != 200) {
throw new IOException(methodResponse.getStatusLine().getReasonPhrase());
}
is = methodResponse.getEntity().getContent();
os = new FileOutputStream(tmpWfsFile);
IOUtils.copy(is, os);
} finally {
if (httpClient.getConnectionManager() != null) {
httpClient.getConnectionManager().closeExpiredConnections();
}
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
}
collection = new GMLStreamingFeatureCollection(tmpWfsFile);
return collection;
}
Aggregations