/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.extension.repository.http.internal;

import java.io.IOException;

import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.auth.AuthScheme;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.AuthState;
import org.apache.http.auth.Credentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;

/**
 * Used to force preemptive authentication.
 *
 * @version $Id$
 * @since 5.2M1
 */
public class PreemptiveAuth implements HttpRequestInterceptor
{
    /**
     * Singleton instance of {@link PreemptiveAuth}.
     */
    public static final PreemptiveAuth INSTANCE = new PreemptiveAuth();

    @Override
    public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException
    {
        AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);

        // If no auth scheme available yet, try to initialize it
        // preemptively
        if (authState.getAuthScheme() == null) {
            AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
            CredentialsProvider credsProvider =
                (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
            HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
            if (authScheme != null) {
                Credentials credentials =
                    credsProvider.getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
                if (credentials == null) {
                    throw new HttpException("No credentials for preemptive authentication");
                }
                authState.update(authScheme, credentials);
            }
        }

    }
}

@@ -1374,16 +1374,20 @@
 rotocol.
+Http
 ClientCo
@@ -1425,25 +1425,20 @@
 rotocol.
-Execution
+Http
 Context;
@@ -1472,24 +1472,28 @@
 tocol.HttpCo
+reCo
 ntext;%0A%0A/**%0A
@@ -1969,32 +1969,36 @@
 xt.getAttribute(
+Http
 ClientContext.TA
@@ -2080,27 +2080,16 @@
 alize it
-%0A        //
  preempt
@@ -2340,16 +2340,20 @@
 tribute(
+Http
 ClientCo
@@ -2445,17 +2445,16 @@
 ute(
-Execution
+HttpCore
 Cont

Fix deprecated calls