• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultUserTokenHandler.java $
3  * $Revision: 659971 $
4  * $Date: 2008-05-25 05:01:22 -0700 (Sun, 25 May 2008) $
5  *
6  * ====================================================================
7  *
8  *  Licensed to the Apache Software Foundation (ASF) under one or more
9  *  contributor license agreements.  See the NOTICE file distributed with
10  *  this work for additional information regarding copyright ownership.
11  *  The ASF licenses this file to You under the Apache License, Version 2.0
12  *  (the "License"); you may not use this file except in compliance with
13  *  the License.  You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  *  Unless required by applicable law or agreed to in writing, software
18  *  distributed under the License is distributed on an "AS IS" BASIS,
19  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  *  See the License for the specific language governing permissions and
21  *  limitations under the License.
22  * ====================================================================
23  *
24  * This software consists of voluntary contributions made by many
25  * individuals on behalf of the Apache Software Foundation.  For more
26  * information on the Apache Software Foundation, please see
27  * <http://www.apache.org/>.
28  *
29  */
30 
31 package org.apache.http.impl.client;
32 
33 import java.security.Principal;
34 
35 import javax.net.ssl.SSLSession;
36 
37 import org.apache.http.auth.AuthScheme;
38 import org.apache.http.auth.AuthState;
39 import org.apache.http.auth.Credentials;
40 import org.apache.http.client.UserTokenHandler;
41 import org.apache.http.client.protocol.ClientContext;
42 import org.apache.http.conn.ManagedClientConnection;
43 import org.apache.http.protocol.ExecutionContext;
44 import org.apache.http.protocol.HttpContext;
45 /**
46  * @deprecated Please use {@link java.net.URL#openConnection} instead.
47  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
48  *     for further details.
49 */
50 
51 @Deprecated
52 public class DefaultUserTokenHandler implements UserTokenHandler {
53 
getUserToken(final HttpContext context)54     public Object getUserToken(final HttpContext context) {
55 
56         Principal userPrincipal = null;
57 
58         AuthState targetAuthState = (AuthState) context.getAttribute(
59                 ClientContext.TARGET_AUTH_STATE);
60         if (targetAuthState != null) {
61             userPrincipal = getAuthPrincipal(targetAuthState);
62             if (userPrincipal == null) {
63                 AuthState proxyAuthState = (AuthState) context.getAttribute(
64                         ClientContext.PROXY_AUTH_STATE);
65                 userPrincipal = getAuthPrincipal(proxyAuthState);
66             }
67         }
68 
69         if (userPrincipal == null) {
70             ManagedClientConnection conn = (ManagedClientConnection) context.getAttribute(
71                     ExecutionContext.HTTP_CONNECTION);
72             if (conn.isOpen()) {
73                 SSLSession sslsession = conn.getSSLSession();
74                 if (sslsession != null) {
75                     userPrincipal = sslsession.getLocalPrincipal();
76                 }
77             }
78         }
79 
80         return userPrincipal;
81     }
82 
getAuthPrincipal(final AuthState authState)83     private static Principal getAuthPrincipal(final AuthState authState) {
84         AuthScheme scheme = authState.getAuthScheme();
85         if (scheme != null && scheme.isComplete() && scheme.isConnectionBased()) {
86             Credentials creds = authState.getCredentials();
87             if (creds != null) {
88                 return creds.getUserPrincipal();
89             }
90         }
91         return null;
92     }
93 
94 }
95