Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
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 */
28 package ch.boye.httpclientandroidlib.client.protocol;
30 import java.io.IOException;
32 import ch.boye.httpclientandroidlib.annotation.Immutable;
34 import ch.boye.httpclientandroidlib.androidextra.HttpClientAndroidLog;
35 /* LogFactory removed by HttpClient for Android script. */
36 import ch.boye.httpclientandroidlib.Header;
37 import ch.boye.httpclientandroidlib.HttpException;
38 import ch.boye.httpclientandroidlib.HttpRequest;
39 import ch.boye.httpclientandroidlib.HttpRequestInterceptor;
40 import ch.boye.httpclientandroidlib.auth.AUTH;
41 import ch.boye.httpclientandroidlib.auth.AuthScheme;
42 import ch.boye.httpclientandroidlib.auth.AuthState;
43 import ch.boye.httpclientandroidlib.auth.AuthenticationException;
44 import ch.boye.httpclientandroidlib.auth.ContextAwareAuthScheme;
45 import ch.boye.httpclientandroidlib.auth.Credentials;
46 import ch.boye.httpclientandroidlib.protocol.HttpContext;
48 /**
49 * Generates authentication header for the target host, if required,
50 * based on the actual state of the HTTP authentication context.
51 *
52 * @since 4.0
53 */
54 @Immutable
55 public class RequestTargetAuthentication implements HttpRequestInterceptor {
57 public HttpClientAndroidLog log = new HttpClientAndroidLog(getClass());
59 public RequestTargetAuthentication() {
60 super();
61 }
63 @SuppressWarnings("deprecation")
64 public void process(final HttpRequest request, final HttpContext context)
65 throws HttpException, IOException {
66 if (request == 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 }
73 String method = request.getRequestLine().getMethod();
74 if (method.equalsIgnoreCase("CONNECT")) {
75 return;
76 }
78 if (request.containsHeader(AUTH.WWW_AUTH_RESP)) {
79 return;
80 }
82 // Obtain authentication state
83 AuthState authState = (AuthState) context.getAttribute(
84 ClientContext.TARGET_AUTH_STATE);
85 if (authState == null) {
86 this.log.debug("Target auth state not set in the context");
87 return;
88 }
90 AuthScheme authScheme = authState.getAuthScheme();
91 if (authScheme == null) {
92 return;
93 }
95 Credentials creds = authState.getCredentials();
96 if (creds == null) {
97 this.log.debug("User credentials not available");
98 return;
99 }
101 if (authState.getAuthScope() != null || !authScheme.isConnectionBased()) {
102 try {
103 Header header;
104 if (authScheme instanceof ContextAwareAuthScheme) {
105 header = ((ContextAwareAuthScheme) authScheme).authenticate(
106 creds, request, context);
107 } else {
108 header = authScheme.authenticate(creds, request);
109 }
110 request.addHeader(header);
111 } catch (AuthenticationException ex) {
112 if (this.log.isErrorEnabled()) {
113 this.log.error("Authentication error: " + ex.getMessage());
114 }
115 }
116 }
117 }
119 }