package org.apache.commons.vfs2.auth;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.vfs2.UserAuthenticationData;
import org.apache.commons.vfs2.UserAuthenticator;
import org.apache.commons.vfs2.util.UserAuthenticatorUtils;
public class StaticUserAuthenticator implements UserAuthenticator, Comparable<StaticUserAuthenticator> {
private static final Log LOG = LogFactory.getLog(StaticUserAuthenticator.class);
private final String username;
private final String password;
private final String domain;
public StaticUserAuthenticator(final String domain, final String username, final String password) {
this.username = username;
this.password = password;
this.domain = domain;
}
@Override
public UserAuthenticationData requestAuthentication(final UserAuthenticationData.Type[] types) {
final UserAuthenticationData data = new UserAuthenticationData();
for (final UserAuthenticationData.Type type : types) {
if (type == UserAuthenticationData.DOMAIN) {
data.setData(UserAuthenticationData.DOMAIN, UserAuthenticatorUtils.toChar(domain));
} else if (type == UserAuthenticationData.USERNAME) {
data.setData(UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(username));
} else if (type == UserAuthenticationData.PASSWORD) {
data.setData(UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(password));
} else {
if (LOG.isDebugEnabled()) {
LOG.debug(StaticUserAuthenticator.class.getSimpleName()
+ " does not support authentication data type '" + type
+ "'; authentication request for this type ignored.");
}
}
}
return data;
}
@Override
public int hashCode() {
final int prime = 37;
int result = 1;
result = prime * result + (domain == null ? 0 : domain.hashCode());
result = prime * result + (password == null ? 0 : password.hashCode());
result = prime * result + (username == null ? 0 : username.hashCode());
return result;
}
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final StaticUserAuthenticator other = (StaticUserAuthenticator) obj;
return equalsNullsafe(domain, other.domain) && equalsNullsafe(username, other.username)
&& equalsNullsafe(password, other.password);
}
private boolean equalsNullsafe(final String thisString, final String otherString) {
if (thisString == null) {
if (otherString != null) {
return false;
}
} else if (!thisString.equals(otherString)) {
return false;
}
return true;
}
@Override
public int compareTo(final StaticUserAuthenticator other) {
int result = compareStringOrNull(domain, other.domain);
result = result == 0 ? compareStringOrNull(username, other.username) : result;
result = result == 0 ? compareStringOrNull(password, other.password) : result;
return result;
}
private int compareStringOrNull(final String thisString, final String otherString) {
if (thisString == null) {
if (otherString != null) {
return -1;
}
} else {
if (otherString == null) {
return 1;
}
final int result = thisString.compareTo(otherString);
if (result != 0) {
return result;
}
}
return 0;
}
@Override
public String toString() {
final StringBuilder buffer = new StringBuilder();
if (domain != null) {
buffer.append(domain).append('\\');
}
if (username != null) {
buffer.append(username);
} else {
buffer.append("(null)");
}
if (password != null) {
buffer.append(":***");
}
return buffer.toString();
}
}