View Javadoc

1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/auth/AuthState.java,v 1.2 2004/04/18 23:51:36 jsdever Exp $
3    * $Revision: 1.2 $
4    * $Date: 2004/04/18 23:51:36 $
5    *
6    * ====================================================================
7    *
8    *  Copyright 1999-2004 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   */
29  
30  package org.apache.commons.httpclient.auth;
31  
32  /***
33   * This class provides detailed information about the state of the
34   * authentication process.
35   * 
36   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
37   * @since 3.0
38   */
39  public class AuthState {
40  
41      /*** Actual authentication scheme */
42      private AuthScheme authScheme = null;
43  
44      /*** Whether preemtive authentication is attempted */
45      private boolean preemptive  = false; 
46        
47      /***
48       * Default constructor.
49       * 
50       */
51      public AuthState() {
52          super();
53      }
54  
55      /***
56       * Preemptively assigns Basic authentication scheme.
57       */
58      public void setPreemptive() {
59          if (this.authScheme != null) {
60              throw new IllegalStateException("Authentication state already initialized");
61          }
62          this.authScheme = AuthPolicy.getAuthScheme("basic");
63          this.preemptive = true;
64      }
65  
66      /***
67       * Invalidates the authentication state by resetting its parameters.
68       */
69      public void invalidate() {
70          this.authScheme = null;
71          this.preemptive = false;
72      }
73      
74      /***
75       * Tests if preemptive authentication is used.
76       * 
77       * @return <tt>true</tt> if using the default Basic {@link AuthScheme 
78       * authentication scheme}, <tt>false</tt> otherwise.
79       */
80      public boolean isPreemptive() {
81          return this.preemptive;
82      }
83      
84      /***
85       * Assigns the given {@link AuthScheme authentication scheme}.
86       * 
87       * @param authScheme the {@link AuthScheme authentication scheme}
88       */
89      public void setAuthScheme(final AuthScheme authScheme) {
90          this.authScheme = authScheme;
91          this.preemptive = false;
92      }
93  
94      /***
95       * Returns the {@link AuthScheme authentication scheme}.
96       * 
97       * @return {@link AuthScheme authentication scheme}
98       */
99      public AuthScheme getAuthScheme() {
100         return authScheme;
101     }
102     
103     /***
104      * Returns the authentication realm.
105      * 
106      * @return the name of the authentication realm
107      */
108     public String getRealm() {
109         if (this.authScheme != null) {
110             return this.authScheme.getRealm();
111         } else {
112             return null;
113         }
114     }
115 }