Galera, nesse post vou compartilhar uma classe que possui operações de busca e atualização de conteúdo texto e de conteúdo do tipo imagem.
Segue abaixo a classe:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Hashtable; import javax.naming.Context; import javax.naming.NamingEnumeration; import javax.naming.NamingException; import javax.naming.directory.Attributes; import javax.naming.directory.BasicAttribute; import javax.naming.directory.DirContext; import javax.naming.directory.InitialDirContext; import javax.naming.directory.ModificationItem; import javax.naming.directory.SearchControls; import javax.naming.directory.SearchResult; import org.apache.commons.io.IOUtils; public class TestAD { private DirContext ldapContext; public TestAD() throws NamingException { Hashtable<String, String> ldapEnv = new Hashtable<String, String>(11); ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); ldapEnv.put(Context.PROVIDER_URL, "ldap://192.168.0.12:389"); ldapEnv.put(Context.SECURITY_AUTHENTICATION, "simple"); ldapEnv.put(Context.SECURITY_PRINCIPAL, "CN=Administrator,CN=Users,DC=algumdc,DC=local"); ldapEnv.put(Context.SECURITY_CREDENTIALS, "senha"); ldapContext = new InitialDirContext(ldapEnv); } public void buscarConteudos() throws NamingException { SearchControls searchCtls = new SearchControls(); //Especifica os atributos que serao retornados String returnedAtts[]={"sn","givenName", "samAccountName", "thumbnailPhoto"}; searchCtls.setReturningAttributes(returnedAtts); //Especifica o escopo de busca searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); //Especifica o filtro de busca - Busca conteúdo com samAccountName = bruno String searchFilter = "(&(samAccountName=bruno))"; //Especifica a base de busca String searchBase = "CN=Users,DC=algumDC,DC=local"; //Contador de resultados int totalResults = 0; // Realiza a busca NamingEnumeration<SearchResult> answer = ldapContext.search(searchBase, searchFilter, searchCtls); //Percorre os resultados retornados while (answer.hasMoreElements()) { SearchResult sr = (SearchResult)answer.next(); totalResults++; System.out.println(">>>" + sr.getName()); Attributes attrs = sr.getAttributes(); System.out.println(">>>>>>" + attrs.get("samAccountName")); System.out.println(">>>>>>" + attrs.get("thumbnailPhoto")); } System.out.println("Total results: " + totalResults); } public void atualizarConteudo() throws FileNotFoundException, IOException, NamingException { ModificationItem[] mods = new ModificationItem[2]; // Atualiza o campo 'givenName' pelo valor especificado mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("givenName", "CHUCK NOIA")); FileInputStream imgFIS = new FileInputStream("/home/bruno/Pictures/FotoPerfil1.jpg"); byte[] imgByte = IOUtils.toByteArray(imgFIS); //Atualiza o campo thumbnailPhoto, precisa enviar um array de byte //Tamanho máximo de arquivo que o campo aceita é de 100kb mods[1] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("thumbnailPhoto", imgByte)); //Realiza o procedimento de atualização ldapContext.modifyAttributes("CN=algum CN,CN=Users,DC=algumDC,DC=local", mods); } public void close() throws NamingException { ldapContext.close(); } public static void main (String[] args) { try { TestAD testAD = new TestAD(); testAD.buscarConteudos(); testAD.atualizarConteudo(); testAD.close(); } catch (Exception e) { e.printStackTrace(); } } } |