|
1 /* |
|
2 * ==================================================================== |
|
3 * Licensed to the Apache Software Foundation (ASF) under one |
|
4 * or more contributor license agreements. See the NOTICE file |
|
5 * distributed with this work for additional information |
|
6 * regarding copyright ownership. The ASF licenses this file |
|
7 * to you under the Apache License, Version 2.0 (the |
|
8 * "License"); you may not use this file except in compliance |
|
9 * with the License. You may obtain a copy of the License at |
|
10 * |
|
11 * http://www.apache.org/licenses/LICENSE-2.0 |
|
12 * |
|
13 * Unless required by applicable law or agreed to in writing, |
|
14 * software distributed under the License is distributed on an |
|
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
|
16 * KIND, either express or implied. See the License for the |
|
17 * specific language governing permissions and limitations |
|
18 * under the License. |
|
19 * ==================================================================== |
|
20 * |
|
21 * This software consists of voluntary contributions made by many |
|
22 * individuals on behalf of the Apache Software Foundation. For more |
|
23 * information on the Apache Software Foundation, please see |
|
24 * <http://www.apache.org/>. |
|
25 * |
|
26 */ |
|
27 |
|
28 package ch.boye.httpclientandroidlib.client.protocol; |
|
29 |
|
30 import java.io.IOException; |
|
31 |
|
32 import ch.boye.httpclientandroidlib.androidextra.HttpClientAndroidLog; |
|
33 /* LogFactory removed by HttpClient for Android script. */ |
|
34 import ch.boye.httpclientandroidlib.HttpException; |
|
35 import ch.boye.httpclientandroidlib.HttpHost; |
|
36 import ch.boye.httpclientandroidlib.HttpResponse; |
|
37 import ch.boye.httpclientandroidlib.HttpResponseInterceptor; |
|
38 import ch.boye.httpclientandroidlib.annotation.Immutable; |
|
39 import ch.boye.httpclientandroidlib.auth.AuthScheme; |
|
40 import ch.boye.httpclientandroidlib.auth.AuthState; |
|
41 import ch.boye.httpclientandroidlib.client.AuthCache; |
|
42 import ch.boye.httpclientandroidlib.client.params.AuthPolicy; |
|
43 import ch.boye.httpclientandroidlib.impl.client.BasicAuthCache; |
|
44 import ch.boye.httpclientandroidlib.protocol.ExecutionContext; |
|
45 import ch.boye.httpclientandroidlib.protocol.HttpContext; |
|
46 |
|
47 /** |
|
48 * Response interceptor that adds successfully completed {@link AuthScheme}s |
|
49 * to the local {@link AuthCache} instance. Cached {@link AuthScheme}s can be |
|
50 * re-used when executing requests against known hosts, thus avoiding |
|
51 * additional authentication round-trips. |
|
52 * |
|
53 * @since 4.1 |
|
54 */ |
|
55 @Immutable |
|
56 public class ResponseAuthCache implements HttpResponseInterceptor { |
|
57 |
|
58 public HttpClientAndroidLog log = new HttpClientAndroidLog(getClass()); |
|
59 |
|
60 public ResponseAuthCache() { |
|
61 super(); |
|
62 } |
|
63 |
|
64 public void process(final HttpResponse response, final HttpContext context) |
|
65 throws HttpException, IOException { |
|
66 if (response == null) { |
|
67 throw new IllegalArgumentException("HTTP request may not be null"); |
|
68 } |
|
69 if (context == null) { |
|
70 throw new IllegalArgumentException("HTTP context may not be null"); |
|
71 } |
|
72 AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE); |
|
73 |
|
74 HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); |
|
75 AuthState targetState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); |
|
76 if (target != null && targetState != null) { |
|
77 if (isCachable(targetState)) { |
|
78 if (authCache == null) { |
|
79 authCache = new BasicAuthCache(); |
|
80 context.setAttribute(ClientContext.AUTH_CACHE, authCache); |
|
81 } |
|
82 cache(authCache, target, targetState); |
|
83 } |
|
84 } |
|
85 |
|
86 HttpHost proxy = (HttpHost) context.getAttribute(ExecutionContext.HTTP_PROXY_HOST); |
|
87 AuthState proxyState = (AuthState) context.getAttribute(ClientContext.PROXY_AUTH_STATE); |
|
88 if (proxy != null && proxyState != null) { |
|
89 if (isCachable(proxyState)) { |
|
90 if (authCache == null) { |
|
91 authCache = new BasicAuthCache(); |
|
92 context.setAttribute(ClientContext.AUTH_CACHE, authCache); |
|
93 } |
|
94 cache(authCache, proxy, proxyState); |
|
95 } |
|
96 } |
|
97 } |
|
98 |
|
99 private boolean isCachable(final AuthState authState) { |
|
100 AuthScheme authScheme = authState.getAuthScheme(); |
|
101 if (authScheme == null || !authScheme.isComplete()) { |
|
102 return false; |
|
103 } |
|
104 String schemeName = authScheme.getSchemeName(); |
|
105 return schemeName.equalsIgnoreCase(AuthPolicy.BASIC) || |
|
106 schemeName.equalsIgnoreCase(AuthPolicy.DIGEST); |
|
107 } |
|
108 |
|
109 private void cache(final AuthCache authCache, final HttpHost host, final AuthState authState) { |
|
110 AuthScheme authScheme = authState.getAuthScheme(); |
|
111 if (authState.getAuthScope() != null) { |
|
112 if (authState.getCredentials() != null) { |
|
113 if (this.log.isDebugEnabled()) { |
|
114 this.log.debug("Caching '" + authScheme.getSchemeName() + |
|
115 "' auth scheme for " + host); |
|
116 } |
|
117 authCache.put(host, authScheme); |
|
118 } else { |
|
119 authCache.remove(host); |
|
120 } |
|
121 } |
|
122 } |
|
123 |
|
124 } |