Subscribe to News

Xmpp Web Service for Total VideoConference - Testing

Author : Despino4

From TechnologicalWiki

Jump to: navigation, search
Xmpp Web Service for Total VideoConferenceDevelopmentTestingReferences

Contents

[edit] Testing the Web Service

After deploying the Web service into a web container, we can see the web service wdsl. For example, if the web service url is "http://212.166.192.140/Xmpp/Xmpp?wsdl" we can get something like this:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://xmpp.vodafone.org/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://xmpp.vodafone.org/" name="XmppService"> <types> <xsd:schema> <xsd:import namespace="http://xmpp.vodafone.org/" schemaLocation="http://212.166.192.140:80/Xmpp/Xmpp?xsd=1"/> </xsd:schema> </types> <message name="sendRequest"> <part name="parameters" element="tns:sendRequest"/> </message> <message name="sendRequestResponse"> <part name="parameters" element="tns:sendRequestResponse"/> </message> <portType name="Xmpp"> <operation name="sendRequest"> <input message="tns:sendRequest"/> <output message="tns:sendRequestResponse"/> </operation> </portType> <binding name="XmppPortBinding" type="tns:Xmpp"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> <operation name="sendRequest"> <soap:operation soapAction=""/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> </binding> <service name="XmppService"> <port name="XmppPort" binding="tns:XmppPortBinding"> <soap:address location="http://212.166.192.140:80/Xmpp/Xmpp"/> </port> </service> </definitions>


where http://212.166.192.140:80/Xmpp/Xmpp?xsd=1 is something like that:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:tns="http://xmpp.vodafone.org/" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0"  
targetNamespace="http://xmpp.vodafone.org/"> <xs:element name="sendRequest" type="tns:sendRequest"/> <xs:element name="sendRequestResponse" type="tns:sendRequestResponse"/> <xs:complexType name="sendRequest"> <xs:sequence> <xs:element name="to" type="xs:string" minOccurs="0"/> <xs:element name="request" type="xs:string" minOccurs="0"/> </xs:sequence> </xs:complexType> <xs:complexType name="sendRequestResponse"> <xs:sequence> <xs:element name="return" type="xs:boolean"/> </xs:sequence> </xs:complexType> </xs:schema>

[edit] Consuming the Web Service

When the web service is deployed, we need to create a client to make use of the web service's add method.

There are several forms to create this client but in this section, we create a standard Java application with Netbeans 6.5:

  1. Choose File > New Project (Ctrl-Shift-N). Select Java Application from the Java category. Name the project XmppClient. Leave Create Main Class selected and accept all other default settings. Click Finish.
  2. Right-click the XmppClient node and choose New > Web Service Client.
  3. In Project, click Browse. Browse to the web service that you want to consume (we can select the web service wsdl ). When you have selected the web service, click OK.
  4. Leave the other settings at default and click Finish.
    The Projects window displays the new web service client, with a node for the add method that you created:


  5. Double-click Main.java so that it opens in the Source Editor. Delete the TODO comment and then drag the add node above into the empty line. You now see the following:
   public static void main(String[] args) {
       // TODO code application logic here
       try { // Call Web Service Operation
           org.vodafone.xmpp.XmppService service = new org.vodafone.xmpp.XmppService();
           org.vodafone.xmpp.Xmpp port = service.getXmppPort();
           // TODO initialize WS operation arguments here
           java.lang.String to = "";
           java.lang.String request = "";
           // TODO process result here
           boolean result = port.sendRequest(to, request);
           System.out.println("Result = "+result);
       } catch (Exception ex) {
           // TODO handle custom exceptions here
       }
   }
  1. Initialize the two Strings. Just change the values of the two Strings above from "" to other values, such as "pruebavf@osami.tid.es" and "This is an example".
  2. In the catch block, replace the commented out TODO with System.out.println("exception" + ex);.
  3. Right-click the project node and choose Run.
    The Output window now shows the sum:
init:
deps-jar:
wsimport-init:
wsimport-client-Xmpp:
files are up to date
wsimport-client-generate:
wsimport-client-compile-depend:
wsimport-client-compile:
compile:
run:
Result = false
BUILD SUCCESSFUL (total time: 11 seconds)

In this case,the result will be false if the response is not received within the configured time interval.The response will be true if a certain response is received from the destiny account.For testing purposes we can send the reply using a xmpp client (pidgin, Pandion, etc ...) or make a python script acting as an echo for the request.

Following there is an explanation to do an XMPP Echo Bot With Twisted And Wokkel:

  1. Prerequisites.
    • Python
    • Twisted 8.x. This is included in most Linux distributions our you can get it from Twisted Python's Web site.
    • Wokkel. Grab the latest from the repository.
      Once these are installed or in your Python path, you can proceed to the actual code.
  2. An Echo Bot Sub-protocol.
    Our echo bot will speak a custom sub-protocol of XMPP. It will listen for any message stanzas and then
    reply to some of them. We'll put this code in echobot.py.
from twisted.words.xish import domish
from wokkel.xmppim import MessageProtocol, AvailablePresence

class EchoBotProtocol(MessageProtocol): def connectionMade(self): print "Connected!"
# send initial presence self.send(AvailablePresence())
def connectionLost(self, reason): print "Disconnected!"
def onMessage(self, msg): print msg
if msg["type"] == 'chat' and hasattr(msg, "body"): reply = domish.Element((None, "message")) reply["to"] = msg["from"] reply["from"] = msg["to"] reply["type"] = 'chat' reply.addElement("body", content="echo: " + str(msg.body))
self.send(reply)

Some things to note about this code. A MessageProtocol simply looks for messages and calls onMessage when one is received. MessageProtocol inherits from XMPPHandler which is the base class for all sub-protocols. XMPPHandler will call connectionMade and connectionLost on connect and disconnect, just like other Twisted protocol classes. All the connection details and authentication are handled by the XMPPClient service, which we will see next.

  1. The Application.
    Twisted includes an application runner called twistd which takes a .tac file as input.
    The tac file sets up the various services that compose the application and manages their life cycle.
    It takes care of making things a daemon, handling PID files, and many other tasks common to applications.
    Here's the echobot.tac file:
from twisted.application import service
from twisted.words.protocols.jabber import jid
from wokkel.client import XMPPClient
from echobot import EchoBotProtocol
application = service.Application("echobot")
xmppclient = XMPPClient(jid.internJID("someuser@example.com/echobot"), "pass") xmppclient.logTraffic = False echobot = EchoBotProtocol() echobot.setHandlerParent(xmppclient) xmppclient.setServiceParent(application)

Be sure to replace 'someuser@example.com' and 'pass' with a real JID and its password. The echo bot application can now be started with twistd -ny echobot.tac or twistd -y echobot.tac if you want to daemonize it. Once it is running, go ahead and send it a message!

Every tac file must defines the application variable. Usually you'll create some services for the application and then use setServiceParent to make them part of the application. For XMPPClient services, we have to use a similar method to activate our sub-protocol handler; we call setHandlerParent to make our sub-protocol part of the xmppclient service.

Main Collaborators