List of usage examples for javax.naming.directory Attribute toString
public String toString()
From source file:com.jaeksoft.searchlib.util.ActiveDirectory.java
public static String getStringAttribute(Attributes attrs, String name) { Attribute attr = attrs.get(name); if (attr == null) return null; String s = attr.toString(); if (StringUtils.isEmpty(s)) return s; int i = s.indexOf(':'); if (i == -1)/*www . ja v a 2s .co m*/ throw new IllegalArgumentException(StringUtils.fastConcat("Wrong returned value: ", s)); return s.substring(i + 1); }
From source file:org.apache.cxf.sts.ldap.LDAPClaimsTest.java
@org.junit.Test @org.junit.Ignore// w w w . j a va 2s . c om public void testLdapTemplate() throws Exception { try { LdapTemplate ldap = (LdapTemplate) appContext.getBean("ldapTemplate"); String user = props.getProperty("claimUser"); Assert.notNull(user, "Property 'claimUser' not configured"); String dn = null; AndFilter filter = new AndFilter(); filter.and(new EqualsFilter("objectclass", "person")).and(new EqualsFilter("cn", user)); //find DN of user AttributesMapper mapper = new AttributesMapper() { public Object mapFromAttributes(Attributes attrs) throws NamingException { return attrs.get("distinguishedName").get(); } }; @SuppressWarnings("rawtypes") List users = ldap.search("OU=users,DC=emea,DC=mycompany,DC=com", filter.toString(), SearchControls.SUBTREE_SCOPE, mapper); Assert.isTrue(users.size() == 1, "Only one user expected"); dn = (String) users.get(0); // get attributes AttributesMapper mapper2 = new AttributesMapper() { public Object mapFromAttributes(Attributes attrs) throws NamingException { Map<String, String> map = new HashMap<String, String>(); NamingEnumeration<? extends Attribute> attrEnum = attrs.getAll(); while (attrEnum.hasMore()) { Attribute att = attrEnum.next(); System.out.println(att.toString()); } map.put("cn", (String) attrs.get("cn").get()); map.put("mail", (String) attrs.get("mail").get()); map.put("sn", (String) attrs.get("sn").get()); map.put("givenName", (String) attrs.get("givenName").get()); return map; } }; ldap.lookup(dn, new String[] { "cn", "mail", "sn", "givenName", "c" }, mapper2); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.openkm.principal.LdapPrincipalAdapter.java
@SuppressWarnings("unchecked") private List<String> ldapSearch(List<String> searchBases, String searchFilter, String attribute) { log.debug("ldapSearch({}, {}, {})", new Object[] { searchBases, searchFilter, attribute }); List<String> al = new ArrayList<String>(); DirContext ctx = null;//from w ww . j ava 2 s .co m Hashtable<String, String> env = getEnvironment(); try { ctx = new InitialDirContext(env); SearchControls searchCtls = new SearchControls(); searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); for (String searchBase : searchBases) { NamingEnumeration<SearchResult> results = ctx.search(searchBase, searchFilter, searchCtls); while (results.hasMore()) { SearchResult searchResult = (SearchResult) results.next(); Attributes attributes = searchResult.getAttributes(); if (attribute.equals("")) { StringBuilder sb = new StringBuilder(); for (NamingEnumeration<?> ne = attributes.getAll(); ne.hasMore();) { Attribute attr = (Attribute) ne.nextElement(); sb.append(attr.toString()); sb.append("\n"); } al.add(sb.toString()); } else { Attribute attrib = attributes.get(attribute); if (attrib != null) { // Handle multi-value attributes for (NamingEnumeration<?> ne = attrib.getAll(); ne.hasMore();) { String value = (String) ne.nextElement(); // If FQDN get only main part if (value.startsWith("CN=") || value.startsWith("cn=")) { String cn = value.substring(3, value.indexOf(',')); log.debug("FQDN: {}, CN: {}", value, cn); al.add(cn); } else { al.add(value); } } } } } } } catch (ReferralException e) { log.error("ReferralException: {}", e.getMessage()); log.error("ReferralInfo: {}", e.getReferralInfo()); log.error("ResolvedObj: {}", e.getResolvedObj()); try { log.error("ReferralContext: {}", e.getReferralContext()); } catch (NamingException e1) { log.error("NamingException logging context: {}", e1.getMessage()); } } catch (NamingException e) { log.error("NamingException: {} (Base: {} - Filter: {} - Attribute: {})", new Object[] { e.getMessage(), searchBases, searchFilter, attribute }); } finally { try { if (ctx != null) { ctx.close(); } } catch (NamingException e) { log.error("NamingException closing context: {}", e.getMessage()); } } log.debug("ldapSearch: {}", al); return al; }
From source file:com.googlecode.fascinator.authentication.custom.ldap.CustomLdapAuthenticationHandler.java
/** * Get the value of an attribute from a search result * //from w w w . j a v a2 s.co m * @param attrName * The name of the attribute that we're interested in * @param sr * The search result * @return The attribute value * @throws NamingException */ private String getAttrValue(String attrName, SearchResult sr) throws NamingException { // Get all attributes Attributes entry = sr.getAttributes(); // Get the attribute value and return Attribute attrValues = entry.get(attrName); if (attrValues == null) return null; String[] strArr = attrValues.toString().split(":"); return strArr[1].trim(); }
From source file:hudson.security.LDAPSecurityRealm.java
/** * Infer the root DN./*from www . j av a 2s .co m*/ * * @return null if not found. */ private String inferRootDN(String server) { try { Hashtable<String, String> props = new Hashtable<String, String>(); if (managerDN != null) { props.put(Context.SECURITY_PRINCIPAL, managerDN); props.put(Context.SECURITY_CREDENTIALS, getManagerPassword()); } props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); props.put(Context.PROVIDER_URL, getServerUrl() + '/'); DirContext ctx = new InitialDirContext(props); Attributes atts = ctx.getAttributes(""); Attribute a = atts.get("defaultNamingContext"); if (a != null) // this entry is available on Active Directory. See http://msdn2.microsoft.com/en-us/library/ms684291(VS.85).aspx return a.toString(); a = atts.get("namingcontexts"); if (a == null) { LOGGER.warning("namingcontexts attribute not found in root DSE of " + server); return null; } return a.get().toString(); } catch (NamingException e) { LOGGER.log(Level.WARNING, "Failed to connect to LDAP to infer Root DN for " + server, e); return null; } }
From source file:org.jahia.services.usermanager.ldap.LDAPUserGroupProvider.java
/** * Map ldap attributes to jahia properties * * @param attributes/*from w w w . j a v a 2 s . com*/ * @param isUser * @return */ private Properties attributesToJahiaProperties(Attributes attributes, boolean isUser) { Properties props = new Properties(); Map<String, String> attributesMapper = isUser ? userConfig.getAttributesMapper() : groupConfig.getAttributesMapper(); for (String propertyKey : attributesMapper.keySet()) { Attribute ldapAttribute = attributes.get(attributesMapper.get(propertyKey)); try { if (ldapAttribute != null && ldapAttribute.get() instanceof String) { props.put(propertyKey, ldapAttribute.get()); } } catch (NamingException e) { logger.error("Error reading LDAP attribute:" + ldapAttribute.toString()); } } return props; }
From source file:org.pentaho.platform.plugin.services.security.userrole.ldap.transform.SearchResultToAttrValueList.java
public Object transform(final Object obj) { Object transformed = obj;//from www. jav a 2 s. com if (obj instanceof SearchResult) { transformed = new HashSet(); Set valueSet = (Set) transformed; SearchResult res = (SearchResult) obj; if (SearchResultToAttrValueList.logger.isDebugEnabled()) { SearchResultToAttrValueList.logger.debug(Messages.getInstance().getString( "SearchResultToAttrValueList.DEBUG_ATTRIBUTES_FROM_SEARCHRESULT", (null != res.getAttributes()) ? res.getAttributes().toString() : "null")); //$NON-NLS-1$ //$NON-NLS-2$ } Attribute attr = res.getAttributes().get(attributeName); if (SearchResultToAttrValueList.logger.isDebugEnabled()) { SearchResultToAttrValueList.logger .debug(Messages.getInstance().getString("SearchResultToAttrValueList.DEBUG_ATTRIBUTE_VALUE", attributeName, (null != attr) ? attr.toString() : "null")); //$NON-NLS-1$ //$NON-NLS-2$ } if (attr != null) { // check for null as node might not have attribute we're looking for try { NamingEnumeration values = attr.getAll(); while (values.hasMore()) { // if tokenName was specified, extract from value; otherwise // store value unchanged Object value = values.next(); if (StringUtils.hasLength(tokenName)) { if ((null != value) && (value instanceof String)) { String tokenValue = extract((String) value, tokenName); if (null != tokenValue) { valueSet.add(tokenValue); } } else { if (SearchResultToAttrValueList.logger.isWarnEnabled()) { SearchResultToAttrValueList.logger.warn(Messages.getInstance() .getString("SearchResultToAttrValueList.WARN_ATTRIBUTE_NOT_A_STRING")); //$NON-NLS-1$ } } } else { if (null != value) { valueSet.add(value.toString()); } } } } catch (NamingException e) { if (SearchResultToAttrValueList.logger.isErrorEnabled()) { SearchResultToAttrValueList.logger.error(Messages.getInstance() .getErrorString("SearchResultToAttrValueList.ERROR_0001_NAMING_EXCEPTION"), e); //$NON-NLS-1$ } } } return transformed; } return transformed; }