private CloseableHttpClient getHttpClient() { int timeout = 10000; RequestConfig config = RequestConfig.custom() .setConnectTimeout(timeout) .setConnectionRequestTimeout(timeout) .setSocketTimeout(timeout) .build(); return HttpClientBuilder .create() .useSystemProperties() .addInterceptorFirst(new OutboundRequestIdSettingInterceptor()) .addInterceptorFirst((HttpRequestInterceptor) new OutboundRequestLoggingInterceptor()) .addInterceptorLast((HttpResponseInterceptor) new OutboundRequestLoggingInterceptor()) .setDefaultRequestConfig(config) .build(); }
public ProxyClient(final HttpParams params) { super(); if (params == null) { throw new IllegalArgumentException("HTTP parameters may not be null"); } this.httpProcessor = new ImmutableHttpProcessor(new HttpRequestInterceptor[] { new RequestContent(), new RequestTargetHost(), new RequestClientConnControl(), new RequestUserAgent(), new RequestProxyAuthentication() } ); this.requestExec = new HttpRequestExecutor(); this.proxyAuthStrategy = new ProxyAuthenticationStrategy(); this.authenticator = new HttpAuthenticator(); this.proxyAuthState = new AuthState(); this.authSchemeRegistry = new AuthSchemeRegistry(); this.authSchemeRegistry.register(AuthPolicy.BASIC, new BasicSchemeFactory()); this.authSchemeRegistry.register(AuthPolicy.DIGEST, new DigestSchemeFactory()); this.authSchemeRegistry.register(AuthPolicy.NTLM, new NTLMSchemeFactory()); this.authSchemeRegistry.register(AuthPolicy.SPNEGO, new SPNegoSchemeFactory()); this.authSchemeRegistry.register(AuthPolicy.KERBEROS, new KerberosSchemeFactory()); this.reuseStrategy = new DefaultConnectionReuseStrategy(); this.params = params; }
private static HttpClientBuilder _getApacheKissHttpClientBuilder() { return HttpClients.custom() .addInterceptorFirst(new RequestDefaultHeaders()) .addInterceptorFirst(new RequestContent()) .addInterceptorFirst(new RequestTargetHost()) .addInterceptorFirst(new RequestClientConnControl()) .addInterceptorFirst(new RequestAddCookies()) .addInterceptorFirst(new ResponseProcessCookies()) .addInterceptorFirst(new RequestAuthCache()) .addInterceptorLast(new HttpRequestInterceptor() { @Override public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { if (request.containsHeader("User-Agent")) { request.removeHeaders("User-Agent"); } if (request.containsHeader("Custom-User-Agent")) { request.addHeader("User-Agent", request.getFirstHeader("Custom-User-Agent").getValue()); request.removeHeaders("Custom-User-Agent"); } } }); }
@Test public void testPreemptiveTargetAndProxyAuth() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/"); final HttpClientContext context = HttpClientContext.create(); context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credProvider); context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target); context.setAttribute(HttpClientContext.HTTP_ROUTE, new HttpRoute(this.target, null, this.proxy, false)); context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, this.targetState); context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, this.proxyState); final AuthCache authCache = new BasicAuthCache(); authCache.put(this.target, this.authscheme1); authCache.put(this.proxy, this.authscheme2); context.setAttribute(HttpClientContext.AUTH_CACHE, authCache); final HttpRequestInterceptor interceptor = new RequestAuthCache(); interceptor.process(request, context); Assert.assertNotNull(this.targetState.getAuthScheme()); Assert.assertSame(this.creds1, this.targetState.getCredentials()); Assert.assertNotNull(this.proxyState.getAuthScheme()); Assert.assertSame(this.creds2, this.proxyState.getCredentials()); }
@Test public void testCredentialsProviderNotSet() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/"); final HttpClientContext context = HttpClientContext.create(); context.setAttribute(HttpClientContext.CREDS_PROVIDER, null); context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target); context.setAttribute(HttpClientContext.HTTP_ROUTE, new HttpRoute(this.target, null, this.proxy, false)); context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, this.targetState); context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, this.proxyState); final AuthCache authCache = new BasicAuthCache(); authCache.put(this.target, this.authscheme1); authCache.put(this.proxy, this.authscheme2); context.setAttribute(HttpClientContext.AUTH_CACHE, authCache); final HttpRequestInterceptor interceptor = new RequestAuthCache(); interceptor.process(request, context); Assert.assertNull(this.targetState.getAuthScheme()); Assert.assertNull(this.targetState.getCredentials()); Assert.assertNull(this.proxyState.getAuthScheme()); Assert.assertNull(this.proxyState.getCredentials()); }
@Test public void testAuthCacheNotSet() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/"); final HttpClientContext context = HttpClientContext.create(); context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credProvider); context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target); context.setAttribute(HttpClientContext.HTTP_ROUTE, new HttpRoute(this.target, null, this.proxy, false)); context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, this.targetState); context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, this.proxyState); context.setAttribute(HttpClientContext.AUTH_CACHE, null); final HttpRequestInterceptor interceptor = new RequestAuthCache(); interceptor.process(request, context); Assert.assertNull(this.targetState.getAuthScheme()); Assert.assertNull(this.targetState.getCredentials()); Assert.assertNull(this.proxyState.getAuthScheme()); Assert.assertNull(this.proxyState.getCredentials()); }
@Test public void testAuthCacheEmpty() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/"); final HttpClientContext context = HttpClientContext.create(); context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credProvider); context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target); context.setAttribute(HttpClientContext.HTTP_ROUTE, new HttpRoute(this.target, null, this.proxy, false)); context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, this.targetState); context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, this.proxyState); final AuthCache authCache = new BasicAuthCache(); context.setAttribute(HttpClientContext.AUTH_CACHE, authCache); final HttpRequestInterceptor interceptor = new RequestAuthCache(); interceptor.process(request, context); Assert.assertNull(this.targetState.getAuthScheme()); Assert.assertNull(this.targetState.getCredentials()); Assert.assertNull(this.proxyState.getAuthScheme()); Assert.assertNull(this.proxyState.getCredentials()); }
@Test public void testNoMatchingCredentials() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/"); this.credProvider.clear(); final HttpClientContext context = HttpClientContext.create(); context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credProvider); context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target); context.setAttribute(HttpClientContext.HTTP_ROUTE, new HttpRoute(this.target, null, this.proxy, false)); context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, this.targetState); context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, this.proxyState); final AuthCache authCache = new BasicAuthCache(); authCache.put(this.target, this.authscheme1); authCache.put(this.proxy, this.authscheme2); context.setAttribute(HttpClientContext.AUTH_CACHE, authCache); final HttpRequestInterceptor interceptor = new RequestAuthCache(); interceptor.process(request, context); Assert.assertNull(this.targetState.getAuthScheme()); Assert.assertNull(this.targetState.getCredentials()); Assert.assertNull(this.proxyState.getAuthScheme()); Assert.assertNull(this.proxyState.getCredentials()); }
@Test public void testCookiesForConnectRequest() throws Exception { final HttpRequest request = new BasicHttpRequest("CONNECT", "www.somedomain.com"); final HttpRoute route = new HttpRoute(this.target, null, false); final HttpClientContext context = HttpClientContext.create(); context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target); context.setAttribute(HttpClientContext.HTTP_ROUTE, route); context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); final HttpRequestInterceptor interceptor = new RequestAddCookies(); interceptor.process(request, context); final Header[] headers1 = request.getHeaders(SM.COOKIE); Assert.assertNotNull(headers1); Assert.assertEquals(0, headers1.length); final Header[] headers2 = request.getHeaders(SM.COOKIE2); Assert.assertNotNull(headers2); Assert.assertEquals(0, headers2.length); }
@Test public void testNoCookieStore() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/"); final HttpRoute route = new HttpRoute(this.target, null, false); final HttpClientContext context = HttpClientContext.create(); context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target); context.setAttribute(HttpClientContext.HTTP_ROUTE, route); context.setAttribute(HttpClientContext.COOKIE_STORE, null); context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); final HttpRequestInterceptor interceptor = new RequestAddCookies(); interceptor.process(request, context); final Header[] headers1 = request.getHeaders(SM.COOKIE); Assert.assertNotNull(headers1); Assert.assertEquals(0, headers1.length); final Header[] headers2 = request.getHeaders(SM.COOKIE2); Assert.assertNotNull(headers2); Assert.assertEquals(0, headers2.length); }
@Test public void testNoCookieSpecRegistry() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/"); final HttpRoute route = new HttpRoute(this.target, null, false); final HttpClientContext context = HttpClientContext.create(); context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target); context.setAttribute(HttpClientContext.HTTP_ROUTE, route); context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, null); final HttpRequestInterceptor interceptor = new RequestAddCookies(); interceptor.process(request, context); final Header[] headers1 = request.getHeaders(SM.COOKIE); Assert.assertNotNull(headers1); Assert.assertEquals(0, headers1.length); final Header[] headers2 = request.getHeaders(SM.COOKIE2); Assert.assertNotNull(headers2); Assert.assertEquals(0, headers2.length); }
@Test public void testNoTargetHost() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/"); final HttpRoute route = new HttpRoute(this.target, null, false); final HttpClientContext context = HttpClientContext.create(); context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, null); context.setAttribute(HttpClientContext.HTTP_ROUTE, route); context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); final HttpRequestInterceptor interceptor = new RequestAddCookies(); interceptor.process(request, context); final Header[] headers1 = request.getHeaders(SM.COOKIE); Assert.assertNotNull(headers1); Assert.assertEquals(0, headers1.length); final Header[] headers2 = request.getHeaders(SM.COOKIE2); Assert.assertNotNull(headers2); Assert.assertEquals(0, headers2.length); }
@Test public void testNoHttpConnection() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/"); final HttpClientContext context = HttpClientContext.create(); context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target); context.setAttribute(HttpCoreContext.HTTP_CONNECTION, null); context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); final HttpRequestInterceptor interceptor = new RequestAddCookies(); interceptor.process(request, context); final Header[] headers1 = request.getHeaders(SM.COOKIE); Assert.assertNotNull(headers1); Assert.assertEquals(0, headers1.length); final Header[] headers2 = request.getHeaders(SM.COOKIE2); Assert.assertNotNull(headers2); Assert.assertEquals(0, headers2.length); }
@Test public void testAddCookiesUsingExplicitCookieSpec() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/"); final RequestConfig config = RequestConfig.custom() .setCookieSpec(CookieSpecs.NETSCAPE).build(); final HttpRoute route = new HttpRoute(this.target, null, false); final HttpClientContext context = HttpClientContext.create(); context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target); context.setAttribute(HttpClientContext.HTTP_ROUTE, route); context.setAttribute(HttpClientContext.REQUEST_CONFIG, config); context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); final HttpRequestInterceptor interceptor = new RequestAddCookies(); interceptor.process(request, context); final CookieSpec cookieSpec = context.getCookieSpec(); Assert.assertTrue(cookieSpec instanceof NetscapeDraftSpec); final Header[] headers1 = request.getHeaders(SM.COOKIE); Assert.assertNotNull(headers1); Assert.assertEquals(1, headers1.length); Assert.assertEquals("name1=value1; name2=value2", headers1[0].getValue()); }
@Test public void testAuthScopeRemotePortWhenDirect() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/stuff"); this.target = new HttpHost("localhost.local"); final HttpRoute route = new HttpRoute(new HttpHost("localhost.local", 1234), null, false); final HttpClientContext context = HttpClientContext.create(); context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target); context.setAttribute(HttpClientContext.HTTP_ROUTE, route); context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); final HttpRequestInterceptor interceptor = new RequestAddCookies(); interceptor.process(request, context); final CookieOrigin cookieOrigin = context.getCookieOrigin(); Assert.assertNotNull(cookieOrigin); Assert.assertEquals(this.target.getHostName(), cookieOrigin.getHost()); Assert.assertEquals(1234, cookieOrigin.getPort()); Assert.assertEquals("/stuff", cookieOrigin.getPath()); Assert.assertFalse(cookieOrigin.isSecure()); }
@Test public void testAuthDefaultHttpPortWhenProxy() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/stuff"); this.target = new HttpHost("localhost.local"); final HttpRoute route = new HttpRoute( new HttpHost("localhost.local", 80), null, new HttpHost("localhost", 8888), false); final HttpClientContext context = HttpClientContext.create(); context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target); context.setAttribute(HttpClientContext.HTTP_ROUTE, route); context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); final HttpRequestInterceptor interceptor = new RequestAddCookies(); interceptor.process(request, context); final CookieOrigin cookieOrigin = context.getCookieOrigin(); Assert.assertNotNull(cookieOrigin); Assert.assertEquals(this.target.getHostName(), cookieOrigin.getHost()); Assert.assertEquals(80, cookieOrigin.getPort()); Assert.assertEquals("/stuff", cookieOrigin.getPath()); Assert.assertFalse(cookieOrigin.isSecure()); }
@Test public void testAuthDefaultHttpsPortWhenProxy() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/stuff"); this.target = new HttpHost("localhost", -1, "https"); final HttpRoute route = new HttpRoute( new HttpHost("localhost", 443, "https"), null, new HttpHost("localhost", 8888), true, TunnelType.TUNNELLED, LayerType.LAYERED); final HttpClientContext context = HttpClientContext.create(); context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target); context.setAttribute(HttpClientContext.HTTP_ROUTE, route); context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); final HttpRequestInterceptor interceptor = new RequestAddCookies(); interceptor.process(request, context); final CookieOrigin cookieOrigin = context.getCookieOrigin(); Assert.assertNotNull(cookieOrigin); Assert.assertEquals(this.target.getHostName(), cookieOrigin.getHost()); Assert.assertEquals(443, cookieOrigin.getPort()); Assert.assertEquals("/stuff", cookieOrigin.getPath()); Assert.assertTrue(cookieOrigin.isSecure()); }
@Test public void testConnectionKeepAliveForDirectRequests() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/"); final HttpClientContext context = HttpClientContext.create(); final HttpHost target = new HttpHost("localhost", 80, "http"); final HttpRoute route = new HttpRoute(target, null, false); context.setAttribute(HttpClientContext.HTTP_ROUTE, route); final HttpRequestInterceptor interceptor = new RequestClientConnControl(); interceptor.process(request, context); final Header header1 = request.getFirstHeader(HTTP.CONN_DIRECTIVE); Assert.assertNotNull(header1); Assert.assertEquals(HTTP.CONN_KEEP_ALIVE, header1.getValue()); final Header header2 = request.getFirstHeader("Proxy-Connection"); Assert.assertNull(header2); }
@Test public void testConnectionKeepAliveForTunneledRequests() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/"); final HttpClientContext context = HttpClientContext.create(); final HttpHost target = new HttpHost("localhost", 443, "https"); final HttpHost proxy = new HttpHost("localhost", 8080); final HttpRoute route = new HttpRoute(target, null, proxy, true, TunnelType.TUNNELLED, LayerType.LAYERED); context.setAttribute(HttpClientContext.HTTP_ROUTE, route); final HttpRequestInterceptor interceptor = new RequestClientConnControl(); interceptor.process(request, context); final Header header1 = request.getFirstHeader(HTTP.CONN_DIRECTIVE); Assert.assertNotNull(header1); Assert.assertEquals(HTTP.CONN_KEEP_ALIVE, header1.getValue()); final Header header2 = request.getFirstHeader("Proxy-Connection"); Assert.assertNull(header2); }
@Test public void testProxyConnectionKeepAliveForRequestsOverProxy() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/"); final HttpClientContext context = HttpClientContext.create(); final HttpHost target = new HttpHost("localhost", 80, "http"); final HttpHost proxy = new HttpHost("localhost", 8080); final HttpRoute route = new HttpRoute(target, null, proxy, false, TunnelType.PLAIN, LayerType.PLAIN); context.setAttribute(HttpClientContext.HTTP_ROUTE, route); final HttpRequestInterceptor interceptor = new RequestClientConnControl(); interceptor.process(request, context); final Header header1 = request.getFirstHeader("Proxy-Connection"); Assert.assertNotNull(header1); Assert.assertEquals(HTTP.CONN_KEEP_ALIVE, header1.getValue()); final Header header2 = request.getFirstHeader(HTTP.CONN_DIRECTIVE); Assert.assertNull(header2); }
@Test public void testPreserveCustomConnectionHeader() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/"); request.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE); final HttpClientContext context = HttpClientContext.create(); final HttpHost target = new HttpHost("localhost", 443, "https"); final HttpHost proxy = new HttpHost("localhost", 8080); final HttpRoute route = new HttpRoute(target, null, proxy, true, TunnelType.TUNNELLED, LayerType.LAYERED); context.setAttribute(HttpClientContext.HTTP_ROUTE, route); final HttpRequestInterceptor interceptor = new RequestClientConnControl(); interceptor.process(request, context); final Header header1 = request.getFirstHeader(HTTP.CONN_DIRECTIVE); Assert.assertNotNull(header1); Assert.assertEquals(HTTP.CONN_CLOSE, header1.getValue()); final Header header2 = request.getFirstHeader("Proxy-Connection"); Assert.assertNull(header2); }
@Test public void testPreserveCustomProxyConnectionHeader() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/"); request.addHeader("Proxy-Connection", HTTP.CONN_CLOSE); final HttpClientContext context = HttpClientContext.create(); final HttpHost target = new HttpHost("localhost", 80, "http"); final HttpHost proxy = new HttpHost("localhost", 8080); final HttpRoute route = new HttpRoute(target, null, proxy, false, TunnelType.PLAIN, LayerType.PLAIN); context.setAttribute(HttpClientContext.HTTP_ROUTE, route); final HttpRequestInterceptor interceptor = new RequestClientConnControl(); interceptor.process(request, context); final Header header1 = request.getFirstHeader("Proxy-Connection"); Assert.assertNotNull(header1); Assert.assertEquals(HTTP.CONN_CLOSE, header1.getValue()); }
@Test public void testDefaultHeaders() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/"); request.addHeader("custom", "stuff"); final List<Header> defheaders = new ArrayList<Header>(); defheaders.add(new BasicHeader("custom", "more stuff")); final HttpContext context = new BasicHttpContext(); final HttpRequestInterceptor interceptor = new RequestDefaultHeaders(defheaders); interceptor.process(request, context); final Header[] headers = request.getHeaders("custom"); Assert.assertNotNull(headers); Assert.assertEquals(2, headers.length); Assert.assertEquals("stuff", headers[0].getValue()); Assert.assertEquals("more stuff", headers[1].getValue()); }
protected HttpClient createHttpClient() { DefaultHttpClient client = new DefaultHttpClient(createClientConnectionManager()); if (useCompression) { client.addRequestInterceptor( new HttpRequestInterceptor() { @Override public void process(HttpRequest request, HttpContext context) { // We expect to received a compression response that we un-gzip request.addHeader("Accept-Encoding", "gzip"); } }); } if (getProxyHost() != null) { HttpHost proxy = new HttpHost(getProxyHost(), getProxyPort()); client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); if(getProxyUser() != null && getProxyPassword() != null) { client.getCredentialsProvider().setCredentials( new AuthScope(getProxyHost(), getProxyPort()), new UsernamePasswordCredentials(getProxyUser(), getProxyPassword())); } } return client; }
@Override protected Client newClient() { ResteasyClientBuilder resteasyClientBuilder = new ResteasyClientBuilder(); ResteasyClient client = resteasyClientBuilder.establishConnectionTimeout(getTimeout(), TimeUnit.MILLISECONDS).socketTimeout(getTimeout(), TimeUnit.MILLISECONDS).build(); AbstractHttpClient httpClient = (AbstractHttpClient) ((ApacheHttpClient4Engine) client.httpEngine()).getHttpClient(); httpClient.setRedirectStrategy(new DefaultRedirectStrategy() { @Override protected boolean isRedirectable(String method) { return true; } }); httpClient.addRequestInterceptor(new HttpRequestInterceptor() { @Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { request.setParams(new AllowRedirectHttpParams(request.getParams())); } }); return client; }
private void initHttpClient() { _params = new SyncBasicHttpParams(); HttpProtocolParams.setVersion(_params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(_params, "UTF-8"); HttpProtocolParams.setUseExpectContinue(_params, false); HttpProtocolParams.setHttpElementCharset(_params, "UTF-8"); _httpproc = new ImmutableHttpProcessor(new HttpRequestInterceptor[] { // Required protocol interceptors new BasicHttpProcessor(), new RequestConnControl(), new RequestContent(), new RequestDate(), new RequestTargetHost(), // Recommended protocol interceptors new RequestUserAgent(), new RequestExpectContinue() }); _httpexecutor = new HttpRequestExecutor(); _httpcontext = new BasicHttpContext(null); _connection = new DefaultHttpClientConnection(); _connectionStrategy = new DefaultConnectionReuseStrategy(); }
/** * * This function adds support for pre-emptive HTTP Authentication for an HttpClient. * * @param httpClient */ public static void makeAuthenticationPreemptive(HttpClient httpClient) { HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() { public void process(final HttpRequest request,final HttpContext context) throws HttpException,IOException{ AuthState authState = (AuthState) context .getAttribute(ClientContext.TARGET_AUTH_STATE); CredentialsProvider credsProvider = (CredentialsProvider) context .getAttribute(ClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost) context .getAttribute(ExecutionContext.HTTP_TARGET_HOST); if (authState.getAuthScheme() == null) { AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); Credentials creds = credsProvider.getCredentials(authScope); if (creds != null) { authState.setAuthScheme(new BasicScheme()); authState.setCredentials(creds); } } } }; ((AbstractHttpClient) httpClient).addRequestInterceptor(preemptiveAuth,0); }
private HttpResponse httpClientRequest(HttpRequestBase request, byte[] userName, byte[] password) throws IOException, ClientProtocolException { HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, 5000); HttpConnectionParams.setSoTimeout(httpParams, 7000); DefaultHttpClient client; client = new DefaultHttpClient(httpParams); BasicHttpContext context = new BasicHttpContext(); if (userName != null && password != null) { client.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(new String(userName), new String(password))); BasicScheme basicAuth = new BasicScheme(); context.setAttribute("preemptive-auth", basicAuth); client.addRequestInterceptor((HttpRequestInterceptor) new PreemptiveAuth(), 0); } HttpResponse response = client.execute(request, context); return response; }
public void setAuthPreemtive(boolean authPreemtive) { /** * Add an HttpRequestInterceptor that will perform preemptive authentication * @see http://hc.apache.org/httpcomponents-client-4.0.1/tutorial/html/authentication.html */ HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() { public void process(final HttpRequest request, final HttpContext context) throws IOException { AuthState authState = (AuthState)context.getAttribute(ClientContext.TARGET_AUTH_STATE); CredentialsProvider credsProvider = (CredentialsProvider)context.getAttribute(ClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost)context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); // If not authentication scheme has been initialized yet if (authState.getAuthScheme() == null) { AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); // Obtain credentials matching the target host Credentials creds = credsProvider.getCredentials(authScope); // If found, generate BasicScheme preemptively if (creds != null) { authState.update(new BasicScheme(), creds); } } } }; m_httpClient.addRequestInterceptor(preemptiveAuth, 0); }
private DefaultHttpClient getHttpClient() { DefaultHttpClient client = new DefaultHttpClient(); // I believe DefaultHttpClient handles redirects by default anyway client.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, Boolean.TRUE); client.addRequestInterceptor(new HttpRequestInterceptor() { public void process( final HttpRequest request, final HttpContext context) throws HttpException, IOException { request.addHeader("Accept", "application/xml"); } }); SSLSocketFactory.getSocketFactory().setHostnameVerifier( SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER ); return client; }
@Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { String baseUrl = (String) context.getAttribute(BASE_URL); String layerId = (String) context.getAttribute(LAYER_ID); try { if (interceptors != null && baseUrl != null) { for (Entry<String, List<HttpRequestInterceptor>> entry : interceptors.getMap().entrySet()) { String key = entry.getKey(); if ("".equals(key) || (layerId != null && layerId.equals(key)) || baseUrl.startsWith(key)) { for (HttpRequestInterceptor inter : entry.getValue()) { inter.process(request, context); } } } } } catch (Exception e) { log.warn("Error processing interceptors: " + e.getMessage()); } }
public void setAuthPreemtive(boolean authPreemtive) { /** * Add an HttpRequestInterceptor that will perform preemptive authentication * @see http://hc.apache.org/httpcomponents-client-4.0.1/tutorial/html/authentication.html */ HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() { public void process(final HttpRequest request, final HttpContext context) throws IOException { AuthState authState = (AuthState)context.getAttribute(ClientContext.TARGET_AUTH_STATE); CredentialsProvider credsProvider = (CredentialsProvider)context.getAttribute(ClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost)context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); // If not authentication scheme has been initialized yet if (authState.getAuthScheme() == null) { AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); // Obtain credentials matching the target host Credentials creds = credsProvider.getCredentials(authScope); // If found, generate BasicScheme preemptively if (creds != null) { authState.setAuthScheme(new BasicScheme()); authState.setCredentials(creds); } } } }; m_httpClient.addRequestInterceptor(preemptiveAuth, 0); }
static private DefaultHttpClient setupHttpClient() { HttpParams connectionParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(connectionParams, 30000); // thirty seconds HttpConnectionParams.setSoTimeout(connectionParams, 30000); HttpClientParams.setRedirecting(connectionParams, false); DefaultHttpClient client = new DefaultHttpClient(connectionParams); client.addRequestInterceptor(new HttpRequestInterceptor() { public void process(HttpRequest request, HttpContext context) { if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) { request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP); } } }); return client; }
@Test public void strippingReferer() throws Exception { ProxyServer bmp = new ProxyServer(8071); bmp.start(); HttpRequestInterceptor stripper = new RefererStripper(); bmp.addRequestInterceptor(stripper); DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability(CapabilityType.PROXY, bmp.seleniumProxy()); WebDriver driver = new FirefoxDriver(caps); driver.get("http://localhost/test_referer.html"); driver.findElement(By.tagName("a")).click(); assertThat( driver.findElement(By.tagName("body")).getText(), containsString("No referer")); Thread.sleep(10000); driver.quit(); bmp.stop(); }
CloseableHttpClient signingClientForServiceName(String serviceName) { AWS4Signer signer = new AWS4Signer(); signer.setServiceName(serviceName); signer.setRegionName(AWS_REGION); HttpRequestInterceptor interceptor = new AWSRequestSigningApacheInterceptor(serviceName, signer, credentialsProvider); return HttpClients.custom() .addInterceptorLast(interceptor) .build(); }
DecompressingHttpClient(HttpClient backend, HttpRequestInterceptor requestInterceptor, HttpResponseInterceptor responseInterceptor) { this.backend = backend; this.acceptEncodingInterceptor = requestInterceptor; this.contentEncodingInterceptor = responseInterceptor; }
public void addRequestInterceptor( final HttpRequestInterceptor itcp, int index) { if (itcp == null) { return; } this.requestInterceptors.add(index, itcp); }
public void removeRequestInterceptorByClass(final Class<? extends HttpRequestInterceptor> clazz) { for (Iterator<HttpRequestInterceptor> it = this.requestInterceptors.iterator(); it.hasNext(); ) { Object request = it.next(); if (request.getClass().equals(clazz)) { it.remove(); } } }
public void process( final HttpRequest request, final HttpContext context) throws IOException, HttpException { for (int i = 0; i < this.requestInterceptors.size(); i++) { HttpRequestInterceptor interceptor = this.requestInterceptors.get(i); interceptor.process(request, context); } }
private HttpRequestInterceptor toRequestInterceptor(AuthorizationScheme authorizationScheme, String key, String secret) { switch (authorizationScheme) { case HMAC_MM_V2: return new HmacMmv2Interceptor(key, secret); default: throw new IllegalArgumentException(authorizationScheme + " is not supported"); } }