private void validate() throws ValidationException {
if (environmentString != null) {
try {
withEnvironment(Environment.valueOf(environmentString.toUpperCase()));
} catch (IllegalArgumentException e) {
throw new ValidationException(String.format("Environment [%s] not recognized.",
environmentString), "api.dfa.environment", e);
}
}
// Check for valid username.
if (this.username == null) {
throw new ValidationException("Username is required.", "username");
}
boolean usingPassword = this.password != null;
boolean usingToken = this.authenticationToken != null;
boolean usingOAuth2 = this.oAuth2Credential != null;
// Check that at least one auth mechanism is being use.
if (!usingPassword && !usingToken && !usingOAuth2) {
throw new ValidationException(
"One of username/password, username/token or username/OAuth2 must be used.",
"");
}
// Check that only one auth mechanism is being used.
if ((usingPassword && usingToken) || (usingPassword && usingOAuth2) ||
(usingToken && usingOAuth2)) {
throw new ValidationException(
"You may only use one of username/password, username/token, and username/OAuth2 at " +
"the same time.",
"");
}
// Check that application name is not empty or the default.
if (Strings.isNullOrEmpty(applicationName)
|| applicationName.contains(DEFAULT_APPLICATION_NAME)) {
throw new ValidationException(String.format(
"Application name must be set and not be the default [%s]", DEFAULT_APPLICATION_NAME),
"applicationName");
}
// Make sure they specify an environment.
try {
new URL(this.endpoint);
} catch (MalformedURLException e) {
throw new ValidationException("Endpoint is required and must be a valid URL.",
"endpoint", e);
}
}