authenticationStrategy.logout();
}
private Profile getProfile() throws Exception {
Profile p = new Profile();
Response serviceResponse;
try {
serviceResponse = authenticationStrategy.executeFeed(PROFILE_URL);
} catch (Exception e) {
throw new SocialAuthException(
"Failed to retrieve the user profile from " + PROFILE_URL,
e);
}
String result;
try {
result = serviceResponse
.getResponseBodyAsString(Constants.ENCODING);
LOG.debug("User Profile :" + result);
} catch (Exception e) {
throw new SocialAuthException("Failed to read response from "
+ PROFILE_URL, e);
}
try {
JSONObject resp = new JSONObject(result);
if (resp.has("Id")) {
p.setValidatedId(resp.getString("Id"));
}
//Fix issue 145
if (resp.has("id")) {
p.setValidatedId(resp.getString("id"));
}
if (resp.has("name")) {
p.setFullName(resp.getString("name"));
}
if (resp.has("first_name")) {
p.setFirstName(resp.getString("first_name"));
}
if (resp.has("last_name")) {
p.setLastName(resp.getString("last_name"));
}
if (resp.has("Location")) {
p.setLocation(resp.getString("Location"));
}
if (resp.has("gender")) {
p.setGender(resp.getString("gender"));
}
if (resp.has("ThumbnailImageLink")) {
p.setProfileImageURL(resp.getString("ThumbnailImageLink"));
}
if (resp.has("birth_day") && !resp.isNull("birth_day")) {
BirthDate bd = new BirthDate();
bd.setDay(resp.getInt("birth_day"));
if (resp.has("birth_month") && !resp.isNull("birth_month")) {
bd.setMonth(resp.getInt("birth_month"));
}
if (resp.has("birth_year") && !resp.isNull("birth_year")) {
bd.setYear(resp.getInt("birth_year"));
}
p.setDob(bd);
}
if (resp.has("emails")) {
JSONObject eobj = resp.getJSONObject("emails");
String email = null;
if (eobj.has("preferred")) {
email = eobj.getString("preferred");
}
if ((email == null || email.isEmpty()) && eobj.has("account")) {
email = eobj.getString("account");
}
if ((email == null || email.isEmpty()) && eobj.has("personal")) {
email = eobj.getString("personal");
}
p.setEmail(email);
}
if (resp.has("locale")) {
p.setLanguage(resp.getString("locale"));
}
serviceResponse.close();
p.setProviderId(getProviderId());
String picUrl = String.format(PROFILE_PICTURE_URL,
accessGrant.getKey());
p.setProfileImageURL(picUrl);
userProfile = p;