rwaldhoff 01/08/08 12:01:18
Modified: httpclient/src/java/org/apache/commons/httpclient
Authenticator.java HttpClient.java
httpclient/src/test/org/apache/commons/httpclient
TestAuthenticator.java
Log:
midpoint in trying to refactor the way in which the authentication challenge is
passed to Authenticator
Revision Changes Path
1.5 +9 -11
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Authenticator.java
Index: Authenticator.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Authenticator.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- Authenticator.java 2001/08/08 16:51:05 1.4
+++ Authenticator.java 2001/08/08 19:01:18 1.5
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Authenticator.java,v
1.4 2001/08/08 16:51:05 rwaldhoff Exp $
- * $Revision: 1.4 $
- * $Date: 2001/08/08 16:51:05 $
+ * $Header:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Authenticator.java,v
1.5 2001/08/08 19:01:18 rwaldhoff Exp $
+ * $Revision: 1.5 $
+ * $Date: 2001/08/08 19:01:18 $
*
* ====================================================================
*
@@ -94,7 +94,7 @@
* @param state State
* @param credentials Credentials to use to answser the challenge
* @return String response to the challenge
- * @deprecated
+ * @deprecated Use {@link
challengeResponse(java.lang.String,org.apache.commons.httpclient.State)}
*/
public static String challengeResponse(State state,
Credentials credentials)
@@ -125,12 +125,10 @@
}
- public static String challengeResponse(State state) throws HttpException {
- log.debug("Authenticator.challengeResponse(State)");
- String challenge = state.getAuthenticateToken();
- if (challenge == null) {
- return null;
- }
+ public static String challengeResponse(String challenge, State state) throws
HttpException {
+ log.debug("Authenticator.challengeResponse(String, State)");
+
+ if(challenge == null) { return null; }
int space = challenge.indexOf(' ');
if(space < 0) {
@@ -166,7 +164,7 @@
* Generate a basic response.
*
* @param credentials Credentials to use to answser the challenge
- * @deprecated
+ * @deprecated Use {@link
basic(java.lang.String,org.apache.commons.httpclient.State)}
*/
public static String basic(State state, Credentials credentials) {
log.debug("Authenticator.basic(State,Credentials)");
1.28 +22 -10
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpClient.java
Index: HttpClient.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpClient.java,v
retrieving revision 1.27
retrieving revision 1.28
diff -u -r1.27 -r1.28
--- HttpClient.java 2001/08/08 17:45:23 1.27
+++ HttpClient.java 2001/08/08 19:01:18 1.28
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpClient.java,v
1.27 2001/08/08 17:45:23 rwaldhoff Exp $
- * $Revision: 1.27 $
- * $Date: 2001/08/08 17:45:23 $
+ * $Header:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpClient.java,v
1.28 2001/08/08 19:01:18 rwaldhoff Exp $
+ * $Revision: 1.28 $
+ * $Date: 2001/08/08 19:01:18 $
*
* ====================================================================
*
@@ -427,6 +427,8 @@
boolean methodProcessed = false;
boolean sentRequestBody = false;
+ String wwwAuthenticateValue = null;
+
openConnection();
while ((retries < 5) && (!methodProcessed)) {
@@ -510,8 +512,11 @@
Header authenticateChallenge =
(Header) responseHeaders.get("www-authenticate");
if (authenticateChallenge != null) {
- state.setAuthenticateToken
- (authenticateChallenge.getValue());
+ wwwAuthenticateValue = authenticateChallenge.getValue();
+ // XXX FIX ME XXX
+ // need to refactor some other stuff before removing
+ // this way of passing the wwwAuthenticateValue
+ state.setAuthenticateToken(wwwAuthenticateValue);
if (connectionInterceptor != null) {
connectionInterceptor.requiredAuthentication();
}
@@ -858,7 +863,7 @@
*/
protected byte[] sendRequestHeader(HttpMethod method)
throws IOException, HttpException {
-
+ log.debug("HttpClient.sendRequestHeader(HttpMethod)");
if (method.hasBeenUsed())
throw new HttpException("Method has already been used");
@@ -906,9 +911,17 @@
}
}
- if (state.getAuthenticateToken() != null) {
-
- String challengeResponse = Authenticator.challengeResponse(state);
+ // XXX FIX ME XXX
+ // need to refactor some other stuff before removing
+ // this way of passing the wwwAuthenticateValue
+ // if (null != wwwAuthenticateValue) {
+ if (null != state.getAuthenticateToken()) {
+ // XXX FIX ME XXX
+ // need to refactor some other stuff before removing
+ // this way of passing the wwwAuthenticateValue
+ // if (null != wwwAuthenticateValue) {
+ // String challengeResponse =
Authenticator.challengeResponse(wwwAuthenticateValue,state);
+ String challengeResponse =
Authenticator.challengeResponse(state.getAuthenticateToken(),state);
if (challengeResponse != null) {
String authorizationHeader = "Authorization: " + challengeResponse
+ "\r\n";
wireLog.info(">> \"" + authorizationHeader + "\"");
@@ -917,7 +930,6 @@
connectionInterceptor.authenticate();
}
}
-
}
// Send expectation header
1.4 +12 -18
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestAuthenticator.java
Index: TestAuthenticator.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestAuthenticator.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- TestAuthenticator.java 2001/08/08 16:51:05 1.3
+++ TestAuthenticator.java 2001/08/08 19:01:18 1.4
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestAuthenticator.java,v
1.3 2001/08/08 16:51:05 rwaldhoff Exp $
- * $Revision: 1.3 $
- * $Date: 2001/08/08 16:51:05 $
+ * $Header:
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestAuthenticator.java,v
1.4 2001/08/08 19:01:18 rwaldhoff Exp $
+ * $Revision: 1.4 $
+ * $Date: 2001/08/08 19:01:18 $
* ====================================================================
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
@@ -18,7 +18,7 @@
* Unit tests for {@link Authenticator}.
*
* @author Rodney Waldhoff
- * @version $Id: TestAuthenticator.java,v 1.3 2001/08/08 16:51:05 rwaldhoff Exp $
+ * @version $Id: TestAuthenticator.java,v 1.4 2001/08/08 19:01:18 rwaldhoff Exp $
*/
public class TestAuthenticator extends TestCase {
@@ -43,9 +43,8 @@
public void testBasicAuthenticationWithNoCreds() {
State state = new State();
- state.setAuthenticateToken("Basic realm=\"realm1\"");
try {
- Authenticator.challengeResponse(state);
+ Authenticator.challengeResponse("Basic realm=\"realm1\"",state);
fail("Should have thrown HttpException");
} catch(HttpException e) {
// expected
@@ -54,9 +53,8 @@
public void testBasicAuthenticationWithNoRealm() {
State state = new State();
- state.setAuthenticateToken("Basic");
try {
- Authenticator.challengeResponse(state);
+ Authenticator.challengeResponse("Basic",state);
fail("Should have thrown HttpException");
} catch(HttpException e) {
// expected
@@ -65,12 +63,12 @@
public void testBasicAuthenticationWithNoChallenge() throws Exception {
State state = new State();
- assert(null == Authenticator.challengeResponse(state));
+ assert(null == Authenticator.challengeResponse((String)null,state));
}
public void testBasicAuthenticationWithNullState() throws Exception {
try {
- Authenticator.challengeResponse(null);
+ Authenticator.challengeResponse("Basic realm=\"realm1\"",(State)null);
fail("Should have thrown NullPointerException");
} catch(NullPointerException e) {
// expected
@@ -79,18 +77,16 @@
public void testBasicAuthenticationWithDefaultCreds() throws Exception {
State state = new State();
- state.setAuthenticateToken("Basic realm=\"realm1\"");
state.setDefaultCredentials(new Credentials("username","password"));
- String response = Authenticator.challengeResponse(state);
+ String response = Authenticator.challengeResponse("Basic
realm=\"realm1\"",state);
String expected = "Basic " + new
String(Base64.encode("username:password".getBytes()));
assertEquals(expected,response);
}
public void testBasicAuthentication() throws Exception {
State state = new State();
- state.setAuthenticateToken("Basic realm=\"realm1\"");
state.setCredentials("realm1",new Credentials("username","password"));
- String response = Authenticator.challengeResponse(state);
+ String response = Authenticator.challengeResponse("Basic
realm=\"realm1\"",state);
String expected = "Basic " + new
String(Base64.encode("username:password".getBytes()));
assertEquals(expected,response);
}
@@ -100,14 +96,12 @@
state.setCredentials("realm1",new Credentials("username","password"));
state.setCredentials("realm2",new Credentials("uname2","password2"));
{
- state.setAuthenticateToken("Basic realm=\"realm1\"");
- String response = Authenticator.challengeResponse(state);
+ String response = Authenticator.challengeResponse("Basic
realm=\"realm1\"",state);
String expected = "Basic " + new
String(Base64.encode("username:password".getBytes()));
assertEquals(expected,response);
}
{
- state.setAuthenticateToken("Basic realm=\"realm2\"");
- String response = Authenticator.challengeResponse(state);
+ String response = Authenticator.challengeResponse("Basic
realm=\"realm2\"",state);
String expected = "Basic " + new
String(Base64.encode("uname2:password2".getBytes()));
assertEquals(expected,response);
}