Example usage for javax.naming.directory SearchResult SearchResult

List of usage examples for javax.naming.directory SearchResult SearchResult

Introduction

In this page you can find the example usage for javax.naming.directory SearchResult SearchResult.

Prototype

public SearchResult(String name, Object obj, Attributes attrs) 

Source Link

Document

Constructs a search result using the result's name, its bound object, and its attributes.

Usage

From source file:net.officefloor.plugin.web.http.security.store.JndiLdapCredentialStoreTest.java

/**
 * Ensure correct credentials.//from  www . ja  va2 s  .  co  m
 */
@SuppressWarnings("unchecked")
public void testCredentials() throws Exception {

    // Create the expected credentials
    final String expectedRaw = "daniel:officefloor:password";
    MessageDigest digest = MessageDigest.getInstance("MD5");
    digest.update(expectedRaw.getBytes(US_ASCII));
    final byte[] expectedCredentials = digest.digest();

    // Obtain the encoded credentials
    final String encodedCredentials = Base64.encodeBase64String(expectedCredentials).trim();
    assertEquals("Incorrect encoded credentials", "msu723GSLovbwuaPnaLcnQ==", encodedCredentials);

    // Mocks
    final NamingEnumeration<SearchResult> searchResults = this.createMock(NamingEnumeration.class);
    final Attributes attributes = this.createMock(Attributes.class);
    final Attribute attribute = this.createMock(Attribute.class);
    final NamingEnumeration<?> userPasswords = this.createMock(NamingEnumeration.class);

    // Objects
    final SearchResult searchResult = new SearchResult("uid=daniel", null, attributes);
    searchResult.setNameInNamespace("uid=daniel,ou=People,dc=officefloor,dc=net");

    // Record
    this.recordReturn(this.context, this.context.search("ou=People,dc=officefloor,dc=net",
            "(&(objectClass=inetOrgPerson)(uid=daniel))", null), searchResults);
    this.recordReturn(searchResults, searchResults.hasMore(), true);
    this.recordReturn(searchResults, searchResults.next(), searchResult);
    this.recordReturn(this.context, this.context.getAttributes("uid=daniel,ou=People,dc=officefloor,dc=net"),
            attributes);
    this.recordReturn(attributes, attributes.get("userPassword"), attribute);
    this.recordReturn(attribute, attribute.getAll(), userPasswords);
    this.recordReturn(userPasswords, userPasswords.hasMore(), true);
    this.recordReturn(userPasswords, userPasswords.next(), "Plain Text Password".getBytes(US_ASCII));
    this.recordReturn(userPasswords, userPasswords.hasMore(), true);
    this.recordReturn(userPasswords, userPasswords.next(), ("{MD5}" + encodedCredentials).getBytes(US_ASCII));

    // Test
    this.replayMockObjects();
    CredentialEntry entry = this.store.retrieveCredentialEntry("daniel", "REALM");
    byte[] actualCredentials = entry.retrieveCredentials();
    this.verifyMockObjects();

    // Validate correct value
    assertEquals("Incorrect credential byte length", expectedCredentials.length, actualCredentials.length);
    for (int i = 0; i < expectedCredentials.length; i++) {
        assertEquals("Incorrect credential byte " + i, expectedCredentials[i], actualCredentials[i]);
    }
}

From source file:net.officefloor.plugin.web.http.security.store.JndiLdapCredentialStoreTest.java

/**
 * Ensure correct roles.//from w  w  w. jav  a 2 s. com
 */
@SuppressWarnings("unchecked")
public void testRoles() throws Exception {

    // Mocks
    final NamingEnumeration<SearchResult> searchResults = this.createMock(NamingEnumeration.class);
    final Attributes attributes = this.createMock(Attributes.class);
    final Attribute attribute = this.createMock(Attribute.class);

    // Objects
    final SearchResult searchResult = new SearchResult("uid=daniel", null, attributes);
    searchResult.setNameInNamespace("uid=daniel,ou=People,dc=officefloor,dc=net");

    // Record obtaining the Credential Entry
    this.recordReturn(this.context, this.context.search("ou=People,dc=officefloor,dc=net",
            "(&(objectClass=inetOrgPerson)(uid=daniel))", null), searchResults);
    this.recordReturn(searchResults, searchResults.hasMore(), true);
    this.recordReturn(searchResults, searchResults.next(), searchResult);

    // Record obtaining the Groups
    this.recordReturn(this.context, this.context.search("ou=Groups,dc=officefloor,dc=net",
            "(&(objectClass=groupOfNames)" + "(member=uid=daniel,ou=People,dc=officefloor,dc=net))", null),
            searchResults);
    this.recordReturn(searchResults, searchResults.hasMore(), true);
    this.recordReturn(searchResults, searchResults.next(), new SearchResult("cn=developers", null, attributes));
    this.recordReturn(attributes, attributes.get("ou"), attribute);
    this.recordReturn(attribute, attribute.get(), "developer");
    this.recordReturn(searchResults, searchResults.hasMore(), true);
    this.recordReturn(searchResults, searchResults.next(), new SearchResult("cn=founders", null, attributes));
    this.recordReturn(attributes, attributes.get("ou"), attribute);
    this.recordReturn(attribute, attribute.get(), "founder");
    this.recordReturn(searchResults, searchResults.hasMore(), false);

    // Test
    this.replayMockObjects();
    CredentialEntry entry = this.store.retrieveCredentialEntry("daniel", "REALM");
    Set<String> roles = entry.retrieveRoles();
    this.verifyMockObjects();

    // Ensure correct roles
    assertEquals("Incorrect number of roles", 2, roles.size());
    assertTrue("Must have developer role", roles.contains("developer"));
    assertTrue("Must have founder role", roles.contains("founder"));
}

From source file:edu.vt.middleware.ldap.dsml.Dsmlv1.java

/**
 * This will take a DSML <code>Element</code> containing an entry of type
 * <dsml:entry name="name"/> and convert it to a LDAP search result.
 *
 * @param  entryElement  <code>Element</code> of DSML content
 *
 * @return  <code>SearchResult</code>
 */// w  w  w . j  a v  a 2s .c  o  m
protected SearchResult createSearchResult(final Element entryElement) {
    String name = "";
    final Attributes entryAttributes = new BasicAttributes(true);
    SearchResult attrResults = null;

    if (entryElement != null) {

        name = entryElement.attributeValue("dn");
        if (name == null) {
            name = "";
        }

        if (entryElement.hasContent()) {

            final Iterator<?> ocIterator = entryElement.elementIterator("objectclass");
            while (ocIterator.hasNext()) {
                final Element ocElement = (Element) ocIterator.next();
                if (ocElement != null && ocElement.hasContent()) {
                    final String ocName = "objectClass";
                    final Attribute entryAttribute = new BasicAttribute(ocName);
                    final Iterator<?> valueIterator = ocElement.elementIterator("oc-value");
                    while (valueIterator.hasNext()) {
                        final Element valueElement = (Element) valueIterator.next();
                        if (valueElement != null) {
                            final String value = valueElement.getText();
                            if (value != null) {
                                entryAttribute.add(value);
                            }
                        }
                    }
                    entryAttributes.put(entryAttribute);
                }
            }

            attrResults = super.createSearchResult(entryElement);
        }
    }

    if (attrResults != null) {
        final Attributes attrs = attrResults.getAttributes();
        if (attrs != null) {
            final NamingEnumeration<? extends Attribute> ae = attrs.getAll();
            if (ae != null) {
                try {
                    while (ae.hasMore()) {
                        entryAttributes.put(ae.next());
                    }
                } catch (NamingException e) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Could not read attribute in SearchResult from parent");
                    }
                }
            }
        }
    }
    return new SearchResult(name, null, entryAttributes);
}