1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

40
1 Web Services – Part II CS 236369, Spring 2010

Transcript of 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

Page 1: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

1

Web Services – Part II

CS 236369, Spring 2010

Page 2: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

2

Axis : Apache EXtensible Interaction System

Page 3: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

3

What is AXIS(2)? The Apache Axis2 project is a Java-based implementation

of both the client and server sides of the Web services equation.

Axis is essentially a SOAP engine – a framework for constructing SOAP processors client side server side

It enables you to : Send, receive and process SOAP messages Create a Web service out of a plain Java class Create implementation classes for both the server and client using

WSDL Easily retrieve the WSDL for a service Create or utilize a REST-based Web service And more…

Page 4: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

4

Remote Method Invocation is not New

java.rmi has been in Java since Java’s early versions In Java RMI, objects can invoke methods of objects that

reside on a remote computer (RMI = Remote Method Invocation)

So, what has been changed? Using HTTP for communication Using agreed protocols, Java can invoke methods that were not

written in Java (e.g., .NET methods) and vice versa A complex registry procedure has been required in RMI

Page 5: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

5

What We Would Like to Create Client applications: applications that can call a

remote Web service Services: methods that can be called by remote

applications Service descriptions: WSDL files that describe

our methods

Page 6: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

6

Live Demo

We will build a web service (both server and client side) using Eclipse WTP (already integrated in your J2EE Eclipse version)

We will use AXIS2 version 1.3 The demo is based on “

Eclipse WTP Tutorials - Creating Bottom Up Web Service via Apache Axis2” with some extensions

Guidlelines appear in the following slides

Page 7: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

Installin AXIS2 Version 1.3 in Eclipse1. Download AXIS2 version 1.3 binary distribution (use this link)

2. Set it in Eclipse: Window Preferences Web Services Axis2 Preferences

Page 8: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

8

Client Applications

Page 9: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

9

Calling Web Services

By now, we already know how to invoke a remote Web service in Java: Open a socket to the remote server Through the socket, send a SOAP request wrapped by

a HTTP request Parse the response (e.g., using SAX/DOM)

However, this approach is cumbersome, and most of it can be automated This is the whole point of using standards…

Page 10: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

10

Web Services Explorer We have a power calculation Web service running on the course server at:

http://ibm411.cs.technion.ac.il/PowerService/services/PowerCalculation?wsdl We will use Eclipse web services explorer in order to explore it before we write a

client for that WS. (Under Run Launching the Web Services Explorer)

Page 11: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

Parameters values for the power function (a^n)

The result

Page 12: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

It’s all SOAP underneath

Page 13: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

13

The WSDL2Java Application

Axis provides a mechanism for communicating with a Web service using stubs

That is, generation of regular Java classes that have an interface similar to that of the Web service and implementation that wraps Web service management

Page 14: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

Creating a new Web service client project

Page 15: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

Settings

The WSDL location on IBM411

Deploy level

Axis 2

Project name (Dynamic Web Application)

Page 16: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

16

package cs236607ws;

import cs236607ws.PowerCalculationStub.Power;

import cs236607ws.PowerCalculationStub.PowerResponse;

public class PowerClient {

public static void main(String[] args) {

try {

int a = 3; int n = 4;

PowerCalculationStub stub = new PowerCalculationStub();

//Define the end point URL:

//PowerCalculationStub stub = new PowerCalculationStub //("http://localhost:8080/

// PowerService_20100613I/services/PowerCalculation?wsdl");

Power p = new Power();

p.setA(a);

p.setN(n);

PowerResponse res = stub.power(p);

System.out.println("The result of " + a + "^" + n + " is: "

+ res.get_return());

} catch (Exception e) {

System.out.println(e.getStackTrace());

}

}

{

The client class Added by the programmer using the classes (stubs) generated by the

WSDL2Java:

You can specifically set the Web Service end point URL

Page 17: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

17

Project HierarchyCreated by WSDL2Java

Created by programmer (previous slide). Not necessarily in same package

AXIS2 configuration

Deployment Descriptor

Page 18: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

18

Server Applications

Page 19: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

19

We will begin with a service example. Given the base and the exponent (integers), our simple service will calculate the result. This is the class:

Creating a Web Service

package cs236607WS;

public class PowerCalculation {

public int power(int a, int n) {return (int) Math.pow(a, n);

}}

Page 20: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

Creating a new Dynamic Web project

Page 21: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

Projext Settings

Runtime (Tomcat server)

Project facets configuration – next slide

Page 22: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

Project Facets ConfigurationsMark Axis2 facet

Page 23: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

Create Package + Class (and build the project)

Page 24: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

Make it a Web service – Create a new Web service project

Page 25: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

Settings

“Start” service level

Bottom up, meaning you start from Java up to WSDL

The class we created

Runtime - Tomcat

Set to Axis2 !!!

The project we created

Press it after the settings

Page 26: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

Run

Page 27: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

What is wrong?

Page 28: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

Correct URL

Let’s look on the Services

Page 29: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

EPR = End Point Reference

Click to view the created WSDL

Page 30: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

30

The Service WSDL

Axis automatically provides a WSDL for each deployed service

To get the WSDL, use the service URL with the empty argument wsdl

http://localhost:8080/PowerServiceExample/services/PowerCalculationWS?wsdl

Page 31: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

31

How Does it Work?

The Servlet AxisServlet of this application is responsible for invoking services

All URLs of the form /services/* are mapped to the AxisServlet Where is it written?

Page 32: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

32

How Does it Work? (Cont.) When a SOAP request arrives, the AxisServlet

object parses the request and invokes the corresponding method of the class associated with the service URL (According to the services that were deployed)

Page 33: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

33

Deploying The Service

One option, in which you include the entire axis2 application is:

Export the completed project to WAR Put this WAR under Tomcat webapps directory That’s it!

Usually this is not required, and all you need to deploy is the service only, while axis2 is already deployed.

For this you will need to put axis2.war file under $CATALINA_BASE/webapp/

Download AXIS2 version 1.3 WAR distribution (use this link)

Page 34: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

34

Service Archive File Now you need to bundle the service files together into a service

archive file. The ‘manual’ way:

1. Create an empty folder. 2. Create a directory called "META-INF" inside that. 3. Put services.xml into the META-INF directory. (this file will be

discussed later)4. If you have the WSDL file corresponding to the Web service, rename

it to service.wsdl and put that into the same directory. (It is not necessary to have a wsdl file in Axis2.)

5. If you want any third-party lib files or you own lib files, crate a directory called "lib" in the same level as META-INF, and drop all your lib files into that.

6. Copy the compiled service implementation classes into the directory. 7. Create a zip file from all those files and rename it to *.jar or *.aar.

Page 35: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

35

Service Archive File (Cont.)

The ‘automatic’ way - use service archive generator Eclipse Plug-in

Once you have the service archive file (aar) drop it into the services sub directory in the Axis2 repository ($CATALINA_BASE/webapp/axis2/WEB-INF/services )

Page 36: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

36

Service Configuration - services.xml The description of services is specified using

services.xml. Each service archive file should have a

services.xml in order to be a valid service and it should be available in the META-INF directory of the archive file. In Eclipse it was created for you.

Page 37: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

37

Services.xml example:

Page 38: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

38

Service Configuration - services.xml (Cont.) Among the attributes & elements of services.xml

are: Name (service element attribute): The service name

will be the name of the archive file if the .aar file contains only one service, or else the name of the service will be the name given by the name attribute.

Scope (service element attribute): The time period during which runtime information of the deployed services will be available. The default value (if you don't enter any value) will be "Request“.

Page 39: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

39

Service Configuration - services.xml (Cont.) Axis2 configuration page A short tutorial about writing your own

services.xml

Page 40: 1 Web Services – Part II CS 236369, Spring 2010. 2 Axis : Apache EXtensible Interaction System.

40

Resources

AXIS2 1.3 Download Eclipse WTP Tutorials - Creating Bottom Up

Web Service via Apache Axis2 HU Developer.com Axis2 Documentation