Web Services in Eclipse A.A. 2012/2013
Outline Apache Axis Web Service Clients Creating Web Services 2
Apache Axis Web Services in Eclipse
WS basics (I) Web services are described by their WSDL file Starting from the information in the WSDL, a WS client must: Prepare call data parameters Encapsulate them in a SOAP XML message Send the message to the specified endpoint, using the right encoding and protocol Wait... (remotely) Receive the SOAP XML response message Decode the message and extract the response data values Handle possible errors and exceptions, and report them to the caller Return the response data to the caller 4
WS basics (II) The provider of the WS (the server side) must: Publish the WSDL corresponding to its calling methods Wait for incoming requests as SOAP XML messages Decode the request parameters and validate their data types Identify the called method, and invoke the right class for handling it (business method) Wait... (locally) Get the response from the business method Catch any exception and possibly create an XML exception message Encapsulate the response values in a SOAP XML message Return the response message to the caller 5
Welcome, Axis Project Axis (axis.apache.org) aims at simplifying and automating all the above steps, as much as possible Automatically creates Java «proxy» classes for calling web services You call a method on the local object The method call is translated into a WS request/response Helps transforming business methods into Web Services Starting from a Java class, creates the WSDL and the Servlets to handle the WS request (bottom-up creation) Starting form a WSDL, create the servlet and Java interfaces to handle the requests (top-down) 6
Axis architecture (client pipeline) 7
Axis architecture (client pipeline) import localhost.axis.bing_jws.*; public class MyBingClient { public static void main( String args[] ) throws Exception { BingService service = new BingServiceLocator(); Bing mybing = service.getbing(); // call instance methods on mybing, ex., say, mybing.foo() mybing.dosomething("hello") ; } } http://wiki.apache.org/ws/frontpage/axis/writingyourclient 8
Axis architecture (server pipeline) 9
Axis architecture (server pipeline) public interface BingService { public String dosomething (String argument); public ComplexTO retrieveresult(otherto value); } public class BingServiceImpl implemente BingService { public String dosomething (String argument) {... } } public ComplexTO retrieveresult(otherto value) {... } 10
Web Service Clients Web Services in Eclipse
Guided example Remote Login Service downloadable from the course web site Offers 2 operations: isvaliduser Two input parameters: username and password (hashed with the SHA1 algorithm) One output parameter: true if the user is valid (i.e., username and password have a corresponding entry in the db, false otherwise) getuserdata Two input parameters: username and password (hashed with the SHA1 algorithm) One output parameter: a UserData object representing the user data stored in the db. 12
AuthenticationInterface WSDL (Eclipse) 13
Parameter data types (isvaliduser) 14
Parameter data types (getuserdata) 15
SOAP messages (isvaliduser): request POST /AuthenticationInterfaceImple HTTP/1.1 Host: Content-Type: application/soap+xml; charset=utf-8 Content-Length: length <soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://login.services.sistinfo.elite.polito.it" xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"> <soapenv:body> <q0:isvaliduser> <q0:username>user1</q0:username> <q0:passwordhash> lkjgirhierhghrihrhoeirh </q0:passwordhash> </q0:isvaliduser> </soapenv:body> </soapenv:envelope> 16
SOAP messages (isvaliduser): response HTTP/1.1 200 OK Content-Type: application/soap+xml; charset=utf-8 Content-Length: length <soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"> <soapenv:body> <isvaliduserresponse xmlns="http://login.services.sistinfo.polito.it"> <isvaliduserreturn>false</isvaliduserreturn> </isvaliduserresponse> </soapenv:body> </soapenv:envelope> 17
SOAP messages (getuserdata): request POST /AuthenticationInterfaceImple HTTP/1.1 Host: Content-Type: application/soap+xml; charset=utf-8 Content-Length: length <soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://login.services.sistinfo.polito.it" xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"> <soapenv:body> <q0:getuserdata> <q0:username>user1</q0:username> <q0:passwordhash> 70153d7bc22acc55ea60ae45bf3af72b254a142c </q0:passwordhash> </q0:getuserdata> </soapenv:body> </soapenv:envelope> 18
SOAP messages (getuserdata): response <soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"> <soapenv:body> <getuserdataresponse xmlns="http://login.services.sistinfo.polito.it"> <getuserdatareturn> <address> Viale dei giusti, 32 </address> <city>topolinia</city> <name>ermenegildo</name> <phonenumber> +99033786453 </phonenumber> <surname>basettoni</surname> <username>user1</username> <zipcode>14654</zipcode> </getuserdatareturn> </getuserdataresponse> </soapenv:body> </soapenv:envelope> 19
Creating the WS client 20
Creation results Axis libraries in project Client proxy and methods 21
Generated classes <<interface>> AuthenticationInterfaceImpl Service implements AuthenticationInterfaceImplServi celocator.getauthenticationinterfaceimpl() org.apache.axis.client. Service extends returns implements <<interface>> AuthenticationInterfaceImpl. isvaliduser( username, passwordhash) implements org.apache.axis.client. Stub extends User Code AuthenticationInterfaceImplProxy AuthenticationInterfaceImplSoap BindingStub Wrap calls 22
User code example WSDL Service I AuthenticationInterfaceImplServiceLocator authimpllocator = new C AuthenticationInterfaceImplServiceLocator(); I AuthenticationInterfaceImpl authimpl = authimpllocator.getauthenticationinterfaceimpl(); WSDL Port valid = authimpl.isvaliduser(username, pwdhash); WSDL Operation 23
References Axis Architecture Guide, http://axis.apache.org/axis/java/architecture-guide.html Currency Convertor Web Service, http://www.webservicex.net/ws/wsdetails.aspx?wsid=10 JAX-RPC specification v 1.1, http://jcp.org/en/jsr/summary?id=101 24
Licenza d uso Queste diapositive sono distribuite con licenza Creative Commons Attribuzione - Non commerciale - Condividi allo stesso modo 2.5 Italia (CC BY-NC-SA 2.5) Sei libero: di riprodurre, distribuire, comunicare al pubblico, esporre in pubblico, rappresentare, eseguire e recitare quest'opera di modificare quest'opera Alle seguenti condizioni: Attribuzione Devi attribuire la paternità dell'opera agli autori originali e in modo tale da non suggerire che essi avallino te o il modo in cui tu usi l'opera. Non commerciale Non puoi usare quest'opera per fini commerciali. Condividi allo stesso modo Se alteri o trasformi quest'opera, o se la usi per crearne un'altra, puoi distribuire l'opera risultante solo con una licenza identica o equivalente a questa. http://creativecommons.org/licenses/by-nc-sa/2.5/it/ 25