View
234
Download
3
Embed Size (px)
Citation preview
11
Web Services
Dr. Miguel A. Labrador
Department of Computer Science & Engineering
http://www.csee.usf.edu/~labrador
2Copyright© Dr. Miguel A. Labrador
2Copyright© Dr. Miguel A. Labrador
2
Outline
• Introduction• Web services components• The Web Services API• Creating a simple Web service
– Server side– MIDlet
3Copyright© Dr. Miguel A. Labrador
3Copyright© Dr. Miguel A. Labrador
3
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
4Copyright© Dr. Miguel A. Labrador
4Copyright© Dr. Miguel A. Labrador
4
The Java ME Platform
Service Provider
CreatesWeb
Service Application
Stub
ClientWSDL Document
UDDI
RequestService
CreatesCreates
Registers Finds and Obtains Service WSDL
5Copyright© Dr. Miguel A. Labrador
5Copyright© Dr. Miguel A. Labrador
5
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
6Copyright© Dr. Miguel A. Labrador
6Copyright© Dr. Miguel A. Labrador
6
The Web Services API
Application
Operating System
CLDC 1.1
MIDP 2.0
JAX-RPC Runtime
Service Provider Interface (SPI)
JAX-RPC Stub
JAXP API
JAX-RPC API
Server
Service Request
Service Response
SOAP XML/HTTP
Client
7Copyright© Dr. Miguel A. Labrador
7Copyright© Dr. Miguel A. Labrador
7
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
8Copyright© Dr. Miguel A. Labrador
8Copyright© Dr. Miguel A. Labrador
8
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
9Copyright© Dr. Miguel A. Labrador
9Copyright© Dr. Miguel A. Labrador
9
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
10Copyright© Dr. Miguel A. Labrador
10Copyright© Dr. Miguel A. Labrador
10
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
11Copyright© Dr. Miguel A. Labrador
11Copyright© Dr. Miguel A. Labrador
11
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
12Copyright© Dr. Miguel A. Labrador
12Copyright© Dr. Miguel A. Labrador
12
Web Service Part
13Copyright© Dr. Miguel A. Labrador
13Copyright© Dr. Miguel A. Labrador
13
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
14Copyright© Dr. Miguel A. Labrador
14Copyright© Dr. Miguel A. Labrador
14
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
15Copyright© Dr. Miguel A. Labrador
15Copyright© Dr. Miguel A. Labrador
15
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>. . .
16Copyright© Dr. Miguel A. Labrador
16Copyright© Dr. Miguel A. Labrador
16
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
17Copyright© Dr. Miguel A. Labrador
17Copyright© Dr. Miguel A. Labrador
17
Creating a MIDlet
18Copyright© Dr. Miguel A. Labrador
18Copyright© Dr. Miguel A. Labrador
18
Creating the MIDlet
19Copyright© Dr. Miguel A. Labrador
19Copyright© Dr. Miguel A. Labrador
19
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
Runnable theInvoker = new Runnable(){ public void run() { int op1 = Integer.parseInt(textField.getString()); int op2 = Integer.parseInt(textField1.getString()); ServerCalculatorService_Stub remoteCalculator =
new ServerCalculatorService_Stub(); int op3; 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();
20Copyright© Dr. Miguel A. Labrador
20Copyright© Dr. Miguel A. Labrador
20
Creating the MIDlet
• The process consists of the following chronological steps – Run the MIDlet