@SuppressWarnings({"unused", "unchecked"})
@Override
public boolean isValid(FormItem formItem, Map formContext) {
boolean result;
TextElement textElement = (TextElement)formItem;
OLog log = Tracing.createLoggerFor(this.getClass());
if (StringHelper.containsNonWhitespace(textElement.getValue())) {
HttpClient httpClient = HttpClientFactory.getHttpClientInstance();
HttpClientParams httpClientParams = httpClient.getParams();
httpClientParams.setConnectionManagerTimeout(2500);
httpClient.setParams(httpClientParams);
try {
// Could throw IllegalArgumentException if argument is not a valid url
// (e.g. contains whitespaces)
HttpMethod httpMethod = new GetMethod(XING_NAME_VALIDATION_URL + textElement.getValue());
// Don't allow redirects since otherwise, we won't be able to get the correct status
httpMethod.setFollowRedirects(false);
// Get the user profile page
httpClient.executeMethod(httpMethod);
int httpStatusCode = httpMethod.getStatusCode();
// Looking at the HTTP status code tells us whether a user with the given Xing name exists.
if (httpStatusCode == HttpStatus.SC_OK) {
// If the user exists, we get a 200...
result = true;
} else if (httpStatusCode == HttpStatus.SC_MOVED_PERMANENTLY) {
// ... and if he doesn't exist, we get a 301.
textElement.setErrorKey("form.name.xing.error", null);
result = false;
} else {
// In case of any exception, assume that the given MSN name is valid (The opposite would block easily upon network problems), and inform the user about this.
textElement.setExampleKey("form.example.xingname.notvalidated", null);
log.warn("Xing name validation: Expected HTTP status 200 or 301, but got " + httpStatusCode);
result = true;
}
} catch (IllegalArgumentException e) {
// The xing name is not url compatible (e.g. contains whitespaces)
textElement.setErrorKey("form.xingname.notvalid", null);
result = false;
} catch (Exception e) {
// In case of any exception, assume that the given MSN name is valid (The opposite would block easily upon network problems), and inform the user about this.
textElement.setExampleKey("form.example.xingname.notvalidated", null);
log.warn("Xing name validation: Exception: " + e.getMessage());
result = true;
}
} else {
result = true;
}