Search in sources :

Example 91 with Attribute

use of javax.naming.directory.Attribute in project OpenAM by OpenRock.

the class SMSAuditor method deriveFinalState.

/**
     * Uses a series of modifications to establish the final state of a set of attributes, based on their original
     * value.
     * @param mods The list of modifications made
     * @return A map representing the final state
     */
private Map<String, Object> deriveFinalState(ModificationItem[] mods) {
    Map<String, Object> finalState = new CaseInsensitiveHashMap<>(getInitialState());
    for (int i = 0; i < mods.length; i++) {
        Attribute currentAttr = mods[i].getAttribute();
        if (mods[i].getModificationOp() == DirContext.REMOVE_ATTRIBUTE) {
            finalState.remove(currentAttr.getID());
        } else {
            HashSet values = new HashSet();
            for (int j = 0; j < currentAttr.size(); j++) {
                try {
                    values.add(currentAttr.get(j));
                } catch (NamingException e) {
                //no operation here
                }
            }
            finalState.put(currentAttr.getID(), values);
        }
    }
    return finalState;
}
Also used : Attribute(javax.naming.directory.Attribute) NamingException(javax.naming.NamingException) CaseInsensitiveHashMap(com.sun.identity.common.CaseInsensitiveHashMap) HashSet(java.util.HashSet)

Example 92 with Attribute

use of javax.naming.directory.Attribute in project OpenAM by OpenRock.

the class CertUtils method getAttributeValue.

/**
     * Retrieves a given attribute value from the provided {@link X500Principal} even if the attribute was enclosed in
     * a multi-valued RDN.
     *
     * @param principal The principal to retrieve the value from.
     * @param attributeName The non-null name of the attribute to retrieve.
     * @return The attribute value from the principal.
     */
public static String getAttributeValue(X500Principal principal, String attributeName) {
    try {
        LdapName ldapName = new LdapName(principal.getName(X500Principal.RFC2253, OID_MAP));
        for (Rdn rdn : ldapName.getRdns()) {
            Attributes attrs = rdn.toAttributes();
            NamingEnumeration<? extends Attribute> values = attrs.getAll();
            while (values.hasMoreElements()) {
                Attribute attr = values.next();
                if (attributeName.equalsIgnoreCase(attr.getID())) {
                    return attr.get() == null ? null : attr.get().toString();
                }
            }
        }
    } catch (NamingException ne) {
        DEBUG.warning("A naming error occurred while trying to retrieve " + attributeName + " from principal: " + principal, ne);
    }
    return null;
}
Also used : Attribute(javax.naming.directory.Attribute) Attributes(javax.naming.directory.Attributes) NamingException(javax.naming.NamingException) Rdn(javax.naming.ldap.Rdn) LdapName(javax.naming.ldap.LdapName)

Example 93 with Attribute

use of javax.naming.directory.Attribute in project nhin-d by DirectProject.

the class RESTSmtpAgentConfigFunctional_Test method setUp.

/**
     * Initialize the servers- LDAP and HTTP.
     */
@SuppressWarnings("unchecked")
@Override
public void setUp() throws Exception {
    // check for Windows... it doens't like file://<drive>... turns it into FTP
    File file = new File("./src/test/resources/bundles/testBundle.p7b");
    if (file.getAbsolutePath().contains(":/"))
        filePrefix = "file:///";
    else
        filePrefix = "file:///";
    CertCacheFactory.getInstance().flushAll();
    /*
		 * Setup the LDAP Server
		 */
    MutablePartitionConfiguration pcfg = new MutablePartitionConfiguration();
    pcfg.setName("lookupTest");
    pcfg.setSuffix("cn=lookupTest");
    // Create some indices
    Set<String> indexedAttrs = new HashSet<String>();
    indexedAttrs.add("objectClass");
    indexedAttrs.add("cn");
    pcfg.setIndexedAttributes(indexedAttrs);
    // Create a first entry associated to the partition
    Attributes attrs = new BasicAttributes(true);
    // First, the objectClass attribute
    Attribute attr = new BasicAttribute("objectClass");
    attr.add("top");
    attrs.put(attr);
    // Associate this entry to the partition
    pcfg.setContextEntry(attrs);
    // As we can create more than one partition, we must store
    // each created partition in a Set before initialization
    Set<MutablePartitionConfiguration> pcfgs = new HashSet<MutablePartitionConfiguration>();
    pcfgs.add(pcfg);
    //
    //
    //
    // add the lookupTestPublic
    //
    //
    pcfg = new MutablePartitionConfiguration();
    pcfg.setName("lookupTestPublic");
    pcfg.setSuffix("cn=lookupTestPublic");
    // Create some indices
    indexedAttrs = new HashSet<String>();
    indexedAttrs.add("objectClass");
    indexedAttrs.add("cn");
    pcfg.setIndexedAttributes(indexedAttrs);
    // Create a first entry associated to the partition
    attrs = new BasicAttributes(true);
    // First, the objectClass attribute
    attr = new BasicAttribute("objectClass");
    attr.add("top");
    attrs.put(attr);
    // Associate this entry to the partition
    pcfg.setContextEntry(attrs);
    // As we can create more than one partition, we must store
    // each created partition in a Set before initialization
    pcfgs.add(pcfg);
    configuration.setContextPartitionConfigurations(pcfgs);
    this.configuration.setWorkingDirectory(new File("LDAP-TEST"));
    // add the private key schema
    ///
    Set<AbstractBootstrapSchema> schemas = configuration.getBootstrapSchemas();
    schemas.add(new PrivkeySchema());
    configuration.setBootstrapSchemas(schemas);
    super.setUp();
    // import the ldif file
    InputStream stream = TestUtils.class.getResourceAsStream("/ldifs/privCertsOnly.ldif");
    if (stream == null)
        throw new IOException("Failed to load ldif file");
    importLdif(stream);
    // setup the mock DNS SRV adapter
    mockLookup = mock(Lookup.class);
    LookupFactory.getFactory().addOverrideImplementation(mockLookup);
    SRVRecord srvRecord = new SRVRecord(new Name("_ldap._tcp.example.com."), DClass.IN, 3600, 0, 1, port, new Name("localhost."));
    when(mockLookup.run()).thenReturn(new Record[] { srvRecord });
    // create the web service and proxy
    ConfigServiceRunner.startConfigService();
    proxy = new ConfigurationServiceProxy(ConfigServiceRunner.getConfigServiceURL());
    certService = new DefaultCertificateService(ConfigServiceRunner.getRestAPIBaseURL(), HttpClientFactory.createHttpClient(), new OpenServiceSecurityManager());
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) Attribute(javax.naming.directory.Attribute) BasicAttribute(javax.naming.directory.BasicAttribute) DefaultCertificateService(org.nhind.config.rest.impl.DefaultCertificateService) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) OpenServiceSecurityManager(org.nhindirect.common.rest.OpenServiceSecurityManager) IOException(java.io.IOException) PrivkeySchema(org.nhindirect.ldap.PrivkeySchema) Name(org.xbill.DNS.Name) AbstractBootstrapSchema(org.apache.directory.server.core.schema.bootstrap.AbstractBootstrapSchema) MutablePartitionConfiguration(org.apache.directory.server.core.configuration.MutablePartitionConfiguration) Lookup(org.nhindirect.stagent.cert.impl.util.Lookup) SRVRecord(org.xbill.DNS.SRVRecord) File(java.io.File) ConfigurationServiceProxy(org.nhind.config.ConfigurationServiceProxy) HashSet(java.util.HashSet)

Example 94 with Attribute

use of javax.naming.directory.Attribute in project uPortal by Jasig.

the class LDAPGroupStore method processLdapResults.

protected void processLdapResults(NamingEnumeration results, ArrayList keys) {
    //long loop1=System.currentTimeMillis();
    try {
        while (results.hasMore()) {
            //long loop2 = System.currentTimeMillis();
            //long cast1=System.currentTimeMillis();
            //looping=looping+loop2-loop1;
            SearchResult result = (SearchResult) results.next();
            //long cast2 = System.currentTimeMillis();
            //long get1 = System.currentTimeMillis();
            Attributes ldapattribs = result.getAttributes();
            //long get2 = System.currentTimeMillis();
            //long set1 = System.currentTimeMillis();
            Attribute attrib = ldapattribs.get(keyfield);
            if (attrib != null) {
                keys.add(String.valueOf(attrib.get()).toLowerCase());
            }
        //long set2 = System.currentTimeMillis();
        //loop1=System.currentTimeMillis();
        //casting=casting+cast2-cast1;
        //setting=setting+set2-set1;
        //getting=getting+get2-get1;
        }
    } catch (NamingException nex) {
        log.error("LDAPGroupStore: error processing results", nex);
    } finally {
        try {
            results.close();
        } catch (Exception e) {
        }
    }
//long time5 = System.currentTimeMillis();
//System.out.println("Result processing took "+(time5-time1)+": "+getting+" for getting, "
//  +setting+" for setting, "+casting+" for casting, "+looping+" for looping,"
//  +(time5-loop1)+" for closing");
}
Also used : Attribute(javax.naming.directory.Attribute) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException) ResourceMissingException(org.apereo.portal.ResourceMissingException) NamingException(javax.naming.NamingException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) GroupsException(org.apereo.portal.groups.GroupsException) SAXException(org.xml.sax.SAXException)

Example 95 with Attribute

use of javax.naming.directory.Attribute in project uPortal by Jasig.

the class SimpleAttributesMapper method mapFromAttributes.

/*
     * Public API.
     */
public Object mapFromAttributes(Attributes attr) {
    // Assertions.
    if (keyAttributeName == null) {
        String msg = "The property 'keyAttributeName' must be set.";
        throw new IllegalStateException(msg);
    }
    if (groupNameAttributeName == null) {
        String msg = "The property 'groupNameAttributeName' must be set.";
        throw new IllegalStateException(msg);
    }
    if (membershipAttributeName == null) {
        String msg = "The property 'membershipAttributeName' must be set.";
        throw new IllegalStateException(msg);
    }
    if (log.isDebugEnabled()) {
        String msg = "SimpleAttributesMapper.mapFromAttributes() :: settings:  keyAttributeName='" + keyAttributeName + "', groupNameAttributeName='" + groupNameAttributeName + "', groupNameAttributeName='" + groupNameAttributeName + "'";
        log.debug(msg);
    }
    LdapRecord rslt;
    try {
        String key = (String) attr.get(keyAttributeName).get();
        String groupName = (String) attr.get(groupNameAttributeName).get();
        IEntityGroup g = new EntityTestingGroupImpl(key, IPerson.class);
        g.setCreatorID("System");
        g.setName(groupName);
        g.setDescription(GROUP_DESCRIPTION);
        List<String> membership = new LinkedList<String>();
        Attribute m = attr.get(membershipAttributeName);
        if (m != null) {
            for (Enumeration<?> en = m.getAll(); en.hasMoreElements(); ) {
                membership.add((String) en.nextElement());
            }
        }
        rslt = new LdapRecord(g, membership);
        if (log.isDebugEnabled()) {
            StringBuilder msg = new StringBuilder();
            msg.append("Record Details:").append("\n\tkey=").append(key).append("\n\tgroupName=").append(groupName).append("\n\tmembers:");
            for (String s : membership) {
                msg.append("\n\t\t").append(s);
            }
            log.debug(msg.toString());
        }
    } catch (Throwable t) {
        log.error("Error in SimpleAttributesMapper", t);
        String msg = "SimpleAttributesMapper failed to create a LdapRecord " + "from the specified Attributes:  " + attr;
        throw new RuntimeException(msg, t);
    }
    return rslt;
}
Also used : IEntityGroup(org.apereo.portal.groups.IEntityGroup) Attribute(javax.naming.directory.Attribute) EntityTestingGroupImpl(org.apereo.portal.groups.EntityTestingGroupImpl) LinkedList(java.util.LinkedList)

Aggregations

Attribute (javax.naming.directory.Attribute)110 Attributes (javax.naming.directory.Attributes)57 NamingException (javax.naming.NamingException)39 BasicAttribute (javax.naming.directory.BasicAttribute)39 BasicAttributes (javax.naming.directory.BasicAttributes)30 ArrayList (java.util.ArrayList)29 SearchResult (javax.naming.directory.SearchResult)25 NamingEnumeration (javax.naming.NamingEnumeration)22 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)18 HashSet (java.util.HashSet)17 DirContext (javax.naming.directory.DirContext)17 SearchControls (javax.naming.directory.SearchControls)17 IOException (java.io.IOException)11 InitialDirContext (javax.naming.directory.InitialDirContext)11 ModificationItem (javax.naming.directory.ModificationItem)11 Hashtable (java.util.Hashtable)9 File (java.io.File)7 List (java.util.List)7 MutablePartitionConfiguration (org.apache.directory.server.core.configuration.MutablePartitionConfiguration)7 AbstractBootstrapSchema (org.apache.directory.server.core.schema.bootstrap.AbstractBootstrapSchema)7