LOG.info("Fetching contacts from " + CONTACTS_URL);
return getContacts(CONTACTS_URL);
}
private List<Contact> getContacts(final String url) throws Exception {
Response serviceResponse;
try {
serviceResponse = authenticationStrategy.executeFeed(url);
} catch (Exception e) {
throw new SocialAuthException("Error while getting contacts from "
+ url, e);
}
if (serviceResponse.getStatus() != 200) {
throw new SocialAuthException("Error while getting contacts from "
+ url + "Status : " + serviceResponse.getStatus());
}
String result;
try {
result = serviceResponse
.getResponseBodyAsString(Constants.ENCODING);
} catch (Exception e) {
throw new ServerDataException("Failed to get response from " + url,
e);
}
LOG.debug("User Contacts list in JSON " + result);
JSONObject resp = new JSONObject(result);
List<Contact> plist = new ArrayList<Contact>();
if (resp.has("data")) {
JSONArray addArr = resp.getJSONArray("data");
LOG.debug("Contacts Found : " + addArr.length());
for (int i = 0; i < addArr.length(); i++) {
JSONObject obj = addArr.getJSONObject(i);
Contact p = new Contact();
if (obj.has("email_hashes")) {
JSONArray emailArr = obj.getJSONArray("email_hashes");
if (emailArr.length() > 0) {
p.setEmailHash(emailArr.getString(0));
}
}
if (obj.has("name")) {
p.setDisplayName(obj.getString("name"));
}
if (obj.has("first_name")) {
p.setFirstName(obj.getString("first_name"));
}
if (obj.has("last_name")) {
p.setLastName(obj.getString("last_name"));
}
if (obj.has("id")) {
p.setId(obj.getString("id"));
}
plist.add(p);
}
}
serviceResponse.close();
return plist;
}