1 31 32 package org.apache.commons.httpclient; 33 34 import java.util.ArrayList ; 35 import org.apache.commons.httpclient.auth.HttpAuthenticator; 36 import org.apache.commons.httpclient.auth.AuthScheme; 37 import org.apache.commons.logging.Log; 38 import org.apache.commons.logging.LogFactory; 39 40 68 public class Authenticator { 69 70 72 75 private static final Log LOG = LogFactory.getLog(Authenticator.class); 76 77 80 public static final String WWW_AUTH = "WWW-Authenticate"; 81 82 83 86 public static final String WWW_AUTH_RESP = "Authorization"; 87 88 89 92 public static final String PROXY_AUTH = "Proxy-Authenticate"; 93 94 95 98 public static final String PROXY_AUTH_RESP = "Proxy-Authorization"; 99 100 101 103 118 public static boolean authenticate(HttpMethod method, HttpState state) 119 throws HttpException, UnsupportedOperationException { 120 121 LOG.trace("enter Authenticator.authenticate(HttpMethod, HttpState)"); 122 123 return authenticate(method, state, false); 124 } 125 126 127 142 public static boolean authenticateProxy(HttpMethod method, HttpState state) 143 throws HttpException, UnsupportedOperationException { 144 145 LOG.trace("enter Authenticator.authenticateProxy(HttpMethod, " 146 + "HttpState)"); 147 148 return authenticate(method, state, true); 149 } 150 151 152 171 private static boolean authenticate(HttpMethod method, HttpState state, 172 boolean proxy) 173 throws HttpException, UnsupportedOperationException { 174 175 LOG.trace("enter Authenticator.authenticate(HttpMethod, HttpState, " 176 + "Header, String)"); 177 return authenticate(method, null, state, proxy); 178 } 179 180 private static boolean authenticate(HttpMethod method, HttpConnection conn, 181 HttpState state, boolean proxy) 182 throws HttpException, UnsupportedOperationException { 183 String challengeheader = proxy ? PROXY_AUTH : WWW_AUTH; 184 185 Header[] headers = method.getResponseHeaders(); 189 ArrayList headerlist = new ArrayList (); 190 for (int i = 0; i < headers.length; i++) { 191 Header header = headers[i]; 192 if (header.getName().equalsIgnoreCase(challengeheader)) { 193 headerlist.add(header); 194 } 195 } 196 headers = (Header[]) headerlist.toArray(new Header[headerlist.size()]); 197 headerlist = null; 198 199 if (headers.length == 0) { 201 if (state.isAuthenticationPreemptive()) { 202 LOG.debug("Preemptively sending default basic credentials"); 203 if (proxy) { 204 return HttpAuthenticator.authenticateProxyDefault(method, conn, state); 205 } else { 206 return HttpAuthenticator.authenticateDefault(method, conn, state); 207 } 208 } 209 return false; 210 } 211 212 AuthScheme authscheme = HttpAuthenticator.selectAuthScheme(headers); 214 if (LOG.isDebugEnabled()) { 215 LOG.debug("Using " + authscheme.getSchemeName() + " authentication scheme"); 216 } 217 if (proxy) { 218 return HttpAuthenticator.authenticateProxy(authscheme, method, conn, state); 219 } else { 220 return HttpAuthenticator.authenticate(authscheme, method, conn, state); 221 } 222 223 } 224 } 225 | Popular Tags |