1 30 31 package org.apache.commons.httpclient.auth; 32 33 40 public class AuthState { 41 42 public static final String PREEMPTIVE_AUTH_SCHEME = "basic"; 43 44 45 private AuthScheme authScheme = null; 46 47 48 private boolean authRequested = false; 49 50 51 private boolean authAttempted = false; 52 53 54 private boolean preemptive = false; 55 56 60 public AuthState() { 61 super(); 62 } 63 64 67 public void invalidate() { 68 this.authScheme = null; 69 this.authRequested = false; 70 this.authAttempted = false; 71 this.preemptive = false; 72 } 73 74 80 public boolean isAuthRequested() { 81 return this.authRequested; 82 } 83 84 90 public void setAuthRequested(boolean challengeReceived) { 91 this.authRequested = challengeReceived; 92 } 93 94 100 public boolean isAuthAttempted() { 101 return this.authAttempted; 102 } 103 104 110 public void setAuthAttempted(boolean challengeResponded) { 111 this.authAttempted = challengeResponded; 112 } 113 114 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 133 public boolean isPreemptive() { 134 return this.preemptive; 135 } 136 137 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 159 public AuthScheme getAuthScheme() { 160 return authScheme; 161 } 162 163 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 } 194 | Popular Tags |