001: /*
002: * $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 $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient.auth;
032:
033: /**
034: * This class provides detailed information about the state of the
035: * authentication process.
036: *
037: * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
038: * @since 3.0
039: */
040: public class AuthState {
041:
042: public static final String PREEMPTIVE_AUTH_SCHEME = "basic";
043:
044: /** Actual authentication scheme */
045: private AuthScheme authScheme = null;
046:
047: /** Whether an authetication challenged has been received */
048: private boolean authRequested = false;
049:
050: /** Whether the authetication challenge has been responsed to */
051: private boolean authAttempted = false;
052:
053: /** Whether preemtive authentication is attempted */
054: private boolean preemptive = false;
055:
056: /**
057: * Default constructor.
058: *
059: */
060: public AuthState() {
061: super ();
062: }
063:
064: /**
065: * Invalidates the authentication state by resetting its parameters.
066: */
067: public void invalidate() {
068: this .authScheme = null;
069: this .authRequested = false;
070: this .authAttempted = false;
071: this .preemptive = false;
072: }
073:
074: /**
075: * Tests whether authenication challenge has been received
076: *
077: * @return <tt>true</tt> if authenication challenge has been received,
078: * <tt>false</tt> otherwise
079: */
080: public boolean isAuthRequested() {
081: return this .authRequested;
082: }
083:
084: /**
085: * Sets authentication request status
086: *
087: * @param challengeReceived <tt>true</tt> if authenication has been requested,
088: * <tt>false</tt> otherwise
089: */
090: public void setAuthRequested(boolean challengeReceived) {
091: this .authRequested = challengeReceived;
092: }
093:
094: /**
095: * Tests whether authenication challenge has been responsed to
096: *
097: * @return <tt>true</tt> if authenication challenge has been responsed to,
098: * <tt>false</tt> otherwise
099: */
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(
121: "Authentication state already initialized");
122: }
123: this .authScheme = AuthPolicy
124: .getAuthScheme(PREEMPTIVE_AUTH_SCHEME);
125: this .preemptive = true;
126: }
127: }
128:
129: /**
130: * Tests if preemptive authentication is used.
131: *
132: * @return <tt>true</tt> if using the default Basic {@link AuthScheme
133: * authentication scheme}, <tt>false</tt> otherwise.
134: */
135: public boolean isPreemptive() {
136: return this .preemptive;
137: }
138:
139: /**
140: * Assigns the given {@link AuthScheme authentication scheme}.
141: *
142: * @param authScheme the {@link AuthScheme authentication scheme}
143: */
144: public void setAuthScheme(final AuthScheme authScheme) {
145: if (authScheme == null) {
146: invalidate();
147: return;
148: }
149: if (this .preemptive
150: && !(this .authScheme.getClass().isInstance(authScheme))) {
151: this .preemptive = false;
152: this .authAttempted = false;
153: }
154: this .authScheme = authScheme;
155: }
156:
157: /**
158: * Returns the {@link AuthScheme authentication scheme}.
159: *
160: * @return {@link AuthScheme authentication scheme}
161: */
162: public AuthScheme getAuthScheme() {
163: return authScheme;
164: }
165:
166: /**
167: * Returns the authentication realm.
168: *
169: * @return the name of the authentication realm
170: */
171: public String getRealm() {
172: if (this .authScheme != null) {
173: return this .authScheme.getRealm();
174: } else {
175: return null;
176: }
177: }
178:
179: public String toString() {
180: StringBuffer buffer = new StringBuffer();
181: buffer.append("Auth state: auth requested [");
182: buffer.append(this .authRequested);
183: buffer.append("]; auth attempted [");
184: buffer.append(this .authAttempted);
185: if (this .authScheme != null) {
186: buffer.append("]; auth scheme [");
187: buffer.append(this .authScheme.getSchemeName());
188: buffer.append("]; realm [");
189: buffer.append(this .authScheme.getRealm());
190: }
191: buffer.append("] preemptive [");
192: buffer.append(this .preemptive);
193: buffer.append("]");
194: return buffer.toString();
195: }
196: }
|