5.3 Using the Application Client

This section provides steps for storing and/or retrieving the state of an LDAP object by an application client. The examples in the procedure below demonstrates storing and/or retrieving an object of the LDAPAttribute class.

5.3.1 Serializing an Object

  1. Specify the -s option to serialize an object.

  2. Specify the host name of Directory server.

  3. Specify login DN and password of the user or administrator.

  4. Specify the LDAP entry and attribute name of the entry you want to store and/or serialize.

  5. Specify the destination where you want to store the attribute object (in this case it is to a file in the OS). The specified attribute object is read from the LDAP server.

  6. Pass the LDAP attribute object read from the directory to the serialization API as follows:

     //call serialization method
     	try{
         	//Construct an Output stream to a File
         ObjectOutputStream out =
             			new ObjectOutputStream(
     				            new FileOutputStream(fileName));
         	//pass the read LDAP attribute object as an argument
         //call writeObject method to write the object
         out.writeObject(ldapattr);
     
        //close the stream after object is written
        		out.close();
     
        //print the serialized object
        System.out.println("Object written =" + ldapattr.toString());
     	}
        	//Handle any exception occurred
        	catch(IOException e){
              System.out.println("Error: " + e.toString());
     }
     

    After entering the above, the XML structure written to the selected file is similar to the following:

     ¬í _sr _com.novell.ldap.LDAPAttributeÖÌ_Fí
     ***********************************************************************
     ** The encrypted data above and below is the Class definition and ***
     ** other data specific to Java Serialization Protocol. The data *******
     ** which is of most application specific interest is as follows... *****
     ************************************************************************
     ****************** Start of application data****************************
     ************************************************************************
     <LDAPAttribute>
         <attr name="uniqueID">
             <value>user10</value>
         </attr>
     </LDAPAttribute>
     ************************************************************************
     ****************** End of application data*****************************
     ************************************************************************
     x
     

In the above output, the data below and above the start and end of application data headings respectively, is of application-specific interest. Other data above and below these headings are specific to the serialization protocol. Also, some serialization-specific demarcation bytes are embedded in the actual data if the data stored is large. An administrator can easily visualize the data presented in this block. If the data is presented in purely raw bytes as obtained by using Java serialization only, it would be very difficult for a user to perceive. This is where the default DSML serialization provides added service over Java serialization.

5.3.2 De-Serializing an Object

  1. Specify the -d option to de-serialize an object.

  2. Specify the filename from which the LDAPAttribute object is to be read.

  3. Call the de-serialization API to read and construct the LDAPAttribute object as follows:

     //call de-serialization method
     	try{
        //Construct an Input stream to File
        		ObjectInputStream in =
          			new ObjectInputStream(
             				new FileInputStream(fileName));
     
        //call readObject method to read and construct the LDAP attribute object
        LDAPAttribute ldapattr = (LDAPAttribute)in.readObject();
     
        //Print the runtime object after de-serialization		
        System.out.println("Object read =" + ldapattr.toString());
     
        //close the input stream		
        in.close();	
     	}
     	//Handle any exceptions occurred
     catch(IOException io){
           		System.out.println("Error: " + io.toString());
     	}
     	catch(ClassNotFoundException ce){
          			System.out.println("Error: " + ce.toString());
     	}
     

    To view and execute the complete code, refer to the following sample programs in LDAP SDK:

    • LDAPAttributeDSMLSerialization.java

    • LDAPSchemaDSMLSerialization.java