RequestAuthCache implements HttpRequestInterceptor { public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { if (request == null) { throw new IllegalArgumentException("HTTP request may not be null"); } if (context == null) { throw new IllegalArgumentException("HTTP context may not be null"); } AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE); if (authCache == null) { this.log.debug("Auth cache not set in the context"); return; } CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute( ClientContext.CREDS_PROVIDER); if (credsProvider == null) { this.log.debug("Credentials provider not set in the context"); return; } HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); AuthState targetState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); if (target != null && targetState != null && targetState.getAuthScheme() == null) { AuthScheme authScheme = authCache.get(target); if (authScheme != null) { doPreemptiveAuth(target, authScheme, targetState, credsProvider); } } HttpHost proxy = (HttpHost) context.getAttribute(ExecutionContext.HTTP_PROXY_HOST); AuthState proxyState = (AuthState) context.getAttribute(ClientContext.PROXY_AUTH_STATE); if (proxy != null && proxyState != null && proxyState.getAuthScheme() == null) { AuthScheme authScheme = authCache.get(proxy); if (authScheme != null) { doPreemptiveAuth(proxy, authScheme, proxyState, credsProvider); } } } RequestAuthCache(); void process(final HttpRequest request, final HttpContext context);  }
@Test(expected=IllegalArgumentException.class) public void testRequestParameterCheck() throws Exception { HttpContext context = new BasicHttpContext(); HttpRequestInterceptor interceptor = new RequestAuthCache(); interceptor.process(null, context); }
@Test(expected=IllegalArgumentException.class) public void testContextParameterCheck() throws Exception { HttpRequest request = new BasicHttpRequest("GET", "/"); HttpRequestInterceptor interceptor = new RequestAuthCache(); interceptor.process(request, null); }
@Test public void testPreemptiveTargetAndProxyAuth() throws Exception { HttpRequest request = new BasicHttpRequest("GET", "/"); HttpContext context = new BasicHttpContext(); context.setAttribute(ClientContext.CREDS_PROVIDER, this.credProvider); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target); context.setAttribute(ExecutionContext.HTTP_PROXY_HOST, this.proxy); context.setAttribute(ClientContext.TARGET_AUTH_STATE, this.targetState); context.setAttribute(ClientContext.PROXY_AUTH_STATE, this.proxyState); AuthCache authCache = new BasicAuthCache(); authCache.put(this.target, this.authscheme1); authCache.put(this.proxy, this.authscheme2); context.setAttribute(ClientContext.AUTH_CACHE, authCache); HttpRequestInterceptor interceptor = new RequestAuthCache(); interceptor.process(request, context); Assert.assertSame(this.authscheme1, this.targetState.getAuthScheme()); Assert.assertSame(this.creds1, this.targetState.getCredentials()); Assert.assertSame(this.authscheme2, this.proxyState.getAuthScheme()); Assert.assertSame(this.creds2, this.proxyState.getCredentials()); }
@Test public void testCredentialsProviderNotSet() throws Exception { HttpRequest request = new BasicHttpRequest("GET", "/"); HttpContext context = new BasicHttpContext(); context.setAttribute(ClientContext.CREDS_PROVIDER, null); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target); context.setAttribute(ExecutionContext.HTTP_PROXY_HOST, this.proxy); context.setAttribute(ClientContext.TARGET_AUTH_STATE, this.targetState); context.setAttribute(ClientContext.PROXY_AUTH_STATE, this.proxyState); AuthCache authCache = new BasicAuthCache(); authCache.put(this.target, this.authscheme1); authCache.put(this.proxy, this.authscheme2); context.setAttribute(ClientContext.AUTH_CACHE, authCache); 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 { HttpRequest request = new BasicHttpRequest("GET", "/"); HttpContext context = new BasicHttpContext(); context.setAttribute(ClientContext.CREDS_PROVIDER, this.credProvider); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target); context.setAttribute(ExecutionContext.HTTP_PROXY_HOST, this.proxy); context.setAttribute(ClientContext.TARGET_AUTH_STATE, this.targetState); context.setAttribute(ClientContext.PROXY_AUTH_STATE, this.proxyState); context.setAttribute(ClientContext.AUTH_CACHE, null); 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 { HttpRequest request = new BasicHttpRequest("GET", "/"); HttpContext context = new BasicHttpContext(); context.setAttribute(ClientContext.CREDS_PROVIDER, this.credProvider); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target); context.setAttribute(ExecutionContext.HTTP_PROXY_HOST, this.proxy); context.setAttribute(ClientContext.TARGET_AUTH_STATE, this.targetState); context.setAttribute(ClientContext.PROXY_AUTH_STATE, this.proxyState); AuthCache authCache = new BasicAuthCache(); context.setAttribute(ClientContext.AUTH_CACHE, authCache); 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 { HttpRequest request = new BasicHttpRequest("GET", "/"); this.credProvider.clear(); HttpContext context = new BasicHttpContext(); context.setAttribute(ClientContext.CREDS_PROVIDER, this.credProvider); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target); context.setAttribute(ExecutionContext.HTTP_PROXY_HOST, this.proxy); context.setAttribute(ClientContext.TARGET_AUTH_STATE, this.targetState); context.setAttribute(ClientContext.PROXY_AUTH_STATE, this.proxyState); AuthCache authCache = new BasicAuthCache(); authCache.put(this.target, this.authscheme1); authCache.put(this.proxy, this.authscheme2); context.setAttribute(ClientContext.AUTH_CACHE, authCache); 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 testAuthSchemeAlreadySet() throws Exception { HttpRequest request = new BasicHttpRequest("GET", "/"); HttpContext context = new BasicHttpContext(); context.setAttribute(ClientContext.CREDS_PROVIDER, this.credProvider); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target); context.setAttribute(ExecutionContext.HTTP_PROXY_HOST, this.proxy); context.setAttribute(ClientContext.TARGET_AUTH_STATE, this.targetState); context.setAttribute(ClientContext.PROXY_AUTH_STATE, this.proxyState); AuthCache authCache = new BasicAuthCache(); authCache.put(this.target, this.authscheme1); authCache.put(this.proxy, this.authscheme2); context.setAttribute(ClientContext.AUTH_CACHE, authCache); this.targetState.setAuthScheme(new BasicScheme()); this.targetState.setCredentials(new UsernamePasswordCredentials("user3", "secret3")); this.proxyState.setAuthScheme(new BasicScheme()); this.proxyState.setCredentials(new UsernamePasswordCredentials("user4", "secret4")); HttpRequestInterceptor interceptor = new RequestAuthCache(); interceptor.process(request, context); Assert.assertNotSame(this.authscheme1, this.targetState.getAuthScheme()); Assert.assertNotSame(this.creds1, this.targetState.getCredentials()); Assert.assertNotSame(this.authscheme2, this.proxyState.getAuthScheme()); Assert.assertNotSame(this.creds2, this.proxyState.getCredentials()); }