Howto:SOAP API Java Sample Code: Difference between revisions
Jump to navigation
Jump to search
Line 37: | Line 37: | ||
public class innoSOAP { | 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: | |||
Modifi 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"; | private static final String MYNAMESPACEURI = "http://www.innovaphone.com/pbx"; | ||
//specific system information | |||
private static final String SOAPACTION = ""; | |||
private static final String SOAPENDPOINTURL = "http:// | private static final String SOAPENDPOINTURL = "http://pbx-pure.eaibrasil.com.br/PBX0/user.soap"; | ||
private static final String ADMSOAPUSER = "soapadm"; //PBX admin user | |||
private static final String ADMSOAPUSERPWD = " | private static final String ADMSOAPUSERPWD = "A2Er@h#dx"; //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 = "Francisco Paletta"; //CN of user to perform Poll on | |||
// Main function | // Main function | ||
Line 77: | Line 77: | ||
callSoapWebService(SOAPENDPOINTURL, SOAPACTION); | 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) { | private static void callSoapWebService(String soapEndpointUrl, String soapAction) { | ||
try { | try { | ||
String session=""; | |||
String user=""; | |||
// Create SOAP Connection | // Create SOAP Connection | ||
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); | SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); | ||
SOAPConnection soapConnection = soapConnectionFactory.createConnection(); | SOAPConnection soapConnection = soapConnectionFactory.createConnection(); | ||
// Send SOAP Message to SOAP Server | // Send SOAP Message to SOAP Server | ||
Line 115: | Line 118: | ||
System.out.println("\n"); | 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 | // Print the SOAP Response | ||
System.out.println("Response SOAP Message:"); | System.out.println("Response SOAP Message:"); | ||
soapResponse.writeTo(System.out); | 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(); | soapConnection.close(); | ||
Line 179: | Line 182: | ||
} | } | ||
//Creates a SOAPmessage with the "Initialize" instruction | |||
private static SOAPMessage createSOAPRequest_Initialize(String soapAction) throws Exception { | private static SOAPMessage createSOAPRequest_Initialize(String soapAction) throws Exception { | ||
MessageFactory messageFactory = MessageFactory.newInstance(); | MessageFactory messageFactory = MessageFactory.newInstance(); | ||
SOAPMessage soapMessage = messageFactory.createMessage(); | SOAPMessage soapMessage = messageFactory.createMessage(); | ||
SOAPPart soapPart = soapMessage.getSOAPPart(); | |||
// SOAP Envelope | // SOAP Envelope | ||
SOAPEnvelope envelope = soapPart.getEnvelope(); | SOAPEnvelope envelope = soapPart.getEnvelope(); | ||
Line 194: | Line 197: | ||
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("user"); | SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("user"); | ||
soapBodyElem1.addTextNode(SOAPUSER); | soapBodyElem1.addTextNode(SOAPUSER); | ||
SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("appl"); | |||
soapBodyElem2.addTextNode("JAVA SOAP CALL"); | soapBodyElem2.addTextNode("JAVA SOAP CALL"); | ||
SOAPElement soapBodyElem3 = soapBodyElem.addChildElement("bool"); | |||
soapBodyElem3.addTextNode("true"); | soapBodyElem3.addTextNode("true"); | ||
SOAPElement soapBodyElem4 = soapBodyElem.addChildElement("v501"); | |||
soapBodyElem4.addTextNode("true"); | soapBodyElem4.addTextNode("true"); | ||
SOAPElement soapBodyElem5 = soapBodyElem.addChildElement("v700"); | |||
soapBodyElem5.addTextNode("true"); | soapBodyElem5.addTextNode("true"); | ||
SOAPElement soapBodyElem6 = soapBodyElem.addChildElement("v800"); | |||
soapBodyElem6.addTextNode("true"); | soapBodyElem6.addTextNode("true"); | ||
SOAPElement soapBodyElem7 = soapBodyElem.addChildElement("vx1000"); | |||
soapBodyElem7.addTextNode("true"); | soapBodyElem7.addTextNode("true"); | ||
Line 211: | Line 214: | ||
headers.addHeader("SOAPAction", soapAction); | headers.addHeader("SOAPAction", soapAction); | ||
String authorization = new sun.misc.BASE64Encoder().encode((ADMSOAPUSER+":"+ADMSOAPUSERPWD).getBytes()); | |||
headers.addHeader("Authorization", "Basic " + authorization); | |||
soapMessage.saveChanges(); | soapMessage.saveChanges(); | ||
Line 219: | Line 222: | ||
System.out.println("Request SOAP Message:"); | System.out.println("Request SOAP Message:"); | ||
soapMessage.writeTo(System.out); | soapMessage.writeTo(System.out); | ||
System.out.println("\n"); | |||
return soapMessage; | return soapMessage; | ||
} | } | ||
//Creates a SOAPmessage with the "UserInitialize" instruction | |||
private static SOAPMessage createSOAPRequest_UserInitialize(String soapAction, String session) throws Exception { | |||
MessageFactory messageFactory = MessageFactory.newInstance(); | MessageFactory messageFactory = MessageFactory.newInstance(); | ||
SOAPMessage soapMessage = messageFactory.createMessage(); | SOAPMessage soapMessage = messageFactory.createMessage(); | ||
SOAPPart soapPart = soapMessage.getSOAPPart(); | |||
// SOAP Envelope | // SOAP Envelope | ||
SOAPEnvelope envelope = soapPart.getEnvelope(); | SOAPEnvelope envelope = soapPart.getEnvelope(); | ||
Line 239: | Line 242: | ||
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("session"); | SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("session"); | ||
soapBodyElem1.addTextNode(session); | soapBodyElem1.addTextNode(session); | ||
SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("user"); | |||
soapBodyElem2.addTextNode(CNUSER); | soapBodyElem2.addTextNode(CNUSER); | ||
SOAPElement soapBodyElem3 = soapBodyElem.addChildElement("follow"); | |||
soapBodyElem3.addTextNode("true"); | soapBodyElem3.addTextNode("true"); | ||
Line 248: | Line 251: | ||
headers.addHeader("SOAPAction", soapAction); | headers.addHeader("SOAPAction", soapAction); | ||
String authorization = new sun.misc.BASE64Encoder().encode((ADMSOAPUSER+":"+ADMSOAPUSERPWD).getBytes()); | |||
headers.addHeader("Authorization", "Basic " + authorization); | |||
soapMessage.saveChanges(); | soapMessage.saveChanges(); | ||
Line 256: | Line 259: | ||
System.out.println("Request SOAP Message:"); | System.out.println("Request SOAP Message:"); | ||
soapMessage.writeTo(System.out); | soapMessage.writeTo(System.out); | ||
System.out.println("\n"); | |||
return soapMessage; | return soapMessage; | ||
} | } | ||
//Creates a SOAPmessage with the "Poll" instruction | |||
private static SOAPMessage createSOAPRequest_Poll(String soapAction, String session) throws Exception { | |||
MessageFactory messageFactory = MessageFactory.newInstance(); | MessageFactory messageFactory = MessageFactory.newInstance(); | ||
SOAPMessage soapMessage = messageFactory.createMessage(); | SOAPMessage soapMessage = messageFactory.createMessage(); | ||
SOAPPart soapPart = soapMessage.getSOAPPart(); | |||
// SOAP Envelope | // SOAP Envelope | ||
SOAPEnvelope envelope = soapPart.getEnvelope(); | SOAPEnvelope envelope = soapPart.getEnvelope(); | ||
Line 281: | Line 284: | ||
headers.addHeader("SOAPAction", soapAction); | headers.addHeader("SOAPAction", soapAction); | ||
String authorization = new sun.misc.BASE64Encoder().encode((ADMSOAPUSER+":"+ADMSOAPUSERPWD).getBytes()); | |||
headers.addHeader("Authorization", "Basic " + authorization); | |||
soapMessage.saveChanges(); | soapMessage.saveChanges(); | ||
Line 289: | Line 292: | ||
System.out.println("Request SOAP Message:"); | System.out.println("Request SOAP Message:"); | ||
soapMessage.writeTo(System.out); | soapMessage.writeTo(System.out); | ||
System.out.println("\n"); | |||
return soapMessage; | return soapMessage; | ||
Line 295: | Line 298: | ||
} | } | ||
Revision as of 15:43, 3 May 2018
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:
Modifi 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://pbx-pure.eaibrasil.com.br/PBX0/user.soap";
private static final String ADMSOAPUSER = "soapadm"; //PBX admin user
private static final String ADMSOAPUSERPWD = "A2Er@h#dx"; //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 = "Francisco Paletta"; //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;
}
}