*/
@SuppressWarnings({"unchecked", "unused"})
@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())) {
// Use an HttpClient to fetch a profile information page from ICQ.
HttpClient httpClient = HttpClientFactory.getHttpClientInstance();
HttpClientParams httpClientParams = httpClient.getParams();
httpClientParams.setConnectionManagerTimeout(2500);
httpClient.setParams(httpClientParams);
HttpMethod httpMethod = new GetMethod(ICQ_NAME_VALIDATION_URL);
NameValuePair uinParam = new NameValuePair(ICQ_NAME_URL_PARAMETER, textElement.getValue());
httpMethod.setQueryString(new NameValuePair[] {uinParam});
// Don't allow redirects since otherwise, we won't be able to get the HTTP 302 further down.
httpMethod.setFollowRedirects(false);
try {
// 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 ICQ name exists.
if (httpStatusCode == HttpStatus.SC_OK) {
// ICQ tells us that a user name is valid if it sends an HTTP 200...
result = true;
} else if (httpStatusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
// ...and if it's invalid, it sends an HTTP 302.
textElement.setErrorKey("form.name.icq.error", null);
result = false;
} else {
// For HTTP status codes other than 200 and 302 we will silently assume that the given ICQ name is valid, but inform the user about this.
textElement.setExampleKey("form.example.icqname.notvalidated", null);
log.warn("ICQ name validation: Expected HTTP status 200 or 301, but got " + httpStatusCode);
result = true;
}
} catch (Exception e) {
// In case of any exception, assume that the given ICQ name is valid (The opposite would block easily upon network problems), and inform the user about this.
textElement.setExampleKey("form.example.icqname.notvalidated", null);
log.warn("ICQ name validation: Exception: " + e.getMessage());
result = true;
}
} else {
result = true;