Home | History | Annotate | Download | only in auth
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/AuthState.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.auth;
     32 
     33 
     34 /**
     35  * This class provides detailed information about the state of the
     36  * authentication process.
     37  *
     38  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
     39  *
     40  * @since 4.0
     41  */
     42 public class AuthState {
     43 
     44     /** Actual authentication scheme */
     45     private AuthScheme authScheme;
     46 
     47     /** Actual authentication scope */
     48     private AuthScope authScope;
     49 
     50     /** Credentials selected for authentication */
     51     private Credentials credentials;
     52 
     53     /**
     54      * Default constructor.
     55      *
     56      */
     57     public AuthState() {
     58         super();
     59     }
     60 
     61     /**
     62      * Invalidates the authentication state by resetting its parameters.
     63      */
     64     public void invalidate() {
     65         this.authScheme = null;
     66         this.authScope = null;
     67         this.credentials = null;
     68     }
     69 
     70     public boolean isValid() {
     71         return this.authScheme != null;
     72     }
     73 
     74     /**
     75      * Assigns the given {@link AuthScheme authentication scheme}.
     76      *
     77      * @param authScheme the {@link AuthScheme authentication scheme}
     78      */
     79     public void setAuthScheme(final AuthScheme authScheme) {
     80         if (authScheme == null) {
     81             invalidate();
     82             return;
     83         }
     84         this.authScheme = authScheme;
     85     }
     86 
     87     /**
     88      * Returns the {@link AuthScheme authentication scheme}.
     89      *
     90      * @return {@link AuthScheme authentication scheme}
     91      */
     92     public AuthScheme getAuthScheme() {
     93         return this.authScheme;
     94     }
     95 
     96 
     97     /**
     98      * Returns user {@link Credentials} selected for authentication if available
     99      *
    100      * @return user credentials if available, <code>null</code otherwise
    101      */
    102     public Credentials getCredentials() {
    103         return this.credentials;
    104     }
    105 
    106 
    107     /**
    108      * Sets user {@link Credentials} to be used for authentication
    109      *
    110      * @param credentials User credentials
    111      */
    112     public void setCredentials(final Credentials credentials) {
    113         this.credentials = credentials;
    114     }
    115 
    116 
    117     /**
    118      * Returns actual {@link AuthScope} if available
    119      *
    120      * @return actual authentication scope if available, <code>null</code otherwise
    121      */
    122      public AuthScope getAuthScope() {
    123         return this.authScope;
    124      }
    125 
    126      /**
    127       * Sets actual {@link AuthScope}.
    128       *
    129       * @param authScope Authentication scope
    130       */
    131      public void setAuthScope(final AuthScope authScope) {
    132         this.authScope = authScope;
    133      }
    134 
    135 
    136     @Override
    137     public String toString() {
    138         StringBuilder buffer = new StringBuilder();
    139         buffer.append("auth scope [");
    140         buffer.append(this.authScope);
    141         buffer.append("]; credentials set [");
    142         buffer.append(this.credentials != null ? "true" : "false");
    143         buffer.append("]");
    144         return buffer.toString();
    145     }
    146 
    147 }
    148