Wednesday, December 5, 2012

Sample client for SPML ValidateUsernameRequest webservice

Following is a sample client class for testing ValidateUsernameRequest SPML webservice:

Pre Requisite:

Have OIMServer.jar file in your class path. OIMServer.Jar file is located at
$IDM_HOME/server/apps/oim.ear/APP-INF/lib directory.

Sample Client Class is as follows:


 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
import com.oracle.xmlns.idm.identity.webservice.spmlservice.SPMLRequestPortType;
import com.oracle.xmlns.idm.identity.webservice.spmlservice.SPMLService;

import java.net.URL;

import java.util.ArrayList;
import java.util.List;

import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.handler.Handler;

import oracle.iam.wsschema.model.spmlv2.core.ServiceHeaderType;
import oracle.iam.wsschema.model.spmlv2custom.username.ValidateUsernameResponseType;


public class SPMLWSClientTest {
 public SPMLWSClientTest() {
  super();
 }
 private static final QName SERVICE_NAME = new QName("http://xmlns.oracle.com/idm/identity/webservice/SPMLService", "SPMLService");

 public static void main(String args[]) throws Exception {

  URL wsdlURL = new URL("http://oimhost:oimport/spml-xsd/SPMLService?wsdl");
  SPMLService ss = new SPMLService(wsdlURL, SERVICE_NAME);
  SPMLRequestPortType port = ss.getSPMLServiceProviderSoap();
         
                try {
                    CustomSOAPHandler sh = new CustomSOAPHandler();
             List<Handler> new_handlerChain = new ArrayList<Handler>();
             new_handlerChain.add(sh);
             ((BindingProvider) port).getBinding().setHandlerChain(new_handlerChain);
                    //port.callService();
                } catch (Throwable e) {
                 e.printStackTrace();
                }

  ServiceHeaderType serviceHeader = new ServiceHeaderType();

  System.out.println("Invoking spmlValidateUsernameRequest...");    
                
                oracle.iam.wsschema.model.spmlv2custom.username.ValidateUsernameRequestType requestData = new oracle.iam.wsschema.model.spmlv2custom.username.ValidateUsernameRequestType();
                                
                oracle.iam.wsschema.model.spmlv2.core.ExecutionModeType async = oracle.iam.wsschema.model.spmlv2.core.ExecutionModeType.ASYNCHRONOUS;
                requestData.setExecutionMode(async);
                requestData.setLocale("en");                
                
                requestData.setUsername("test0298");        
                
                
                
                ValidateUsernameResponseType valReturn = port.spmlValidateUsernameRequest(requestData, serviceHeader);
  System.out.println("spmlValidateUsername.result= " + valReturn.getStatus());

 }

}
The output from the 

Invoking spmlValidateUsernameRequest...
spmlValidateUsername.result= SUCCESS


The CustomSoapHandler used to build the SOAP Security Header information is as follows:

Note: The username and password in the below class have to be modified to the correct values.

 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
import java.util.Collections;
import java.util.Set;

import javax.xml.namespace.QName;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPHeader;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;


public class CustomSOAPHandler implements SOAPHandler<SOAPMessageContext> {

    private static final String AUTH_PREFIX = "wsse";
    private static final String AUTH_NS =
        "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";


    public boolean handleMessage(SOAPMessageContext context) {

        try {
            SOAPEnvelope envelope =
                context.getMessage().getSOAPPart().getEnvelope();
            SOAPFactory soapFactory = SOAPFactory.newInstance();
            SOAPElement wsSecHeaderElm =
                soapFactory.createElement("Security", AUTH_PREFIX, AUTH_NS);
            Name wsSecHdrMustUnderstandAttr =
                soapFactory.createName("mustUnderstand", "S",
                                       "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
            wsSecHeaderElm.addAttribute(wsSecHdrMustUnderstandAttr, "1");
            SOAPElement userNameTokenElm =
                soapFactory.createElement("UsernameToken", AUTH_PREFIX,
                                          AUTH_NS);
            Name userNameTokenIdName =
                soapFactory.createName("id", "wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
            userNameTokenElm.addAttribute(userNameTokenIdName,
                                          "UsernameToken-ORbTEPzNsEMDfzrI9sscVA22");
            SOAPElement userNameElm =
                soapFactory.createElement("Username", AUTH_PREFIX, AUTH_NS);
            userNameElm.addTextNode("xelsysadm");
            SOAPElement passwdElm =
                soapFactory.createElement("Password", AUTH_PREFIX, AUTH_NS);
            Name passwdTypeAttr = soapFactory.createName("Type");
            passwdElm.addAttribute(passwdTypeAttr,
                                   "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
            passwdElm.addTextNode("Password123");
            userNameTokenElm.addChildElement(userNameElm);
            userNameTokenElm.addChildElement(passwdElm);
            wsSecHeaderElm.addChildElement(userNameTokenElm);
            if (envelope.getHeader() == null) {
                SOAPHeader sh = envelope.addHeader();
                sh.addChildElement(wsSecHeaderElm);
            } else {
                SOAPHeader sh = envelope.getHeader();
                sh.addChildElement(wsSecHeaderElm);
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return true;
    }

    public Set<QName> getHeaders() {
        return Collections.emptySet();
    }

    public boolean handleFault(SOAPMessageContext context) {
        return false;
    }

    public void close(javax.xml.ws.handler.MessageContext context) {
    }
}

3 comments:

Unknown said...

HI,

I am getting below error while I am running the program.

Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: Unable to add security token for identity, token uri =http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.0#SAMLAssertionID


Can you please guide me for the same.

Thanks
Shant

Unknown said...

Hi,

Can you post java client for Reset password?
I am using class oracle.iam.wsschema.model.spmlv2.password.ResetPasswordRequestType


Thanks,
Yogesh

Unknown said...

THANKS! I've struggled with UsernameToken security for the invocation of those SPML services and after hours searching on the internet this post did the trick.

On the other hand I have to mention that I also had issues when invoking the services, obtaining the error: Method spmlBatchRequest is exposed as WebMethod, but there is no corresponding wsdl operation with name {http://xmlns.oracle.com/idm/identity/webservice/SPMLService}SPMLBatchRequest in the wsdl:portType{http://xmlns.oracle.com/idm/identity/webservice/SPMLService}SPMLRequestPortType.

This was solved by creating a Web Service Client with NetBeans, indicating the WSDL URL, creating a class with a main class with this code in it:

ValidateUsernameRequestType validateUser = new ValidateUsernameRequestType();
validateUser.setUsername("12000944");
ValidateUsernameResponseType retValUser = spmlValidateUsernameRequest(validateUser);
System.out.println("spml validate user =" + retValUser.isValid());

After this drag and drop the desired method (spmlValidateUsernameRequest) from the WebService references and adding the following code before the return statement:

CustomSOAPHandler sh = new CustomSOAPHandler();
List new_handlerChain = new ArrayList();
new_handlerChain.add(sh);
((BindingProvider) port).getBinding().setHandlerChain(new_handlerChain);
return port.spmlValidateUsernameRequest(body);
}

Thanks again.



C. Chiappe