Return physicalDeliveryOfficeName from AD using LDAP with Java

0

I'm creating a Java application that integrates with the active directory. But I'm having trouble returning a specific data: the Office field (Office - physicalDeliveryOfficeName).

Theoretically, its logic should work in a similar way to the other methods I have, but I always get NullPointerException when trying to return this field. The other methods that return other data (sAMAccountName, cn, givenName, mail, and the like all work fine). I'll put the code down here.

This is the getGivenName(Pessoa) method, one that works correctly.

    public String getGivenName(Pessoa p) throws NamingException { 
            String givenName = "";
            try {
                NamingEnumeration<SearchResult> result = this.searchUser(p); 
                if (result.hasMoreElements()) { 
                    SearchResult sr = (SearchResult) result.next(); 
                    Attributes attrs = sr.getAttributes();
                    givenName = attrs.get("givenName").toString(); 
                    givenName = givenName.substring(givenName.indexOf(":") + 1);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return givenName; 
        }

This is the method I use to connect to AD, with the LDAP query on it. Also works correctly

public NamingEnumeration<SearchResult> searchUser(Pessoa p) throws NamingException { 
        String filter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=" + p.getUsername() + "))";

        return this.dirContext.search("DC=umc,DC=br", filter, this.searchCtls); 
    }

This is the method I said, which returns null and hampers me.

public String getPhysicalDeliveryOfficeName(Pessoa p) throws NamingException {
    String physicalDeliveryOfficeName = "";
    try {
        NamingEnumeration<SearchResult> result = this.searchUser(p); 
        if (result.hasMoreElements()) { 
            SearchResult sr = (SearchResult) result.next(); 
            Attributes attrs = sr.getAttributes();
            physicalDeliveryOfficeName = attrs.get("physicalDeliveryOfficeName").toString(); 
            physicalDeliveryOfficeName = physicalDeliveryOfficeName.substring(physicalDeliveryOfficeName.indexOf(":") + 1); 
        }

        physicalDeliveryOfficeName = physicalDeliveryOfficeName.replaceAll("[^\d.]", "");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return physicalDeliveryOfficeName; 
}

In this field, the employee's plate number is stored, and need this number as the primary key for a database in a database. But because of the problem I said, I can not return the data, I do not know why. The image below has more details of the information and how it is displayed.

    
asked by anonymous 23.10.2016 / 00:26

1 answer

0

Check in the Users or Users section. Some will not have this attribute.

    
20.07.2018 / 22:56