Howto:SOAP API Java Sample Code

From innovaphone-wiki

Jump to: navigation, search
3rd party input
this is 3rd party content not provided by innovaphone, see history for authors.

Contents

Applies To

This information applies to

  • any innovaphone PBX platform
  • Java Runtime Environment

innovaphone PBX web services can be used from Java Runtime Environment. Here is how


More Information

Configuration

Modify the global variables SOAPENDPOINTURL, ADMSOAPUSER, ADMSOAPUSERPWD, SOAPUSER and CNUSER according to your specific system.

Running Sample Code

This code is a simple Java console application. It will run on your OS console terminal. As dependencies, you will need JDK to compile the code and JRE to execute it.

To compile, save the code on file innoSOAP.java and provide that file as parameter to javac command.

  • The below code was compiled on javac version 1.8.0 and run on java version 1.8.0 (It was not tested on other versions)
  • You may get a warning regarding the function sun.misc.BASE64Encoder().encode() because it is deprecated. Nevertheless the code will run. You may remove that section of the code (used just for authentication on the PBX) but you will have to allow "public" access to /PBX0/user.soap on HTTP(security risk!!!).


The sample application

This section will discuss the sample code. It is not a decent description of the PBX SOAP API. For this, see the related articles.


import javax.xml.soap.*;
import org.w3c.dom.NodeList;
 
public class innoSOAP {
/*
The example below demonstrates the use of SOAP integration to innovaphone PBX.
 
Reference to innovaphone SOAP integration can be found on:
http://wiki.innovaphone.com/index.php?title=Reference:SOAP_API
 
INSTRUCTIONS:
Modify the global variables below according to your specific system.
 
The program will perform the following tasks:
1 - Send a SOAP message with the instruction "Initialize" and grab the session ID.
2 - Referencing the session ID it will send a SOAP message with the instruction "UserInitialize" and grab the user ID.
3 - Loop on an SOAP instruction "Poll" to keep looking for information on the user CNUSER (refer to global variables below)
User information and Call information.
(The Poll call remains opened until information is provided or time out. In case of timeout PBX sends an emtpy response
and then another Poll call takes place)
 
Created by:
Francisco Paletta
 
Version: 0.2
*/

 
 
//innovaphone general information
private static final String MYNAMESPACE = "pbx";
private static final String MYNAMESPACEURI = "http://www.innovaphone.com/pbx";
 
//specific system information
private static final String SOAPACTION = "";
private static final String SOAPENDPOINTURL = "http://192.168.0.1/PBX0/user.soap";
private static final String ADMSOAPUSER = "admin"; //PBX admin user
private static final String ADMSOAPUSERPWD = "ip311"; //PBX admin password
private static final String SOAPUSER = "SOAP"; //SOAP user object in PBX with group access to other objects
private static final String CNUSER = "User 1"; //CN of user to perform Poll on
 
// Main function
public static void main(String args[]) {
callSoapWebService(SOAPENDPOINTURL, SOAPACTION);
}
 
//Prints all nodes on the nodelist
private static void nodeListPrint(NodeList list, String tab){
NodeList childList;
for (int i = 0; i < list.getLength(); i++) {
System.out.print(tab+String.valueOf(list.item(i).getNodeName()));
childList = list.item(i).getChildNodes();
if(childList.getLength() != 0) {
if(!childList.item(0).getNodeName().equals("#text")) {
System.out.println("");
nodeListPrint(childList, tab+" ");
}
else {
System.out.print(" -> ");
System.out.println(String.valueOf(childList.item(0).getTextContent()));
}
} else {
System.out.println("");
}
}
}
 
//Starts web connection and execute the SOAP calls
private static void callSoapWebService(String soapEndpointUrl, String soapAction) {
try {
 
String session="";
String user="";
 
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
 
 
// Send SOAP Message to SOAP Server
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest_Initialize(soapAction), soapEndpointUrl);
// Print the SOAP Response
System.out.println("Response SOAP Message:");
soapResponse.writeTo(System.out);
System.out.println("\n");
 
SOAPBody body = soapResponse.getSOAPBody();
NodeList returnList = body.getElementsByTagName("InitializeResponse");
 
System.out.println("Response Parsed:");
nodeListPrint(returnList,"");
System.out.println("");
 
for (int k = 0; k < returnList.getLength(); k++) {
NodeList innerResultList = returnList.item(k).getChildNodes();
for (int l = 0; l < innerResultList.getLength(); l++) {
if (innerResultList.item(l).getNodeName().equalsIgnoreCase("return")) {
session = String.valueOf(innerResultList.item(l).getTextContent().trim());
}
}
}
 
soapResponse = soapConnection.call(createSOAPRequest_UserInitialize(soapAction, session), soapEndpointUrl);
 
// Print the SOAP Response
System.out.println("Response SOAP Message:");
soapResponse.writeTo(System.out);
System.out.println("\n");
 
body = soapResponse.getSOAPBody();
returnList = body.getElementsByTagName("UserInitializeResponse");
 
System.out.println("Response Parsed:");
nodeListPrint(returnList,"");
System.out.println("");
 
for (int k = 0; k < returnList.getLength(); k++) {
NodeList innerResultList = returnList.item(k).getChildNodes();
for (int l = 0; l < innerResultList.getLength(); l++) {
if (innerResultList.item(l).getNodeName().equalsIgnoreCase("return")) {
user = String.valueOf(innerResultList.item(l).getTextContent().trim());
}
}
}
 
boolean returnValue = true;
while(returnValue){
soapResponse = soapConnection.call(createSOAPRequest_Poll(soapAction, session), soapEndpointUrl);
//The call for Poll remains opened until PBX sends a response. In case of timeout PBX sends an empty response.
//The loop will guarantee another Poll call is send to keep "channel" running.
 
// Print the SOAP Response
System.out.println("Response SOAP Message:");
soapResponse.writeTo(System.out);
System.out.println("\n");
 
body = soapResponse.getSOAPBody();
returnList = body.getElementsByTagName("PollResponse");
System.out.println("Response Parsed:");
nodeListPrint(returnList,"");
System.out.println("");
}
 
soapConnection.close();
} 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();
}
}
 
//Creates a SOAPmessage with the "Initialize" instruction
private static SOAPMessage createSOAPRequest_Initialize(String soapAction) throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
 
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration(MYNAMESPACE, MYNAMESPACEURI);
 
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("Initialize");
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("user");
soapBodyElem1.addTextNode(SOAPUSER);
SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("appl");
soapBodyElem2.addTextNode("JAVA SOAP CALL");
SOAPElement soapBodyElem3 = soapBodyElem.addChildElement("bool");
soapBodyElem3.addTextNode("true");
SOAPElement soapBodyElem4 = soapBodyElem.addChildElement("v501");
soapBodyElem4.addTextNode("true");
SOAPElement soapBodyElem5 = soapBodyElem.addChildElement("v700");
soapBodyElem5.addTextNode("true");
SOAPElement soapBodyElem6 = soapBodyElem.addChildElement("v800");
soapBodyElem6.addTextNode("true");
SOAPElement soapBodyElem7 = soapBodyElem.addChildElement("vx1000");
soapBodyElem7.addTextNode("true");
 
 
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", soapAction);
 
String authorization = new sun.misc.BASE64Encoder().encode((ADMSOAPUSER+":"+ADMSOAPUSERPWD).getBytes());
headers.addHeader("Authorization", "Basic " + authorization);
 
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;
}
 
//Creates a SOAPmessage with the "UserInitialize" instruction
private static SOAPMessage createSOAPRequest_UserInitialize(String soapAction, String session) throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
 
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration(MYNAMESPACE, MYNAMESPACEURI);
 
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("UserInitialize");
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("session");
soapBodyElem1.addTextNode(session);
SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("user");
soapBodyElem2.addTextNode(CNUSER);
SOAPElement soapBodyElem3 = soapBodyElem.addChildElement("follow");
soapBodyElem3.addTextNode("true");
 
 
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", soapAction);
 
String authorization = new sun.misc.BASE64Encoder().encode((ADMSOAPUSER+":"+ADMSOAPUSERPWD).getBytes());
headers.addHeader("Authorization", "Basic " + authorization);
 
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;
}
 
//Creates a SOAPmessage with the "Poll" instruction
private static SOAPMessage createSOAPRequest_Poll(String soapAction, String session) throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
 
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration(MYNAMESPACE, MYNAMESPACEURI);
 
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("Poll");
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("session");
soapBodyElem1.addTextNode(session);
 
 
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", soapAction);
 
String authorization = new sun.misc.BASE64Encoder().encode((ADMSOAPUSER+":"+ADMSOAPUSERPWD).getBytes());
headers.addHeader("Authorization", "Basic " + authorization);
 
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;
}
 
}

Known Problems

Related Articles

Reference:SOAP API

Howto:PBX SOAP Api C-sharp sample code

Howto:SOAP with PHP5

Personal tools