Home AppWorks

AuthenticateUser SOAP service through JAVA

I am trying to use Document Management web services
when i use AuthenticateUser to create token with SOAPUI tool and use the token to get detials it is working fine
but when i create that token with Java code its not working

bellow is my sample java code

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.soap.*;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
//import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class SOAPClientSAAJ {

// SAAJ - SOAP Client Testing
public static void main(String args[]) {
        The example below requests from the Web Service at:

        To call other WS, change the parameters below, which are:
         - the SOAP Endpoint URL (that is, where the service is responding from)
         - the SOAP Action

        Also change the contents of the method createSoapEnvelope() in this class. It constructs
         the inner part of the SOAP envelope that is actually sent.
    String soapEndpointUrl = "http://server:8080/cws/services/Authentication";
    String soapAction = "AuthenticateUser";

    System.out.println(callSoapWebService(soapEndpointUrl, soapAction));

private static void createSoapEnvelope(SOAPMessage soapMessage) throws SOAPException {
    SOAPPart soapPart = soapMessage.getSOAPPart();

    String myNamespace = "urn";
    String myNamespaceURI = "urn:Core.service.livelink.opentext.com";

    // SOAP Envelope
    SOAPEnvelope envelope = soapPart.getEnvelope();
    envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI);

        Constructed SOAP Request Message:
        <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:myNamespace="https://www.w3schools.com/xml/">

    // SOAP Body
    SOAPBody soapBody = envelope.getBody();

    SOAPElement authElem = soapBody.addChildElement("AuthenticateUser", myNamespace);
    SOAPElement soapBodyElem = authElem.addChildElement("userName", myNamespace);
    SOAPElement soapBodyElem1 = authElem.addChildElement("userPassword", myNamespace);

static String callSoapWebService(String soapEndpointUrl, String soapAction) {
    try {
        // Create SOAP Connection
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection soapConnection = soapConnectionFactory.createConnection();

        // Send SOAP Message to SOAP Server
        SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(soapAction), soapEndpointUrl);

        NodeList resList = soapResponse.getSOAPBody().getElementsByTagName("AuthenticateUserResult");
        Node node = (Node) resList.item(0);
        Object someMsgContent = node != null ? node.getTextContent() : "";
       //Document resDoc = loadXMLString(soapResponse);
        // Print the SOAP Response
        System.out.println("Response SOAP Message:");

        return someMsgContent.toString();
    } catch (Exception e) {
        //System.err.println("\nError occurred while sending SOAP Request to Server!\nMake sure you have the correct endpoint URL and SOAPAction!\n");
    return null;

private static SOAPMessage createSOAPRequest(String soapAction) throws Exception {
    MessageFactory messageFactory = MessageFactory.newInstance();
    SOAPMessage soapMessage = messageFactory.createMessage();


    MimeHeaders headers = soapMessage.getMimeHeaders();
    headers.addHeader("SOAPAction", soapAction);


    /* Print the request message, just for debugging purposes */
    System.out.println("Request SOAP Message:");

    return soapMessage;

public static Document loadXMLString(String response) throws Exception
    DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    InputSource is = new InputSource(new StringReader(response));

    return db.parse(is);

/*public static List<String> getFullNameFromXml(String response, String tagName) throws Exception {
    Document xmlDoc = loadXMLString(response);
    NodeList nodeList = xmlDoc.getElementsByTagName(tagName);
    List<String> ids = new ArrayList<String>(nodeList.getLength());
    for(int i=0;i<nodeList.getLength(); i++) {
        Node x = nodeList.item(i);
    return ids;


Sign In or Register to comment.