authenticationStrategy.logout();
}
private Profile getProfile() throws Exception {
LOG.debug("Obtaining user profile");
Profile profile = new Profile();
Response serviceResponse = null;
try {
serviceResponse = authenticationStrategy.executeFeed(PROFILE_URL
+ authenticationStrategy.getAccessGrant().getKey());
} catch (Exception e) {
throw new SocialAuthException(
"Failed to retrieve the user profile from " + PROFILE_URL,
e);
}
if (serviceResponse.getStatus() != 200) {
throw new SocialAuthException(
"Failed to retrieve the user profile from " + PROFILE_URL
+ ". Staus :" + serviceResponse.getStatus());
}
Element root;
try {
root = XMLParseUtil.loadXmlResource(serviceResponse
.getInputStream());
} catch (Exception e) {
throw new ServerDataException(
"Failed to parse the profile from response." + PROFILE_URL,
e);
}
if (root != null) {
String fname = XMLParseUtil.getElementData(root, "first-name");
String lname = XMLParseUtil.getElementData(root, "last-name");
NodeList dob = root.getElementsByTagName("date-of-birth");
if (dob != null && dob.getLength() > 0) {
Element dobel = (Element) dob.item(0);
if (dobel != null) {
String y = XMLParseUtil.getElementData(dobel, "year");
String m = XMLParseUtil.getElementData(dobel, "month");
String d = XMLParseUtil.getElementData(dobel, "day");
BirthDate bd = new BirthDate();
if (m != null) {
bd.setMonth(Integer.parseInt(m));
}
if (d != null) {
bd.setDay(Integer.parseInt(d));
}
if (y != null) {
bd.setYear(Integer.parseInt(y));
}
profile.setDob(bd);
}
}
String picUrl = XMLParseUtil.getElementData(root, "picture-url");
String id = XMLParseUtil.getElementData(root, "id");
if (picUrl != null) {
profile.setProfileImageURL(picUrl);
}
String email = XMLParseUtil.getElementData(root, "email-address");
if (email != null) {
profile.setEmail(email);
}
NodeList location = root.getElementsByTagName("location");
if (location != null && location.getLength() > 0) {
Element locationEl = (Element) location.item(0);
String loc = XMLParseUtil.getElementData(locationEl, "name");
if (loc != null) {
profile.setLocation(loc);
}
}
Map<String, String> map = new HashMap<String, String>();
NodeList phoneNodes = root.getElementsByTagName("phone-number");
if (phoneNodes != null && phoneNodes.getLength() > 0) {
Element phoneEl = (Element) phoneNodes.item(0);
String type = XMLParseUtil
.getElementData(phoneEl, "phone-type");
String phone = XMLParseUtil.getElementData(phoneEl,
"phone-number");
if (type != null && type.length() > 0 && phone != null) {
map.put(type, phone);
}
}
String mainAddress = XMLParseUtil.getElementData(root,
"main-address");
if (mainAddress != null) {
map.put("mainAddress", mainAddress);
}
if (map != null && !map.isEmpty()) {
profile.setContactInfo(map);
}
profile.setFirstName(fname);
profile.setLastName(lname);
profile.setValidatedId(id);
profile.setProviderId(getProviderId());
LOG.debug("User Profile :" + profile.toString());
userProfile = profile;
}
return profile;
}