1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.extension.repository.http.internal

File PreemptiveAuth.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

6
10
1
1
72
37
4
0.4
10
1
4

Classes

Class Line # Actions
PreemptiveAuth 43 10 0% 4 17
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.extension.repository.http.internal;
21   
22    import java.io.IOException;
23   
24    import org.apache.http.HttpException;
25    import org.apache.http.HttpHost;
26    import org.apache.http.HttpRequest;
27    import org.apache.http.HttpRequestInterceptor;
28    import org.apache.http.auth.AuthScheme;
29    import org.apache.http.auth.AuthScope;
30    import org.apache.http.auth.AuthState;
31    import org.apache.http.auth.Credentials;
32    import org.apache.http.client.CredentialsProvider;
33    import org.apache.http.client.protocol.HttpClientContext;
34    import org.apache.http.protocol.HttpContext;
35    import org.apache.http.protocol.HttpCoreContext;
36   
37    /**
38    * Used to force preemptive authentication.
39    *
40    * @version $Id: 4b49a846698e63198f91addc7dafccbe5a9c531d $
41    * @since 5.2M1
42    */
 
43    public class PreemptiveAuth implements HttpRequestInterceptor
44    {
45    /**
46    * Singleton instance of {@link PreemptiveAuth}.
47    */
48    public static final PreemptiveAuth INSTANCE = new PreemptiveAuth();
49   
 
50  0 toggle @Override
51    public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException
52    {
53  0 AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
54   
55    // If no auth scheme available yet, try to initialize it preemptively
56  0 if (authState.getAuthScheme() == null) {
57  0 AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
58  0 CredentialsProvider credsProvider =
59    (CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER);
60  0 HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
61  0 if (authScheme != null) {
62  0 Credentials credentials =
63    credsProvider.getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
64  0 if (credentials == null) {
65  0 throw new HttpException("No credentials for preemptive authentication");
66    }
67  0 authState.update(authScheme, credentials);
68    }
69    }
70   
71    }
72    }