* The header value to parse.
* @return The parsed challenge response.
*/
public static ChallengeResponse parseResponse(Request request,
Logger logger, String header) {
ChallengeResponse result = null;
if (header != null) {
int space = header.indexOf(' ');
if (space != -1) {
String scheme = header.substring(0, space);
String credentials = header.substring(space + 1);
result = new ChallengeResponse(new ChallengeScheme("HTTP_"
+ scheme, scheme), credentials);
if (result.getScheme().equals(ChallengeScheme.HTTP_BASIC)) {
try {
credentials = new String(Base64.decode(result
.getCredentials()), "US-ASCII");
int separator = credentials.indexOf(':');
if (separator == -1) {
// Log the blocking
logger
.warning("Invalid credentials given by client with IP: "
+ ((request != null) ? request
.getClientInfo()
.getAddress() : "?"));
} else {
result.setIdentifier(credentials.substring(0,
separator));
result.setSecret(credentials
.substring(separator + 1));
// Log the authentication result
if (logger != null) {
logger
.info("Basic HTTP authentication succeeded: identifier="
+ result.getIdentifier() + ".");
}
}
} catch (UnsupportedEncodingException e) {
logger.log(Level.WARNING, "Unsupported encoding error",
e);
}
} else {
// Authentication failed, scheme not supported
logger
.log(
Level.WARNING,
"Authentication failed: unsupported scheme used: "
+ result.getScheme().getName()
+ ". Please override the authenticate method.");
}
}
}