Howto:SOAP API Admin Java Sample Code

From innovaphone wiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
3rd party input
this is 3rd party content not provided by innovaphone, see history for authors.

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 innoSOAPadm.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 javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder;

import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.w3c.dom.NamedNodeMap;

import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPException; import javax.xml.soap.SOAPMessage; import javax.xml.soap.SOAPElement; import javax.xml.soap.SOAPBody;

import javax.xml.parsers.ParserConfigurationException; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import java.io.StringReader; import java.io.IOException;


import javax.xml.soap.Node;

public class innoSOAPadm {

   //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 = "PBX User Four"; //CN of user to perform Poll on
   // Main function
   public static void main(String args[]) {
       callSoapWebService(SOAPENDPOINTURL, SOAPACTION);
   }
   //Starts web connection and execute the SOAP calls
   private static void callSoapWebService(String soapEndpointUrl, String soapAction) {
       try {
           
           String session="";
           String user="";
           String response="";
           
           // Create SOAP Connection
           SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
           SOAPConnection soapConnection = soapConnectionFactory.createConnection();   
           SOAPMessage soapResponse = soapConnection.call(createSOAPRequest_Admin(soapAction, session), soapEndpointUrl);
           // Print the SOAP Response
           System.out.println("Response SOAP Message Trimmed to <return>:");
           SOAPBody body = soapResponse.getSOAPBody();
           NodeList returnList = body.getElementsByTagName("return");
           response=returnList.item(0).getTextContent();
           System.out.println(response);
           System.out.println("");
           
           System.out.println("Response parsed to Nodes and Attributes:");
           DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
           DocumentBuilder builder = factory.newDocumentBuilder();
           InputSource is = new InputSource(new StringReader(response));
           Document doc = builder.parse(is);
           nodeListPrint(doc.getElementsByTagName("show"));
           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 "Admin" instruction
   private static SOAPMessage createSOAPRequest_Admin(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("Admin");
       SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("xml");
       Name sdcPDUPduName = envelope.createName("type", "xsi", "http://www.innovaphone.com/pbx");
       soapBodyElem2.addAttribute(sdcPDUPduName, "xsd:string");
       soapBodyElem2.setTextContent("<show><user cn=\"" + CNUSER + "\" /></show>");
               
       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;
   }
   
   //Prints all nodes on the nodelist
   private static void nodeListPrint(NodeList list){
       NodeList childList;     
       for (int i = 0; i < list.getLength(); i++) {
           System.out.println("<"+String.valueOf(list.item(i).getNodeName())+">");
           try {
               if(list.item(i).hasAttributes()) {
                   nodeListAttribPrint(list.item(i).getAttributes());
               }
               if(list.item(i).hasChildNodes()) {
                   System.out.println("");
                   childList = list.item(i).getChildNodes();
                   nodeListPrint(childList);
               } else {
                   System.out.println("");
               }
           } catch(Exception x) {
               System.out.println("\nError getting attributes or child nodes....");
           }
       } 
   }
   
   private static void nodeListAttribPrint(NamedNodeMap node) {
       for (int i = 0; i < node.getLength(); i++) {
           System.out.println(node.item(i).getNodeName() + " = \"" + node.item(i).getNodeValue() + "\"");
       }
   }
   

}


Known Problems

Related Articles

Reference:SOAP API

Howto:PBX SOAP Api C-sharp sample code

Howto:SOAP with PHP5