Presentation is loading. Please wait.

Presentation is loading. Please wait.

Web Services Overview Inspired by slides written by Marlon Pierce Community Grids Lab Indiana University.

Similar presentations


Presentation on theme: "Web Services Overview Inspired by slides written by Marlon Pierce Community Grids Lab Indiana University."— Presentation transcript:

1 Web Services Overview Inspired by slides written by Marlon Pierce Community Grids Lab Indiana University

2 What Are Web Services? The Web services framework is an XML-based distributed object/service/component system. The Web services framework is an XML-based distributed object/service/component system. Intended to support machine-to-machine interactions over the network.Intended to support machine-to-machine interactions over the network. The basic idea is to build a platform and programming language-independent distributed invocation system out of existing Web standards. The basic idea is to build a platform and programming language-independent distributed invocation system out of existing Web standards. Very loosely defined, when compared to CORBA, etc. Very loosely defined, when compared to CORBA, etc. Inherits both good and bad of the Web Inherits both good and bad of the Web Scalable, simple, distributedScalable, simple, distributed But no centralized management: must be tolerant of failures.But no centralized management: must be tolerant of failures.

3 Basic Architectures Browser Web Server HTTP GET/POST DB JDBC Web Server DB JDBC Browser Web Server SOAP GUI Client SOAP WSDL HTTP Web Services

4 Explanation of Previous Slide The diagram on the left represents a standard web application. The diagram on the left represents a standard web application. Browsers converse with web servers using HTTP GET/POST methods.Browsers converse with web servers using HTTP GET/POST methods. Servlets or CGI scripts process the parameters and take action (for example, connection to a DB).Servlets or CGI scripts process the parameters and take action (for example, connection to a DB). Examples: Google, AmazonExamples: Google, Amazon On the right, we have a Web services system. On the right, we have a Web services system. Interactions may be either through the browser or through a desktop client (in Java, Python, etc.)Interactions may be either through the browser or through a desktop client (in Java, Python, etc.) Examples: Google, Amazon (again !!!)Examples: Google, Amazon (again !!!)

5 SOAP e WSDL SOAP: Simple Object Access Protocol SOAP: Simple Object Access Protocol XML Message format between client and service.XML Message format between client and service. WSDL: Web Service Description Language. WSDL: Web Service Description Language. Describes how the service is to be usedDescribes how the service is to be used Similar to Java Interface.Similar to Java Interface. Guideline for constructing SOAP messages.Guideline for constructing SOAP messages. WSDL is an XML language for writing Application Programmer Interfaces (APIs).WSDL is an XML language for writing Application Programmer Interfaces (APIs).

6 Amazon and Google Experiment with Web Services Both Google and Amazon offer Web services. Both Google and Amazon offer Web services. Why? To allow partners to develop custom user interfaces and applications that use Google and Amazon data and services. Why? To allow partners to develop custom user interfaces and applications that use Google and Amazon data and services. You can download their APIs and try them. You can download their APIs and try them. http://www.google.com/apis/http://www.google.com/apis/http://www.google.com/apis/ http://www.amazon.com/webserviceshttp://www.amazon.com/webserviceshttp://www.amazon.com/webservices

7 Why Use Web Services? Web services provide a clean separation between a functionality and its user interface. Web services provide a clean separation between a functionality and its user interface. This allows a company (Google) with a sophisticated functionality and huge amounts of data to make that functionality available to its partners. This allows a company (Google) with a sophisticated functionality and huge amounts of data to make that functionality available to its partners. “Don’t worry about how PageRank works or web robots or data storage. We will do that. You just use this WSDL API to build your client application and use our search engine.”“Don’t worry about how PageRank works or web robots or data storage. We will do that. You just use this WSDL API to build your client application and use our search engine.”

8 Google’s PageRank Google’s PageRank is a search engine that was developed by two Stanford students. Google’s PageRank is a search engine that was developed by two Stanford students. It is an open algorithm published in scholarly journals, conferences. It is an open algorithm published in scholarly journals, conferences. Previous search engines were all proprietary.Previous search engines were all proprietary.

9 When to Use Web Services? Two or more organizations need to cooperate Two or more organizations need to cooperate One needs to write an application that uses another’s service.One needs to write an application that uses another’s service. Services can be upgraded independently of clients. Services can be upgraded independently of clients. Google can improve PageRank implemenation without telling me.Google can improve PageRank implemenation without telling me. Just don’t change the WSDL.Just don’t change the WSDL. Services can be easily expressed with simple request/response semantics and simple state. Services can be easily expressed with simple request/response semantics and simple state.

10 XML and Web services XML provides a natural substrate for distributed computing: XML provides a natural substrate for distributed computing: It’s just a data description.It’s just a data description. Independent from platform and programming language.Independent from platform and programming language. So let’s describe the pieces. So let’s describe the pieces. Web Services Description Language (WSDL) Web Services Description Language (WSDL) Describes how to invoke a service.Describes how to invoke a service. Can bind to SOAP for actual invocation.Can bind to SOAP for actual invocation. Simple Object Access Protocol (SOAP) Simple Object Access Protocol (SOAP) Wire protocol extension for conveying Remote Procedure Calls (RPC).Wire protocol extension for conveying Remote Procedure Calls (RPC). Can be carried over HTTP or SMTP.Can be carried over HTTP or SMTP.

11 Before Going On… In the next several slides we’ll go into the details of WSDL and SOAP. In the next several slides we’ll go into the details of WSDL and SOAP. But in practice, you don’t need to work directly with either. But in practice, you don’t need to work directly with either. Most tools generate the WSDL for you from your Java class.Most tools generate the WSDL for you from your Java class. SOAP messages are constructed by classes.SOAP messages are constructed by classes. Generated client stubs will even hide SOAP classes behind a local proxy that looks like a local class but actually constructs SOAP calls to the remote server.Generated client stubs will even hide SOAP classes behind a local proxy that looks like a local class but actually constructs SOAP calls to the remote server.

12 Web Services Description Language Defines what your service does and how it is invoked

13 WSDL Overview WSDL is an XML-based Interface Definition Language. WSDL is an XML-based Interface Definition Language. You can define the APIs for all of your services in WSDL.You can define the APIs for all of your services in WSDL. WSDL docs are broken into five major parts: WSDL docs are broken into five major parts: Data definitions (in XML) for custom typesData definitions (in XML) for custom types Abstract message definitions (request, response)Abstract message definitions (request, response) Organization of messages into “portTypes” and “operations” (which correspond to classes and methods).Organization of messages into “portTypes” and “operations” (which correspond to classes and methods). Protocol bindings (to SOAP, for example)Protocol bindings (to SOAP, for example) Service point locations (URLs)Service point locations (URLs) Some interesting features Some interesting features A single WSDL document can describe several versions of an interface.A single WSDL document can describe several versions of an interface. A single WSDL doc can describe several services.A single WSDL doc can describe several services.

14 The Java Code public String[] execLocalCommand(String command) { Runtime rt = Runtime.getRuntime(); Runtime rt = Runtime.getRuntime(); String stdout="",stderr=""; String stdout="",stderr=""; try { try { Process p = rt.exec(command); Process p = rt.exec(command); BufferedReader in= BufferedReader in= new BufferedReader(new InputStreamReader(p.getInputStream())); new BufferedReader(new InputStreamReader(p.getInputStream())); BufferedReader err= BufferedReader err= new BufferedReader(new InputStreamReader(p.getErrorStream())); new BufferedReader(new InputStreamReader(p.getErrorStream()));

15 Java Code Continued String line; String line; while((line=in.readLine())!= null) {stdout+=line+"\n";} while((line=in.readLine())!= null) {stdout+=line+"\n";} in.close(); in.close(); while ((line=err.readLine())!=null) {stderr+=line+"\n";} while ((line=err.readLine())!=null) {stderr+=line+"\n";} err.close(); err.close(); }//End of try{} }//End of try{} catch (Exception eio) {…} catch (Exception eio) {…} String[] retstring=new String[2]; String[] retstring=new String[2]; retstring[0]=stdout; retstring[0]=stdout; retstring[1]=stderr; retstring[1]=stderr; return retstring; return retstring; } //End of method

16 WSDL Example: Job Submission Our example is a simple service that can execute remote commands (local on the server). Our example is a simple service that can execute remote commands (local on the server). Service implementation (in Java) has a single method Service implementation (in Java) has a single method ExecLocal takes a single string argument (the command to be executed)ExecLocal takes a single string argument (the command to be executed) returns a string array (with standard out and error).returns a string array (with standard out and error). In this case the WSDL interface is mapped to a Java interface. In this case the WSDL interface is mapped to a Java interface.

17 The Full WSDL The following slide contains the WSDL definition for the Job Submit service. The following slide contains the WSDL definition for the Job Submit service. Some data definitions are omitted.Some data definitions are omitted. As you can see, WSDL is very verbose As you can see, WSDL is very verbose Typically, you don’t write WSDLTypically, you don’t write WSDL The WSDL file is generated from the Java class by Apache Axis.The WSDL file is generated from the Java class by Apache Axis. We will go through the parts of the doc in some detail. We will go through the parts of the doc in some detail.

18

19 WSDL Elements: Types, Messages Types: describes custom XML data types used in messages (optional). Types: describes custom XML data types used in messages (optional). We’ll see an example for defining arrays.We’ll see an example for defining arrays. Message: abstractly defines the messages that need to be exchanged. Message: abstractly defines the messages that need to be exchanged. Each method/function in the interface contains 0-1 request and 0-1 response messages.Each method/function in the interface contains 0-1 request and 0-1 response messages. Each message consists of part elements. Usually you need one part for each variable sent or received. Parts can either be XML primitive types or custom complex types.Each message consists of part elements. Usually you need one part for each variable sent or received. Parts can either be XML primitive types or custom complex types.

20 Types for Job Submission The job submission service receives a string (the command) and returns a 2-elements array. The job submission service receives a string (the command) and returns a 2-elements array. Strings are XML Schema primitive types, so we don’t need a special definition in our WSDL. Strings are XML Schema primitive types, so we don’t need a special definition in our WSDL. Arrays are not primitive types. They are defined in the SOAP encoding schema, so we will import the definition of this XML schema. Arrays are not primitive types. They are defined in the SOAP encoding schema, so we will import the definition of this XML schema. In other words, SOAP has rules for array encoding; simple XML has not.In other words, SOAP has rules for array encoding; simple XML has not.

21 WSDL types for Custom Data Definition <schema targetNamespace="http://.../GCWS/services/Submitjob" xmlns:impl="http://.../GCWS/services/Submitjob" xmlns= " http://www.w3.org/2001/XMLSchema" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" >

22 We start with some useful namespace definitions. We start with some useful namespace definitions. We next import the soap encoding schema We next import the soap encoding schema It has the array definitions we need.It has the array definitions we need. import means a simple cut-and-paste of the entire soap encoding XML schema.import means a simple cut-and-paste of the entire soap encoding XML schema. Finally, we define our own local XML complex type, Array_of_string. Finally, we define our own local XML complex type, Array_of_string. This extends the SOAP array typeThis extends the SOAP array type We restrict this to String arraysWe restrict this to String arrays Inserting a Type

23 Message Elements Our service implementation has one method: Our service implementation has one method: public String[] execLocalCommand(String cmd) This will require one “request” message and one “response” message. This will require one “request” message and one “response” message. Each message has one part : Each message has one part : Request message must send the String cmd.Request message must send the String cmd. Response must get back the String[] array (defined previously as a custom type).Response must get back the String[] array (defined previously as a custom type). If we had to pass two input variables, our “request” message would need two part elements. If we had to pass two input variables, our “request” message would need two part elements. Note: the name attributes of messages are important, because they are referred later! Note: the name attributes of messages are important, because they are referred later!

24 Message Examples for Job Submission Service <wsdl:message name="execLocalCommandRequest"> <wsdl:part type="xsd:string" /> <wsdl:message name="execLocalCommandResponse"> <wsdl:part type="impl:Array_of_string" />

25 portTypes portType elements map messages to operations. portType elements map messages to operations. You can think of You can think of portType==classportType==class operation==methodoperation==method Operations can contain input, output, and fault bindings for messages. Operations can contain input, output, and fault bindings for messages. An operation may support one of the following message styles: An operation may support one of the following message styles: One-way: request onlyOne-way: request only Two-way: request/responseTwo-way: request/response Solicit-response: server “push” and client responseSolicit-response: server “push” and client response Notification: one-way server pushNotification: one-way server push

26 portType for JobSubmit We previously defined the messages and types needed. Now we bind them into the portType structure. We previously defined the messages and types needed. Now we bind them into the portType structure. PortType names are important PortType names are important They will be referenced by binding element.They will be referenced by binding element. Note: names of previously defined messages are used as references in the operations. Note: names of previously defined messages are used as references in the operations.

27 <wsdl:input message="impl:execLocalCommandRequest" name="execLocalCommandRequest" /> <wsdl:output message="impl:execLocalCommandResponse" name="execLocalCommandResponse" /> portType definition

28 Some Notes on the PortType Definition PortTypes refer to messages by name PortTypes refer to messages by name The message attributes in and elements of refer to the name attributes of the previously defined messages.The message attributes in and elements of refer to the name attributes of the previously defined messages. The operation and portType names will similarly be used for reference in forthcoming tags.The operation and portType names will similarly be used for reference in forthcoming tags.

29 PortType Bindings portTypes are abstract interface definitions. portTypes are abstract interface definitions. portTypes don’t say anything about how to invoke a remote method.portTypes don’t say anything about how to invoke a remote method. Remote invocations are defined in binding elements. Remote invocations are defined in binding elements. WSDL specification provides different types of bindings: WSDL specification provides different types of bindings: SOAP (default)SOAP (default) HTTPHTTP MIME (e-mail)MIME (e-mail)

30 SOAP Bindings for JobSubmit Service Note that the binding element contains a mixture of tags from different namespaces (wsdl and wsdlsoap). Note that the binding element contains a mixture of tags from different namespaces (wsdl and wsdlsoap). WSDL child elements for binding element are operation, input, and output. WSDL child elements for binding element are operation, input, and output. WSDLSOAP elements give instructions to map WSDL to the protocol of choice (SOAP in this case). WSDLSOAP elements give instructions to map WSDL to the protocol of choice (SOAP in this case). The binding element name is important, it will be used as a reference by port definitions. The binding element name is important, it will be used as a reference by port definitions.

31 <wsdl:binding name="SubmitjobSoapBinding" type="impl:SJwsImp"> <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" /> <wsdlsoap:body encodingStyle=http://schemas.xmlsoap.org/soap/encoding/ namespace="http://.../GCWS/services/Submitjob" use="encoded" /> <wsdlsoap:body encodingStyle=http://schemas.xmlsoap.org/soap/encoding/ namespace=http://.../GCWS/services/Submitjob use="encoded" />

32 A Closer Look at SOAP Binding All this really means is “encode the message with the rules in encodingStyle and put it in the SOAP body.” All this really means is “encode the message with the rules in encodingStyle and put it in the SOAP body.” The bindings are just instructions that must be implemented by the SOAP message generator. The bindings are just instructions that must be implemented by the SOAP message generator.

33 Service and Port Definitions We have defined the class interfaces (portTypes) and the rules for binding to a particular protocol. We have defined the class interfaces (portTypes) and the rules for binding to a particular protocol. port elements define how the bindings (and thus the portTypes) are associated with a particular server. port elements define how the bindings (and thus the portTypes) are associated with a particular server. The service element collects ports. The service element collects ports.

34 <wsdl:port binding="impl:SubmitjobSoapBinding" name="Submitjob"> <wsdlsoap:address location="http://.../GCWS/services/Submitjob" /> Service and Port Elements for the Job Submission Service

35 Explanation Note that the port element’s binding attribute points to the appropriate binding element by name. Note that the port element’s binding attribute points to the appropriate binding element by name. The only purpose of the port element is to point to a service location (a URL). The only purpose of the port element is to point to a service location (a URL). Ports are child elements of the service element. A service can contain one or more ports. Ports are child elements of the service element. A service can contain one or more ports. a single portType may correspond to several ports, each with a different protocol binding and service address.a single portType may correspond to several ports, each with a different protocol binding and service address.

36 Simple Object Access Protocol A message format for exchanging structured, typed information

37 SOAP Basics SOAP is a format of XML messages for exchanging structured, typed data. SOAP is a format of XML messages for exchanging structured, typed data. SOAP is not a transport protocol. You must attach your message to a transport mechanism like HTTP. SOAP is not a transport protocol. You must attach your message to a transport mechanism like HTTP.

38 SOAP Structure A SOAP message is contained in an envelop. A SOAP message is contained in an envelop. The envelop element in turn contains (in order) The envelop element in turn contains (in order) An optional header with one or more entries.An optional header with one or more entries. A body element that contains the XML messages.A body element that contains the XML messages.

39 SOAP Headers Headers are really just extension points where you can include elements from other namespaces. Headers are really just extension points where you can include elements from other namespaces. i.e., headers can contain arbitrary XML.i.e., headers can contain arbitrary XML. Header entries may optionally have a mustUnderstand attribute. Header entries may optionally have a mustUnderstand attribute. mustUnderstand=1 means the message recipient must process the header element.mustUnderstand=1 means the message recipient must process the header element. If mustUnderstand=0 or is missing, the header element is optional.If mustUnderstand=0 or is missing, the header element is optional.

40 SOAP Body The body contains the XML message that you are transmitting. The body contains the XML message that you are transmitting. The message format is not specified by SOAP. The message format is not specified by SOAP. The tag pairs are just a way to notify the recipient that the actual XML message is contained there.The tag pairs are just a way to notify the recipient that the actual XML message is contained there. The recipient decides what to do with the message.The recipient decides what to do with the message.

41 Developing Web Services Using Apache Axis to develop Java implementations of Web services.

42 Web Service Development Tools Web service toolkits exist for various programming languages: Web service toolkits exist for various programming languages: C++,Python, Perl, Microsoft.NETC++,Python, Perl, Microsoft.NET We’ll concentrate on building Java Web services with Apache Axis. We’ll concentrate on building Java Web services with Apache Axis.

43 Apache Axis Overview Apache Axis is a toolkit for converting Java applications into Web services. Apache Axis is a toolkit for converting Java applications into Web services. Axis service deployment tools allow you to publish your service in a particular application server (Tomcat). Axis service deployment tools allow you to publish your service in a particular application server (Tomcat). Client side: Axis client tools allow you to convert WSDL into client “stubs” (or “proxies”). Client side: Axis client tools allow you to convert WSDL into client “stubs” (or “proxies”). Server side: Axis runtime tools accept incoming SOAP requests and redirect them to the appropriate service. Server side: Axis runtime tools accept incoming SOAP requests and redirect them to the appropriate service.

44 Developing and Deploying a Service Download and install Tomcat and Axis. Download and install Tomcat and Axis. Write a Java implementation Write a Java implementation Our SubmitJob is a simple example.Our SubmitJob is a simple example. Compile it with Tomcat.Compile it with Tomcat. Write a deployment descriptor (WSDD) for your service. Write a deployment descriptor (WSDD) for your service. It will be used by Axis runtime to direct SOAP calls.It will be used by Axis runtime to direct SOAP calls. Use Axis tools to install your WSDD file. Use Axis tools to install your WSDD file. It tells the axis servlet to load your class and direct SOAP requests to it.It tells the axis servlet to load your class and direct SOAP requests to it. That’s it. That’s it. Axis will automatically generate the WSDL for your service.Axis will automatically generate the WSDL for your service.

45 <deployment name="Submitjob" xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <parameter name="className" value="WebFlowSoap.SJwsImp"/> <parameter name="allowedMethods" value="execLocalCommand"/> Sample WSDD

46 Effect Axis will create a service called Axis will create a service called http://your.server/services/SubmitJobhttp://your.server/services/SubmitJob WSDL for service is available from WSDL for service is available from http://your.server/services/SubmitJob?wsdlhttp://your.server/services/SubmitJob?wsdl A list of all services is available from A list of all services is available from http://your.server/serviceshttp://your.server/services

47 WSDL generated starting from the Java implementation.

48 Building a Client with Axis Obtain the WSDL file. Obtain the WSDL file. Generate client stubs Generate client stubs Stubs look like local objectsStubs look like local objects They convert method invocations into SOAP messages.They convert method invocations into SOAP messages. Write a client application with the stubs Write a client application with the stubs Can be a Java GUI, a JSP page, etc.Can be a Java GUI, a JSP page, etc. Compile everything and run. Compile everything and run.

49 Sample Java Client Code /**Create SubmitJob client object and point to the service you want to use */ SubmiJob sjws = new SubmitJobServiceLocator().getSubmitjob(new URL(http://your.server/services/SubmitJob)); URL(http://your.server/services/SubmitJob)); /** Invoke the method as if local. */ String[] messages = sjws.execLocalCommand(command); sjws.execLocalCommand(command);


Download ppt "Web Services Overview Inspired by slides written by Marlon Pierce Community Grids Lab Indiana University."

Similar presentations


Ads by Google