modifyAnAttribute.java revision 1fb669628faa2eb90d370eb249980b3a1807baba
a23fd118e437af0a7877dd313db8fdaa3537c675yl/*
a23fd118e437af0a7877dd313db8fdaa3537c675yl * CDDL HEADER START
a23fd118e437af0a7877dd313db8fdaa3537c675yl *
a23fd118e437af0a7877dd313db8fdaa3537c675yl * The contents of this file are subject to the terms of the
a23fd118e437af0a7877dd313db8fdaa3537c675yl * Common Development and Distribution License, Version 1.0 only
a23fd118e437af0a7877dd313db8fdaa3537c675yl * (the "License"). You may not use this file except in compliance
a23fd118e437af0a7877dd313db8fdaa3537c675yl * with the License.
a23fd118e437af0a7877dd313db8fdaa3537c675yl *
a23fd118e437af0a7877dd313db8fdaa3537c675yl * You can obtain a copy of the license at
a23fd118e437af0a7877dd313db8fdaa3537c675yl * trunk/opends/resource/legal-notices/OpenDS.LICENSE
a23fd118e437af0a7877dd313db8fdaa3537c675yl * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
a23fd118e437af0a7877dd313db8fdaa3537c675yl * See the License for the specific language governing permissions
a23fd118e437af0a7877dd313db8fdaa3537c675yl * and limitations under the License.
a23fd118e437af0a7877dd313db8fdaa3537c675yl *
a23fd118e437af0a7877dd313db8fdaa3537c675yl * When distributing Covered Code, include this CDDL HEADER in each
a23fd118e437af0a7877dd313db8fdaa3537c675yl * file and include the License file at
a23fd118e437af0a7877dd313db8fdaa3537c675yl * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
a23fd118e437af0a7877dd313db8fdaa3537c675yl * add the following below this CDDL HEADER, with the fields enclosed
a23fd118e437af0a7877dd313db8fdaa3537c675yl * by brackets "[]" replaced with your own identifying information:
a23fd118e437af0a7877dd313db8fdaa3537c675yl * Portions Copyright [yyyy] [name of copyright owner]
8347601bcb0a439f6e50fc36b4039a73d08700e1yl *
a23fd118e437af0a7877dd313db8fdaa3537c675yl * CDDL HEADER END
a23fd118e437af0a7877dd313db8fdaa3537c675yl *
a23fd118e437af0a7877dd313db8fdaa3537c675yl *
a23fd118e437af0a7877dd313db8fdaa3537c675yl * Portions Copyright 2007 Sun Microsystems, Inc.
a23fd118e437af0a7877dd313db8fdaa3537c675yl */
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675ylimport java.util.Properties;
a23fd118e437af0a7877dd313db8fdaa3537c675ylimport java.lang.*;
a23fd118e437af0a7877dd313db8fdaa3537c675ylimport java.util.Hashtable;
a23fd118e437af0a7877dd313db8fdaa3537c675ylimport javax.naming.Context;
a23fd118e437af0a7877dd313db8fdaa3537c675ylimport javax.naming.NamingException;
a23fd118e437af0a7877dd313db8fdaa3537c675ylimport javax.naming.directory.Attribute;
a23fd118e437af0a7877dd313db8fdaa3537c675ylimport javax.naming.directory.Attributes;
a23fd118e437af0a7877dd313db8fdaa3537c675ylimport javax.naming.ldap.LdapContext;
a23fd118e437af0a7877dd313db8fdaa3537c675ylimport javax.naming.ldap.InitialLdapContext;
a23fd118e437af0a7877dd313db8fdaa3537c675ylimport javax.naming.CompositeName;
a23fd118e437af0a7877dd313db8fdaa3537c675ylimport javax.naming.directory.BasicAttribute;
a23fd118e437af0a7877dd313db8fdaa3537c675ylimport javax.naming.directory.BasicAttributes;
a23fd118e437af0a7877dd313db8fdaa3537c675ylimport javax.naming.*;
a23fd118e437af0a7877dd313db8fdaa3537c675ylimport javax.naming.directory.ModificationItem;
a23fd118e437af0a7877dd313db8fdaa3537c675ylimport java.util.HashSet;
a23fd118e437af0a7877dd313db8fdaa3537c675ylimport java.util.StringTokenizer;
a23fd118e437af0a7877dd313db8fdaa3537c675ylimport java.util.Iterator;
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl/**
a23fd118e437af0a7877dd313db8fdaa3537c675yl * modify an entry with an attribute
a23fd118e437af0a7877dd313db8fdaa3537c675yl * the operation can be a replace, delete or a add new attribute
a23fd118e437af0a7877dd313db8fdaa3537c675yl * if expectedErrorCode is set, we compare it with the ldap error code returned in the exception
a23fd118e437af0a7877dd313db8fdaa3537c675yl * the function returns 0 if the code are equals
a23fd118e437af0a7877dd313db8fdaa3537c675yl * otherwise, returns 1
a23fd118e437af0a7877dd313db8fdaa3537c675yl */
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675ylpublic class modifyAnAttribute {
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl public static void main(String[] args) {
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl String hostname=null;
a23fd118e437af0a7877dd313db8fdaa3537c675yl String ldapPort=null;
a23fd118e437af0a7877dd313db8fdaa3537c675yl String principal=null;
a23fd118e437af0a7877dd313db8fdaa3537c675yl String credential=null;
a23fd118e437af0a7877dd313db8fdaa3537c675yl String attributeToModify=null;
a23fd118e437af0a7877dd313db8fdaa3537c675yl String dnToModify=null;
a23fd118e437af0a7877dd313db8fdaa3537c675yl String newAttributeValue=null;
a23fd118e437af0a7877dd313db8fdaa3537c675yl String changetype=null;
a23fd118e437af0a7877dd313db8fdaa3537c675yl String errorCode=null;
a23fd118e437af0a7877dd313db8fdaa3537c675yl String errorMessage=null;
a23fd118e437af0a7877dd313db8fdaa3537c675yl String listAttributesToModify=null;
a23fd118e437af0a7877dd313db8fdaa3537c675yl String expectedErrorCode="0";
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl int ind1;
a23fd118e437af0a7877dd313db8fdaa3537c675yl String attributeName;
a23fd118e437af0a7877dd313db8fdaa3537c675yl String attributeValue;
a23fd118e437af0a7877dd313db8fdaa3537c675yl Hashtable envLdap = new Hashtable();
a23fd118e437af0a7877dd313db8fdaa3537c675yl LdapContext ctx;
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl Attributes attributes = new BasicAttributes();
a23fd118e437af0a7877dd313db8fdaa3537c675yl HashSet attributeSet = new HashSet();
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl for (int k=0; k< args.length; k++) {
a23fd118e437af0a7877dd313db8fdaa3537c675yl String opt1 = args[k];
a23fd118e437af0a7877dd313db8fdaa3537c675yl String val1 = args[k+1];
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl if (opt1.equals("-h")) {
a23fd118e437af0a7877dd313db8fdaa3537c675yl hostname = val1;
a23fd118e437af0a7877dd313db8fdaa3537c675yl }
a23fd118e437af0a7877dd313db8fdaa3537c675yl if (opt1.equals("-p")) {
a23fd118e437af0a7877dd313db8fdaa3537c675yl ldapPort = val1;
8347601bcb0a439f6e50fc36b4039a73d08700e1yl }
8347601bcb0a439f6e50fc36b4039a73d08700e1yl if (opt1.equals("-D")) {
8347601bcb0a439f6e50fc36b4039a73d08700e1yl principal = val1;
a23fd118e437af0a7877dd313db8fdaa3537c675yl }
a23fd118e437af0a7877dd313db8fdaa3537c675yl if (opt1.equals("-w")) {
a23fd118e437af0a7877dd313db8fdaa3537c675yl credential = val1;
a23fd118e437af0a7877dd313db8fdaa3537c675yl }
a23fd118e437af0a7877dd313db8fdaa3537c675yl if (opt1.equals("-d")) {
a23fd118e437af0a7877dd313db8fdaa3537c675yl dnToModify = val1;
a23fd118e437af0a7877dd313db8fdaa3537c675yl }
a23fd118e437af0a7877dd313db8fdaa3537c675yl if (opt1.equals("-v")) {
a23fd118e437af0a7877dd313db8fdaa3537c675yl newAttributeValue = val1;
a23fd118e437af0a7877dd313db8fdaa3537c675yl }
a23fd118e437af0a7877dd313db8fdaa3537c675yl if (opt1.equals("-a")) {
a23fd118e437af0a7877dd313db8fdaa3537c675yl attributeToModify = val1;
a23fd118e437af0a7877dd313db8fdaa3537c675yl }
a23fd118e437af0a7877dd313db8fdaa3537c675yl if (opt1.equals("-t")) {
a23fd118e437af0a7877dd313db8fdaa3537c675yl changetype = val1;
a23fd118e437af0a7877dd313db8fdaa3537c675yl }
a23fd118e437af0a7877dd313db8fdaa3537c675yl if (opt1.equals("-E")) {
a23fd118e437af0a7877dd313db8fdaa3537c675yl expectedErrorCode = val1;
a23fd118e437af0a7877dd313db8fdaa3537c675yl }
a23fd118e437af0a7877dd313db8fdaa3537c675yl if (opt1.equals("-l")) {
a23fd118e437af0a7877dd313db8fdaa3537c675yl listAttributesToModify = val1;
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl ind1= val1.indexOf (":");
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl attributeName=val1.substring (0,ind1);
a23fd118e437af0a7877dd313db8fdaa3537c675yl attributeValue=val1.substring (ind1+1);
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl BasicAttribute attrToComplete = null;
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl Iterator it = attributeSet.iterator();
a23fd118e437af0a7877dd313db8fdaa3537c675yl while (attrToComplete == null && it.hasNext()) {
a23fd118e437af0a7877dd313db8fdaa3537c675yl BasicAttribute attr = (BasicAttribute) it.next();
a23fd118e437af0a7877dd313db8fdaa3537c675yl if ((attr.getID()).equalsIgnoreCase(attributeName)) {
a23fd118e437af0a7877dd313db8fdaa3537c675yl attrToComplete = attr;
a23fd118e437af0a7877dd313db8fdaa3537c675yl }
a23fd118e437af0a7877dd313db8fdaa3537c675yl }
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl if (attrToComplete == null) {
a23fd118e437af0a7877dd313db8fdaa3537c675yl attrToComplete = new BasicAttribute(attributeName);
a23fd118e437af0a7877dd313db8fdaa3537c675yl attributeSet.add(attrToComplete);
a23fd118e437af0a7877dd313db8fdaa3537c675yl }
a23fd118e437af0a7877dd313db8fdaa3537c675yl attributeValue=attributeValue.replaceAll("QUOT","\\\"");
a23fd118e437af0a7877dd313db8fdaa3537c675yl attrToComplete.add(attributeValue);
a23fd118e437af0a7877dd313db8fdaa3537c675yl }
a23fd118e437af0a7877dd313db8fdaa3537c675yl k++;
a23fd118e437af0a7877dd313db8fdaa3537c675yl }
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl if ( attributeToModify != null && newAttributeValue != null ) {
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl BasicAttribute attrToComplete = null;
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl attrToComplete = new BasicAttribute(attributeToModify);
a23fd118e437af0a7877dd313db8fdaa3537c675yl attributeSet.add(attrToComplete);
a23fd118e437af0a7877dd313db8fdaa3537c675yl newAttributeValue=newAttributeValue.replaceAll("QUOT","\\\"");
a23fd118e437af0a7877dd313db8fdaa3537c675yl attrToComplete.add(newAttributeValue);
a23fd118e437af0a7877dd313db8fdaa3537c675yl }
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl Iterator it2 = attributeSet.iterator();
a23fd118e437af0a7877dd313db8fdaa3537c675yl while (it2.hasNext()) {
a23fd118e437af0a7877dd313db8fdaa3537c675yl BasicAttribute attr = (BasicAttribute)it2.next();
a23fd118e437af0a7877dd313db8fdaa3537c675yl attributes.put(attr);
a23fd118e437af0a7877dd313db8fdaa3537c675yl }
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl String provider = "ldap://" + hostname + ":" + ldapPort + "/";
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl envLdap.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory");
a23fd118e437af0a7877dd313db8fdaa3537c675yl envLdap.put(Context.SECURITY_AUTHENTICATION, "simple");
a23fd118e437af0a7877dd313db8fdaa3537c675yl envLdap.put(Context.SECURITY_PRINCIPAL, principal);
a23fd118e437af0a7877dd313db8fdaa3537c675yl envLdap.put(Context.SECURITY_CREDENTIALS, credential);
a23fd118e437af0a7877dd313db8fdaa3537c675yl envLdap.put(Context.PROVIDER_URL, provider);
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl try {
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl CompositeName entryDN = new CompositeName (dnToModify);
a23fd118e437af0a7877dd313db8fdaa3537c675yl System.out.println(changetype + " attribute " + attributeToModify + " for entry " + dnToModify);
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl // connect to server
a23fd118e437af0a7877dd313db8fdaa3537c675yl ctx = new InitialLdapContext(envLdap, null);
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl // replace attribute
a23fd118e437af0a7877dd313db8fdaa3537c675yl if (changetype.equals("replace")) {
a23fd118e437af0a7877dd313db8fdaa3537c675yl ctx.modifyAttributes(entryDN, LdapContext.REPLACE_ATTRIBUTE , attributes);
a23fd118e437af0a7877dd313db8fdaa3537c675yl }
a23fd118e437af0a7877dd313db8fdaa3537c675yl else if (changetype.equals("add")) {
a23fd118e437af0a7877dd313db8fdaa3537c675yl // add attribute
a23fd118e437af0a7877dd313db8fdaa3537c675yl ctx.modifyAttributes(entryDN, LdapContext.ADD_ATTRIBUTE , attributes);
a23fd118e437af0a7877dd313db8fdaa3537c675yl }
a23fd118e437af0a7877dd313db8fdaa3537c675yl else if (changetype.equals("delete")) {
a23fd118e437af0a7877dd313db8fdaa3537c675yl // add attribute
a23fd118e437af0a7877dd313db8fdaa3537c675yl ctx.modifyAttributes(entryDN, LdapContext.REMOVE_ATTRIBUTE , attributes);
a23fd118e437af0a7877dd313db8fdaa3537c675yl }
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl ctx.close();
a23fd118e437af0a7877dd313db8fdaa3537c675yl } catch (CommunicationException e1) {
a23fd118e437af0a7877dd313db8fdaa3537c675yl errorMessage = e1.getMessage();
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl } catch (NamingException e2) {
a23fd118e437af0a7877dd313db8fdaa3537c675yl errorMessage = e2.getMessage();
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl } catch (Exception e3) {
a23fd118e437af0a7877dd313db8fdaa3537c675yl errorMessage= e3.getMessage();
a23fd118e437af0a7877dd313db8fdaa3537c675yl }
a23fd118e437af0a7877dd313db8fdaa3537c675yl // No error, the modify is success
a23fd118e437af0a7877dd313db8fdaa3537c675yl if ( errorMessage == null ) {
a23fd118e437af0a7877dd313db8fdaa3537c675yl errorCode="0";
a23fd118e437af0a7877dd313db8fdaa3537c675yl }
a23fd118e437af0a7877dd313db8fdaa3537c675yl else {
a23fd118e437af0a7877dd313db8fdaa3537c675yl System.out.println (errorMessage);
a23fd118e437af0a7877dd313db8fdaa3537c675yl int ind=errorMessage.indexOf("-");
a23fd118e437af0a7877dd313db8fdaa3537c675yl errorCode=errorMessage.substring(18, ind-1);
a23fd118e437af0a7877dd313db8fdaa3537c675yl }
a23fd118e437af0a7877dd313db8fdaa3537c675yl // Compare the errorCode and the expected error Code
a23fd118e437af0a7877dd313db8fdaa3537c675yl int diff=expectedErrorCode.compareTo(errorCode);
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl if (diff == 0 ){
a23fd118e437af0a7877dd313db8fdaa3537c675yl // Got the expected error Code. The test is success
a23fd118e437af0a7877dd313db8fdaa3537c675yl System.exit(0);
a23fd118e437af0a7877dd313db8fdaa3537c675yl }
a23fd118e437af0a7877dd313db8fdaa3537c675yl else {
a23fd118e437af0a7877dd313db8fdaa3537c675yl System.out.println ("Error: modify request didn't return the expected error Code");
a23fd118e437af0a7877dd313db8fdaa3537c675yl System.out.println ("Expected " + expectedErrorCode + "; Got" + errorCode);
a23fd118e437af0a7877dd313db8fdaa3537c675yl System.exit(1);
a23fd118e437af0a7877dd313db8fdaa3537c675yl }
a23fd118e437af0a7877dd313db8fdaa3537c675yl
a23fd118e437af0a7877dd313db8fdaa3537c675yl}
a23fd118e437af0a7877dd313db8fdaa3537c675yl}
a23fd118e437af0a7877dd313db8fdaa3537c675yl