/* * Method responsible for generating appropriate REST URLs * * @param ProfileEntity ( Ref Class : ProfileEntity ) * @param ProfileType ( Ref Class : ProfileType ) * @param params : ( Ref Class : ProfileParams ) * @return String */ protected String resolveProfileUrl( String profileEntity, String profileType, Map<String, String> params) { if (logger.isLoggable(Level.FINEST)) { logger.entering(sourceClass, "resolveCommunityUrl", profileEntity + profileType); } StringBuilder proBaseUrl = new StringBuilder(ProfileBaseUrl); if (StringUtil.isEmpty(profileEntity)) { profileEntity = ProfileEntity.NONADMIN.getProfileEntityType(); // Default // Entity // Type } if (StringUtil.isEmpty(profileType)) { profileType = ProfileType.GETPROFILE.getProfileType(); // Default // Profile // Type } if (AuthUtil.INSTANCE.getAuthValue(endpoint).equalsIgnoreCase("oauth")) { if (profileEntity.equalsIgnoreCase(ProfileEntity.NONADMIN.getProfileEntityType())) { proBaseUrl.append(seperator).append("oauth"); } } if (profileEntity.equalsIgnoreCase("")) { // if it is non admin API then // no need to append anythin proBaseUrl.append(seperator).append(profileType); } else { proBaseUrl.append(profileEntity).append(seperator).append(profileType); } // Add required parameters if (null != params) { if (params.size() > 0) { proBaseUrl.append("?"); boolean setSeperator = false; for (Map.Entry<String, String> param : params.entrySet()) { if (setSeperator) { proBaseUrl.append("&"); } String paramvalue = ""; try { paramvalue = URLEncoder.encode(param.getValue(), "UTF-8"); } catch (UnsupportedEncodingException e) { } proBaseUrl.append(param.getKey() + "=" + paramvalue); setSeperator = true; } } } if (logger.isLoggable(Level.FINEST)) { logger.log(Level.FINEST, Messages.ProfileInfo_7 + proBaseUrl.toString()); } return proBaseUrl.toString(); }
/** * Wrapper method to get a list of pending invites for a user * * @param parameters list of query string parameters to pass to API * @return Collection<ConnectionEntry> * @throws ProfileServiceException */ private Collection<ConnectionEntry> getMyInvites(Map<String, String> parameters) throws ProfileServiceException { if (logger.isLoggable(Level.FINEST)) { logger.entering(sourceClass, "getMyInvites", parameters); } Document data = null; Collection<ConnectionEntry> invites = null; String url = resolveProfileUrl( ProfileEntity.NONADMIN.getProfileEntityType(), ProfileType.CONNECTIONS.getProfileType()); data = executeGet(url, parameters, ClientService.FORMAT_XML); if (parameters.containsKey("outputType")) { if (parameters.get("outputType").equalsIgnoreCase("profile")) { invites = Converter.returnConnectionEntries(data, "profile"); } else invites = Converter.returnConnectionEntries(data, "connection"); } else { invites = Converter.returnConnectionEntries(data, "connection"); } if (logger.isLoggable(Level.FINEST)) { logger.exiting(sourceClass, "getMyInvites"); } return invites; }
/** * Wrapper method to update a User's profile * * @param Profile * @return returns true if profile is updated successfully * @throws ProfileServiceException */ public boolean updateProfile(Profile profile) throws ProfileServiceException { if (logger.isLoggable(Level.FINEST)) { logger.entering(sourceClass, "update", profile); } if (profile == null) { throw new IllegalArgumentException(Messages.InvalidArgument_3); } boolean result = true; Map<String, String> parameters = new HashMap<String, String>(); parameters.put(ProfileRequestParams.OUTPUT, "vcard"); parameters.put(ProfileRequestParams.FORMAT, "full"); Map<String, String> headers = new HashMap<String, String>(); headers.put(Headers.ContentType, Headers.ATOM); if (isEmail(profile.getReqId())) { parameters.put(ProfileRequestParams.EMAIL, profile.getReqId()); } else { parameters.put(ProfileRequestParams.USERID, profile.getReqId()); } Object updateProfilePayload = profile.constructUpdateRequestBody(); String url = resolveProfileUrl( ProfileEntity.NONADMIN.getProfileEntityType(), ProfileType.UPDATEPROFILE.getProfileType()); result = executePut(url, parameters, headers, updateProfilePayload, ClientService.FORMAT_NULL); profile.clearFieldsMap(); removeProfileDataFromCache(profile.getReqId()); if (logger.isLoggable(Level.FINEST)) { logger.exiting(sourceClass, "update"); } return result; }
/** * Wrapper method to send Invite to a user to become colleague * * @param profile profile of the user to whom the invite is to be sent * @param inviteMsg Invite message to the other user * @return value is true if invite is sent successfully else value is false * @throws ProfileServiceException */ public boolean sendInvite(Profile profile, String inviteMsg) throws ProfileServiceException { if (logger.isLoggable(Level.FINEST)) { logger.entering(sourceClass, "getColleagues", inviteMsg); } if (profile == null) { throw new IllegalArgumentException(StringUtil.format("A null profile was passed")); } Map<String, String> parameters = new HashMap<String, String>(); String url = resolveProfileUrl( ProfileEntity.NONADMIN.getProfileEntityType(), ProfileType.CONNECTIONS.getProfileType()); if (isEmail(profile.getReqId())) { parameters.put(ProfileRequestParams.EMAIL, profile.getReqId()); } else { parameters.put(ProfileRequestParams.USERID, profile.getReqId()); } parameters.put("connectionType", "colleague"); XMLProfilesPayloadBuilder builder = XMLProfilesPayloadBuilder.INSTANCE; Object content = builder.generateInviteRequestPayload(inviteMsg); // getClientService().post(url, parameters, content); boolean result = executePost(url, parameters, null, content, null); if (logger.isLoggable(Level.FINEST)) { logger.exiting(sourceClass, "getColleagues"); } return result; }
/** * Wrapper method to search profiles based on different parameters * * @param parameters list of query string parameters to pass to API * @return Collection<Profile> */ public Collection<Profile> searchProfiles(Map<String, String> parameters) { if (logger.isLoggable(Level.FINEST)) { logger.entering(sourceClass, "searchProfiles", parameters); } Document data = null; Collection<Profile> profiles = null; if (null == parameters) { parameters = new HashMap<String, String>(); } try { String url = resolveProfileUrl( ProfileEntity.NONADMIN.getProfileEntityType(), ProfileType.SEARCH.getProfileType()); data = (Document) getClientService().get(url, parameters); profiles = Converter.returnProfileEntries(this, data); } catch (ClientServicesException e) { if (logger.isLoggable(Level.SEVERE)) { logger.log(Level.SEVERE, Messages.ProfileServiceException_1 + "searchProfiles()", e); } } if (logger.isLoggable(Level.FINEST)) { logger.exiting(sourceClass, "getColleagues"); } return profiles; }
/** * Wrapper method is used to delete/ignore a Invite * * @param connectionId unique id of the connection * @return returns true if invite is deleted successfully * @throws ProfileServiceException */ public boolean deleteInvite(String connectionId) throws ProfileServiceException { if (logger.isLoggable(Level.FINEST)) { logger.entering(sourceClass, "deleteInvite", connectionId); } Map<String, String> parameters = new HashMap<String, String>(); parameters.put(ProfileRequestParams.CONNECTIONID, connectionId); String url = resolveProfileUrl( ProfileEntity.NONADMIN.getProfileEntityType(), ProfileType.CONNECTION.getProfileType()); boolean result = executeDelete(url, parameters); if (logger.isLoggable(Level.FINEST)) { logger.exiting(sourceClass, "deleteInvite"); } return result; }
/** * Wrapper method to get common colleagues of two users * * @param sourceId userid or email of first user * @param targetId userid or email of second user * @return Collection<ConnectionEntry> * @throws ProfileServiceException */ private Collection<ConnectionEntry> getColleaguesInCommon( String sourceId, String targetId, Map<String, String> parameters) throws ProfileServiceException { if (logger.isLoggable(Level.FINEST)) { logger.entering(sourceClass, "checkColleague"); } if (StringUtil.isEmpty(sourceId)) { throw new IllegalArgumentException(Messages.InvalidArgument_4); } if (StringUtil.isEmpty(targetId)) { throw new IllegalArgumentException(Messages.InvalidArgument_5); } Document data = null; if (parameters == null) { parameters = new HashMap<String, String>(); } String url = resolveProfileUrl( ProfileEntity.NONADMIN.getProfileEntityType(), ProfileType.CONNECTIONS_IN_COMMON.getProfileType()); if (isEmail(sourceId)) { StringBuilder value = new StringBuilder(sourceId); value = value.append(",").append(targetId); parameters.put(ProfileRequestParams.EMAIL, value.toString()); } else { StringBuilder value = new StringBuilder(sourceId); value = value.append(",").append(targetId); parameters.put(ProfileRequestParams.USERID, value.toString()); } parameters.put("connectionType", "colleague"); data = executeGet(url, parameters, ClientService.FORMAT_XML); Collection<ConnectionEntry> colleaguesInCommon = Converter.returnConnectionEntries(data, "connectionEntry"); if (logger.isLoggable(Level.FINEST)) { logger.exiting(sourceClass, "getColleagues"); } return colleaguesInCommon; }
/** * Wrapper method to accept a Invite * * @param connectionId unique id of the connection * @param title message to the other user * @param content message to the other user * @return if invite is accepted then return true * @throws ProfileServiceException */ public boolean acceptInvite(String connectionId, String title, String content) throws ProfileServiceException { if (logger.isLoggable(Level.FINEST)) { logger.entering(sourceClass, "acceptInvite", connectionId); } Map<String, String> parameters = new HashMap<String, String>(); parameters.put(ProfileRequestParams.CONNECTIONID, connectionId); String url = resolveProfileUrl( ProfileEntity.NONADMIN.getProfileEntityType(), ProfileType.CONNECTION.getProfileType()); XMLProfilesPayloadBuilder builder = XMLProfilesPayloadBuilder.INSTANCE; Object payload = builder.generateAcceptInvitePayload(connectionId, title, content); boolean result = executePut(url, parameters, null, payload, null); if (logger.isLoggable(Level.FINEST)) { logger.exiting(sourceClass, "acceptInvite"); } return result; }
/** * Wrapper method to get a user's report to chain * * @param id unique identifier of the user whose direct reports are needed, it can be email or * userID * @param parameters list of query string parameters to pass to API * @return List of Profiles * @throws ProfileServiceException */ public Collection<Profile> getReportToChain(String id, Map<String, String> parameters) throws ProfileServiceException { if (logger.isLoggable(Level.FINEST)) { logger.entering(sourceClass, "getReportToChain", parameters); } if (parameters == null) parameters = new HashMap<String, String>(); String url = resolveProfileUrl( ProfileEntity.NONADMIN.getProfileEntityType(), ProfileType.REPORTINGCHAIN.getProfileType()); Document data = executeGet(url, parameters, ClientService.FORMAT_XML); Collection<Profile> reportingChain = Converter.returnProfileEntries(this, data); if (logger.isLoggable(Level.FINEST)) { logger.exiting(sourceClass, "getReportToChain"); } return reportingChain; }
/** * Wrapper method to get check if two users are colleagues * * @param sourceId userid or email of first user * @param targetId userid or email of second user * @param parameters list of query string parameters to pass to API * @return ConnectionEntry * @throws ProfileServiceException */ private ConnectionEntry checkColleague( String sourceId, String targetId, Map<String, String> parameters) throws ProfileServiceException { if (logger.isLoggable(Level.FINEST)) { logger.entering(sourceClass, "checkColleague"); } if (StringUtil.isEmpty(sourceId)) { throw new IllegalArgumentException(Messages.InvalidArgument_4); } if (StringUtil.isEmpty(targetId)) { throw new IllegalArgumentException(Messages.InvalidArgument_5); } Document data = null; if (parameters == null) { parameters = new HashMap<String, String>(); } String url = resolveProfileUrl( ProfileEntity.NONADMIN.getProfileEntityType(), ProfileType.CONNECTION.getProfileType()); if (isEmail(sourceId)) { parameters.put(ProfileRequestParams.SOURCEEMAIL, sourceId); } else { parameters.put(ProfileRequestParams.SOURCEUSERID, sourceId); } if (isEmail(targetId)) { parameters.put(ProfileRequestParams.TARGETEMAIL, targetId); } else { parameters.put(ProfileRequestParams.TARGETUSERID, targetId); } parameters.put("connectionType", "colleague"); data = executeGet(url, parameters, ClientService.FORMAT_XML); ConnectionEntry connection = Converter.returnConnectionEntries(data, "connectionEntry").iterator().next(); if (logger.isLoggable(Level.FINEST)) { logger.exiting(sourceClass, "getColleagues"); } return connection; }
/* * Method responsible for loading the profile. * * @param profile */ protected void load(Profile profile) throws ProfileServiceException { if (logger.isLoggable(Level.FINEST)) { logger.entering(sourceClass, "load", profile); } // Do a cache lookup first. If cache miss, make a network call to get // Profile Document data = getProfileDataFromCache(profile.getReqId()); if (data != null) { profile.setData(data); } else { Map<String, String> parameters = new HashMap<String, String>(); if (isEmail(profile.getReqId())) { parameters.put("email", profile.getReqId()); } else { parameters.put("userid", profile.getReqId()); } String url = resolveProfileUrl( ProfileEntity.NONADMIN.getProfileEntityType(), ProfileType.GETPROFILE.getProfileType()); Object result = executeGet(url, parameters, ClientService.FORMAT_XML); if (result != null) { profile.setData((Document) result); addProfileDataToCache(profile.getUniqueId(), (Document) result); } else { profile.setData(null); } if (logger.isLoggable(Level.FINEST)) { logger.exiting(sourceClass, "load"); } } }
/** * Wrapper method to get user's colleagues * * @param profile profile of the user whose contacts are to be returned * @param parameters list of query string parameters to pass to API * @return Profiles of User's colleagues * @throws ProfileServiceException */ public Profile[] getColleagues(Profile profile, Map<String, String> parameters) throws ProfileServiceException { if (logger.isLoggable(Level.FINEST)) { logger.entering(sourceClass, "getColleagues", parameters); } if (profile == null) { throw new IllegalArgumentException(StringUtil.format("A null profile was passed")); } Document data = null; Profile[] colleagues = null; if (parameters == null) parameters = new HashMap<String, String>(); String url = resolveProfileUrl( ProfileEntity.NONADMIN.getProfileEntityType(), ProfileType.CONNECTIONS.getProfileType()); if (isEmail(profile.getReqId())) { parameters.put(ProfileRequestParams.EMAIL, profile.getReqId()); } else { parameters.put(ProfileRequestParams.USERID, profile.getReqId()); } if (parameters.get("connectionType") != null) // this is to remove any other values put in by sample user in following parameters.remove("connectionType"); // mandatory parameters if (parameters.get("outputType") != null) parameters.remove("outputType"); parameters.put("connectionType", "colleague"); parameters.put("outputType", "profile"); data = executeGet(url, parameters, ClientService.FORMAT_XML); colleagues = Converter.convertToProfiles(this, data); if (logger.isLoggable(Level.FINEST)) { logger.exiting(sourceClass, "getColleagues"); } return colleagues; }
/** * Wrapper method to update a User's profile photo * * @param Profile * @return returns true, if profile photo is updated * @throws ProfileServiceException */ public boolean updateProfilePhoto(Profile profile) throws ProfileServiceException { if (logger.isLoggable(Level.FINEST)) { logger.entering(sourceClass, "updatePhoto", profile.toString()); } if (profile == null) { throw new IllegalArgumentException(Messages.InvalidArgument_3); } boolean returnVal = false; Map<String, String> parameters = new HashMap<String, String>(); if (isEmail(profile.getReqId())) { parameters.put(ProfileRequestParams.EMAIL, profile.getReqId()); } else { parameters.put(ProfileRequestParams.USERID, profile.getReqId()); } String filePath = profile.getFieldsMap().get("imageLocation"); if (!StringUtil.isEmpty(filePath)) { File file; try { file = new File(filePath); } catch (Exception e) { logger.log(Level.SEVERE, Messages.ProfileInfo_9 + "updateProfilePhoto()", e); throw new ProfileServiceException(e); } String name = filePath.substring(filePath.lastIndexOf('\\') + 1); int dot = name.lastIndexOf('.'); String ext = null; if (dot > -1) { ext = name.substring(dot + 1); // add one for the dot! } if (StringUtil.isEmpty(ext)) { try { throw new Exception(Messages.UpdateProfileInfo_1); } catch (Exception e) { returnVal = false; } } Map<String, String> headers = new HashMap<String, String>(); if (ext.equalsIgnoreCase("jpg")) { headers.put( Headers.ContentType, "image/jpeg"); // content-type should be image/jpeg for file extension - jpeg/jpg } else { headers.put(Headers.ContentType, "image/" + ext); } String url = resolveProfileUrl( ProfileEntity.NONADMIN.getProfileEntityType(), ProfileType.UPDATEPROFILEPHOTO.getProfileType()); returnVal = executePut(url, parameters, headers, file, ClientService.FORMAT_NULL); profile.clearFieldsMap(); removeProfileDataFromCache(profile.getReqId()); } if (logger.isLoggable(Level.FINEST)) { logger.exiting(sourceClass, "updatePhoto" + returnVal); } return returnVal; }