View Javadoc

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