Monday, November 24, 2014

How to Create SPML Applications for SIM

Version: Sun Identity Manager 8.1
References: https://docs.oracle.com/cd/E19225-01/820-5597/ahvap/index.html
https://docs.oracle.com/cd/E19225-01/820-5597/ahvch/index.html

1. The necessary JAR files can be found in "$WSHOME//WEB-INF/lib" directory. The following JAR files are needed to built SIM SPML applications:

  • openspml.jar
  • mail.jar
  • soap.jar
2. Given below is sample JAVA code to demonstrate the use of SPML client:

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openspml.client.LighthouseClient;
import org.openspml.message.ExtendedRequest;
import org.openspml.message.FilterTerm;
import org.openspml.message.ModifyRequest;
import org.openspml.message.SearchRequest;
import org.openspml.message.SearchResponse;
import org.openspml.message.SearchResult;
import org.openspml.message.SpmlResponse;
import org.openspml.util.SpmlException;

/**
 * Sun Identity Manager: SPML Client
 * Test class for demonstration purposes
 * @author oracle
 */
public class SIMSPMLClient
{
    // SIM environment information
    private static final String simServiceAccountUser = "configurator";
    private static final String simServiceAccountPassword = "configurator";
    private static final String simPort = "8080";
    private static final String simHostname = "localhost";
    private static final String simWebServiceURL = "http://" + simHostname + ":" + simPort +"/idm/servlet/rpcrouter2";
   
    // Class logger
    private static final Logger logger = Logger.getLogger(SIMSPMLClient.class.getName());
   
    /**
     * Test Driver
     * @param args
     */
    public static void main(String[] args)
    {
        LighthouseClient client = null;
        String accountId = null;
        String resourceAccountName = "Database Table";
       
        try
        {
            // Setup SIM SMPL client
            client = new LighthouseClient();
            client.setUrl(simWebServiceURL);
            client.setUser(simServiceAccountUser);
            client.setPassword(simServiceAccountPassword);
          
            // Call method to modify user attributes
            accountId = "jdoe";
            HashMap<String, Object> modAttrs = new HashMap<String, Object>();
            modAttrs.put("email", "jdoe@gmail.com");
            modAttrs.put("status", "Active");
            modifyUserAttributes(client, accountId, modAttrs);
          
            // Provision target resource
            accountId = "gbass";
            HashMap<String, Object> resAttrs = new HashMap<String, Object>();
            resAttrs.put("resources", "Database Table"); //delimited by comma
            resAttrs.put("status", "Created");
            //modifyUserAttributes(client, accountId, resAttrs);
           
            // Deprovision target resource, but does not remove assignment in SIM
            ExtendedRequest dreq = new ExtendedRequest();
            dreq.setOperationIdentifier("deleteUser");
            dreq.setAttribute("accountId","gbass");
            dreq.setAttribute("accounts","Database Table");
            //SpmlResponse res = client.request(dreq);
            //System.out.println(res.getResult());
           
            // Deprovision target resources and removes assignment
            HashMap<String, Object> resRmAttrs = new HashMap<String, Object>();
            resAttrs.put("resources", "");
            //modifyUserAttributes(client, accountId, resRmAttrs);

            // Call custom workflow
            String simWorkFlowName = "Custom-Workflow";
            String username = "jmann";
            HashMap<String, Object> wfAttrs = new HashMap<String, Object>();
            wfAttrs.put("ACTION", "MODIFY");
            wfAttrs.put("__UID__", username);
            wfAttrs.put("__NAME__", username);
            wfAttrs.put("__PASSWORD__", "jmann");
            wfAttrs.put("EMAIL", "jmann8@gmail.com");
            wfAttrs.put("LASTNAME", "");
            wfAttrs.put("USERNAME", username);
            //callSimWorkFlow(client, simWorkFlowName, wfAttrs);

           // Disable resource account
           accountId = "gbass";
           //disableResourceAccount(client, accountId, resourceAccountName);

           // Enable resource account
           //enableResourceAccount(client, accountId, resourceAccountName);
          
           // Create SIM account
           HashMap<String, String> attrs = new HashMap<String,String>();
           attrs.put("lastname", "Parker");
           attrs.put("firstname", "Peter");
           attrs.put("email", "pparker@gmail.com");
           attrs.put("password","Password1");
           //client.createUser("pparker", attrs);
          
           // Delete SIM account
           List<String> accounts = new ArrayList<String>();
           //client.deleteUser("pparker", accounts);
           
           // Search SIM User based on accountId
           SearchRequest req = new SearchRequest();
           req.addAttribute("lastname");
           req.addAttribute("email");
           FilterTerm ft = new FilterTerm();
           ft.setOperation(FilterTerm.OP_EQUAL);
           ft.setName("accountId");
           ft.setValue("jdoe");
           req.addFilterTerm(ft);
           SearchResponse res = (SearchResponse)client.request(req);
           System.out.println(res.getResults());
           List results = res.getResults();
           if (results != null) 
           {
               for (int i = 0 ; i < results.size() ; i++)
               {
                   SearchResult sr = (SearchResult) results.get(i);
                   System.out.println("Identifier=" + sr.getIdentifierString() + " sn=" + sr.getAttribute("lastname").getValue() + " email=" + sr.getAttribute("email").getValue());
              }
           }         
       }
      
       catch (SpmlException ex)
       {
           logger.log(Level.SEVERE, null, ex);
       }
      
       catch (MalformedURLException ex)
       {
           logger.log(Level.SEVERE, null, ex);
       }
      
       finally
       {
           if (client != null)
           {
               try
               {
                   client.logout();
               }
              
               catch (SpmlException ex)
               {
                   logger.log(Level.SEVERE, null, ex);
               }
           }
       }
    }

   
    /**
     * Modification request to modify attributes on a user.
     * SIM user attributes and resource attributes can be modified by this method.
     * Also provisioning of resource account can be made.
     * @param client        SPML client with service account logged in
     * @param accountId     Identifier to match against SIM account and make modifications
     * @param attributes    Attributes to modify on user
     * @return              SIM SPML response
     * @throws SpmlException
     */
    public static SpmlResponse modifyUserAttributes(LighthouseClient client, String accountId, Map<String, Object> attributes) throws SpmlException
    {
        // Create a request to send to SIM
        ModifyRequest req = new ModifyRequest();
        req.setIdentifier(accountId);
       
        // SIM attributes to modify
        for (Map.Entry<String, Object> entry : attributes.entrySet())
        {
            req.addModification(entry.getKey(), entry.getValue());
        }

        // Send request to SIM and SIM sends a response back
        SpmlResponse response = client.request(req);
        logger.log(Level.INFO, "Response Code: {0}", new Object[] {response.getResult()});
       
        return response;
    }

   
    /**
     * Call a SIM work flow to be executed.
     * @param client        SPML client with service account logged in
     * @param workflow      Name of SIM work flow
     * @param attributes    Attributes includes accountId to be processed by work flow
     * @return              SIM SPML response
     * @throws SpmlException
     */
    public static SpmlResponse callSimWorkFlow(LighthouseClient client, String workFlowName, Map<String, Object> attributes) throws SpmlException
    {
        // Create a request to send to SIM
        ExtendedRequest req = new ExtendedRequest();
        req.setOperationIdentifier("launchProcess");
        req.setAttribute("process", workFlowName);
       
        // SIM attributes to sent to work flow
        for (Map.Entry<String, Object> entry : attributes.entrySet())
        {
            req.setAttribute(entry.getKey(), entry.getValue());
        }

        // Send request to SIM and SIM sends a response back
        SpmlResponse response = client.request(req);
        logger.log(Level.INFO, "Response Code: {0}", new Object[] {response.getResult()});

        return response;
    }

    /**
     * Disables a resource account in SIM
     * @param client        SPML client with service account logged in
     * @param accountId     SIM user whose resource account is going to be disabled
     * @param accountName   Resource account to disable
     * @return
     * @throws SpmlException
     */
    public static SpmlResponse disableResourceAccount(LighthouseClient client, String accountId, String accountName) throws SpmlException
    {
        ExtendedRequest req = new ExtendedRequest();
        req.setOperationIdentifier("disableUser");
        req.setAttribute("accountId", accountId);
        req.setAttribute("accounts", accountName);
        SpmlResponse response = client.request(req);
        logger.log(Level.INFO, "Response Code: {0}", new Object[] {response.getResult()});
        return response;
    }

    /**
     * Enable a resource account in SIM
     * @param client        SPML client with service account logged in
     * @param accountId     SIM user whose resource account is going to be disabled
     * @param accountName   Resource account to disable
     * @return
     * @throws SpmlException
     */
    public static SpmlResponse enableResourceAccount(LighthouseClient client, String accountId, String accountName) throws SpmlException
    {
        ExtendedRequest req = new ExtendedRequest();
        req.setOperationIdentifier("enableUser");
        req.setAttribute("accountId", accountId);
        req.setAttribute("accounts", accountName);
        SpmlResponse response = client.request(req);
        logger.log(Level.INFO, "Response Code: {0}", new Object[] {response.getResult()});
        return response;
    }
}

No comments:

Post a Comment