SlideShare a Scribd company logo
Stateful SOAP Webservices with Java and PHP
International PHP Conference 2008 – Spring Edition
Thorsten Rinne
Introduction

❙ Thorsten Rinne
❙ 31 years old
❙ Graduated in computer science
❙ Project manager at Mayflower GmbH, Munich
   ❙ Reporting applications
   ❙ Critical bank applications
   ❙ PHP Consulting
❙ PHP software development since 1999
❙ Founder and main developer of Open Source FAQ-
  management software phpMyFAQ since 2001
❙ Zend Certified Engineer (PHP 5)

                                                   Stateful SOAP Webservices
                                                    © MAYFLOWER GmbH 2008 2
Summary

❙ Introduction
❙ SOAP and PHP
❙ Stateful SOAP Webservices
❙ Implemention
❙ Real world example
❙ Questions and answers




                              Stateful SOAP Webservices
                               © MAYFLOWER GmbH 2008 3
What is SOAP?

❙ SOAP is a protocol for exchanging XML-based messages
  over computer networks
❙ It uses HTTP, HTTPS or SMTP
❙ SOAP is the successor of XML-RPC
❙ Advantages
   ❙ using SOAP over HTTP allows for easier communication
     through proxies and firewalls
   ❙ platform independent
   ❙ language independent
❙ Disadvantages
   ❙ Slower than technologies like CORBA
   ❙ Bad performance with binary data
                                                            Stateful SOAP Webservices
                                                             © MAYFLOWER GmbH 2008 4
A simple SOAP message

❙ Simple structure of a SOAP message

 <?xml version=quot;1.0quot;?>
 <s:Envelope
  xmlns:s=quot;http://www.w3.org/2001/12/soap-envelopequot;>
     <s:Header>
     </s:Header>
     <s:Body>
     </s:Body>
 </s:Envelope>




                                           Stateful SOAP Webservices
                                            © MAYFLOWER GmbH 2008 5
Using SOAP with PHP

❙ ext/soap can be used to write SOAP servers and clients
❙ Support of subsets of SOAP 1.1, SOAP 1.2 and WSDL 1.1
  specifications
❙ No built-in support for WS-Addressing!
❙ SOAP server
  $server = new SoapServer('some.wsdl',
       array('soap_version' => SOAP_1_2));


❙ SOAP client
  $client = new SoapClient('some.wsdl',
       array('soap_version' => SOAP_1_2));


                                                           Stateful SOAP Webservices
                                                            © MAYFLOWER GmbH 2008 6
Stateful SOAP Webservices

❙ By default, SOAP webservices are stateless
❙ A stateful SOAP webservice is a webservice that maintains state
  information between message calls
❙ Session ID is stored in the SOAP header with WS-Addressing (WS-A)
❙ How do stateful SOAP webservices work?
   ❙ Request by SOAP client
     CS
   ❙ SOAP session will be created by SOAP server
   ❙ Response from SOAP server to SOAP client with Session ID
     CS
   ❙ More interaction between client and server
❙ Used in mulit-user environments with multiple requests/responses
                                                             Stateful SOAP Webservices
                                                              © MAYFLOWER GmbH 2008 7
Implementation

Apache Axis2 example:

<wsa:ReplyTo>
   <wsa:Address>
      http://www.w3.org/2005/08/addressing/anonymous
   </wsa:Address>
   <wsa:ReferenceParameters>
      <axis2:ServiceGroupId xmlns:axis2=
         quot;http://ws.apache.org/namespaces/axis2quot;>
            urn:uuid:65E9C56F702A398A8B11513011677354
      </axis2:ServiceGroupId>
   </wsa:ReferenceParameters>
</wsa:ReplyTo>

                                            Stateful SOAP Webservices
                                             © MAYFLOWER GmbH 2008 8
PHP implementation

❙ ext/soap doesn‘t support SOAP sessions
❙ Would be possible with WSO2 Web Services
  Framework/PHP (WSO2 WSF/PHP) extension
❙ We have to overwrite the
  SoapClient::__doRequest() method from PHP to
  implement WS-Addressing support
❙ First, we have to add a WS-A class written by Rob Richards
  (http://www.cdatazone.org/files/soap-wsa.phps)
    ❙ Parses to the SOAP XML header
    ❙ Sets the session ID
    ❙ Also support for WS-Security (WS-S) if needed




                                                               Stateful SOAP Webservices
                                                                © MAYFLOWER GmbH 2008 9
PHP implementation (I)

class MyProject_SOAPClient extends SoapClient
{
    public function __doRequest($request, $location, $saction, $version)
    {
        $dom = new DOMDocument();
        $dom->loadXML($request);
        $wsasoap = new WSASoap($dom);
        $wsasoap->addAction($saction);
        $wsasoap->addTo($location);
        $wsasoap->addMessageID(); // Sets the session ID
        $wsasoap->addReplyTo();
        $request = $wsasoap->saveXML();
        return parent::__doRequest($request, $location, $saction, $version);
    }
}


                                                               Stateful SOAP Webservices
                                                                © MAYFLOWER GmbH 2008 10
PHP implementation (II)

class OurService {
    /* … */
    $axis2session = $this->_getSoapSession($this->soapClient->__getLastResponse());
    $soapHeader    = new SoapHeader('http://ws.apache.org/namespaces/axis2',
                                    'ServiceGroupId',$axis2session);
    $this->soapClient->__soapCall('getData', array(), null, $soapHeader);
    /* … */
    private function _getSoapSession($response) {
        $soapsession = '';
        $xml = new XMLReader();
        $xml->XML($response);
        while ($xml->read()) {
            if (strpos($xml->name, 'axis2:ServiceGroupId') !== false) {
                $xml->read();
                $soapsession = $xml->value;
                $xml->read();
            }
        }
        return $soapsession;
    }
                                                                Stateful SOAP Webservices
}
                                                                    © MAYFLOWER GmbH 2008 11
Welcome to the real world

❙ Various applications based on
   ❙ Java (main application)
   ❙ Excel with a included DLL written in C++
   ❙ Access/Visual Basic
   ❙ Web application in PHP (our project)
❙ All applications were using the same calculation logic but
  implemented in different programming language
❙ Big problems when the logic changes
❙ Solution
   ❙ Migration of the Access tool into the PHP application
   ❙ Build a SOAP service on top of the Java classes
   ❙ Replace the C++ written library and our PHP based library
     with the SOAP webservice
                                                               Stateful SOAP Webservices
                                                                © MAYFLOWER GmbH 2008 12
Welcome to the real world!
      Old architecture

                                     Microsoft Office Client PC

                                                       Microsoft Access and Excel




                                      mod_php
                                                                  MySQL
                                    Apache 2.0
                                                   Linux
J2EE Cluster (Calculation engine)                                            Stateful SOAP Webservices
                                                                              © MAYFLOWER GmbH 2008 13
Welcome to the real world!
  Current architecture
             J2EE Cluster (Calculation engine)
                                                      Microsoft Office Client PC




    Transfer by SFTP



                                    SOAP over HTTPS



                                           SOAP
                 SOAP

                                         mod_php
                 Axis2
java.class
                                                                   MySQL
                                       Apache 2.0
             Tomcat 5.5
                                                      Linux
                                                                             Stateful SOAP Webservices
                                                                              © MAYFLOWER GmbH 2008 14
Questions and answers




                        Stateful SOAP Webservices
                         © MAYFLOWER GmbH 2008 15
Thank you very much!

Thorsten Rinne, Dipl.-Inf. (FH)
Mayflower GmbH
Mannhardtstraße 6
D-80538 München
Germany
+49 (89) 24 20 54 – 31
thorsten.rinne@mayflower.de

More Related Content

Stateful SOAP Webservices

  • 1. Stateful SOAP Webservices with Java and PHP International PHP Conference 2008 – Spring Edition Thorsten Rinne
  • 2. Introduction ❙ Thorsten Rinne ❙ 31 years old ❙ Graduated in computer science ❙ Project manager at Mayflower GmbH, Munich ❙ Reporting applications ❙ Critical bank applications ❙ PHP Consulting ❙ PHP software development since 1999 ❙ Founder and main developer of Open Source FAQ- management software phpMyFAQ since 2001 ❙ Zend Certified Engineer (PHP 5) Stateful SOAP Webservices © MAYFLOWER GmbH 2008 2
  • 3. Summary ❙ Introduction ❙ SOAP and PHP ❙ Stateful SOAP Webservices ❙ Implemention ❙ Real world example ❙ Questions and answers Stateful SOAP Webservices © MAYFLOWER GmbH 2008 3
  • 4. What is SOAP? ❙ SOAP is a protocol for exchanging XML-based messages over computer networks ❙ It uses HTTP, HTTPS or SMTP ❙ SOAP is the successor of XML-RPC ❙ Advantages ❙ using SOAP over HTTP allows for easier communication through proxies and firewalls ❙ platform independent ❙ language independent ❙ Disadvantages ❙ Slower than technologies like CORBA ❙ Bad performance with binary data Stateful SOAP Webservices © MAYFLOWER GmbH 2008 4
  • 5. A simple SOAP message ❙ Simple structure of a SOAP message <?xml version=quot;1.0quot;?> <s:Envelope xmlns:s=quot;http://www.w3.org/2001/12/soap-envelopequot;> <s:Header> </s:Header> <s:Body> </s:Body> </s:Envelope> Stateful SOAP Webservices © MAYFLOWER GmbH 2008 5
  • 6. Using SOAP with PHP ❙ ext/soap can be used to write SOAP servers and clients ❙ Support of subsets of SOAP 1.1, SOAP 1.2 and WSDL 1.1 specifications ❙ No built-in support for WS-Addressing! ❙ SOAP server $server = new SoapServer('some.wsdl', array('soap_version' => SOAP_1_2)); ❙ SOAP client $client = new SoapClient('some.wsdl', array('soap_version' => SOAP_1_2)); Stateful SOAP Webservices © MAYFLOWER GmbH 2008 6
  • 7. Stateful SOAP Webservices ❙ By default, SOAP webservices are stateless ❙ A stateful SOAP webservice is a webservice that maintains state information between message calls ❙ Session ID is stored in the SOAP header with WS-Addressing (WS-A) ❙ How do stateful SOAP webservices work? ❙ Request by SOAP client CS ❙ SOAP session will be created by SOAP server ❙ Response from SOAP server to SOAP client with Session ID CS ❙ More interaction between client and server ❙ Used in mulit-user environments with multiple requests/responses Stateful SOAP Webservices © MAYFLOWER GmbH 2008 7
  • 8. Implementation Apache Axis2 example: <wsa:ReplyTo> <wsa:Address> http://www.w3.org/2005/08/addressing/anonymous </wsa:Address> <wsa:ReferenceParameters> <axis2:ServiceGroupId xmlns:axis2= quot;http://ws.apache.org/namespaces/axis2quot;> urn:uuid:65E9C56F702A398A8B11513011677354 </axis2:ServiceGroupId> </wsa:ReferenceParameters> </wsa:ReplyTo> Stateful SOAP Webservices © MAYFLOWER GmbH 2008 8
  • 9. PHP implementation ❙ ext/soap doesn‘t support SOAP sessions ❙ Would be possible with WSO2 Web Services Framework/PHP (WSO2 WSF/PHP) extension ❙ We have to overwrite the SoapClient::__doRequest() method from PHP to implement WS-Addressing support ❙ First, we have to add a WS-A class written by Rob Richards (http://www.cdatazone.org/files/soap-wsa.phps) ❙ Parses to the SOAP XML header ❙ Sets the session ID ❙ Also support for WS-Security (WS-S) if needed Stateful SOAP Webservices © MAYFLOWER GmbH 2008 9
  • 10. PHP implementation (I) class MyProject_SOAPClient extends SoapClient { public function __doRequest($request, $location, $saction, $version) { $dom = new DOMDocument(); $dom->loadXML($request); $wsasoap = new WSASoap($dom); $wsasoap->addAction($saction); $wsasoap->addTo($location); $wsasoap->addMessageID(); // Sets the session ID $wsasoap->addReplyTo(); $request = $wsasoap->saveXML(); return parent::__doRequest($request, $location, $saction, $version); } } Stateful SOAP Webservices © MAYFLOWER GmbH 2008 10
  • 11. PHP implementation (II) class OurService { /* … */ $axis2session = $this->_getSoapSession($this->soapClient->__getLastResponse()); $soapHeader = new SoapHeader('http://ws.apache.org/namespaces/axis2', 'ServiceGroupId',$axis2session); $this->soapClient->__soapCall('getData', array(), null, $soapHeader); /* … */ private function _getSoapSession($response) { $soapsession = ''; $xml = new XMLReader(); $xml->XML($response); while ($xml->read()) { if (strpos($xml->name, 'axis2:ServiceGroupId') !== false) { $xml->read(); $soapsession = $xml->value; $xml->read(); } } return $soapsession; } Stateful SOAP Webservices } © MAYFLOWER GmbH 2008 11
  • 12. Welcome to the real world ❙ Various applications based on ❙ Java (main application) ❙ Excel with a included DLL written in C++ ❙ Access/Visual Basic ❙ Web application in PHP (our project) ❙ All applications were using the same calculation logic but implemented in different programming language ❙ Big problems when the logic changes ❙ Solution ❙ Migration of the Access tool into the PHP application ❙ Build a SOAP service on top of the Java classes ❙ Replace the C++ written library and our PHP based library with the SOAP webservice Stateful SOAP Webservices © MAYFLOWER GmbH 2008 12
  • 13. Welcome to the real world! Old architecture Microsoft Office Client PC Microsoft Access and Excel mod_php MySQL Apache 2.0 Linux J2EE Cluster (Calculation engine) Stateful SOAP Webservices © MAYFLOWER GmbH 2008 13
  • 14. Welcome to the real world! Current architecture J2EE Cluster (Calculation engine) Microsoft Office Client PC Transfer by SFTP SOAP over HTTPS SOAP SOAP mod_php Axis2 java.class MySQL Apache 2.0 Tomcat 5.5 Linux Stateful SOAP Webservices © MAYFLOWER GmbH 2008 14
  • 15. Questions and answers Stateful SOAP Webservices © MAYFLOWER GmbH 2008 15
  • 16. Thank you very much! Thorsten Rinne, Dipl.-Inf. (FH) Mayflower GmbH Mannhardtstraße 6 D-80538 München Germany +49 (89) 24 20 54 – 31 thorsten.rinne@mayflower.de