Stateful SOAP Webservices

16

Click here to load reader

Transcript of Stateful SOAP Webservices

Page 1: Stateful SOAP Webservices

Stateful SOAP Webservices with Java and PHP

International PHP Conference 2008 – Spring EditionThorsten Rinne

Page 2: Stateful SOAP Webservices

© MAYFLOWER GmbH 2008 2

Stateful SOAP Webservices

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)

Page 3: Stateful SOAP Webservices

© MAYFLOWER GmbH 2008 3

Stateful SOAP Webservices

Summary

❙ Introduction

❙ SOAP and PHP

❙ Stateful SOAP Webservices

❙ Implemention

❙ Real world example

❙ Questions and answers

Page 4: Stateful SOAP Webservices

© MAYFLOWER GmbH 2008 4

Stateful SOAP Webservices

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

Page 5: Stateful SOAP Webservices

© MAYFLOWER GmbH 2008 5

Stateful SOAP Webservices

A simple SOAP message

❙ Simple structure of a SOAP message

<?xml version="1.0"?><s:Envelopexmlns:s="http://www.w3.org/2001/12/soap-envelope">

<s:Header></s:Header><s:Body></s:Body>

</s:Envelope>

Page 6: Stateful SOAP Webservices

© MAYFLOWER GmbH 2008 6

Stateful SOAP Webservices

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));

Page 7: Stateful SOAP Webservices

© MAYFLOWER GmbH 2008 7

Stateful SOAP Webservices

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 clientC S

❙ SOAP session will be created by SOAP server

❙ Response from SOAP server to SOAP client with Session IDC S

❙ More interaction between client and server

❙ Used in mulit-user environments with multiple requests/responses

Page 8: Stateful SOAP Webservices

© MAYFLOWER GmbH 2008 8

Stateful SOAP Webservices

Implementation

Apache Axis2 example:

<wsa:ReplyTo><wsa:Address>

http://www.w3.org/2005/08/addressing/anonymous</wsa:Address><wsa:ReferenceParameters>

<axis2:ServiceGroupId xmlns:axis2="http://ws.apache.org/namespaces/axis2">

urn:uuid:65E9C56F702A398A8B11513011677354</axis2:ServiceGroupId>

</wsa:ReferenceParameters></wsa:ReplyTo>

Page 9: Stateful SOAP Webservices

© MAYFLOWER GmbH 2008 9

Stateful SOAP Webservices

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 theSoapClient::__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

Page 10: Stateful SOAP Webservices

© MAYFLOWER GmbH 2008 10

Stateful SOAP Webservices

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);

}

}

Page 11: Stateful SOAP Webservices

© MAYFLOWER GmbH 2008 11

Stateful SOAP Webservices

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;

}}

Page 12: Stateful SOAP Webservices

© MAYFLOWER GmbH 2008 12

Stateful SOAP Webservices

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

Page 13: Stateful SOAP Webservices

© MAYFLOWER GmbH 2008 13

Stateful SOAP Webservices

Welcome to the real world!Old architecture

Microsoft Office Client PC

MySQL

Linux

mod_php

Apache 2.0

Microsoft Access and Excel

J2EE Cluster (Calculation engine)

Page 14: Stateful SOAP Webservices

© MAYFLOWER GmbH 2008 14

Stateful SOAP Webservices

Welcome to the real world!Current architecture

Microsoft Office Client PC

MySQL

Linux

mod_php

Apache 2.0

SOAP

Axis2

Tomcat 5.5

SOAP

J2EE Cluster (Calculation engine)

java.class

Transfer by SFTP

SOAP over HTTPS

Page 15: Stateful SOAP Webservices

© MAYFLOWER GmbH 2008 15

Stateful SOAP Webservices

Questions and answers

Page 16: Stateful SOAP Webservices

Thank you very much!

Thorsten Rinne, Dipl.-Inf. (FH)Mayflower GmbHMannhardtstraße 6D-80538 MünchenGermany+49 (89) 24 20 54 – [email protected]