Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package org.esupportail.ldapRestService.service; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.esupportail.commons.exceptions.GroupNotFoundException; import org.esupportail.commons.exceptions.UserNotFoundException; import org.esupportail.commons.services.ldap.LdapException; import org.esupportail.commons.services.ldap.LdapGroup; import org.esupportail.commons.services.ldap.LdapGroupService; import org.esupportail.commons.services.ldap.LdapUser; import org.esupportail.commons.services.ldap.LdapUserAndGroupService; import org.esupportail.ldapRestService.exceptions.LdapUserNotFoundException; import org.esupportail.ldapRestService.exceptions.LdapGroupNotFoundException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.support.LdapUtils; /** * * @author mohamed */ public class Ldap { @Autowired private LdapUserAndGroupService ldapService; @Autowired private LdapGroupService ldapGroupMembersService; @Autowired private LdapTemplate ldapTemplate; @Value("${ldap.group.groupMemberAttr}") private String groupMemberAttribute; private LdapUtils ldapUtils; private String searchAttribute; private String userPagerAttribute; private static final Logger logger = LoggerFactory.getLogger(Ldap.class); // ok public LdapUser getLdapUserByUserUid(final String ldapUserUid) throws LdapUserNotFoundException, LdapException { LdapUser ldapUser = null; try { ldapUser = ldapService.getLdapUser(ldapUserUid); logger.info(ldapUser.toString()); } catch (UserNotFoundException e) { throwLdapUserNotFoundException(e, ldapUserUid); } return ldapUser; } // not ok public List<LdapUser> getUsersByToken(String token) { logger.info("Etree dans getUserByToken"); List<LdapUser> users = ldapService.getLdapUsersFromToken(token); for (LdapUser user : users) { logger.info("User correspondant au token : " + user.getId()); } return users; } public List<LdapUser> getLdapUsersByGroupId(String groupId) throws LdapUserNotFoundException, NullPointerException, LdapGroupNotFoundException { logger.info("Entree dans getLdapUsersByGroupId"); logger.info("Le group a chercher est : " + groupId); List<LdapUser> ldapUserList = null; try { logger.info("Entree dans try"); LdapGroup ldapGroup = ldapGroupMembersService.getLdapGroup(groupId); logger.info("ldapGroup => " + ldapGroup.toString()); List<LdapUser> users = this.getMembers(ldapGroup); logger.info(users.toString()); } catch (GroupNotFoundException e) { LdapGroupNotFoundException(e, groupId); } return ldapUserList; } public LdapGroup getLdapGroup(String groupId) { LdapGroup ldapGroup = ldapGroupMembersService.getLdapGroup(groupId); return ldapGroup; } // Returns a list of name of LdapGroup for the token public List<String> getLdapGroupsNameFromToken(String token) { List<String> groupsName = new ArrayList<String>(); List<LdapGroup> listLg = ldapGroupMembersService.getLdapGroupsFromToken(token); for (LdapGroup group : listLg) { groupsName.add(group.getId()); } return groupsName; } // Returns members of a LdapGroup public List<LdapUser> getMembers(LdapGroup group) throws LdapUserNotFoundException { List<LdapUser> users = new ArrayList<LdapUser>(); List<String> usersId = group.getAttributes(groupMemberAttribute); ArrayList<String> uids = new ArrayList<String>(); for (String userId : usersId) { Pattern p = Pattern.compile("uid=(.+?),"); Matcher m = p.matcher(userId); while (m.find()) { String uid = m.group(1); uids.add(uid); } } users = this.getLdapUsersFromUids(uids); logger.info(users.toString()); return users; } // Sets LdapTemplate public void setLdapTemplate(LdapTemplate ldapTemplate) { this.ldapTemplate = ldapTemplate; } // ok will not be used public List<String> getLoginsFromLdapUsers(List<LdapUser> ldapUsers) { List<String> logins = null; for (LdapUser ldapUser : ldapUsers) { logins.add(ldapUser.getId()); } return logins; } // ok => will not be used public ArrayList<LdapUser> getLdapUsersFromUids(ArrayList<String> logins) throws LdapUserNotFoundException { logger.info("Entree dans getLdapUsersFromUids"); // List of ldapUser object ArrayList<LdapUser> listOfLdapUser = new ArrayList<LdapUser>(); for (String userUid : logins) { try { logger.info("Entree dans try"); logger.info("Login du user a chercher : " + userUid); LdapUser user = this.getLdapUserByUserUid(userUid); logger.info("User trouve : " + user.toString()); listOfLdapUser.add(user); } catch (LdapUserNotFoundException e) { logger.info("Le user [" + userUid + "] n\'a pas ete trouve : ", e); } } return listOfLdapUser; } // Returns a list of recipients from a string logins public ArrayList<String> recipientToArrayList(String recipients) { ArrayList<String> recipientsList = new ArrayList<String>(); String[] parts = recipients.split(","); for (String recipient : parts) { recipientsList.add(recipient); } return recipientsList; } public void setUserSearchAttribute(final String searchAttribute) { this.searchAttribute = searchAttribute; } public void setLdapService(final LdapUserAndGroupService ldapGroupService) { this.ldapService = ldapGroupService; } private void throwLdapUserNotFoundException(UserNotFoundException e, final String ldapUserUid) throws LdapUserNotFoundException { final String messageStr = "Impossible to find user with id : [" + ldapUserUid + "]"; logger.debug(messageStr, e); throw new LdapUserNotFoundException(messageStr, e); } private void LdapGroupNotFoundException(GroupNotFoundException e, String groupId) throws LdapGroupNotFoundException { final String messageStr = "Impossible to find group with id : [" + groupId + "]"; logger.debug(messageStr, e); throw new LdapGroupNotFoundException(messageStr, e); } }