View Javadoc

1   /*
2    * $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 $
3    * $Revision: 290260 $
4    * $Date: 2005-09-19 16:37:48 -0400 (Mon, 19 Sep 2005) $
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      public static final String PREEMPTIVE_AUTH_SCHEME = "basic";
42      
43      /*** Actual authentication scheme */
44      private AuthScheme authScheme = null;
45  
46      /*** Whether an authetication challenged has been received */
47      private boolean authRequested = false;
48  
49      /*** Whether the authetication challenge has been responsed to */
50      private boolean authAttempted = false;
51  
52      /*** Whether preemtive authentication is attempted */
53      private boolean preemptive  = false; 
54        
55      /***
56       * Default constructor.
57       * 
58       */
59      public AuthState() {
60          super();
61      }
62  
63      /***
64       * Invalidates the authentication state by resetting its parameters.
65       */
66      public void invalidate() {
67          this.authScheme = null;
68          this.authRequested = false;
69          this.authAttempted = false;
70          this.preemptive = false;
71      }
72  
73      /*** 
74       * Tests whether authenication challenge has been received
75       *  
76       * @return <tt>true</tt> if authenication challenge has been received, 
77       *  <tt>false</tt> otherwise
78       */
79      public boolean isAuthRequested() {
80          return this.authRequested;
81      }
82          
83      /*** 
84       * Sets authentication request status
85       *  
86       * @param challengeReceived <tt>true</tt> if authenication has been requested, 
87       *  <tt>false</tt> otherwise
88       */
89      public void setAuthRequested(boolean challengeReceived) {
90          this.authRequested = challengeReceived;
91      }
92      
93      /*** 
94       * Tests whether authenication challenge has been responsed to
95       *  
96       * @return <tt>true</tt> if authenication challenge has been responsed to, 
97       *  <tt>false</tt> otherwise
98       */
99      public boolean isAuthAttempted() {
100         return this.authAttempted;
101     }
102         
103     /*** 
104      * Sets authentication attempt status
105      *  
106      * @param challengeResponded <tt>true</tt> if authenication has been attempted, 
107      *  <tt>false</tt> otherwise
108      */
109     public void setAuthAttempted(boolean challengeResponded) {
110         this.authAttempted = challengeResponded;
111     }
112     
113     /***
114      * Preemptively assigns Basic authentication scheme.
115      */
116     public void setPreemptive() {
117         if (!this.preemptive) {
118             if (this.authScheme != null) {
119                 throw new IllegalStateException("Authentication state already initialized");
120             }
121             this.authScheme = AuthPolicy.getAuthScheme(PREEMPTIVE_AUTH_SCHEME);
122             this.preemptive = true;
123         }
124     }
125 
126     /***
127      * Tests if preemptive authentication is used.
128      * 
129      * @return <tt>true</tt> if using the default Basic {@link AuthScheme 
130      * authentication scheme}, <tt>false</tt> otherwise.
131      */
132     public boolean isPreemptive() {
133         return this.preemptive;
134     }
135     
136     /***
137      * Assigns the given {@link AuthScheme authentication scheme}.
138      * 
139      * @param authScheme the {@link AuthScheme authentication scheme}
140      */
141     public void setAuthScheme(final AuthScheme authScheme) {
142         if (authScheme == null) {
143             invalidate();
144             return;
145         }
146         if (this.preemptive && !(this.authScheme.getClass().isInstance(authScheme))) {
147             this.preemptive = false;
148             this.authAttempted = false;
149         }
150         this.authScheme = authScheme;
151     }
152 
153     /***
154      * Returns the {@link AuthScheme authentication scheme}.
155      * 
156      * @return {@link AuthScheme authentication scheme}
157      */
158     public AuthScheme getAuthScheme() {
159         return authScheme;
160     }
161     
162     /***
163      * Returns the authentication realm.
164      * 
165      * @return the name of the authentication realm
166      */
167     public String getRealm() {
168         if (this.authScheme != null) {
169             return this.authScheme.getRealm();
170         } else {
171             return null;
172         }
173     }
174     
175     public String toString() {
176         StringBuffer buffer = new StringBuffer();
177         buffer.append("Auth state: auth requested [");
178         buffer.append(this.authRequested);
179         buffer.append("]; auth attempted [");
180         buffer.append(this.authAttempted);
181         if (this.authScheme != null) {
182             buffer.append("]; auth scheme [");
183             buffer.append(this.authScheme.getSchemeName());
184             buffer.append("]; realm [");
185             buffer.append(this.authScheme.getRealm());            
186         }
187         buffer.append("] preemptive [");
188         buffer.append(this.preemptive);
189         buffer.append("]");
190         return buffer.toString();
191     }
192 }