package com.empresa; import java.util.Hashtable; import javax.naming.Context; import javax.naming.directory.BasicAttribute; import javax.naming.directory.DirContext; import javax.naming.directory.InitialDirContext; import javax.naming.directory.ModificationItem; public class ActualizarLDAP { private DirContext dirContext = null; ModificationItem[] modItemsOne = new ModificationItem[1]; public ActualizarLDAP() { try { String url = "ldap://<IP_SERVER>:389"; String conntype = "simple"; String AdminDn = "cn=root"; String password = "xxx"; Hashtable<String, String> environment = new Hashtable<String, String>(); Hashtable<String, String> environmentVar = new Hashtable<String, String>(); environmentVar.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); environmentVar.put(Context.PROVIDER_URL, url); environmentVar.put(Context.SECURITY_AUTHENTICATION, conntype); environmentVar.put(Context.SECURITY_PRINCIPAL, AdminDn); environmentVar.put(Context.SECURITY_CREDENTIALS, password); dirContext = new InitialDirContext(environmentVar); } catch (Exception ee) { ee.printStackTrace(); } } public boolean updateJPEGPHOTO(String entryDN, String base64) { boolean flag = false; try { modItemsOne[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("jpegPhoto", base64)); dirContext.modifyAttributes(entryDN, modItemsOne); flag = true; } catch (Exception exception) { exception.printStackTrace(); } return flag; } public static void main(String[] args){ String entryDN = "uid=example,O=EMPRESA"; String base64 = "data:image/png;base64,iVBORw0KGgoAAAANSUh..."; ActualizarLDAP actualizarLDAP = new ActualizarLDAP(); if (actualizarLDAP.updateJPEGPHOTO(entryDN, base64)) { System.out.println("Foto actualizada"); } else { System.out.println("Fallo actualizar foto"); } } }
Fuente:
- http://java2db.com/jndi-ldap-programming/update-or-modify-ldap-entry-using-java-jndi
- http://www.coderpanda.com/tag/update-attribute-in-ldap-using-java/