LDAP Adding entries to the server directory :: how to ?
Hello all,
I have a code that adds the entries to the LDAP directory server and this code is in JAVA
But this code does compile but when i run it it gives Runtime error as
::::: Invalid credentials::::::
The server is running when the code is run. for this do i need to specify certain schema in the .CONF file or anything else
The server I am using is OpenLDAP for Windows and Netscape directory SDK for JAVA
plz tell me about some another server available for LDAP support(free version) with JAVA (For Windows)
Plz tell me about the server and other information about LDAP Directory server so that i could find support for it easily
The code is as below::::::::::::::::::
import netscape.ldap.*;
import java.util.*;
public class Add {
public static void main( String[] args )
{
String dn = "uid=ac" ;
String objectclass_values[] = { "top","person","organizationalPerson","inetOrgPerson" };
LDAPAttributeSet attrs = new LDAPAttributeSet();
LDAPAttribute attr = new LDAPAttribute( "objectclass" );
for( int i = 0; i < objectclass_values.length; i++ ) {
attr.addValue( objectclass_values[i] );
}
attrs.add( attr );
attrs.add( new LDAPAttribute( "uid", "wbjensen" ) );
LDAPEntry myEntry = new LDAPEntry( dn, attrs );
LDAPConnection ld = null;
int status = -1;
try {
ld = new LDAPConnection();
String MY_HOST = "localhost";
int MY_PORT = 389;
ld.connect( MY_HOST, MY_PORT );
String MGR_DN = "dc=monarch,dc=com";
String MGR_PW = "ali";
ld.authenticate( MGR_DN, MGR_PW );
System.out.println("Entry established");
ld.add( myEntry );
System.out.println( "Entry added" );
System.out.println("Entry established1111");
}
catch( LDAPException e ) {
if ( e.getLDAPResultCode() == LDAPException.ENTRY_ALREADY_EXISTS )
System.out.println( "Error: Entry already present" );
else
System.out.println( "Error: " + e.toString()+"AA che error" );
}
if ( (ld != null) && ld.isConnected() ) {
try {
ld.disconnect();
} catch ( LDAPException e ) {
System.out.println( "Error: " + e.toString() );
}
}
System.exit(status);
}
}
Regards
Jignesh
[2512 byte] By [
j.gohel] at [2007-11-19 7:33:11]

# 1 Re: LDAP Adding entries to the server directory :: how to ?
From looking at the code, I have questions about the following line:
String MGR_DN = "dc=monarch,dc=com";
This does not look to be a good distinguishedName (dn) for an account. I would expect this to be something more like "cn=manager".
Try using the account you set up as the administrator / manager of the directory.
The following may cause you issues as well:
String dn = "uid=ac" ;
I think this should probably be something like
String dn = "uid=ac,dc=monarch,dc=com" ;
or
String dn = "uid=ac,ou=users,dc=monarch,dc=com" ;
the dn should represent the full path to this user object. This path has to include the container where the user object will reside.
The second example dn above uses an imaginary organizationalUnit (ou) - I do not know what, if any ou's exist in your directory - and the domainComponent (dc) that contains the ou.
Also check to see how the directory set up - it may want the commonName (cn) to be part of the dn instead of uid.
And, I think you may be missing a mandatory attribute for the new user object - cn - this is usually a combination of the first and last names and can be part of the dn.
HTH
# 2 Re: LDAP Adding entries to the server directory :: how to ?
Hello
Thanks for the reply.
I thought that adding
::: String dn="uid=ac,ou=java,o=cygnet" ::::
this type of entry would be directly be added to the directory .Is it necesary to have the RDN's ie the ending ones ie if my baseDN is
"cn=Manager,dc=monarch,dc=com"
then the dn should have entry like
"uid=ac,ou=cygnet,dc=monarch,dc=com"
Is it so or else the baseDN and the entry DN could be different as i have specified
Please specify as i am a newbie
Thanks
Regards
Jignesh
# 3 Re: LDAP Adding entries to the server directory :: how to ?
After looking at the Sun LDAPEntry (http://docs.sun.com/source/816-5618-10/netscape/ldap/LDAPEntry.html) web page, I think that the dn for the new LDAPEntry should be the full dn not an rdn - I think that is how the object is found.
From the Sun LDAPConnection (http://docs.sun.com/source/816-5618-10/netscape/ldap/LDAPConnection.html) web page:
add
public void add(LDAPEntry entry)
throws LDAPException
Adds an entry to the directory.
Before using this method, you need to create an LDAPEntry object and use it to specify the distinguished name and attributes of the new entry. Make sure to specify values for all required attributes in the entry. If all required attributes are not specified and the LDAP server checks the entry against the schema, an LDAPException may be thrown (where the LDAP result code is OBJECT_CLASS_VIOLATION).
For example, the following section of code creates an LDAPEntry object for a new entry and uses the object to add the new entry to the directory. Because the definition of the LDAP inetOrgPerson class specifies that the cn, sn, and objectclass attributes are required, these attributes are specified as part of the new entry. (mail is not required but is shown here as an example of specifying additional attributes.)
Here is a good example of creating a user in LDAP using java from LDAP GURU (http://www.ldapguru.com/modules/newbb/viewtopic.php?topic_id=1999&forum=6&post_id=6017)
public class Add
{
public static void main( String[] args )
{
/* Specify the DN we're adding */
String dn = "uid=wbjensen, ou=People, o=Airius.com";
/* Specify the attributes of the entry */
String objectclass_values[] = { "top",
"person",
"organizationalPerson",
"inetOrgPerson" };
String cn_values[] = { "William B Jensen",
"William Jensen",
"Bill Jensen" };
String sn_values[] = { "Jensen" };
String givenname_values[] = { "William", "Bill" };
String telephonenumber_values[] = { "+1 415 555 1212" };
LDAPAttributeSet attrs = new LDAPAttributeSet();
LDAPAttribute attr = new LDAPAttribute( "objectclass" );
for( int i = 0; i < objectclass_values.length; i++ )
{
attr.addValue( objectclass_values[i] );
}
attrs.add( attr );
attr = new LDAPAttribute( "cn" );
for( int i = 0; i < cn_values.length; i++ )
{
attr.addValue( cn_values[i] );
}
attrs.add( attr );
attr = new LDAPAttribute( "sn" );
for( int i = 0; i < sn_values.length; i++ )
{
attr.addValue( sn_values[i] );
}
attrs.add( attr );
attr = new LDAPAttribute( "givenname" );
for( int i = 0; i < givenname_values.length; i++ )
{
attr.addValue( givenname_values[i] );
}
attrs.add( attr );
attr = new LDAPAttribute( "telephonenumber" );
for( int i = 0; i < telephonenumber_values.length; i++ )
{
attr.addValue( telephonenumber_values[i] );
}
attrs.add( attr );
attrs.add( new LDAPAttribute( "uid", "wbjensen" ) );
/* Create an entry with this DN and these attributes */
LDAPEntry myEntry = new LDAPEntry( dn, attrs );
LDAPConnection ld = null;
int status = -1;
try
{
ld = new LDAPConnection();
/* Connect to server */
String MY_HOST = "localhost";
int MY_PORT = 389;
ld.connect( MY_HOST, MY_PORT );
/* Authenticate to the server as directory manager */
String MGR_DN = "cn=Directory Manager";
String MGR_PW = "bigDaddyPassword";
ld.authenticate( MGR_DN, MGR_PW );
/* Now add the entry to the directory */
ld.add( myEntry );
System.out.println( "Entry added" );
}
catch( LDAPException e )
{
if ( e.getLDAPResultCode() == LDAPException.ENTRY_ALREADY_EXISTS )
System.out.println( "Error: Entry already present" );
else
System.out.println( "Error: " + e.toString() );
}
/* Done, so disconnect */
if ( (ld != null) && ld.isConnected() )
{
try
{
ld.disconnect();
}
catch ( LDAPException e )
{
System.out.println( "Error: " + e.toString() );
}
}
System.exit(status);
}
}
To sum it up, I believe you need to specify the dn of the new user when you create the LDAPEntry object.
then the dn should have entry like
"uid=ac,ou=cygnet,dc=monarch,dc=com"
HTH
# 4 Re: LDAP Adding entries to the server directory :: how to ?
Hello
thanks for your reply. Now when i used the dn that u had specified to me the authentication is done successfully and the contact to the server is done but while adding the entry
LDAPConnection ld = new LDAPConnection();
ld.add(myEntry);
This line of the code is not executd and the error ie runtime is ::
::: Operations Error::::
What shall be the reason
Plz reply
Rgards
Jignesh
# 5 Re: LDAP Adding entries to the server directory :: how to ?
Hello
I am able to connect to the ldap server by calling the Add.java class into any .java class and it connects successfully but the entry is not added
The code for the Add.java is being checked standalone also only it does not run with the servlet.
But when i call the same code within the servlet it does not show the result
The log file within the container od the servlet shows
LDAPException not found in doPost() method
Here is my servlet code
package com.chat.java;
import netscape.ldap.*;
import netscape.ldap.util.*;
import netscape.ldap.controls.*;
import java.io.*;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.swing.*;
import netscape.ldap.LDAPConnection;
import com.chat.ChatRoomList;
import com.chat.ChatRoom;
import com.chat.Add;
public class LoginServlet extends HttpServlet
{ LDAPConnection ld ;
boolean flag = false;
String s1,s2;
int i = 1800;
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
doPost(req,res);
}
public void doPost(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws ServletException, IOException
{
String s = "jdbc:odbc:chat";
contextPath = httpservletrequest.getContextPath();
s1 = httpservletrequest.getParameter("nickname");
s2 = httpservletrequest.getParameter("pwd");
s1 = s1.trim().toLowerCase();
if(s2.length() > 0)
s2 = s2.trim().toLowerCase();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connection = DriverManager.getConnection(s);
Statement statement = connection.createStatement();
for(ResultSet resultset = statement.executeQuery("select * from Login"); resultset.next();)
{
String s3 = resultset.getString(1);
String s4 = resultset.getString(2);
if(s1.equals(s3) && s2.equals(s4))
{
flag = true;
}
}
}//try ends here
catch(SQLException sqlexception)
{
System.out.println("Error..." + sqlexception);
}
catch(ClassNotFoundException classnotfoundexception)
{
System.out.println("Error..." + classnotfoundexception);
}
if(flag=true)
{
try
{
ChatRoomList chatroomlist = (ChatRoomList)getServletContext().getAttribute("chatroomlist");
boolean flag1 = chatroomlist.chatterExists(s1);
// THIS IS THE PLACE WHICH DOES NOT HAPPEN
// THE OBJECT IS NOT CREATED
// EXECUTION AFTER IT STOPS
Add add = new Add();
System.out.println("After add");
if(flag1)
{
JOptionPane.showMessageDialog(null,"U have already Login.....","Information",JOptionPane.INFORMATION_MESSAGE);
}
else
{
HttpSession httpsession = httpservletrequest.getSession(true);
int i = 1800;
String s2 = getServletContext().getInitParameter("sessionTimeout");
if(s2 != null)
try
{
i = Integer.parseInt(s2);
i *= 60;
}
catch(NumberFormatException numberformatexception)
{ }
httpsession.setMaxInactiveInterval(i);
httpsession.setAttribute("nickname", s1);
}
}//try ends
catch(Exception exception)
{
System.out.println("Exception thrown in LoginServlet: " + exception.getMessage());
exception.printStackTrace();
}
}//if ends here
else
{
JOptionPane.showMessageDialog(null,"Server is saying that Invalid User","Information",JOptionPane.INFORMATION_MESSAGE);
}
}//do post ends here
private final void _mththis()
{
contextPath = "";
}
public LoginServlet()
{
_mththis();
}
private String contextPath;
ObjectOutputStream out=null;
}
Please help
Regards
Jignesh
# 6 Re: LDAP Adding entries to the server directory :: how to ?
Ok, let's see............
This line of the code is not executd and the error ie runtime is ::
::: Operations Error::::
I think this error is probably due to an object class constraint violation - you are creating an object of type inetOrgPerson which requires the following attributes be set, these are mandatory attributes. The example provided earlier did not set all the manadatory attributes.
From my previous post:
Because the definition of the LDAP inetOrgPerson class specifies that the cn, sn, and objectclass attributes are required
For the second issue, the one with the servlet:
But when i call the same code within the servlet it does not show the result
The log file within the container od the servlet shows
LDAPException not found in doPost() method
The big difference I see is that in the sample code you are catching the LDAPException as an LDAPException and in the servlet you are catching it as an Exception. Does the Add class throw the exception back to the servlet and does it make a difference if you try to catch an LDAPException there?
# 7 Re: LDAP Adding entries to the server directory :: how to ?
Hello
Thanks for the reply. Now I am able to make entries to the LDAP server via the Add.java class but not throuh the servlet it runs as a standalone
When integarted with the servlet the code does not do any entry
Even it does not connect to the server . I am using the Netscape sdk for java so is it i need to include all the classes of the sdk in the container of the servlet.do i need to include the .jars to the root of the container
And one more thing that if I only want to make the entry of the user as
.......uid , company name and organization unit
which object class should i include in the Adder.java class .As i have included the code in the 1st post of mine tell me what modifications are required
regards
Jignesh
# 8 Re: LDAP Adding entries to the server directory :: how to ?
Even it does not connect to the server . I am using the Netscape sdk for java so is it i need to include all the classes of the sdk in the container of the servlet.do i need to include the .jars to the root of the container
Since I don't do alot of java programming I am not sure what you will need to do to correct this.
And one more thing that if I only want to make the entry of the user as
.......uid , company name and organization unit
which object class should i include in the Adder.java class
You can continue to use the inetOrgPerson, just set the sn and cn to the uid.
# 9 Re: LDAP Adding entries to the server directory :: how to ?
Hello
I have ran the whole code to add the entries standalone and the entries are added
But when i did it using servlet the servlet does not execute any line of the code even no print statement I could see on the console of the web server
I suppose the whole code is being bypassed coz theres no error in the log file
Plz help
Jignesh
# 10 Re: LDAP Adding entries to the server directory :: how to ?
Are you getting an error about the servlet not beign able to find the Add class or could it be accessing a different class with the same name?
Is the Add class and its supporting classes in the classpath for the servlet?
I would think it is a path type issue, but I don't know.
# 11 Re: LDAP Adding entries to the server directory :: how to ?
Hello Sir
I am not getting any error regarding the Add.java class not found but the classes that are being extended as a part of the Add.java class
(LDAPEntry,LDAPAttribute,LDAPAttributeSet,LDAPConnection) are not being reached by the code in the servlet even when i have included the classes of LDAP into the servlet container
What shall be the error
Plz help coz its urgent
Thanks
Regards
Jignesh
-- Samay samay Balwan , Nahi manushya balwan--
# 12 Re: LDAP Adding entries to the server directory :: how to ?
LIke I said before, I am not a big java person, you may want to ask about the java servlet on the java board - someone that watches that board may be able to answer this question when I can not.