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:

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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
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