Design of Distributed Software 2.7 Web Services 1.Introduction : Service Oriented Architectures 2.Technology building blocks 3.Java Web Services
Design of Distributed Software2 Web Service 1. Introduction “A Web service is a software application identified by a URI whose interfaces and binding are capable of being defined, described, and discovered by XML artifacts, and supports direct interactions with other software applications using XML-based messages via Internet-based protocols.” Service interface Service implementation Service implementation : -any application - any programming language - any platform - remotely available through web service provider Service definition/interface : - defines the service - in XML-syntax - not necessarily on same machine as service implementation - includes : - data types involved - supported method calls - network location WEB SERVICE XML- message
Design of Distributed Software3 Service-Oriented Architectures 1. Introduction Vision Services should be dynamically discoverable, Separation between service description and implementation Assemble application “ad hoc” based on discovered services “Service-oriented architectures” provider requestor registry broker owner of the service runs the service remotely offers XML-description of service manages info repository on providers and services white pages : business info on provider (name, description, …) green pages : info how to use the service (policies, software bindings, …) yellow pages : key-word based search engine for services discovers and invokes service can be person/application/service
Design of Distributed Software4 Service-Oriented Architectures 1. Introduction provider requestor registry 1 : publish 2 : find Alternative : no registry - direct publishing : offline mechanism to locate service - use HTTP GET request on service provider webserver Dynamic publishing Query public/private registry Specifies service type Can include QoS requirements 3 : bind retrieve service description (how to access service ?) default : from provider alternative : from broker access the service
Design of Distributed Software5 Web Service Technology 2. Technology “Simple Object Access Protocol” RPC-protocol for web services used to access remote service “Web Services Description Language” description of how to access web service used to access remote service “Representational State Transfer” XML directly over HTTP URI per method
Design of Distributed Software6 Web Service Technology 2. Technology Servlet JSP/ASP Web service CORBA RMI HTTP SOAP over HTTP/SMTP IIOP binary protocol object oriented text protocol not object oriented text protocol object oriented
Design of Distributed Software7 SOAP : philosophy and architecture 2. Technology text-oriented protocol (XML-messages) messages can have attachments object oriented (descendant from XML-RPC) specifies object attributes methods to call easy access : on top of HTTP or SMTP (no blocking, clear text) Layered approach SOAP RPC Encoding Rules Messaging Framework HTTP Binding SMTP Binding HTTPSMTP Syntax of SOAP-messages “Envelopes” Mapping to transport protocol Map RPC request To message (data-type -> XML) Rules for SOAP-RPC
Design of Distributed Software8 SOAP messages 2. Technology SOAP envelope SOAP header SOAP body SOAP block … … Container for SOAP message Defines relevant xml namespaces, encodings Directives to SOAP-processor Not always forwarded by intermediary SOAP-nodes Header content standardized by SOAP Payload, message data To be processed by ultimate receiver SOAP-processor = environment receiving/processing/forwarding SOAP-message (e.g. J2EE application server,.NET server, …)
Design of Distributed Software9 SOAP message : request example 2. Technology POST /InStock HTTP/1.1 Host: Content-Type: application/soap+xml; charset=utf-8 Content-Length: nnn <soap:Envelope xmlns:soap=" soap:encodingStyle=" IBM HTTP protocol binding Specify namespaces And encoding rules RPC-specification SOAP RPC request stock.GetStockPrice(“IBM”)
Design of Distributed Software10 SOAP message : reply example 2. Technology HTTP/ OK Content-Type: application/soap; charset=utf-8 Content-Length: nnn <soap:Envelope xmlns:soap=" soap:encodingStyle=" “return 34.5” SOAP RPC reply
Design of Distributed Software11 SOAP message : parameters 2. Technology <ns1:getPrice xmlns:ns1= SOAP-ENV:encodingStyle= SOAP and Web Services Method declared as : getPrice(String, String) Invoked through : price.getPrice(“ ”,” SOAP and Web Services”) Native Soap types : string base64Binary(binary formatted objects, e.g. JavaBeans) integer, byte, short, int, long decimal, float, double boolean dateTime, time, date, duration
Design of Distributed Software12 WSDL Technology XML grammar to specify collection of “access end points” (1 URL specifies a single access end point) designed to automate application-to-application interaction (or B2B interaction) defines the communication protocol to be used at runtime message format methods to be invoked parameter lists, return types … WSDL descriptions can be automatically generated for existing code stub classes can be generated from WSDL descriptions Web Services Description Language
Design of Distributed Software13 A simple Web Service JAX-RPC package random; import java.rmi.Remote; import java.rmi.RemoteException; public interface RandomServiceIF extends Remote { public String sayHello(String s) throws RemoteException; public int getRandomNumber(int max) throws RemoteException; } RandomServiceIF.java interface MUST extend java.rmi.Remote each method MUST throw java.rmi.RemoteException can NOT be in default package method arguments must obey to special rules (map to SOAP-types) 2. Technology
Design of Distributed Software14 A simple Web Service JAX-RPC <definitions name="RandomService" targetNamespace="urn:RandomService" xmlns:tns="urn:RandomService" xmlns=" xmlns:xsd=" xmlns:soap=" RandomService.wsdl Automatically generated 2. Technology
Design of Distributed Software15 A simple Web Service JAX-RPC <soap:body encodingStyle=" use="encoded" namespace="urn:RandomService"/> <soap:body encodingStyle=" use="encoded" namespace="urn:RandomService"/> <soap:body encodingStyle=" use="encoded" namespace="urn:RandomService"/> <soap:body encodingStyle=" use="encoded" namespace="urn:RandomService"/> RandomService.wsdl Automatically generated 2. Technology
Design of Distributed Software16 A simple Web Service JAX-RPC After deployment to server 2. Technology
Design of Distributed Software17 A simple Web Service JAX-RPC After deployment to server 2. Technology
Design of Distributed Software18 WSDL1.1 : document structure 2. Technology * * * * * * * container for service description global declaration of document scope namespaces similar to java-import (modularization of WSDL docs) include of namespaces (not file itself !) contains definition of all datatypes used in messages currently : XSD (XML Schema Definitions) defines message structure (for each part : type and name are defined) specifies legal operations for web service endpoint (=group of actions) : method definition specifies method name input message output message error message
Design of Distributed Software19 WSDL1.1 : document structure 2. Technology * * * * * * * concrete protocol & data format spec for portType standard soap-bindings available defines URL for service endpoint = single access point for webservice each port specifies name binding info URL of webservice ( element)
Design of Distributed Software20 WSDL1.1 : supported interactions 2. Technology Request – response before clientserver 1 2 Solicit - response before clientserver 1 2 One-way (asynchronous) only clientserver 1 Notification only clientserver 1
Design of Distributed Software21 WSDL1.1: different styles RPC/encoded RPC/literal Document/encoded (not used) Document/literal 2. Technology
Design of Distributed Software22 RPC/encoded 2. Technology public void myMethod(int x, float y); -XSI types usually overhead - no easy validation - not WS-I compliant (WS-I defines interoperability rules)
Design of Distributed Software23 RPC/literal 2. Technology ; public void myMethod(int x, float y); - no overhead from XSI types - still no easy validation -WS-I compliant
Design of Distributed Software24 Document/literal 2. Technology public void myMethod(int x, float y); - no operation name (difficult dispatching) - easy validation (soap:body can be defined in a schema) - not WS-I compliant
Design of Distributed Software25 Document/literal wrapped 2. Technology public void myMethod(int x, float y); - operation name is available - easy validation (soap:body can be defined in a schema) - WS-I compliant
Design of Distributed Software26 Summary WSDL styles Use document/literal wrapped except: when overloaded operations then: use document/literal unwrapped however: when several methods have same signature then: use RPC/literal in case of data graphs (e.g. binary trees), literal style is not an option, prefer RPC/encoded public void myMethod(int x, float y); public void myMethod(int x); public void myMethod(int x, float y); public void myMethod(int x); public void myOtherMethod(int x, float y); 2. Technology
Design of Distributed Software27 WS-Addressing WS-Addressing defines standard ways to route a message over multiple transports or direct a response to a third party. Without WS-Addressing: a standard SOAP request is sent over HTTP, the URI of the HTTP request serves as the message's destination. The message response is packaged in the HTTP response and received by the client over the HTTP connection. For example, a client application might send a request over JMS and ask to receive the response through or SMS. To enable these kinds of applications, WS- Addressing incorporates delivery, reply-to, and fault handler addressing information into a SOAP envelope. 2. Technology
Design of Distributed Software28 WS-Addressing example <S:Envelope xmlns:S=" xmlns:wsa=" Technology
Design of Distributed Software29 WSDL2.0 W3C working draft since 2004, recommendation since july 2007 rarely used for the moment: WSDL 1.1 still widely used support for WS-Addressing interface type instead of port type no operation overloading allowed 2. Technology
Design of Distributed Software Web Services 1.Introduction : Service Oriented Architectures 2.Technology building blocks 3.Java Web Services 4.Netbeans support for Web Services
Design of Distributed Software31 Java Web Services 3. Java WS Web service front-end implemented through Servlet Application server Servlet EJBs Other resources e.g. JCA-adapters (Java Connector Architecture) JMS-resources (Java Message Service) SOAP/HTTP Servlet responsibilitiet : SOAP Parsing Resource lookup Behaviour handling
Design of Distributed Software32 JAX WS Java EE 5 provides a robust platform on which web services can be built and deployed (previous JAX-RPC) Web service can be built either as: Option 1: regular Java class Option 2: EJB 3 stateless session bean Option 2 allows for use of declarative transactions and security. Advantage Option 1: can be run in a web container, no need for application container. Both options can use annotations, definition of life cycle methods. Option 1 requires annotation processing in an external Annotation Processing Tool (APT) 3. Java WS
Design of Distributed Software33 EJB as a Web = public class eBayBean implements eBayInterface private EntityManager em; public eBayBean() = "bidNumber") public Long = "User") String = "Item") Long = "Price") Double bidPrice) { return persistBid(userId, itemId, bidPrice); } private Long persistBid(String userId, Long itemId, Double bidPrice) { } 3. Java WS
Design of Distributed annotation WebService { String name() default ""; String targetNamespace() default ""; String serviceName() default ""; String wsdlLocation() default ""; String endpointInterface() default ""; String portName() default ""; }; 3. Java WS
Design of Distributed Software35 annotation annotation SOAPBinding { public enum Style { DOCUMENT, RPC }; public enum Use { LITERAL, ENCODED }; public enum ParameterStyle { BARE, WRAPPED }; Style style() default Style.DOCUMENT; Use use() default Use.LITERAL; ParameterStyle parameterStyle() default ParameterStyle.WRAPPED; } 3. Java WS
Design of Distributed Software36 annotation public class eBayBean { public Long addBid(..) { = "true") public Long persistBid(..) { } 3. Java WS
Design of Distributed Software37 annotation annotation { String operationName() default ""; String action() default "" ; boolean exclude() default false; }; 3. Java WS
Design of Distributed Software38 public Long = "user", mode = WebParam.Mode.IN) String userId,...) {... } 3. Java WS
Design of Distributed Software39 annotation annotation { public enum Mode { IN, OUT, INOUT }; String name() default ""; String targetNamespace() default ""; Mode mode() default Mode.IN; boolean header() default false; String partName() default ""; }; 3. Java WS
Design of Distributed annotation = "bidNumber") public Long addBid(...){} { String name() default "return"; String targetNamespace() default ""; boolean header() default false; String partName() default ""; }; 3. Java WS
Design of Distributed Software41 Accessing a web service from an EJB The client for the EJB web service can be any of the following types: Java application client Dynamic proxy Dynamic Invocation Interface (DII) import javax.xml.ws.WebServiceRef " private static eBayService ebs; public static void main(String [] args) { try { eBayBean ebay = ebs.geteBayBeanPort(); System.out.println("Bid Successful, BidId Received is:“ +ebay.addBid(“test", 1001, 185,0 )); } catch (Exception ex) { ex.printStackTrace(); } 3. Java WS
Design of Distributed Software42 EJB as a web service public class AmazonOrderBean implements AmazonOrder private AmazonDeliverySEI deliveryService; public String checkOrderDeliverStatus(String shipId) {... String deliveryStatus = deliveryService.checkDeliveryStatus(shipId);... } 3. Java WS