Status: Open
Status: Answered
Status: Closed
Status: Duplicate

AuthenticateUser SOAP service through JAVA

0
Posted Nov 20 by Kasim Shaik.

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:
         https://www.w3schools.com/xml/tempconvert.asmx?op=CelsiusToFahrenheit


        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-ENV:Header/>
            <SOAP-ENV:Body>
                <myNamespace:CelsiusToFahrenheit>
                    <myNamespace:Celsius>100</myNamespace:Celsius>
                </myNamespace:CelsiusToFahrenheit>
            </SOAP-ENV:Body>
        </SOAP-ENV:Envelope>
        */

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

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

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:");
        soapResponse.writeTo(System.out);
        System.out.println();

        soapConnection.close();
        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");
        e.printStackTrace();
    }
    return null;
}

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

    createSoapEnvelope(soapMessage);

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

    soapMessage.saveChanges();

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

    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);
        ids.add(x.getFirstChild().getNodeValue());             
        System.out.println(nodeList.item(i).getFirstChild().getNodeValue());
    }
    return ids;
}*/

}


 You have subscribed and will receive email notifications of updates to this topic. To unsubscribe, uncheck the checkbox.

Statistics

Related categories

Your answer

To leave an answer, please sign in.