List of usage examples for javax.naming.ldap LdapName LdapName
public LdapName(List<Rdn> rdns)
From source file:edu.internet2.middleware.psp.ldap.LdapSpmlTarget.java
/** * Normalize LDAP DN using {@link org.apache.directory.shared.ldap.name.LdapDN}. This will convert RDN * attributeTypes to lowercase, which is of interest since Active Directory usually (?) returns attributeTypes * uppercased.// w w w . j a v a 2s . c o m * * @param dn the ldap dn * @return the lowercased and normalized dn * @throws InvalidNameException if the dn is not a valid ldap name */ public static String canonicalizeDn(String dn) throws InvalidNameException { return new LdapName(unescapeForwardSlash(dn)).toString(); }
From source file:edu.kit.scc.ldap.LdapPosixGroupDao.java
/** * Updates a POSIX group in the LDAP directory. * /*ww w . j a v a 2 s . co m*/ * @param group the {@link PosixGroup} to update * @return the {@link PosixGroup} updated */ public PosixGroup updateGroup(PosixGroup group) { BasicAttribute posixGroupBasicAttribute = new BasicAttribute("objectclass"); posixGroupBasicAttribute.add("posixGroup"); Attributes posixGroupAttributes = new BasicAttributes(); posixGroupAttributes.put(posixGroupBasicAttribute); if (group.getCommonName() != null) { posixGroupAttributes.put("cn", group.getCommonName()); } if (group.getGidNumber() != null) { posixGroupAttributes.put("gidNumber", String.valueOf(group.getGidNumber())); } if (group.getUserPassword() != null) { posixGroupAttributes.put("userPassword", group.getUserPassword()); } if (group.getDescription() != null) { posixGroupAttributes.put("description", group.getDescription()); } LdapName groupDn = LdapUtils.emptyLdapName(); try { groupDn = new LdapName(groupBase); groupDn.add("cn=" + group.getCommonName()); log.debug("Update {}", groupDn.toString()); ldapTemplate.bind(groupDn, null, posixGroupAttributes); return group; } catch (InvalidNameException ex) { log.error("ERROR {}", ex.toString()); } return null; }
From source file:edu.jhu.pha.vospace.oauth.AuthorizationServlet.java
private String checkCertificate(HttpServletRequest request) { java.security.cert.X509Certificate[] certs = (java.security.cert.X509Certificate[]) request .getAttribute("javax.servlet.request.X509Certificate"); if (null != certs) { if (certs[0] != null) { String dn = certs[0].getSubjectX500Principal().getName(); try { LdapName ldn = new LdapName(dn); Iterator<Rdn> rdns = ldn.getRdns().iterator(); String org = null, cn = null; while (rdns.hasNext()) { Rdn rdn = (Rdn) rdns.next(); if (rdn.getType().equalsIgnoreCase("O")) org = (String) rdn.getValue(); else if (rdn.getType().equalsIgnoreCase("CN")) cn = (String) rdn.getValue(); }/*from www .ja v a 2s . co m*/ if (cn != null) { return cn; } else { logger.error("Error authenticating the user: cn not found in certificate."); throw new PermissionDeniedException("401 Unauthorized"); } } catch (javax.naming.InvalidNameException e) { } } } return null; }
From source file:com.redhat.lightblue.rest.auth.jboss.CertLdapLoginModule.java
private String getLDAPAttribute(String certificatePrincipal, String searchAttribute) throws NamingException { String searchName = new String(); LdapName name = new LdapName(certificatePrincipal); for (Rdn rdn : name.getRdns()) { if (rdn.getType().equalsIgnoreCase(searchAttribute)) { searchName = (String) rdn.getValue(); break; }//w ww . j a va 2 s.co m } return searchName; }
From source file:org.apache.syncope.core.sync.LDAPDomainSyncActions.java
private LdapName resolveDnOnSyncope(SyncopeUser user, SyncResultsHandler handler) { String domain = user.getAttribute("domain").getValuesAsStrings().iterator().next(); SyncopeSyncResultHandler intHandler = (SyncopeSyncResultHandler) handler; LdapName dnOnSyncope = null;/* w w w . j a va 2 s . co m*/ // Get ConnInstance object to retrieve Configuration of current connector String baseContextUser = null; StringBuilder sb = new StringBuilder(); ConnInstance connInstance = intHandler.getSyncTask().getResource().getConnector(); // Search of connector property containing base context(s) for (ConnConfProperty property : connInstance.getConfiguration()) { if ("baseContexts".equals(property.getSchema().getName())) { baseContextUser = (String) property.getValues().get(0); } } try { if (!"/".equals(user.getAttribute("domain").getValuesAsStrings().iterator().next())) { sb.append("uid=").append(user.getUsername()).append(",ou=").append(domain).append(",") .append(baseContextUser); dnOnSyncope = new LdapName(sb.toString()); } else { sb.append("uid=").append(user.getUsername()).append(",").append(baseContextUser); dnOnSyncope = new LdapName(sb.toString()); } } catch (InvalidNameException ex) { LOG.error("ERROR CONSTRUCTING LDAP DN" + ex.getMessage()); } return dnOnSyncope; }
From source file:fr.mtlx.odm.TestSessionImpl.java
@Test public void testSearch() throws InvalidNameException, SizeLimitExceededException, MappingException { Name dn = new LdapName("ou=personnes"); FilterBuilder<Person> fb = sessionFactory.filterBuilder(Person.class); List<Person> entries = session.getOperations(Person.class).search(dn) .add(fb.not(fb.objectClass("ENTPerson"))).list(); assertNotNull(entries);//w w w .j a v a 2 s . c om assertTrue(entries.size() > 0); assertTrue(Iterables.any(entries, new Predicate<Person>() { @Override public boolean apply(Person entry) { return entry != null && session.isPersistent(entry); } })); }
From source file:edu.kit.scc.ldap.LdapPosixGroupDao.java
/** * Deletes the {@link PosixGroup} from the LDAP directory. * //from ww w.j a v a 2 s . com * @param group the {@link PosixGroup} to delete * @return true if success */ public boolean deleteGroup(PosixGroup group) { LdapName groupDn = LdapUtils.emptyLdapName(); try { groupDn = new LdapName(groupBase); groupDn.add("cn=" + group.getCommonName()); log.debug("Delete {}", groupDn.toString()); ldapTemplate.unbind(groupDn); return true; } catch (InvalidNameException ex) { log.error("ERROR {}", ex.toString()); // ex.printStackTrace(); } return false; }
From source file:fr.mtlx.odm.TestSessionImpl.java
@Test public void testPagedSearch() throws InvalidNameException, MappingException { Name dn = new LdapName("ou=personnes"); int n = 0;/*from w w w . j ava2s . c om*/ FilterBuilder<Person> fb = sessionFactory.filterBuilder(Person.class); Iterable<List<Person>> results = session.getOperations(Person.class).search(dn) .add(fb.not(fb.objectClass("ENTPerson"))).pages(5); assertNotNull(results); Iterator<List<Person>> iterator = results.iterator(); assertNotNull(iterator); assertTrue(iterator.hasNext()); for (List<Person> page : results) { n += page.size(); assertTrue(Iterables.any(page, new Predicate<Person>() { @Override public boolean apply(Person entry) { return entry != null && session.isPersistent(entry); } })); } assertTrue(n > 0); }
From source file:edu.kit.scc.ldap.LdapPosixGroupDao.java
/** * Adds a POSIX user to the specified POSIX group. * // ww w.ja va 2 s .co m * @param group the POSIX group * @param memberUid the POSIX user's uid * @return true on success */ public boolean addMember(PosixGroup group, String memberUid) { ModificationItem[] modificationItems = new ModificationItem[] { new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("memberUid", memberUid)) }; LdapName groupDn = LdapUtils.emptyLdapName(); try { groupDn = new LdapName(groupBase); groupDn.add("cn=" + group.getCommonName()); log.debug("Add member {} to {}", memberUid, groupDn.toString()); ldapTemplate.modifyAttributes(groupDn, modificationItems); return true; } catch (AttributeInUseException ex) { log.error("ERROR {}", ex.toString()); } catch (InvalidNameException ex) { log.error("ERROR {}", ex.toString()); } return false; }
From source file:ldap.SearchUtility.java
/** * This does a tree walk to find all the elements at a given level. * WARNING: for low level elements this may involve a VERY LARGE NUMBER OF DIRECTORY * READS! For example, if a tree has a ten fold fan out; top/area/group/division/section * returning all sections would return 10,000 sections and require 1,000 separate directory * accesses!// w ww . j ava2 s . c o m * * This returns a List of the LdapNames of the elements at a particular level (e.g. a list of the * LdapNames of all divisions). Use this method if you are going to do further directory operations * with the return values, such as looking up staff. * * @param type * @return a list of distringuished names of appropriate elements */ public List<LdapName> getStructureElementNames(String type, DirContext context) throws NamingException { int depth = getStructureLevel(type); //return getElementNames(new LdapName(Config.SEARCH_BASE_DN), depth); return getElementNames(new LdapName(LdapConstants.ldapSearchBaseDn), depth, context); }