OTDS Authentication (Java)
This tutorial explains how to use OTDS authentication with the CWS API using Java and Eclipse.
Outline
- Create a New Project
- Create CWS Client Proxies
- Write Your Code
- Run the Program
- Source Code
- References
Create a New Project
The first thing we need is a new project in Eclipse to work with.
-
Open Eclipse.
-
Open the File menu and select New → Java Project.
-
Enter a name for the project and then select Finish.
Create CWS Client Proxies
Next, we need create the client proxies for each CWS service we want to use.
-
Open a new command prompt.
-
Use the wsimport command to generate the client proxies for each service. Enter the following commands:
wsimport -keep http://localhost:8080/cws/services/Authentication?wsdl
wsimport -keep http://localhost:8080/cws/services/DocumentManagement?wsdl
wsimport -keep http://localhost:8080/otdsws/services/Authentication?wsdl
-
Use the jar command to bundle the generated class files into a .jar file. Enter the following command:
jar cvfM cws.jar com/opentext*
-
Copy the cws.jar file into your eclipse project and add it to your build path by right clicking the cws.jar file in the Package Explorer and select Build Path → Add to Build Path.
Write Your Code
Now that the services are ready to use we can go ahead and start writing some code. The first thing to do is to authenticate a user with OTDS. You will need to change the USERNAME
and PASSWORD
to use valid credentials of a user on your system.
// The user's credentials String USERNAME = "username@OTCS"; String PASSWORD = "password"; // Create the OTDS Authentication service client AuthenticationService otdsAuthService = new AuthenticationService(); com.opentext.ecm.services.authws.Authentication authClient = otdsAuthClient = otdsAuthService.getAuthenticationPort(); // Store the OTDS authentication token String otdsAuthToken = null; // Call the authenticate() method to get an authentication token try { System.out.print("Authenticating User..."); otdsAuthToken = otdsAuthClient.authenticate(USERNAME, PASSWORD); System.out.println("SUCCESS!\n"); } catch (AuthenticationException_Exception e) { System.out.println("FAILED!\n"); System.out.println(e.getFaultInfo().getFaultCode() + " : " + e.getMessage()); return; }
Next, we need to validate the OTDS authentication token with Content Server. To do this we need to use the CWS Authentication service and call the validateUser() method and pass in the OTDS authentication token.
// Create the CWS Authentication service client Authentication_Service authService = new Authentication_Service(); Authentication authClient = authService.getBasicHttpBindingAuthentication(); // Store the CWS authentication token String cwsAuthToken = null; // Call the validateUser() method to validate the OTDS authentication token with Content Server try { System.out.print("Validating User..."); cwsAuthToken = authClient.validateUser(otdsAuthToken); System.out.println("SUCCESS!\n"); } catch (SOAPFaultException e) { System.out.println("FAILED!\n"); System.out.println(e.getFault().getFaultCode() + " : " + e.getMessage()); return; }
We now have a CWS authentication token so we can use the other CWS services as we normally would. Here is an example using the DocumentManagement service to get a user's favorites:
// Create the DocumentManagement service client DocumentManagement_Service docManService = new DocumentManagement_Service(); DocumentManagement docManClient = docManService.getBasicHttpBindingDocumentManagement(); // Create the OTAuthentication object and set the authentication token OTAuthentication otAuth = new OTAuthentication(); otAuth.setAuthenticationToken(cwsAuthToken); // We need to manually set the SOAP header to include the authentication token try { // The namespace of the OTAuthentication object final String ECM_API_NAMESPACE = "urn:api.ecm.opentext.com"; // Create a SOAP header SOAPHeader header = MessageFactory.newInstance().createMessage().getSOAPPart().getEnvelope().getHeader(); // Add the OTAuthentication SOAP header element SOAPHeaderElement otAuthElement = header.addHeaderElement(new QName(ECM_API_NAMESPACE, "OTAuthentication")); // Add the AuthenticationToken SOAP element SOAPElement authTokenElement = otAuthElement.addChildElement(new QName(ECM_API_NAMESPACE, "AuthenticationToken")); authTokenElement.addTextNode(otAuth.getAuthenticationToken()); // Set the SOAP header on the docManClient ((WSBindingProvider) docManClient).setOutboundHeaders(Headers.create(otAuthElement)); } catch (SOAPException e) { System.out.println("Failed to set authentication SOAP header!\n"); System.out.println(e.getMessage()); System.out.println(e.getStackTrace()); return; } // Store the favorites List<Node> favorites = null; // Call the getAllFavorites() method to get the user's favorites try { System.out.print("Getting the user's favorites..."); favorites = docManClient.getAllFavorites(); System.out.println("SUCCESS!\n"); } catch (SOAPFaultException e) { System.out.println("FAILED!\n"); System.out.println(e.getFault().getFaultCode() + " : " + e.getMessage()); return; } // Output the user's favorites System.out.println("User's Favorites:\n"); if (favorites.size() > 0) { for (Node node : favorites) { System.out.println(node.getName()); } } else { System.out.println("No Favorites."); } System.out.println();
Run the Program
If everything is setup correctly you should be able to successfully run the program.