Soap Ui Using Guide - Notes
Soap Ui Using Guide - Notes
Soap Ui Using Guide - Notes
Jan Christian Bryne, chrb@ii.uib.no Anders Lanzen, Anders.Lanzen@bccs.uib.no Computational Biology Unit, Bergen Center for Computational Science
Contents
1 About this document 2 XML 2.1 XML Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2 Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Web Service Technologies 3.1 SOAP . . . . . . . . . . . . . . . . . . . . . . . . 3.1.1 SOAP Engines . . . . . . . . . . . . . . . 3.2 Web Services Description Language (WSDL) . . 3.2.1 Exercise 1 : Exploring the ELM Database 3.2.2 Web services interoperability (WS-I) . . . . . . . . . . . . . . . . . . WSDL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3 4 4 4 6 6 7 7 8 9 10 10 11 12 14 14 14 15 16 18 18 18 19 20 21 21 21
4 Web Service Development 4.1 Development methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.1.1 Binding styles: . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Document/literal wrapped style . . . . . . . . . . . . 4.2 Exercise 2 - Creating a Web Service for the long-orfs program . . . . . . 4.2.1 About the long-orfs program . . . . . . . . . . . . . . . . . . . . 4.2.2 About Eclipse WTP . . . . . . . . . . . . . . . . . . . . . . . . . 4.2.3 Explore the EchoService example . . . . . . . . . . . . . . . . . . 4.2.4 Create your Web Service Interface . . . . . . . . . . . . . . . . . Hints: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.2.5 Validate your WSDL . . . . . . . . . . . . . . . . . . . . . . . . . 4.2.6 Generate server code from your WSDL . . . . . . . . . . . . . . 4.2.7 Connect back-end code with generated code . . . . . . . . . . . . Hints: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.2.8 Deploying your Web Service . . . . . . . . . . . . . . . . . . . . . 4.2.9 Try out your Web Service using soapUI . . . . . . . . . . . . . . 4.3 Exercise 3 - Creating a client using a simple scripting language (Python)
2 XML
XML is short for eXtensible Markup Language. Information is structured using elements and attributes. Elements are the name of the tags:
<Element> ... </Element>
It is generally easier for computers to parse information structured with elements instead of as attributes.
2.2 Namespaces
XML namespaces is a method to avoid conicts between elements with the same name. This type of conict may arise if an XML document contains elements from several XML Schema and an element name occurs in more than one schema. It can be resolved by dening a namespace for each element, such as:
<namespace:Element> ... </namespace:Element>
To avoid repeating a namespace declaration for each element that belongs to it, a prex declaration can be made in a parent element. If, for example we have two dierent elements with the name element and want to use two separate namespaces for these, we would write:
The rst element in the example above belongs to namespace http://mysite.com/NS1 while the second belongs to http://mysite.com/NS2. The use of http network addresses for namespace declarations is just a convention. Any string could be used. If XML concepts such as namespaces and XML Schema seem too abstract or unfamiliar, have a look at the XML Tutorial at W3 Schools at http://www.w3schools.com/xml/.
3.1 SOAP
SOAP is a framework for exchange of XML documents. It enjoys the status of a recommendation by the World Wide Web Consortium (W3C), which can be found at http://www.w3.org/TR/soap/. SOAP introduces some additional structure in XML documents. A SOAP Message is an Envelope element that consists of an optional Header element and a required Body element:
<Envelope> <Header> ... </Header> <Body> ... </Body> </Envelope>
This structure allows separation of meta data contained in the Header from the message payload contained in the Body. Such meta data can be routing information, expiration information, or other. The header is usually not used in Plain Vanilla (ordinary) Web Services, but we will show an example later in the course that will demonstrate how it may be used. The Body element can contain any XML.
SOAP also denes: a processing model a mechanism for error handling an extensibility model a mechanism for data representation a convention for Remote Procedure Calls (RPC) a protocol binding framework
Simple overview of a WSDL document (each element may occur more than once):
<definitions> <types/> <message/> <part/> </message> <portType> <operation/> </portType> <binding/> <service> <port/> </service> </definitions>
As you can see from the overview above, WSDL document consists of the following parts: types: data type declarations using some type system (normally XML Schema). External XML Schema may also be imported. message elements : abstract denitions of what can be communicated, i.e. incoming and outgoing messages, and their data types. Data types are dened in part child elements and may refer to types declared in the types element described above. portType: a Port Type contains one or more operation elements. Each Operation abstractly species an action supported by the service and the Messages that may be sent to and from it. The set of Operations in a Port Type is supported by one or more endpoints. binding: a Binding denes message format and protocol details for Operations and messages dened by a Port Type, i.e. the WSDL-to-SOAP mapping of the service (providing that the Binding uses SOAP). The data formatting regulations and encoding are usually RPC/encoded, RPC/literal or document/literal. This is also known as the style of the Web Service. service: A collection of endpoints. Each endpoint is represented by a port element and each port denes a network address and a name for a Binding, thus dening a Web Service instance.
it requires a getELMRequest as input message. What data type does such a message contain? Browse through the WSDL upwards and look at getELM in the portType declaration and then the getELMRequest Message and then the getELM element in the types declaration. As you can see, this element contains an ELMAccessionType that is not declared in this WSDL. In fact, it is imported from the XML Schema at http://api.bioinfo.no/schema/ELM.xsd as can be seen in the import declaration in the beginning of types. Open this XML Schema in a new window and look at the type declaration of the ELMAccessionType. Can you deduct anything about the format of the Accession Numbers used in ELM from this? How many characters does such an Accession Number have?
10
Figure 4.1: Data ow between user and provider (client and server) of a Web Service Method 1 is the faster strategy and requires little eort from the programmer. However, the resulting WSDL le and server stub is a result of the tool with which they were generated. This leaves little control over the details to the developer. In some cases, the resulting Web Service generated can only be understood by clients using the same tool for de-serialization. To ensure that the WSDL le and service supports the rapidly evolving standards and best practices, including WS-I (see 3.2.2) , ne-grained control over the design is often necessary. With existing tools, developers are usually forced to operate on a lower level and design the WSDL le manually to ensure proper interoperability. Because of this, our recommendation is to use method 2 in most situations.
11
The naming conventions of RPC vs. Document is perhaps unfortunate, as there is nothing wrong in having a Literal style Web Service with operations that behave as remote procedure calls. The term RPC simply refers to a convention for how to de-serialize objects to SOAP and nothing more. Our recommendation is to use the Document/literal Wrapped style in most cases. Only this style will be described here, but if you are interested in the other styles and how they dier, have a look at the very well-written guide Which style of WSDL should I use? at http://www128.ibm.com/developerworks/webservices/library/ws-whichwsdl/. The Document/literal wrapped style The Document/literal wrapped style is a special case of the Literal/wrapped style. This style prescribes that any data type declarations are described using an XML Schema. The SOAP messages sent to or from a Web Service following this style can thus be validated against this schema using an XML validator. This is an important and useful feature! Further, each message part used for an input message must refer to an element declared in the data type declaration that has the same name as the operation. This is called a wrapper element. Thanks to this, the operation name appears in the incoming SOAP message. If this sounds too abstract, have a look at the following example of a WSDL of a service with an operation called getDNASequence. This method accepts two positions and returns the DNA Sub-sequence from a particular bacterial genome between these two positions, encoded as a string.
<types> <schema> <element name="getDNASequence"> <complexType><sequence> <element name="start" type="xsd:int"/> <element name="end" type="xsd:int"/> </sequence></complexType> </element> <element name="getDNASequenceResponse"> <complexType><sequence> <element name="dnaSequence" type="xsd:int"/> </sequence></complexType> </element> </schema> </types> <message name="getDNASequenceRequest"> <part name="parameters" element="getDNASequence"/> </message>
12
<message name="getDNASequenceResponse"> <part name="parameters" element="getDNASequenceResponse"/> </message> <portType name="PT"> <operation name="getDNASequence"> <input message="getDNASequenceRequest"/> <output message="getDNASequenceResponse"/> </operation> </portType> <binding ... />
A wrapped SOAP message for an incoming request to this operation would look like this:
<soap:envelope> <soap:body> <getDNASequence> <start>1</start> <end>2500</end> </getDNASequence> </soap:envelope> </soap:body>
A Document/literal wrapped style Web Service has the following characteristics: The input message in the WSDL has a single part that refers to an element. This wrapper element has the same name as the operation. There are no attributes in the wrapper element. This style has a number of advantages that, in our opinion, outweighs the disadvantage of the fairly complicated WSDL document: No type declarations for variables in SOAP messages (as opposed to the encoded style). The body of the SOAP messages is dened by a schema and can be validated against it. The method name appears in incoming SOAP messages. The style is WS-I compliant. The ELM database Web Service, from the exercise in 3.2.1 above, uses the Document/literal wrapped style.
13
14
with tools for developing J2EE Web applications. WTP adds a lot of functionality, but we will use only the XML editor and WS-I validator. For more information, visit www.eclipse.org/webtools/. NOTE: You are free to use any editors you would like during the course, but we recommend the use of WTP Eclipse because it removes a lot of irrelevant complexity in the development environment. The main parts of the Java Perspective in the Eclipse Workbench are shown in Figure 4.2.
Figure 4.2: The main parts of the Java Perspective in the Eclipse Workbench (default)
15
Explore the contents of the source directory (src). In this simple example, SayHi.java constitutes the back-end code. All other classes are part of the server stub and has been auto-generated from the WSDL document. EchoServiceBindingImpl.java has then been manually modied to call the back-end code in a meaningful way. Double click on build.xml to open it. This is an Ant Tasks le congured to perform standard tasks such as generating the server stub and deploying the complete Web Service. Note how the available tasks are shown in the Outline tab to the right in the workbench. The EchoService is already deployed as a Web Service on your local computer and you can have a look at it using the tool SOAPUI. To launch SOAPUI, use the icon on the desktop. First of all you need to create a new project. This is done using File > new WSDL project. Name your project for example EchoService and accept to create the suggested project le. Then, you need to add the external WSDL le. Do this by right-clicking on the project in the list of projects (in the top left panel) and select Add WSDL from URL. When prompted for the address, type http://localhost:8080/axis/services/EchoService?wsdl. Select yes to the question about creating default requests. SOAPUI has now created a SOAP envelope template for you in order to send requests to the Web Service. Double-click this request template (Request 1 under operation SayHi in your service) to open it in a new frame. You can now ll in the contents of the element Hi where a question mark has been put. Click the green play button in the top left corner of the request frame! You can then see the response SOAP envelope that is sent back to you. The contents of its HiResponse Element is the same as you sent it (hence, EchoService). Notice how the elements in the request and response elements correspond to the declarations in the WSDL!
16
A number of jars (class libraries) were made available to the projects build path. This is done by selecting Project > Properties > Java Build Path and then selecting Add Jars in the Libraries tab. The back-end code for accessing the long-orfs program was imported to the source directory (src). This code is contained in the package longorfs. Created a skeleton WSDL le called LongORFs.wsdl inside the wsdl folder. Now, to complete the Web Service: Open LongORFs.wsdl in Eclipse WTPs WSDL Editor by double-clicking on it. The WSDL Editor enables two views to a WSDL le: source or graph. You can switch between these by clicking on the tabs right underneath the editor window (see Fig. 3.1.1). We recommend always using the source view when writing the WSDL from scratch. Start writing the WSDL by creating an XML Schema to dene the complex data types you want to return from the Web Service. An ORF prediction data type denition (for the output) and a Genome sequence (for the input) should be enough in this case. Continue by dening the single element data types to be sent as input and output from the operations of the Web Service. In this case it is probably enough with one operation, e.g. getORFs. Use the Document/literal wrapped style! Create your Message Elements. (e.g. getORFsRequest and getORFsResponse) and connect them to your wrapper data types. Create your Port Type Element and its Operation(s). Connect its Input and Output Elements to your message elements Create your Binding Element. Specify your binding style (using a soap:binding element). Add the Operation(s) from your Port Type. These also need input and output elements, but there is a subtle dierence from how they are specied here compared to in the abstract Binding: They do not have attributes and they need child elements specifying their content in terms of SOAP encoding. In this case, it is enough with a SOAP body, using literal encoding: <soap:body encodingStyle= "http://schemas.xmlsoap.org/soap/encoding/" use="literal" /> Create your concrete endpoint, i.e. the Service Element and its Port. Put http://localhost:8080/axis/LongORFs as your network address.
17
Hints: Instances of the class ORF in the longorfs package represents a predicted ORF. Review this class when designing your data types. The format of an ORF is quite simple. It has a start and a stop position represented by integers, a number, a strand (+ or -) and a score. Use the Document/literal wrapped style. Remember the rules from 4.1.1 above! Each operation you want your Web Service to perform must have a wrapper data type denition for its input using the same name as the operation. For the output it must have a data type with the same name as the operation plus Response. Have a look at the WSDL from EchoService or the ELMdb WSDL from previous exercises and imitate the style.
18
Figure 4.3: Re-validate your WSDL each time you have corrected an error! Besides the server stub, Axis has generated two Axis WSDD les; deploy.wsdd and undeploy.wsdd. These have been moved to the conf directory by ant. They are conguration les that are needed by Axis to correctly deploy and undeploy your service. More about this later. For now, we will only assure once more that our WSDL follows the Document/literal style. Open the le deploy.wsdd and look at the attributes of the <service> element in it. It should have the attributes style="wrapped" use="literal".If you get something else, go back to your WSDL and try to nd out what does not comply with the Document/literal Wrapped style (see 4.1.1). Then generate the server stub again, until the right attributes appear in deploy.wsdd.
19
Figure 4.4: Running the generateServerStub task using Ant and click on a class or method to jump to it.) Now its time to connect the server code with the back-end code. This is simply done by modifying the getORFs method (or whatever name you chose for the operation in the WSDL) in LongORFsBindingImpl. The only back-end class you need to be concerned with is the LongORFsParser. The long-orfs program is called by creating a new instance of LongORFsParser and then call its run() method. This will return a Longorfs_output instance from which you can get an array of ORF objects. These objects are what you should return from your service, but you can not simply return these objects. The reason for this is that the parser has its own class denition of ORFs, and the auto-generated code has a dierent class denition. You therefore need to translate the list of ORFs from one type to the other. Hint: You can iterate over the list and run orf1.setxxx(orf2.getxxx()) on each variable. Hints:
20
To better understand how to use the LongORFsParser, open the Longorfs_Client. This also contains a suitable sequence string that you can use for testing.
21
First of all, use your favourite text editor to create a new python script. First of all, we need to import a couple of classes from the SOAPpy library. This is done by adding the following lines to your script:
from SOAPpy import SOAPProxy from SOAPpy import Types
An instance of SOAPProxy acts as a proxy to the Web Service endpoint of its URL variable. When constructed, it will automatically have the same methods as the Web Service it acts as a proxy for. To create a SOAPProxy is very easy:
url = http://www.webservices.com/the-service/endpoint namespace = my_namespace longORFService = SOAPProxy(url, namespace)
The response from the Web Service will automatically be serialised into a Python object when such a request is carried out (this object is returned by the method invoked). Print out some information about the response object on the screen. To print something to standard out in Python, simply use:
print the text you want to print.
To make a reference to a variable, the same convention as in Java applies, i.e. the variable variable in the data type dataType can be reached by dataType.variable. The variables will have the same name and type as those in the complex type element returned. As can be seen from the data type declaration of the WSDL of your longORFs service.
22
Index
Attribute (XML), 4 Backend code, 10, 14 Binding (WSDL), 8 Element (XML), 4 Endpoint, 7 Envelope (SOAP), 6 HTTP, 6 Message (WSDL), 8, 12 Message format (WSDL), 7 Namespaces (XML), 4 Operation (WSDL), 7, 12 Port Type (WSDL), 8 Protocol, 7 Python, 21 RPC, 7, 11 Serialization, 7 Service (WSDL), 8 SOAP, 6 SOAP Message, 6, 12 SOAPpy, 21 Style (binding -), 8, 11 Types (WSDL), 8 WS-I, 9, 13 WSDL, 7, 9 XML Schema, 4, 12
23