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