1+ package com .loopj .android .http ;
2+
13/*
24 Android Asynchronous Http Client
35 Copyright (c) 2011 James Smith <[email protected] > 46 https://loopj.com
5-
67 Licensed under the Apache License, Version 2.0 (the "License");
78 you may not use this file except in compliance with the License.
89 You may obtain a copy of the License at
9-
1010 https://www.apache.org/licenses/LICENSE-2.0
11-
1211 Unless required by applicable law or agreed to in writing, software
1312 distributed under the License is distributed on an "AS IS" BASIS,
1413 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1514 See the License for the specific language governing permissions and
1615 limitations under the License.
1716*/
1817
19- package com .loopj .android .http ;
20-
2118import android .content .Context ;
19+ import android .media .session .MediaSession ;
2220import android .os .Looper ;
2321
2422import java .io .IOException ;
5048import cz .msebera .android .httpclient .HttpResponse ;
5149import cz .msebera .android .httpclient .HttpResponseInterceptor ;
5250import cz .msebera .android .httpclient .HttpVersion ;
51+ import cz .msebera .android .httpclient .auth .AuthSchemeRegistry ;
5352import cz .msebera .android .httpclient .auth .AuthScope ;
5453import cz .msebera .android .httpclient .auth .AuthState ;
5554import cz .msebera .android .httpclient .auth .Credentials ;
7675import cz .msebera .android .httpclient .conn .ssl .SSLSocketFactory ;
7776import cz .msebera .android .httpclient .entity .HttpEntityWrapper ;
7877import cz .msebera .android .httpclient .impl .auth .BasicScheme ;
78+ import cz .msebera .android .httpclient .impl .client .BasicCredentialsProvider ;
7979import cz .msebera .android .httpclient .impl .client .DefaultHttpClient ;
8080import cz .msebera .android .httpclient .impl .conn .tsccm .ThreadSafeClientConnManager ;
8181import cz .msebera .android .httpclient .params .BasicHttpParams ;
@@ -251,6 +251,11 @@ public void process(HttpResponse response, HttpContext context) {
251251 httpClient .addRequestInterceptor (new HttpRequestInterceptor () {
252252 @ Override
253253 public void process (final HttpRequest request , final HttpContext context ) throws HttpException , IOException {
254+
255+ AuthSchemeRegistry authSchemeRegistry = new AuthSchemeRegistry ();
256+ authSchemeRegistry .register ("Bearer" , new BearerAuthSchemeFactory ());
257+ context .setAttribute (ClientContext .AUTHSCHEME_REGISTRY , authSchemeRegistry );
258+
254259 AuthState authState = (AuthState ) context .getAttribute (ClientContext .TARGET_AUTH_STATE );
255260 CredentialsProvider credsProvider = (CredentialsProvider ) context .getAttribute (
256261 ClientContext .CREDS_PROVIDER );
@@ -259,7 +264,11 @@ public void process(final HttpRequest request, final HttpContext context) throws
259264 if (authState .getAuthScheme () == null ) {
260265 AuthScope authScope = new AuthScope (targetHost .getHostName (), targetHost .getPort ());
261266 Credentials creds = credsProvider .getCredentials (authScope );
262- if (creds != null ) {
267+ if (creds instanceof TokenCredentials ) {
268+ authState .setAuthScheme (new BearerAuthSchemeFactory .BearerAuthScheme ());
269+ authState .setCredentials (creds );
270+ }
271+ else if (creds != null ) {
263272 authState .setAuthScheme (new BasicScheme ());
264273 authState .setCredentials (creds );
265274 }
@@ -791,6 +800,29 @@ public void removeHeader(String header) {
791800 clientHeaderMap .remove (header );
792801 }
793802
803+ /**
804+ * Sets bearer authentication for the request. Uses AuthScope.ANY. This is the same as
805+ * setBearerAuth('token',AuthScope.ANY, false)
806+ * @param token Bearer Token
807+ */
808+ public void setBearerAuth (String token ) {
809+ setBearerAuth (token , AuthScope .ANY , false );
810+ }
811+
812+
813+ /**
814+ * Sets bearer authentication for the request. You should pass in your AuthScope for security. It
815+ * should be like this setBearerAuth("token", new AuthScope("host",port,AuthScope.ANY_REALM), false)
816+ * @param token Bearer Token
817+ * @param scope an AuthScope object
818+ * @param preemptive sets authorization in preemptive manner
819+ */
820+ public void setBearerAuth (String token , AuthScope scope , boolean preemptive ) {
821+ TokenCredentials credentials = new TokenCredentials (token );
822+ setCredentials (scope , credentials );
823+ setAuthenticationPreemptive (preemptive );
824+ }
825+
794826 /**
795827 * Sets basic authentication for the request. Uses AuthScope.ANY. This is the same as
796828 * setBasicAuth('username','password',AuthScope.ANY)
@@ -1635,4 +1667,4 @@ public void consumeContent() throws IOException {
16351667 super .consumeContent ();
16361668 }
16371669 }
1638- }
1670+ }
0 commit comments