private static void closeContext(DirContext context) { try { context.close(); } catch (NamingException ignored) { } }
/** * The time to wait before the search fails; the default is zero, meaning forever. * * @param searchTimeLimit the time limit for the search (in milliseconds). */ public void setSearchTimeLimit(int searchTimeLimit) { searchControls.setTimeLimit(searchTimeLimit); }
/** * We need to make sure the search controls has the return object flag set to true, in * order for the search to return DirContextAdapter instances. * @param originalControls * @return */ private static SearchControls buildControls(SearchControls originalControls) { return new SearchControls(originalControls.getSearchScope(), originalControls.getCountLimit(), originalControls.getTimeLimit(), originalControls.getReturningAttributes(), RETURN_OBJECT, originalControls.getDerefLinkFlag()); }
/** * verify if there is a mail server registered to the domain name. and return the email servers count */ public static int mailServersCount( String hostName ) throws NamingException { Hashtable<String, String> env = new Hashtable<String, String>(); env.put( "java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory" ); DirContext ictx = new InitialDirContext( env ); Attributes attrs = ictx.getAttributes( hostName, new String[] { "MX" } ); Attribute attr = attrs.get( "MX" ); if ( attr == null ) { return ( 0 ); } return ( attr.size() ); }
private SearchControls createSearchControl(String[] returningAttributes) { SearchControls controls = new SearchControls(); controls.setSearchScope(searchScope); controls.setTimeLimit(searchTimeLimit); controls.setReturningAttributes(returningAttributes); return controls; }
@Override public void clearCredentials() throws RealmUnavailableException { try { Attributes attributes = new BasicAttributes(); attributes.put(new BasicAttribute(algorithmAttributeName)); attributes.put(new BasicAttribute(hashAttributeName)); attributes.put(new BasicAttribute(seedAttributeName)); attributes.put(new BasicAttribute(sequenceAttributeName)); context.modifyAttributes(distinguishedName, DirContext.REMOVE_ATTRIBUTE, attributes); } catch (NoSuchAttributeException e) { // ignore if already clear } catch (NamingException e) { throw log.ldapRealmCredentialClearingFailed(distinguishedName, e); } } }
private QueryBuilder() { controls.setSearchScope(SearchControls.SUBTREE_SCOPE); controls.setReturningAttributes(new String[0]); }
private static Attributes mockAttributes(NameValues... namedValues) throws NamingException { Attributes attributes = new BasicAttributes(); for (NameValues namedValue : namedValues) { Attribute attr = new BasicAttribute(namedValue.name); for (String value : namedValue.values) { attr.add(value); } attributes.put(attr); } return attributes; }
@Override public SupportLevel getCredentialAcquireSupport(final Class<? extends Credential> credentialType, final String algorithmName, final AlgorithmParameterSpec parameterSpec, final Supplier<Provider[]> providers) { if (credentialType != PasswordCredential.class) { return SupportLevel.UNSUPPORTED; } Attribute algorithmAttribute = attributes.get(algorithmAttributeName); Attribute hashAttribute = attributes.get(hashAttributeName); Attribute seedAttribute = attributes.get(seedAttributeName); Attribute sequenceAttribute = attributes.get(sequenceAttributeName); if (algorithmAttribute != null && hashAttribute != null && seedAttribute != null && sequenceAttribute != null && (algorithmName == null || algorithmAttribute.contains(algorithmName))) { return SupportLevel.SUPPORTED; } return SupportLevel.UNSUPPORTED; }
private BasicAttributes createSrvRecords() { BasicAttributes records = new BasicAttributes(); BasicAttribute record = new BasicAttribute("SRV"); // the structure of the service records is: // priority weight port hostname // for more information: http://en.wikipedia.org/wiki/SRV_record record.add("20 80 389 kdc3.springsource.com."); record.add("10 70 389 kdc.springsource.com."); record.add("20 20 389 kdc4.springsource.com."); record.add("10 30 389 kdc2.springsource.com"); records.put(record); return records; } }
public static DirContext createDirContext(Map<String, String> environment) throws NamingException { return new InitialDirContext(new Hashtable<>(environment)); } }
/** * If true then searches the entire subtree as identified by context, if false (the * default) then only searches the level identified by the context. * * @param searchSubtree true the underlying search controls should be set to * SearchControls.SUBTREE_SCOPE rather than SearchControls.ONELEVEL_SCOPE. */ public void setSearchSubtree(boolean searchSubtree) { searchControls.setSearchScope(searchSubtree ? SearchControls.SUBTREE_SCOPE : SearchControls.ONELEVEL_SCOPE); }
public Object executeWithContext(DirContext ctx) throws NamingException { for (GrantedAuthority authority : authorities) { String group = convertAuthorityToGroup(authority); DistinguishedName fullDn = LdapUtils.getFullDn(userDn, ctx); ModificationItem addGroup = new ModificationItem(modType, new BasicAttribute(groupMemberAttributeName, fullDn.toUrl())); ctx.modifyAttributes(buildGroupDn(group), new ModificationItem[] { addGroup }); } return null; } });
/** * Specifies the attributes that will be returned as part of the search. * <p> * null indicates that all attributes will be returned. An empty array indicates no * attributes are returned. * * @param attrs An array of attribute names identifying the attributes that will be * returned. Can be null. */ public void setReturningAttributes(String[] attrs) { searchControls.setReturningAttributes(attrs); }
/** * Sets the maximum number of entries to be returned as a result of the search. * <br> * 0 indicates no limit: all entries will be returned. * @param limit The maximum number of entries that will be returned. * @return the current instance of the builder */ public QueryBuilder limit(int limit) { controls.setCountLimit(limit); return this; }
@Override public void modifyAttributes(String name, int mod_op, Attributes attrs) throws NamingException { delegating.modifyAttributes(name, mod_op, attrs); }
/** * Sets the corresponding property on the {@link SearchControls} instance used in the * search. * * @param deref the derefLinkFlag value as defined in SearchControls.. */ public void setDerefLinkFlag(boolean deref) { searchControls.setDerefLinkFlag(deref); }
@Override public void clearCredentials() throws RealmUnavailableException { try { Attributes attributes = new BasicAttributes(); attributes.put(new BasicAttribute(userPasswordAttributeName)); context.modifyAttributes(distinguishedName, DirContext.REMOVE_ATTRIBUTE, attributes); } catch (NoSuchAttributeException e) { // ignore if already clear } catch (NamingException e) { throw log.ldapRealmCredentialClearingFailed(distinguishedName, e); } } }
/** * If set to true, a subtree scope search will be performed. If false a single-level * search is used. * * @param searchSubtree set to true to enable searching of the entire tree below the * <tt>groupSearchBase</tt>. */ public void setSearchSubtree(boolean searchSubtree) { int searchScope = searchSubtree ? SearchControls.SUBTREE_SCOPE : SearchControls.ONELEVEL_SCOPE; this.searchControls.setSearchScope(searchScope); }
@Override public void modifyAttributes(String name, ModificationItem[] mods) throws NamingException { delegating.modifyAttributes(name, mods); }