260 likes | 349 Views
Explore how AJAX and WS integration can transform three-tier applications into more efficient two-tier setups. Learn about SOAPHttpRequest, consuming web services, WS-* support, and the future of REST vs. SOAP. See code examples and best practices for implementing SOAPHttpRequest to streamline API interactions. Discover how to leverage existing SOAP engines and shorten the learning curve for handling SOAP envelopes in your applications.
E N D
Three - Tier No More: Integration - Ready Applications with AJAX and WS Samisa Abeysinghe (samisa@wso2.com) WSO2 Inc.
Who am I? • A developer Apache Axis2/C and Axis C++ • Member of Apache Software Foundation • Software Architect WSO2 Inc.
Agenda • Three-tier to Two-tier • XMLHttpRequest Characteristics • Introducing SOAPHttpRequest • Consuming Web Services • WS-* Support • REST vs. SOAP • Present and the Future
Three-tier to Two-tier Web Client Web Server App Server HTTP SOAP Web Client App Server HTTP / SOAP
Case for Dropping a Tier • Web services – SOAP over HTTP • Web browser can handle XML • Let browser talk SOAP
First Generation - XMLHttpRequest • Construct XML representing SOAP envelope • SOAP is XML<envelope> <header>...headers...</header> <body>...payload...</body></envelope> • Send over HTTP • Concerns • You need to build the request SOAP envelope • Need to strip SOAP in response
First Generation Example xmlhttp = new XMLHttpRequest(); xmlhttp.open("POST", "http://api.google.com/search/beta2",true); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4) { alert(xmlhttp.responseText) } } xmlhttp.setRequestHeader("Man", "POST http://api.google.com/search/beta2 HTTP/1.1") xmlhttp.setRequestHeader("MessageType", "CALL") xmlhttp.setRequestHeader("Content-Type", "text/xml")
First Generation Example (Contd.) xmlhttp.send("<?xml version='1.0' encoding='UTF-8'?>"+"\n\n"+ '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">'+ '<soapenv:Body>'+ '<ns1:doSpellingSuggestion xmlns:ns1="urn:GoogleSearch"'+ ' soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"'+ ' xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"'+ ' xmlns:xsd="http://www.w3.org/1999/XMLSchema">'+ '<key xsi:type="xsd:string">TcP7PPxQFHJtaZQO2OzWIPwdu2bUjYKD</key>'+ '<phrase xsi:type="xsd:string">salvasion</phrase>'+ '</ns1:doSpellingSuggestion>'+ '</soapenv:Body>'+ '</soapenv:Envelope>')
Next Generation - SOAPHttpRequest • Let AJAX handle SOAP for you • You just worry about request payload and response payload • Inherited form XMLHttpRequest • Hence same familiar API • Benefit • SOAP envelope is handled for you
Behind the Scenes • Used Apache Axis2/C as base • Implemented a Firefox extension • Extended Firefox AJAX API • Works both on Windows and Linux • Planning on an ActiveX object to support Microsoft IE
Objectives for SOAPHttpRequest • Use an API similar to XMLHttpRequest • Shorten learning curve • Leverage the power of an existing SOAP engine • Axis2/C is written with integration in mind as a portable C library • Written to support full web services stack
Consuming Web Services • Invoking Google spell checker:// create request object var req = new SOAPHttpRequest(); // set SOAP version to be SOAP 1.1 req.soapVer = 1.1; // set callback to be called on response req.onreadystatechange = listenStatus; // set up endpoint information; syntax:open(method, uri, isAsynchronous) req.open("POST", "http://api.google.com/search/beta2", false); // send request with payload req.send (reqContent);
Consuming Web Services - Payload • XML payload build using DOM API//create the root nodevar req_node = document.createElementNS ("urn:GoogleSearch", "ns1:doSpellingSuggestion" );req_node. setAttribute ("xmlns:xsd", "http://www.w3.org/2001/XMLSchema" );//create the node for keyvar key_node = document.createElement ("key");key_node. setAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "type", "xsd:string" );var key_text = document.createTextNode (key );key_node.appendChild(key_text);//create the node for phrase|var phrase_node = document.createElement ("phrase");phrase_node. setAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "type", "xsd:string" );var phrase_text = document.createTextNode (phrase);phrase_node. appendChild(phrase_text );//attach nodesreq_node. appendChild (key_node );req_node. appendChild (phrase_node );reqContent = req_node; //attach nodesreq_node. appendChild (key_node );req_node. appendChild (phrase_node );reqContent = req_node;
Consuming Web Services - Callback • Captures result// responseXML contains response domvar resultContent = req. responseXML;// Process result content as you wishHandleResponse (resultContent);
<?xml version='1.0' encoding='UTF-8'?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema"> <SOAP-ENV:Body> <ns1:doSpellingSuggestionResponse xmlns:ns1="urn:GoogleSearch" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <return xsi:type="xsd:string">tungsten</return> </ns1:doSpellingSuggestionResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope> XML in/out Model Request <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Header></soapenv:Header><soapenv:Body> <ns1:doSpellingSuggestion xmlns:ns1="urn:GoogleSearch" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema"> <key xsi:type="xsd:string">wCumFQpQFHL7+coIxlNKUGtyVsgrVAnb</key> <phrase xsi:type="xsd:string">tungsston</phrase> </ns1:doSpellingSuggestion> </soapenv:Body></soapenv:Envelope> Response
Non-Blocking • Set the third parameter of open to true req.open("POST", "http://api.google.com/search/beta2", true);
WS-* Support • Axis2/C designed to support WS-* • Concept of modules • WS-Addressing built in • WS-Security and WS-Reliable Messaging available as separate modules • Advantage of using Axis2/C as base • Any module available at C level freely available at AJAX level
WS-Addressing • SOAP Header sample<soapenv:Header xmlns:wsa="http://www.w3.org/2005/08/addressing"> <wsa:To>http://localhost:9090/axis2/services/echo</wsa:To> <wsa:Action>http://ws.apache.org/axis2/c/samples/echoString</wsa:Action> <wsa:MessageID>cb00ebf2-39ad-1db1-36e2-001125ce1ac4</wsa:MessageID></soapenv:Header> • Need for addressing in enterprise • reply to : where to send the reply • fault to : where to send the fault • e.g. Booking flight: send bill to finance, if error let me know
WS-Addressing with AJAX // engage addressing req.engage ( "addressing", "version" ); // set WSA action req.options({ wsa_action:"http://ws.apache.org/axis2/c/samples/echoString" } ); • API allows you to select the version • Addressing specific parameters could be set as options
REST vs. SOAP • REST and SOAP are different religions • Both have devotees • At 30,000 feet • REST is light weight; SOAP is heavy weight • REST QoS based on transport; SOAP has array of message level QoS options • Both has use cases • You will need SOAP for some use cases
Present • You can try WSO2 Tungsten Firefox (v 1.5.0.1) extension • Open source with Apache 2.0 license • http://dist.wso2.net/products/tungsten/ajax/xpi/installation.html • Can consume Web services with XML in/out model • WS-Addressing and WS-Security UsernameToken integrated • Can use both on Linux and Windows with Firefox
Future • Full WS-Security, MTOM and WS-RM support • ActiveX object for Windows IE • WS-Policy, WS-Eventing coming up with Axis2/C • Would be available to AJAX extension
Conclusion • Full power of Web services stack available to AJAX programmer • Familiar API • Based on well designed, proved Apache Axis2 Web services architecture • Designed by industry leaders • C implementation: fast and portable design
Links • WSO2 Tungsten XPI • http://dist.wso2.net/products/tungsten/ajax/xpi/ • Apache Axis2/C • http://ws.apache.org/axis2/c/ • This Presentation • http://people.apache.org/~samisa/
Thank you Time for Q&A