2789N/A/*
2789N/A * CDDL HEADER START
2789N/A *
2789N/A * The contents of this file are subject to the terms of the
2789N/A * Common Development and Distribution License, Version 1.0 only
2789N/A * (the "License"). You may not use this file except in compliance
2789N/A * with the License.
2789N/A *
2789N/A * You can obtain a copy of the license at
2789N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE
2789N/A * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
2789N/A * See the License for the specific language governing permissions
2789N/A * and limitations under the License.
2789N/A *
2789N/A * When distributing Covered Code, include this CDDL HEADER in each
2789N/A * file and include the License file at
2789N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
2789N/A * add the following below this CDDL HEADER, with the fields enclosed
2789N/A * by brackets "[]" replaced with your own identifying information:
2789N/A * Portions Copyright [yyyy] [name of copyright owner]
2789N/A *
2789N/A * CDDL HEADER END
2789N/A *
2789N/A *
3215N/A * Copyright 2008 Sun Microsystems, Inc.
2789N/A */
2789N/A
2789N/Aimport java.util.Hashtable;
3600N/Aimport javax.naming.Context;
3600N/Aimport javax.naming.NamingException;
3600N/Aimport javax.naming.directory.Attributes;
3600N/Aimport javax.naming.ldap.LdapContext;
3600N/Aimport javax.naming.ldap.InitialLdapContext;
3600N/Aimport javax.naming.CompositeName;
3600N/Aimport javax.naming.directory.BasicAttribute;
3600N/Aimport javax.naming.directory.BasicAttributes;
3600N/Aimport javax.naming.CommunicationException;
3600N/Aimport java.util.HashSet;
2789N/Aimport java.util.Iterator;
2789N/A
2789N/A/**
3600N/A * Modify an entry with an attribute.
3600N/A * The operation can be a replace, delete or a add new attribute
3600N/A * The function returns the ldap error code
3600N/A */
2789N/Apublic class modifyAnAttribute {
2789N/A
3600N/A /**
3600N/A * Main.
3600N/A *
3600N/A * @param args arguments
3600N/A */
3600N/A public static void main(String[] args) {
2789N/A
3600N/A String hostname=null;
3600N/A String ldapPort=null;
3600N/A String principal=null;
3600N/A String credential=null;
3600N/A String attributeToModify=null;
3600N/A String dnToModify=null;
3600N/A String newAttributeValue=null;
3600N/A String changetype=null;
3600N/A String errorCode=null;
3600N/A String errorMessage=null;
3600N/A String listAttributesToModify=null;
2789N/A
3600N/A int ind1;
3600N/A String attributeName;
3600N/A String attributeValue;
3600N/A Hashtable envLdap = new Hashtable();
3600N/A LdapContext ctx;
3600N/A
3600N/A Attributes attributes = new BasicAttributes();
3600N/A HashSet attributeSet = new HashSet();
3600N/A
3600N/A
3600N/A for (int k=0; k< args.length; k++) {
3600N/A String opt1 = args[k];
3600N/A String val1 = args[k+1];
2789N/A
3600N/A if (opt1.equals("-h")) {
3600N/A hostname = val1;
3600N/A }
3600N/A if (opt1.equals("-p")) {
3600N/A ldapPort = val1;
3600N/A }
3600N/A if (opt1.equals("-D")) {
3600N/A principal = val1;
3600N/A }
3600N/A if (opt1.equals("-w")) {
3600N/A credential = val1;
3600N/A }
3600N/A if (opt1.equals("-d")) {
3600N/A dnToModify = val1;
3600N/A }
3600N/A if (opt1.equals("-v")) {
3600N/A newAttributeValue = val1;
3600N/A }
3600N/A if (opt1.equals("-a")) {
3600N/A attributeToModify = val1;
3600N/A }
3600N/A if (opt1.equals("-t")) {
3600N/A changetype = val1;
3600N/A }
3600N/A if (opt1.equals("-l")) {
3600N/A listAttributesToModify = val1;
2789N/A
3600N/A ind1= val1.indexOf(":");
3600N/A
3600N/A attributeName=val1.substring(0,ind1);
3810N/A if (ind1+1 < val1.length()) {
3810N/A // assume empty strings == no specific value
3810N/A attributeValue=val1.substring(ind1+1);
3810N/A } else {
3810N/A attributeValue = null;
3810N/A }
3600N/A
3600N/A BasicAttribute attrToComplete = null;
2789N/A
3600N/A Iterator it = attributeSet.iterator();
3600N/A while (attrToComplete == null && it.hasNext()) {
3600N/A BasicAttribute attr = (BasicAttribute) it.next();
3600N/A if ((attr.getID()).equalsIgnoreCase(attributeName)) {
3600N/A attrToComplete = attr;
3600N/A }
3600N/A }
3600N/A
3600N/A if (attrToComplete == null) {
3600N/A attrToComplete = new BasicAttribute(attributeName);
3600N/A attributeSet.add(attrToComplete);
3600N/A }
3810N/A if (attributeValue != null) {
3810N/A // as opposed to (attributeValue == null), for example in some
3810N/A // attribute delete operations
3810N/A attributeValue=attributeValue.replaceAll("QUOT","\\\"");
3810N/A attrToComplete.add(attributeValue);
3810N/A }
3600N/A }
3600N/A k++;
3600N/A }
3600N/A
3810N/A if ( attributeToModify != null &&
3810N/A ( newAttributeValue != null || changetype.equals("delete") ) ) {
3600N/A
3600N/A BasicAttribute attrToComplete = null;
3600N/A
3600N/A attrToComplete = new BasicAttribute(attributeToModify);
3600N/A attributeSet.add(attrToComplete);
3810N/A if (newAttributeValue != null) {
3810N/A // as opposed to (attributeValue == null), for example in some
3810N/A // attribute delete operations
3810N/A newAttributeValue=newAttributeValue.replaceAll("QUOT","\\\"");
3810N/A attrToComplete.add(newAttributeValue);
3810N/A }
3600N/A }
3600N/A
3600N/A Iterator it2 = attributeSet.iterator();
3600N/A while (it2.hasNext()) {
3600N/A BasicAttribute attr = (BasicAttribute)it2.next();
3600N/A attributes.put(attr);
3600N/A }
3600N/A
2789N/A String provider = "ldap://" + hostname + ":" + ldapPort + "/";
2789N/A
3600N/A envLdap.put("java.naming.factory.initial",
3600N/A "com.sun.jndi.ldap.LdapCtxFactory");
3600N/A envLdap.put(Context.SECURITY_AUTHENTICATION, "simple");
3600N/A envLdap.put(Context.SECURITY_PRINCIPAL, principal);
3600N/A envLdap.put(Context.SECURITY_CREDENTIALS, credential);
3600N/A envLdap.put(Context.PROVIDER_URL, provider);
2789N/A
3600N/A try {
3600N/A CompositeName entryDN = new CompositeName(dnToModify);
3600N/A System.out.println("Modify the entry " + dnToModify);
2789N/A
3600N/A // connect to server
3600N/A ctx = new InitialLdapContext(envLdap, null);
2789N/A
3600N/A // replace attribute
3600N/A if (changetype.equals("replace")) {
3600N/A ctx.modifyAttributes(entryDN,
3600N/A LdapContext.REPLACE_ATTRIBUTE,
3600N/A attributes);
3600N/A } else if (changetype.equals("add")) {
3600N/A // add attribute
3600N/A ctx.modifyAttributes(entryDN,
3600N/A LdapContext.ADD_ATTRIBUTE,
3600N/A attributes);
3600N/A } else if (changetype.equals("delete")) {
3600N/A // add attribute
3600N/A ctx.modifyAttributes(entryDN,
3600N/A LdapContext.REMOVE_ATTRIBUTE,
3600N/A attributes);
3600N/A }
2789N/A
3600N/A ctx.close();
3600N/A } catch (CommunicationException e1) {
3600N/A errorMessage = e1.getMessage();
3600N/A } catch (NamingException e2) {
3600N/A errorMessage = e2.getMessage();
2789N/A } catch (Exception e3) {
3600N/A errorMessage= e3.getMessage();
3600N/A }
3600N/A
3600N/A // No error, the modify is success
2789N/A if ( errorMessage == null ) {
3600N/A errorCode="0";
3600N/A } else {
3600N/A System.out.println(errorMessage);
3600N/A int ind=errorMessage.indexOf("-");
3600N/A if ( ind > 0 ) {
3600N/A errorCode=errorMessage.substring(18, ind-1);
3600N/A } else errorCode="0";
2789N/A }
3600N/A
2789N/A int RC = Integer.parseInt(errorCode);
2789N/A System.exit(RC);
3600N/A }
3600N/A
3215N/A}