All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.commons.httpclient.auth.AuthState Maven / Gradle / Ivy

There is a newer version: 2024.11.18751.20241128T090041Z-241100
Show newest version
/*
 * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/auth/AuthState.java,v 1.3 2004/11/02 19:39:16 olegk Exp $
 * $Revision: 480424 $
 * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
 *
 * ====================================================================
 *
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * .
 *
 */

package org.apache.commons.httpclient.auth;

/**
 * This class provides detailed information about the state of the
 * authentication process.
 * 
 * @author Oleg Kalnichevski
 * @since 3.0
 * @deprecated Jakarta Commons HttpClient 3.x is deprecated in the Jenkins project.
 *  It is not recommended to use it in any new code.
 *  Instead, use HTTP client API plugins as a dependency in your code.
 *  E.g. 
 *      Apache HttpComponents Client API 4.x Plugin or
 *  Async HTTP Client Plugin.
 */
@Deprecated
public class AuthState {

    public static final String PREEMPTIVE_AUTH_SCHEME = "basic";
    
    /** Actual authentication scheme */
    private AuthScheme authScheme = null;

    /** Whether an authetication challenged has been received */
    private boolean authRequested = false;

    /** Whether the authetication challenge has been responsed to */
    private boolean authAttempted = false;

    /** Whether preemtive authentication is attempted */
    private boolean preemptive  = false; 
      
    /**
     * Default constructor.
     * 
     */
    public AuthState() {
        super();
    }

    /**
     * Invalidates the authentication state by resetting its parameters.
     */
    public void invalidate() {
        this.authScheme = null;
        this.authRequested = false;
        this.authAttempted = false;
        this.preemptive = false;
    }

    /** 
     * Tests whether authenication challenge has been received
     *  
     * @return true if authenication challenge has been received, 
     *  false otherwise
     */
    public boolean isAuthRequested() {
        return this.authRequested;
    }
        
    /** 
     * Sets authentication request status
     *  
     * @param challengeReceived true if authenication has been requested, 
     *  false otherwise
     */
    public void setAuthRequested(boolean challengeReceived) {
        this.authRequested = challengeReceived;
    }
    
    /** 
     * Tests whether authenication challenge has been responsed to
     *  
     * @return true if authenication challenge has been responsed to, 
     *  false otherwise
     */
    public boolean isAuthAttempted() {
        return this.authAttempted;
    }
        
    /** 
     * Sets authentication attempt status
     *  
     * @param challengeResponded true if authenication has been attempted, 
     *  false otherwise
     */
    public void setAuthAttempted(boolean challengeResponded) {
        this.authAttempted = challengeResponded;
    }
    
    /**
     * Preemptively assigns Basic authentication scheme.
     */
    public void setPreemptive() {
        if (!this.preemptive) {
            if (this.authScheme != null) {
                throw new IllegalStateException("Authentication state already initialized");
            }
            this.authScheme = AuthPolicy.getAuthScheme(PREEMPTIVE_AUTH_SCHEME);
            this.preemptive = true;
        }
    }

    /**
     * Tests if preemptive authentication is used.
     * 
     * @return true if using the default Basic {@link AuthScheme 
     * authentication scheme}, false otherwise.
     */
    public boolean isPreemptive() {
        return this.preemptive;
    }
    
    /**
     * Assigns the given {@link AuthScheme authentication scheme}.
     * 
     * @param authScheme the {@link AuthScheme authentication scheme}
     */
    public void setAuthScheme(final AuthScheme authScheme) {
        if (authScheme == null) {
            invalidate();
            return;
        }
        if (this.preemptive && !(this.authScheme.getClass().isInstance(authScheme))) {
            this.preemptive = false;
            this.authAttempted = false;
        }
        this.authScheme = authScheme;
    }

    /**
     * Returns the {@link AuthScheme authentication scheme}.
     * 
     * @return {@link AuthScheme authentication scheme}
     */
    public AuthScheme getAuthScheme() {
        return authScheme;
    }
    
    /**
     * Returns the authentication realm.
     * 
     * @return the name of the authentication realm
     */
    public String getRealm() {
        if (this.authScheme != null) {
            return this.authScheme.getRealm();
        } else {
            return null;
        }
    }
    
    public String toString() {
        StringBuffer buffer = new StringBuffer();
        buffer.append("Auth state: auth requested [");
        buffer.append(this.authRequested);
        buffer.append("]; auth attempted [");
        buffer.append(this.authAttempted);
        if (this.authScheme != null) {
            buffer.append("]; auth scheme [");
            buffer.append(this.authScheme.getSchemeName());
            buffer.append("]; realm [");
            buffer.append(this.authScheme.getRealm());            
        }
        buffer.append("] preemptive [");
        buffer.append(this.preemptive);
        buffer.append("]");
        return buffer.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy