200 likes | 352 Views
Web Services. Dr. Miguel A. Labrador Department of Computer Science & Engineering labrador@csee.usf.edu http://www.csee.usf.edu/~labrador. Outline. Introduction Web services components The Web Services API Creating a simple Web service Server side MIDlet. Web Services.
E N D
Web Services Dr. Miguel A. Labrador Department of Computer Science & Engineering labrador@csee.usf.edu http://www.csee.usf.edu/~labrador
Outline • Introduction • Web services components • The Web Services API • Creating a simple Web service • Server side • MIDlet
Web Services • Created to allow any networked application client to find applications located anywhere in the Internet and use them as if they were locally installed applications • Regardless of platform • To realize this vision, four critical aspects are needed: • Develop and deploy applications that can be accessed and used by everyone • Describe the services • Web Services Definition Language (WSDL) • Find the services • Universal Description, Discovery, and Integration (UDDI) registry • Not really needed if you know the URL where the service is offered • Connect and use the services • Standard protocols and languages • HTTP, SOAP, RPC, XML
The Java ME Platform Request Service Creates Service Provider Stub Web Service Application Creates Creates WSDL Document Client UDDI Finds and Obtains Service WSDL Registers
The Web Services API • Based on JSR 172, J2ME Web Services APIs (WSA) • The purpose of this specification is to define an optional package that provides standard access from Java ME devices to Web services • Two optional packages • Java API for XML-based RPC (JAX-RPC) • Allows mobile devices to access remote XML-based Web services • Java API for interacting with SOAP-based Web services • Implementation of RPC in Java • Java API for XML Processing (JAXP) • Adds XML parsing capabilities to the Java ME platform • Can be used by all applications
The Web Services API Client • JAXP API • Application • JAX-RPC API • JAX-RPC Stub • Service Provider Interface (SPI) • JAX-RPC Runtime • Server Service Request • MIDP 2.0 • CLDC 1.1 SOAP XML/HTTP • Operating System Service Response
The JAX-RPC Package • Contains the javax.microedition.xml.rpc, javax.xml.namespace, javax.xml.rpc and javax.rmi packages • All classes and interfaces that make up the stub, SPI, and runtime • Stub makes requests to the runtime and receives service responses through the service provider interface • An instance of the stub class is generated by the stub generator. The instance is used to: • Set the properties of the stub to invoke an RPC • Make all appropriate format conversions • Create objects describing the input and return parameters • Encode the input values • Invoke the RPC • Decode the return value • The runtime contains all the functionality needed to transfer data to and from the server on behalf of the application
The JAXP Package • Subset of the J2SE JAXP API • Includes the javax.xml.parsers, org.xmal.sax, org.xmal.sax.helpers packages • Parse XML data
Creating a Web Service • Creating a Web service is very much automated in NetBeans • Let us go through the process of creating a Web service that functions like a calculator • MIDlet asks user for operands and type of operation (sum, …) • MIDlet sends request to Web server • Web server calculates the result and sends it back to MIDlet • Need to develop Web server part and MIDlet
Web Service Part • The process consists of the following chronological steps • Define operations to perform (sum, subtraction, multiplication, division) • Define input and output parameters • Create a New Application Project • From the Categories list, select Web • From the Projects list, select Web Application • Write in the name of your project • Click Next and Finish • Create a new package for the project • Select project in Projects’ tab and right click on it • Select New->Java Package • Write in name and click on Finish
Web Service Part • The process consists of the following chronological steps • Create Web Service • Select project in Projects’ tab and right click on it • Select New->Web Service • Write in Name • Select package just created and Finish • The configuration screen appears to define operations and parameters • Design the service
Web Service Part • The process consists of the following chronological steps • Click on “Source” button to see the Web Service code that NetBeans automatically creates for you • Only need to include the code that performs each function in the respective part
Creating a Web Service • The process consists of the following chronological steps • Save changes • Click on service and right click for Clean and Build • Click on service and right click to Undeploy and Deploy your project • NetBeans also automatically creates the WSDL file • http://IPadd:port (Glassfish 12796)/Project Name (Context Path from Properties/Run menu)/Web service name(CalculatorService)?wsdl • http://131.247.3.235:12796/WebAppExample/NewWebServiceService?wsdl • http://localhost:12796/LbsBook/ServerCalculatorService?wsdl
Example WSDL File • <?xml version="1.0" encoding="UTF-8" ?> • <!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is • JAX-WS RI 2.1.3.1-hudson-417-SNAPSHOT. • --> • <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is • JAX-WS RI 2.1.3.1-hudson-417-SNAPSHOT. • --> • <definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss- • wssecurity-utility-1.0.xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" • xmlns:tns="http://services.client.book.edu.usf.cse/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" • xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://services.client.book.edu.usf.cse/" • name="ServerCalculatorService"> • - <types> • - <xsd:schema> • <xsd:import namespace="http://services.client.book.edu.usf.cse/" • schemaLocation="http://localhost:12796/LbsBook/ServerCalculatorService?xsd=1" /> • </xsd:schema> • </types> • - <message name="calculateSum"> • <part name="parameters" element="tns:calculateSum" /> • </message> • - <message name="calculateSumResponse"> • <part name="parameters" element="tns:calculateSumResponse" /> • </message> • - <message name="calculateSub"> • <part name="parameters" element="tns:calculateSub" /> • </message> • . . .
Creating the MIDlet • The process consists of the following chronological steps • Next step is to create the MIDlet • Create New Project Mobility, MIDP Application • Write in name and click Finish • Create package as before • Select Project, right click, New Visual MIDlet • Write in the MIDlet name, select package • Select New Java ME Web Service Client • Provide URL of WSDL document • Click on Retrieve WSDL • NetBean creates application skeleton • Maps WSDL definitions to Java representation and XML data types to Java types • Design Visual MIDlet
Creating the MIDlet • The process consists of the following chronological steps • Add the code public void commandAction(Command command, Displayable displayable) { // write pre-action user code here if (displayable == form) { if (command == okCommand) { // write pre-action user code here // write post-action user code here RunnabletheInvoker = new Runnable(){ public void run() { intop1 = Integer.parseInt(textField.getString()); intop2 = Integer.parseInt(textField1.getString()); ServerCalculatorService_StubremoteCalculator = new ServerCalculatorService_Stub(); intop3; try { op3 = remoteCalculator.calculateSum(op1, op2); textField2.setString(""+op3); } catch (RemoteException ex) { ex.printStackTrace(); textField2.setString(ex.getMessage()); } } }; Thread t = new Thread(theInvoker); t.start();
Creating the MIDlet • The process consists of the following chronological steps • Run the MIDlet