620 likes | 634 Views
Explore SOAP in Web Services, learn about SOAP message exchange models, components like Envelope and Body, and handling faults in SOAP-based services.
E N D
Web Services and SOAP Web Services
Topics • SOAP Overview • Web Services • Deploying Web Services • Describing Web Services • Registering Web Services • Security • Web Services and J2EE Web Services
SOAP Overview • Simple Object Access Protocol (SOAP) • Interoperable XML-based communication mechanism for distributed computing • Many vendor and language implementations available • Web Services are built using SOAP as a communication mechanism • W3C working on SOAP 1.2 • Alternatives • RMI – Java-centric • CORBA - Complicated Web Services
SOAP Goals • Interoperability • Simple and lightweight • Not considered: • distributed garbage collection • batching of messages • objects-by-reference (which requires distributed garbage collection) • activation (which requires objects-by-reference) Web Services
The Big Picture HTTP/S Browser Data Sources Web Tier Business Logic (EJB, COM, Java) SOAP Client SOAP SOAP Service HTTP/S Client Java/RMI/IIOP/COM Web Services
Web Service Protocol Stack Workflow/Business Processes Web Service Discovery Web Service Description SOAP HTTP Jabber JMS SMTP …. Transport Web Services
SOAP Components • Envelope • Describes message, processing requirements, and message contents. Routing, delivery, etc. • Encapsulates data being transferred • Encoding Mechanism • Standard representation for application data types • RPC Conventions • Mechanism to issue remote procedure calls and receive a response Web Services
Example SOAP Request POST /StockQuote HTTP/1.1Host: www.stockquoteserver.comContent-Type: text/xml; charset="utf-8"Content-Length: nnnnSOAPAction: "Some-URI"<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <SOAP-ENV:Body> <m:GetLastTradePrice xmlns:m=“urn:DB3Service"> <symbol>DIS</symbol> </m:GetLastTradePrice> </SOAP-ENV:Body></SOAP-ENV:Envelope> Web Services
Example SOAP Response HTTP/1.1 200 OKContent-Type: text/xml; charset="utf-8"Content-Length: nnnn<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> <SOAP-ENV:Body> <m:GetLastTradePriceResponse xmlns:m=“urn:DB3Service"> <Price>34.5</Price> </m:GetLastTradePriceResponse> </SOAP-ENV:Body></SOAP-ENV:Envelope> Web Services
Message Exchange Model • Spec-wise – a one-way transmission between a sender and a receiver • Frequently, used in a request/response pattern • RPC • Document transmission Web Services
SOAP Message Optional Mandatory Content intended For receiver From O’Reilly’s ‘Web Service Essentials’ Web Services
Envelope • ‘Wrapper’ around the entire message • Namespace for envelope, header, and body <SOAP-ENV:Envelopexmlns:SOAP-ENV=http://schemas.xmlsoap.org/soap/envelope/ • Can contain an optional header • Contains exactly one body element Web Services
Header • Contains header blocks • Contextual information (like IIOP Context) • Authentication credentials • Transaction ids • At most, one header element can be present. If present, must be first element in the envelope Web Services
Header Attributes • mustUnderstand • Attribute on a header element • Recipient must understand the header element or must reject the message • <s:header> • <m:transaction xmlns:m=“soap-transaction” • s:mustUnderstand=“true” • <transactionId>505</transactionId> • </m:transaction> • </s:header> Web Services
Body • Contains the actual message being transmitted between sender and receiver • Can contain any valid, well-formed XML • Can not contain processing instructions or DTD references • <SOAP-ENV:Body> <m:GetLastTradePriceResponse xmlns:m=“urn:DB3Service"> <Price xsi:type=“xsd:float”>34.5</Price> </m:GetLastTradePriceResponse> • </SOAP-ENV:Body> Web Services
When things go wrong • FaultElement is returned in the Body <SOAP-ENV:Body> <SOAP-ENV:Fault> <faultcode xsi:type="xsd:string">SOAP-ENV:Client</faultcode> <faultstring xsi:type="xsd:string">The client has sent an invalid request</faultstring> </SOAP-ENV:Fault> </SOAP-ENV:Body> Web Services
Pre-defined fault codes • SOAP-ENV:VersionMismatch • Invalid namespace in SOAP envelope • SOAP-ENV:MustUnderstand • Could not process a header attribute with ‘mustUnderstand’ set to true • SOAP-ENV:Client • Error in client request e.g. bad parameters, bad method name • SOAP-ENV:Server • Server can not process request Web Services
Message Encoding • Specified rules for encoding application data types • Uses XMLSchema • Defines arrays and references • Optional • SOAP-ENV:encodingStyle attribute • 1.1=http://schemas.xmlsoap.org/soap/encoding/ • 1.2=http://www.w3.org/2001/09/soap-encoding <ns1:getPriceResponse xmlns:ns1="urn:DB3Service" SOAP-ENV:encodingStyle="http://www.w3.org/2001/09/soap-encoding"> <value xsi:type="xsd:double">2.71828</value> </ns1:getPriceResponse> Web Services
How are types specified? • Apache SOAP adds an explicit xsi:type attribute for each element • .NET omits xsl:type – assumes external schema definition • See upcoming interoperability slides Web Services
Some basic xml schema types • string • Some string data • binary • 1000101110 • short • -7, 135 • date • 2002-03-22 • Boolean • 1,0,true,false Web Services
Compound Types • Arrays • Structs • Binary data should be transmitted as xsi:type=“SOAP-ENC:base64” Web Services
Array Example <?xml version='1.0' encoding='UTF-8'?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2001/09/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <SOAP-ENV:Body> <ns1:getAuthorsResponse xmlns:ns1="urn:DB2Service" SOAP-ENV:encodingStyle="http://www.w3.org/2001/09/soap-encoding"> <return xmlns:ns2="http://www.w3.org/2001/09/soap-encoding" xsi:type="ns2:Array" ns2:arrayType="xsd:string[2]"> <author xsi:type="xsd:string">Author 37</author> <author xsi:type="xsd:string">Author 99</author> </return> </ns1:getAuthorsResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope> Web Services
Custom encoding rules • Embed an entire document in the body <ns1:getReportResponse xmlns:ns1="urn:DB3Service" SOAP-ENV:encodingStyle= "http://xml.apache.org/xml-soap/literalxml"> <return> <report serial=“1945"> <name>ReportName</name> <analyst>Dan</analyst> </report> </return> </ns1:getReportResponse> Web Services
Security • Still evolving • Confidentially • Can use transport mechanism (HTTPS) • XML Encryption standard for document contents • Authentication • SSL Certificate • Digitally Sign SOAP message • Authorization • Secure Authorization Markup Language (SAML) • http://www.oasis-open.org/committees/security/ Web Services
Certificate Authority Corporate Authorization Service Certificate Authentication 2. Retrieve user’s access permissions Security Server 1. Authenticate (SOAP/HTTPS) 3. Signed security assertions (SOAP/HTTPS) 4. Invoke (SOAP/HTTPS) (Passes Assertions) Web Service 5. Business operation Application (Any Technology) Web Services
Security (Cont) • Firewall filtering options • MIME type of text/xml-soap • SOAP-Action • Force M-POST requests • 510 Not Extended HTTP status code from server • SOAPMethodName Web Services
SOAP Implementations • Apache SOAP • Basic SOAP implementation • See xml.apache.org/soap/ • Apache AXIS • Next generation SOAP implementation • See xml.apache.org/axis/ • SOAP::Lite • Perl implementation. See www.cpan.org • .NET • See msdn.microsoft.com (.NET SDK) • Web services deployed to IIS Web Services
Interoperability • Minor issues exist between these implementations • .NET requires parameters to be named and typed • Issue for default SOAP::Lite (PERL) behavior • Different ideas of the SOAPAction Header • Not perfect but can be made to inter-operate • Interoperability labs and info • http://www.xmethods.net/ilab/ • http://www.mssoapinterop.org/ Web Services
Apache AXIS • Java-centric SOAP implementation • Runs as a J2EE web application • Receives SOAP request • Deserializes call parameters • Invokes method on your java class Web Services
Web Server Client 1. SOAP/HTTP 9. SOAP/HTTP 2. Forwards to Proxy 8. SOAP SOAP Proxy (AXIS) 3. De-serializes SOAP Message to Java object(s) Calls Java class 7. Serializes java objects To SOAP encoding 4. Invoke registered service 6. response 5. Perform service Java Class File (Web Service) Web Services
AXIS Installation • Copy webapps\axis directory to Servlet container’s webapps directory • Copy xerces.jar (or JAXP jar files) to axis\lib directory • Copy your web service class files to the WEB-INF\classes subdirectory (.jar files can go into WEB-INF\lib Web Services
Basic Deployment • Simple • cp someclass.java webapps\axis\someclass.jws • Better • Write a Web Services Deployment Descriptor(WSDD) Web Services
Hello Service • package corej2ee.exercise.webservice; • public class HelloService { • public String getHelloMessage() { • return "Web Service Hello"; • } • } • cp HelloService.java $TOMCAT_HOME/webapps/axis/HelloService.jws Web Services
Client Application package corej2ee.exercise.webservice; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import javax.xml.rpc.namespace.QName; public class TestHelloService { public static void main(String [] args) { try { String endpoint="http://localhost:9090/axis/HelloService.jws"; String method="getHelloMessage"; Service service = new Service(); Call call = (Call) service.createCall(); Web Services
Client Application call.setTargetEndpointAddress( new java.net.URL(endpoint) ); call.setOperationName( method ); call.setReturnType( org.apache.axis.encoding.XMLType.XSD_STRING ); String ret = (String) call.invoke(new Object[0]); System.out.println("Got " + ret); } catch (Exception e) { System.err.println(e.toString()); } } } Web Services
Production Deployment • Deployment descriptor • Can specify other services that run when service is invoked <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <service name=“HelloService" provider="java:RPC"> <parameter name="className" value=“corej2ee.exercise.webservice.HelloService"/> <parameter name="allowedMethods" value="*"/> </service> </deployment> Web Services
Deployment (Cont) • java org.apache.axis.client.AdminClient deploy.wsdd • -lurl sets the AxisServlet URL • -hhostName sets the AxisServlet host • -pportNumber sets the AxisServlet port • -sservletPath sets the path to the AxisServlet • -ffileName specifies that a simple file protocol should be used • -uusername sets the username • -ppassword sets the password • -d sets the debug flag (for instance, -ddd would set it to 3) • -tname sets the transport chain touse • list will list the currently deployed services • quit will quit (???) • passwd value changes the admin password • xmlConfigFile deploys or undeploys Axis components and web services • If -l or -h -p -s are not set, the AdminClient will invoke http://localhost:8080/axis/servlet/AxisServlet Web Services
Serializing Java Objects • AXIS can automatically serialize simple Java object types to/from the standard SOAP encoding • Complex objects require developer input • BeanSerializer • Custom Serializers Web Services
BeanSerializer • When complex objects are passed, AXIS must be told how to convert the object to/from XML • If object is a Java Bean, the BeanSerializer class can be used • Have to specify mapping in deployment descriptor and the client Web Services
Serializers In deployment descriptor: <beanMapping qname="myNS:HelloStruct" xmlns:myNS="urn:HelloService" languageSpecificType=“model.HelloMessage"/> In client program: call.registerTypeMapping(HelloMessage.class, qn, new org.apache.axis.encoding.ser.BeanSerializerFactory(HelloMessage.class, qn), new org.apache.axis.encoding.ser.BeanDeserializerFactory(HelloMessage.class, qn)); • Uses reflection to invoke all get() methods and serialize • Can write your own serializers and register them • non Java Bean classes • efficiency Web Services
Describing Web Services • Web Services Description Language (WSDL) • Grammar for defining web services • Describes service • Input/output • Message encoding • Transport required • Address information • Initially developed by IBM, Ariba, Microsoft • Given a WSDL description, automated tools can generate stubs to call the service Web Services
<?xml version="1.0" encoding="UTF-8"?> <wsdl:definitions targetNamespace=http://localhost:9090/axis/HelloService.jws xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:SOAP-ENC=http://schemas.xmlsoap.org/soap/encoding/ xmlns:impl="http://localhost:9090/axis/HelloService.jws-impl" xmlns:intf=http://localhost:9090/axis/HelloService.jws xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap=http://schemas.xmlsoap.org/wsdl/soap/ xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <wsdl:message name="getHelloMessageResponse"> <wsdl:part name="return" type="xsd:string"/> </wsdl:message> <wsdl:message name="getHelloMessageRequest"> </wsdl:message> Web Services
<wsdl:portType name="HelloService"> <wsdl:operation name="getHelloMessage"> <wsdl:input message="intf:getHelloMessageRequest"/> <wsdl:output message="intf:getHelloMessageResponse"/> </wsdl:operation> </wsdl:portType> Web Services
<wsdl:binding name="HelloServiceSoapBinding" type="intf:HelloService"> <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="getHelloMessage"> <wsdlsoap:operation soapAction=""/> <wsdl:input> <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:9090/axis/HelloService.jws" use="encoded"/> </wsdl:input> <wsdl:output> <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:9090/axis/HelloService.jws" use="encoded"/> </wsdl:output> </wsdl:operation> </wsdl:binding> Web Services
<wsdl:service name="HelloServiceService"> <wsdl:port binding="intf:HelloServiceSoapBinding" name="HelloService"> <wsdlsoap:address location="http://localhost:9090/axis/HelloService.jws"/> </wsdl:port> </wsdl:service> </wsdl:definitions> Web Services
Auto-Generated client stubs • Invoke • java org.apache.axis.wsdl.WSDL2Java (WSDL-file-URL) • http://localhost:9090/axis/HelloService.jws?WSDL • Generated 04/07/2002 03:29 PM 285 HelloService.java 04/07/2002 03:29 PM 482 HelloServiceService.java 04/07/2002 03:29 PM 1,262 HelloServiceServiceLocator.java 04/07/2002 03:29 PM 4,580 HelloServiceSoapBindingStub.java • Follows JAX-RPC spec for generated client bindings Web Services
Client Program package corej2ee.exercise.webservice; public class WSDLHelloClient { public static void main(String[] args) { localhost.HelloServiceServiceLocator service = new localhost.HelloServiceServiceLocator(); // Get the stub localhost.HelloService port=null; try { port = service.getHelloService(); } catch(javax.xml.rpc.ServiceException se2) { se2.printStackTrace(); System.exit(-2); } Web Services
// Make the actual call String msg="Nothing"; try { msg=port.getHelloMessage(); } catch(java.rmi.RemoteException re) { re.printStackTrace(); System.exit(-3); } System.out.println("Client got: " + msg); } } Web Services
Discovering Web Services • Universal Description, Discovery, and Integration (UDDI) • Lists description of a business and services offered • Can federate registries • Find service in registry, automatically create proxies, and invoke • Web Services Inspection Language (WSIL) • IBM and Microsoft proposal • Simpler. More of a white pages paradigm Web Services