29.3 Notification Example

The following code example shows how one might use the Notification service to send an email message using a pre-defined system template. To get a reference to the SOAP endpoint for the Notification service, a call is made to the getNotificationStub() method. After acquiring the stub interface, the code sets the email notification template as well as values for the built-in tokens in the template. In addition, the code specifies values for the requestTitle and initiatorFullName tokens. For each token, the code creates an Entry object. Once all of the entries have been created, it packages the entry array into a map of type NotificationMap, which is then passed to the sendNotification method on the stub.

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.xml.rpc.Stub;
import java.rmi.RemoteException;
//
// Notification imports
import com.novell.ws.client.notification.IRemoteNotification;
import com.novell.ws.client.notification.BuiltInTokens;
import com.novell.ws.client.notification.Entry;
import com.novell.ws.client.notification.EntryArray;
import com.novell.ws.client.notification.StringArray;
import com.novell.ws.client.notification.NotificationMap;
import com.novell.ws.client.notification.IRemoteNotification;
import com.novell.ws.client.notification.NotificationService;


public class NotificationTest
{
  private static final int LOCALHOST    = 0;  // localhost
  private static final int TESTSERVER      = 1;  // testserver
  private static final int SELECTED_URL     = TESTSERVER;

      private String [] SERVER_URLS = {
    "http://localhost:8080/IDMProv/notification/service",
    "http://testserver:8080/IDMProv/notification/service"
  }; 
  private String url = SERVER_URLS[SELECTED_URL];
  private String username = "cn=admin,ou=idmsample,o=netiq";
  private String password = "test";

  public void emailNotificationTestCase()
  throws Exception
  {
        System.out.println("\nCalling emailNotificationTestCase() test
case");

        try
        {
    String targetEmailAddress = "jsmith@somewhere.com";
            //
            // Get the notification stub
            IRemoteNotification notificationStub =
getNotificationStub(url, username, password);
            
            BuiltInTokens builtInTokens = new BuiltInTokens();
            //
            // Set the To: entry
            Entry to = new Entry();
            to.setKey(builtInTokens.getTO());
            StringArray arr = new StringArray(new
String[]{targetEmailAddress} );
            to.setValues(arr);
            //
            // Set which email template to use : list in iManager
(Workflow Admin->Email Templates)
            Entry notificationTemplate = new Entry();

notificationTemplate.setKey(builtInTokens.getNOTIFICATION_TEMPLATE_DN());
            //
            // Use one of the email templates specifying DN
            String EMAIL_TEMPLATE_NAME = "Provisioning Notification";
            String templateDN = "cn=" + EMAIL_TEMPLATE_NAME +
",cn=Default Notification Collection,cn=Security";
            arr = new StringArray(new String[]{templateDN} );
            notificationTemplate.setValues(arr);
            //
            // Substitute key values defined in email templates
            Entry token1 = new Entry();
            token1.setKey("requestTitle"); // key is %requestTitle%
            arr = new StringArray(new String[]{"Sample Email using
Notification Web Service"} );
            token1.setValues(arr);
            Entry token2 = new Entry();
            token2.setKey("initiatorFullName");
            arr = new StringArray(new String[]{username} );
            token2.setValues(arr);
            //
            // Setup the notification map
            NotificationMap map = new NotificationMap();
            Entry[] entries = new
Entry[]{to,notificationTemplate,token1,token2};
            EntryArray entryArray = new EntryArray();
            entryArray.setEntry(entries);
            map.setEntries(entryArray);
            //
            // Make the notification endpoint call
            notificationStub.sendNotification(map);
        }
        catch(RemoteException error) 
        {
            System.out.println(error.getMessage() );
            throw new Exception(error.getMessage() );
        }
     }


   /**
     * Method to obtain the remote interface to the Notification
endpoint
     * @param _url
     * @param _username
     * @param _password
     * @return IRemoteNotification interface 
     * @throws Exception
     */
    private IRemoteNotification getNotificationStub(String _url,
String _username, String _password)
    throws Exception
    {
        Properties properties = new Properties();
        properties.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");

        String lookup =
"xmlrpc:soap:com.novell.ws.client.notification.NotificationService";

        InitialContext ctx = new InitialContext();
        NotificationService svc = (NotificationService)
ctx.lookup(lookup);

        Stub stub = (Stub)svc.getIRemoteNotificationPort();

        stub._setProperty(Stub.USERNAME_PROPERTY, _username);
        stub._setProperty(Stub.PASSWORD_PROPERTY, _password);
        stub._setProperty(Stub.SESSION_MAINTAIN_PROPERTY,
Boolean.TRUE);
        stub._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY, _url);

        return (IRemoteNotification) stub;
    }

}