mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

smaguin
21.42.2007 b7e59840238780cbdb0b996a41578359084a7751
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * Copyright (c) 1998. Sun Microsystems, Inc. All Rights Reserved.
 *
 * "@(#)addAnewEntry.java   1.2 98/04/22 SMI"
 */
 
import java.util.Properties;
import java.lang.*;
import java.util.Hashtable;
import   javax.naming.Context;
import   javax.naming.NamingException;
import   javax.naming.directory.Attribute;
import   javax.naming.directory.Attributes;
import   javax.naming.ldap.LdapContext;
import   javax.naming.ldap.InitialLdapContext;
import  javax.naming.CompositeName;
import   javax.naming.directory.BasicAttribute;
import   javax.naming.directory.BasicAttributes;
import  javax.naming.*;
import  javax.naming.directory.ModificationItem;
import  java.util.HashSet;
import  java.util.StringTokenizer;
import java.util.Iterator;
 
/**
  *  add a new   entry
  *  input : 
  *    -D : principal
  *    -w : credential
  *    -p : ldapport
  *    -h : ldaphost
  *    -d : dn to add
  *    -v : attribut name:attribut value
  *        : this option is multi valued
  */
 
public class addAnEntry {
 
    public static void main(String[] args) {
 
     String hostname=null;
     String ldapPort=null;
     String principal=null;
     String credential=null;
     String dnToAdd=null;
     String attributeToAdd=null;
 
     
     int ind1;
     String attributeName;
     String attributeValue;
     
     Attributes attributes = new BasicAttributes(); 
     HashSet attributeSet = new HashSet(); 
 
     for (int k=0; k< args.length; k++) {
         String opt1 = args[k]; 
         String val1 = args[k+1];
         
         if (opt1.equals("-h")) {
             hostname = val1;
         }
         if (opt1.equals("-p")) {
             ldapPort = val1;
         }
         if (opt1.equals("-D")) {
             principal = val1;
         }
         if (opt1.equals("-w")) {
             credential = val1;
         }
         if (opt1.equals("-d")) {
             dnToAdd = val1;
         }
         if (opt1.equals("-v")) {
            attributeToAdd = val1;
             
            ind1= val1.indexOf (":");
 
            attributeName=val1.substring (0,ind1);
            attributeValue=val1.substring (ind1+1);
 
            BasicAttribute attrToComplete = null;
         
            Iterator it = attributeSet.iterator();
            while (attrToComplete == null && it.hasNext()) {
                BasicAttribute attr = (BasicAttribute) it.next();
                if ((attr.getID()).equalsIgnoreCase(attributeName)) {
                    attrToComplete = attr;
                }
            }
            
            if (attrToComplete == null) {
                attrToComplete = new BasicAttribute(attributeName);
                attributeSet.add(attrToComplete);
            }
            attrToComplete.add(attributeValue);
         }
         k++;
     } 
      
     
     Iterator it2 = attributeSet.iterator();
     while (it2.hasNext()) {
         BasicAttribute attr = (BasicAttribute)it2.next();
         attributes.put(attr);
     }
     
    String provider = "ldap://"  + hostname + ":" + ldapPort  + "/";
 
    Hashtable envLdap  = new Hashtable();
    
    envLdap.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory");
    envLdap.put(Context.SECURITY_AUTHENTICATION, "simple");
    envLdap.put(Context.SECURITY_PRINCIPAL,  principal);
    envLdap.put(Context.SECURITY_CREDENTIALS, credential);
    envLdap.put(Context.PROVIDER_URL, provider);
 
    LdapContext ctx;
    
    try {
        
        CompositeName entryDN = new CompositeName (dnToAdd);
        System.out.println("Add a new  entry " + entryDN); 
        
        // connect to server
        ctx = new InitialLdapContext(envLdap, null);    
 
        ctx.createSubcontext(entryDN, attributes);
 
        ctx.close();
        
 
    } catch (CommunicationException e1) {
        String error = e1.getMessage();
 
        System.out.println(" Catch exception : " + e1.getMessage());
        System.exit(1);
    } catch (NamingException e2) {
         // resultCode = ; 
        System.out.println(" Catch exception : " + e2.getMessage());
        System.exit(1);
    } catch (Exception e3) {
 
            System.out.println(" Catch exception : " + e3.getMessage());
            System.exit(1);
    }
}
}