118
Web Services Using Apache Axis Atul Kahate [email protected]

AK 3 web services using apache axis

Embed Size (px)

DESCRIPTION

Web Services using Apache Axis by Atul Kahate Sir

Citation preview

Page 1: AK 3   web services using apache axis

Web Services Using Apache Axis

Atul Kahate

[email protected]

Page 2: AK 3   web services using apache axis

Background

Page 3: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 3

Web Evolution

XMLXML

ProgrammabilityProgrammabilityConnectivityConnectivity

HTMLHTML

PresentationPresentation

TCP/IPTCP/IP

Technology

Technology

Innovation

Innovation

FTP,FTP, E-mail, Gopher

E-mail, GopherWeb Pages

Web Pages

Browse Browse the Webthe Web

Program Program the Webthe Web

Web Services

Web Services

Page 4: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 4

Distributed Programming

Remote Procedure Call

(RPC)

Program A calls Program B remotely (different process, possibly different computer)

Remote Objects

Method M1 of Object A calls method M2 of Object B remotely

(different process, possibly different computer)

Page 5: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 5

Middleware Software that allows clients and

servers to communicate with each other remotely

Provides a programming model above the basic building blocks of processes and message passing (i.e. above RPC/remote objects)

See Figure

Page 6: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 6

Middleware Layers

Applications

Remote objects, RMI, and Events

Request-Response protocol

External data representationOperating system

Middleware layers

Page 7: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 7

Middleware Features

Provides Location transparency (e.g. events

are handled remotely) Independence from communication

protocol details (e.g. TCP or UDP) Operating systems (e.g. CR-LF) Computer hardware (e.g. Byte

ordering)

Page 8: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 8

Interfaces In order to control the possible

interactions between modules, an explicit interface is defined for each module

Interface of a module = Procedures + Variables that can be accessed from other modules Everything else is hidden inside Implementation can change, but not interface

Page 9: AK 3   web services using apache axis

Web Service - Details

Page 10: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 10

Web Services Versus HTTP

Client-server programming

Programs with specific function

in mind

HTTP based communicatio

n

Generic functionality accessed using a

browser

Web Services Specific functionality accessed using HTTP

Page 11: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 11

Web Server versus Web Service

Web Server

Provides basic HTTP service

Web Service

Provides a service based on operations

defined in its interface

Page 12: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 12

Web Services Terminology Web Services expose useful functionality to Web

users through a standard Web protocol. In most cases, the protocol used is SOAP.

Web services provide a way to describe their interfaces in enough detail to allow a user to build a client application to talk to them. This description is usually provided in an XML document called a Web Services Description Language (WSDL) document.

Web services are registered so that potential users can find them easily. This is done with Universal Discovery Description and Integration (UDDI).

Page 13: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 13

Why Web Services? Suppose we want to provide a service to the

general public or some business partners They will send us two strings, which we will

concatenate and send back Different users may be using different

languages (Java and C#) Their platforms may differ (Windows, Linux) There may be firewalls in-between We can use a Web service to deal with this

Page 14: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 14

How to do it? Suppose our URL is www.ttdev.com We can create a Web service named

SimpleService The URL would be

www.ttdev.com/SimpleService (called as the end point of the Web service)

A Web service can support one or more operations The operation can be concat, in this case

Page 15: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 15

Web Service ConceptA Web server running at http://www.ttdev.com

A Web service running at the path /SimpleService

An operation

An operation

Name: concat

Name: …

Full path of the Web service is http://www/ttdev.com/SimpleService

Page 16: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 16

Assigning Namespace We need to assign a namespace to

make our operation globally unique

Suppose we decide on http://www/ttdev.com/ss

Then, Local name = concat Qualified name (QName) =

http://www/ttdev.com/ss/concat

Page 17: AK 3   web services using apache axis

Web Service Styles

RPC StyleDocument Style

Page 18: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 18

Our Example Web Service

Our Web service is supposed to accept two strings (s1, s2) and return anotherOperation concat

Local name: concat

Namespace: http://www.ttdev.com/ss

Parameters:

s1: string

s2: string

Return: string

Page 19: AK 3   web services using apache axis

RPC Style Web Services

Page 20: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 20

Dealing with Data Types What do the strings mean now? Are

they Java strings? No – because, Web services are

language neutral Hence, we must be able to map these to

a neutral format That neutral format happens to be the

various data types defined by XML schema specifications

Page 21: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 21

Data Types Refined

Operation concat

Local name: concat

Namespace: http://www.ttdev.com/ss

Parameters:

s1: string in http://www.w3.org/2001/XMLSchema

s2: string in http://www.w3.org/2001/XMLSchema

Return: string in http://www.w3.org/2001/XMLSchema

Page 22: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 22

“Messages” and “Parts”

In Web services: Method call = Input message Parameter = Part Return value = Output message

Let us modify our operation based on these concepts

Page 23: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 23

Adding Messages and Parts

Operation concat

Local name: concat

Namespace: http://www.ttdev.com/ss

Input message:

Part 1:

Name: s1

Type: string in http://www.w3.org/2001/XMLSchema

Part 2:

Name: s2

Type: string in http://www.w3.org/2001/XMLSchema

Output message:

Part 1:

Name: return

Type: string in http://www.w3.org/2001/XMLSchema

Page 24: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 24

Calling Web service

When someone calls our Web service, they can send us an XML element as the input message, such as:

<test:concat xmlns:test=http://ttdev.com/ss>

<s1>abc</s1>

<s2>123</s2>

</test:concat>

Page 25: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 25

Responding back from a Web service

In response, we can send the following XML message back to the caller:

<test:output xmlns:test=http://ttdev.com/ss>

abc123

</test:output>

Page 26: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 26

What is “RPC” here? The QName and the names of the

parts are used to create the input message

<test:concat xmlns:test=http://ttdev.com/ss>

<s1>abc</s1>

<s2>123</s2>

</test:concat>

QName

Namespace prefix

Parts

Input message

Page 27: AK 3   web services using apache axis

Document Style Web Services

Page 28: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 28

What is the difference?

RPC and Document style Web services generally differ on two counts: In document style Web services, input

parameters are not sent as different parts; but instead they are combined into a single part

There is an XML schema to validate the part

Page 29: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 29

Document Style Web Service

Operation concatLocal name: concatNamespace: http://www.ttdev.com/ssInput message:Part 1: Name: concatRequest Element:Output message:Part 1: Name: concatResponse Type: string in http://www.w3.org/2001/XMLSchema

<xsd:schema targetNamespace = ”http://www.ttdev.com/ss” xmlns:xsd = ”http://www.w3.org/2001/XMLSchema”> <xsd:element name = “concatRequest”> <xsd:complexType> <xsd:sequence> <xsd:element name = “s1” type = “xsd:string” /> <xsd:element name = “s2” type = “xsd:string” /> <xsd:sequence> <xsd:complexType> </xsd:element></xsd:schema>

<test:concatRequest xmlns:test=”http://www.ttdev.com/ss”> <s1>abc</s1> <s2>123</s2></test:concatRequest>

Operation details

XML schema

Sample message

Page 30: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 30

Sample Return (Output) Message

<test:concatResponse xmlns:test = “http://www.ttdev.com/ss” xmlns:xsd = “http://www.w3.org/2001/XMLSchema” xmlns:xsi = “http://www/w3.org/2001/XMLSchema-Instance” xsi:type = “xsd:string”> abc123</test:concatRespnose>

Indicates that the return type is string, which is defined in the Schema-Instance namespace

Page 31: AK 3   web services using apache axis

Using Port types

Page 32: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 32

Port Types

Port Type allows us to combine several operations This port has nothing to do with the

network ports – these are completely different concepts!

We can consider the following: Port = Class Operation = Static method within the

class

Page 33: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 33

Using Port TypesWeb service

Port typeLocal name: StringUtilNamespace: http://ttdev.com/ss

OperationLocal name: concatNamespace: http://ttdev.com/ss…

OperationLocal name: separateNamespace: http://ttdev.com/ss…

Schema…

Port typeLocal name: DateUtilNamespace: http://ttdev.com/ss

OperationLocal name: getDifferenceNamespace: http://ttdev.com/ss…

OperationLocal name: convertFormatNamespace: http://ttdev.com/ss…

Page 34: AK 3   web services using apache axis

Binding

Page 35: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 35

What is Binding? A port type may allow us to access it using

various message formats/protocols; e.g. XML RPC/Document style as seen earlier (called

as Simple Object Access Protocol or SOAP) Text format

concat (s1 = “abc”, s2 = “123) HTTP request Email

Each of these is called as binding

Page 36: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 36

Web service

Schema…

Port type: stringUtilconcat…

BindingName: binding_1Port type: stringUtilFormat: SOAPTransport: HTTP

BindingName: binding_2Port type: stringUtilFormat: TEXTTransport: SMTP

ExamplePOST http://www.ttdev.com/ss/abc

<concatRequest> <s1>abc</s1> <s2>123</s2></concatRequest>

ExampleFROM: [email protected]: …

concat (s1 = “abc”, s2 = “123”)

Page 37: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 37

Which Binding should we use?

SOAP/HTTP is most common We can use it almost everywhere It means that

The underlying protocol for traffic is HTTP

The message format is a layer over HTTP, called as SOAP

Page 38: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 38

More on Ports Suppose that too many people are trying to access our

Web service Hence, we decide to make it available on more than one

computer Example

Binding 1 on computers c1, c2, and c3 Binding 2 on computer c3 Hence, we have four ports (three using binding 1 and

another one using binding 2) Meaning: Requests received by these three computers

will be forwarded to a computer hiding behind for processing? NO

Instead, it means that some software implements the port types installed on these computers

Page 39: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 39

Web service

Schema…

Port type: stringUtilconcat…

BindingName: binding_1Port type: stringUtilFormat: SOAPTransport: HTTP

BindingName: binding_2Port type: stringUtilFormat: TEXTTransport: SMTP

Deployed on

Port 1

Deployed on

Port 2

Deployed on

Port 3

Port 4

C1 C2 C3

Page 40: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 40

Adding Details to Ports

To tell others about port details, we need to add the port specifications to our Web services interface

Page 41: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 41

Web service

Schema…

Port type: stringUtilconcat…

BindingName: binding_1Port type: stringUtilFormat: SOAPTransport: HTTP

BindingName: binding_2Port type: stringUtilFormat: TEXTTransport: SMTP

Port

Name: Port 1

Binding:Endpoint: …

Port

Name: Port 2

Binding: Endpoint: …

Port

Name: Port 3

Binding: Endpoint: …

Deployed on

Name: Port 4

Binding:Endpoint: …

Page 42: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 42

Target Namespace

Specifies the URL where our Web service resides

Page 43: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 43

Web service

Target namespace: http://ttdev.com/ss Schema…

Port type: stringUtilconcat…

BindingName: binding_1Port type: stringUtilFormat: SOAPTransport: HTTP

BindingName: binding_2Port type: stringUtilFormat: TEXTTransport: SMTP

Port

Name: Port 1

Binding:Endpoint: …

Port

Name: Port 2

Binding: Endpoint: …

Port

Name: Port 3

Binding: Endpoint: …

Deployed on

Name: Port 4

Binding:Endpoint: …

Page 44: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 44

WSDL

We have just finished describing the interface of our Web service in a language called as Web Services Description Language (WSDL).

Page 45: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 45

SummaryService

Port

PortType

Operation

Message

Message

Operation

Message

Message

Binding

Port

PortType

Operation

Message

Message

Operation

Message

Message

Binding

Page 46: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 46

WSDL and JavaWSDL portType Java Interface

<portType name=”BookQuote”> public interface BookQuote {

<operation name=”GetBookPrice”> <input name=”isbn”

message=”GetBookPriceRequest”/> <output name=”price”

message=”GetBookPriceResponse”/>

</operation>

public float getBookPrice (String isbn);

<operation name=”GetBulkBookPrice”>

<input name=”request” message=”GetBulkBookPriceRequest”/>

<output name=”prices” message=”GetBulkBookPriceResponse”/>

</operation>

public float getBulkBookPrice (String isbn, int quantity);

<operation name=”GetBookISBN”> <input name=”title”

message=”GetBulkBookISBNRequest”/>

<output name=”isbn” message=”GetBulkBookISBNResponse”/>

</operation>

public String getBookISBN (String bookTitle);

</portType> }

Page 47: AK 3   web services using apache axis

Overview of SOAP

Page 48: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 48

SOAP Message Structure

Envelope

Header

Header element

Header element

Body

Body element

Body element

Page 49: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 49

SOAP Example – Request<env:Envelope xmlns:env="http://www.w3.org/2001/06/soap-

envelope"><env:Body> <m:ValidatePostcode

env:encodingStyle="http://www.w3.org/2001/06/soap-encoding" xmlns:m="http://www.somesite.com/Postcode"> <Postcode>WC1A8GH</Postcode> <Country>UK</Country> </m:ValidatePostcode></env:Body></env:Envelope>

Page 50: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 50

SOAP Example – Response<env:Envelope xmlns:env="http://www.w3.org/2001/06/soap-envelope" ><env:Body> <m:ValidatePostcodeResponse env:encodingStyle="http://www.w3.org/2001/06/soap-

encoding" xmlns:m="http://www.somesite.com/Postcode"><Valid>Yes</Valid> </m:ValidatePostcodeResponse></env:Body></env:Envelope>

Page 51: AK 3   web services using apache axis

Web Services using Apache Axis

Page 52: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 52

How Axis Works

Page 53: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 53

What is Apache Axis?

AXIS is a SOAP engine Allows creation and processing of

SOAP messages Third generation of Apache SOAP

(which began at IBM as SOAP4J)

Microsoft Word Document

Page 54: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 54

Axis History IBM contributed an early implementation of

the SOAP protocol to Apache in 1999, which became known as Apache SOAP 2.1

This was based on an earlier implementation called as SOAP4J, which was a monolithic application – difficult to modify/maintain

Gave way to Apache Extensible Interaction System (AXIS), instead of being called as SOAP 3.0

Page 55: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 55

Installing Axis Download axis-bin-1_4.zip from

http://ws.apache.org/axis Download JavaMail API from

http://java.sun.com/products/javamail/downloads/index.html

Download JavaBeans Activation Framework from http://java.sun.com/products/javabeans/glasgow/jaf.html

Page 56: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 56

Install Tomcat

Download Apache Tomcat from http://jakarta.apache.org

Page 57: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 57

Installing Apache Axis

From the unzipped folder above, copy the axis-1_4\webapps\axis sub-folder in c:\tomcat\webapps and look at the directory tree shown next

Page 58: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 58

Page 59: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 59

Set AXIS_HOME

C:\tomcat\webapps\axis-1_4

Page 60: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 60

Set CLASSPATH [CLASSPATH should have all the jar files (and mail.jar file) from C:\tomcat\webapps\axis\WEB-INF\lib]

C:\tomcat\webapps\axis\WEB-INF\lib\axis.jar;C:\tomcat\webapps\axis\WEB-INF\lib\axis-ant.jar;C:\tomcat\webapps\axis\WEB-INF\lib\commons-discovery-0.2.jar;C:\tomcat\webapps\axis\WEB-INF\lib\commons-logging-1.0.4.jar;C:\tomcat\webapps\axis\WEB-INF\lib\jaxrpc.jar;C:\tomcat\webapps\axis\WEB-INF\lib\log4j-1.2.8.jar;C:\tomcat\webapps\axis\WEB-INF\lib\saaj.jar;C:\tomcat\webapps\axis\WEB-INF\lib\wsdl4j-1.5.1.jar;C:\tomcat\webapps\axis\WEB-INF\lib\mail.jar;

Page 61: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 61

Test Axis Installation

http://localhost:8080/axis/

Should see the following screen

Page 62: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 62

Axis Screen

Page 63: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 63

Testing Axis Press the first tag named Validation

to test the local installation's configuration.

This page will list the various dependencies needed for a correct installation.

If there are problems, it will list them and provide solutions.

See next slide

Page 64: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 64

Page 65: AK 3   web services using apache axis

Developing the First Web Service (Hello Service)

Page 66: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 66

Step 1: Develop the Service C:\tomcat\webapps\axis\WEB-INF\

classes\HelloService.javapublic class HelloService {

public String getHelloMessage (){ return "Hello World"; }}

Page 67: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 67

Step 2: Develop the Client C:\tomcat\webapps\axis\WEB-INF\classes\HelloServiceClient.javaimport java.net.URL;

import org.apache.axis.client.Service;import org.apache.axis.client.Call;

public class HelloServiceClient {

public static void main(String[] args) {

try { URL url = new URL("http://localhost:8080/axis/services/HelloService");

Service service = new Service();

Call call = (Call)service.createCall(); call.setTargetEndpointAddress(url); Object result = call.invoke("getHelloMessage", new Object[]{}); System.out.println(result);

} catch(Exception exception){ exception.printStackTrace(); } }}

Page 68: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 68

Step 3: Write a Web Services Deployment Descriptor C:\tomcat\webapps\axis\WEB-INF\classes\deploy-hello-web-service.wsdd

<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

<service name="HelloService" provider="java:RPC"> <parameter name="className" value="HelloService"/> <parameter name="allowedMethods" value="*"/> <requestFlow> <handler type="soapmonitor"/> </requestFlow> <responseFlow> <handler type="soapmonitor"/> </responseFlow> </service>

</deployment>

Page 69: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 69

Step 4: Compile and Deploy Start Tomcat Compile Web Service and client code Run admin client to deploy the service

java org.apache.axis.client.AdminClient deploy-hello-web-service.wsdd

Run Web service client and check if it runs fine – also check SOAP Monitor

Page 70: AK 3   web services using apache axis

Second Web Service

Page 71: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 71

Web Service Code (Server-side) // C:\tomcat\webapps\axis\WEB-INF\classes\calculatorService.java public class CalculatorService { public Object getCalculate(Object opr1, Object opr2,Object opr ) { System.out.println ("Inside CalculatorService"); Object result=null;

if (opr.toString().equals("+")) result = new Integer (((Integer) opr1).intValue()+ ((Integer) opr2).intValue()); else if (opr.toString().equals("-")) result = new Integer (((Integer) opr1).intValue ()-((Integer) opr2).intValue()); else if(opr.toString().equals("*")) result = new Integer (((Integer) opr1).intValue()*((Integer) opr2).intValue()); else if(opr.toString().equals("/")) result = new Integer (((Integer) opr1).intValue()/((Integer) opr2).intValue()); System.out.println("Completed CalculatorService"); return result; } }

Page 72: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 72

Deploying Web Service – 1 Compile the Web service (i.e. the

Java class) Create a file named deploy.wsdd in

the same folder, containing the following entries:

<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <service name="CalculatorService" provider="java:RPC"> <parameter name="className" value="CalculatorService"/> <parameter name="allowedMethods" value="*"/></service></deployment>

Page 73: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 73

Deploying Web Service – 2

Then run the following at the command prompt in the folder C:\tomcat\webapps\axis\WEB-INF\classes java org.apache.axis.client.AdminClient

deploy.wsdd You should get the following screen

Page 74: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 74

Deploying Web Service – 3

Page 75: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 75

Click on “List” Below

Page 76: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 76

We should be able to see our Service Running

Page 77: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 77

Where are we?

We have downloaded and set up Apache Axis successfully

We have written a Web Service (server-side) and have deployed it successfully

Page 78: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 78

Write the Web Service Client

//C:\tomcat\webapps\axis\WEB-INF\classes\CalculatorClient.java /** * CalculatorClient.java */ import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.namespace.QName; import org.apache.axis.client.Call; import org.apache.axis.client.Service;

public class CalculatorClient extends HttpServlet {

public void init(ServletConfig config) throws ServletException { super.init(config); System.out.println("CalculatorClient Initialized.") ; }

protected void doGet(HttpServletRequest request ,HttpServletResponse response)throws ServletException, IOException { processRequest(request, response); }

protected void doPost(HttpServletRequest request ,HttpServletResponse response)throws ServletException, IOException { processRequest(request, response); }

protected void processRequest (HttpServletRequest request ,HttpServletResponse response)throws ServletException, IOException { System.out.println("Processing Request."); response.setContentType("text/html"); PrintWriter out = response.getWriter (); String firstOper = request.getParameter("oper1"); String secondOper = request.getParameter("oper2"); String operator = request.getParameter("Oper"); String result = null; out.println("<html>"); out.println("<head><title>Axis</title></head>"); out.println("<body bgcolor=#F0F0F0 >"); out.println("<center>"); out.println("<table BORDER='4' BORDERCOLOR='#007FFF' cellpadding=3 celspacing=3 align=center width=50% bgcolor=CCCCCC>"); out.println("<tr><td colspan=2 align=center><b><font size='3' face='Verdana'>Webservice-Axis Demo</font></td></tr>"); out.println("</table>"); out.println("</form>"); out.println("</body>"); out.println("</html>"); if(firstOper != null && secondOper != null && operator != null) {

try { result = callAxisWebService(firstOper,secondOper,operator);

} catch (Exception e) { e.printStackTrace(); System.out.println("Exception in processing request."); } out.println("<html>"); out.println("<head><title>Axis</title></head>"); out.println("<body bgcolor=#F0F0F0 >"); out.println("<center>"); out.println("<form>"); out.println("<table BORDER='4' BORDERCOLOR='#007FFF' cellpadding=3 celspacing=3 align=center width=50% bgcolor=CCCCCC>"); out.println("<tr><td colspan=2 align=center><b><font size='2' face='Verdana'>Response from Calculate Webservice</font></td></tr>"); out.println("<tr><td colspan=2 align=center>"); out.println("<tr><td><font size='2' face='Verdana'>Operand 1:</td><td>"+firstOper+"</font></td><tr>"); out.println("<tr><td><font size='2' face='Verdana'>Operand 2:</td><td>"+secondOper+"</font></td><tr>"); out.println("<tr><td><font size='2' face='Verdana'>Operator:</td><td>"+operator+"</font></td><tr>"); out.println(" <tr><td><font size='2' face='Verdana'>Result:</td><td>"+result+"</font></td><tr>"); out.println("</table>"); out.println("</form>"); out.println("</body>"); out.println("</html>"); } out.println("<html>"); out.println("<head><title>Axis</title></head>"); out.println("<body bgcolor=#F0F0F0 >"); out.println("<center>"); out.println("<form method=post action='CalculatorClient'>"); out.println("<br/>"); out.println("<table BORDER='4' BORDERCOLOR='#007FFF' cellpadding=3 celspacing=3 align=center width=50% bgcolor=CCCCCC>"); out.println("<tr><td colspan=2 align=center><b><font size='2' face='Verdana'>WebService-Axis Client</font></td></tr>"); out.println("<tr><td colspan=2 align=center>"); out.println("<tr><td><font size='2' face='Verdana'>Enter Operand 1:</td><td><input type=text name=oper1 size='20'></font></td><tr>"); out.println("<tr><td><font size='2' face='Verdana'>Enter Operand 2:</td><td><input type=text name=oper2 size='20'></font></td><tr>"); out.println("<tr><td><font size='2' face='Verdana'>Select Operation:</td><td><SELECT NAME='Oper'>"); out.println("<OPTION>--Select Operation--"); out.println("<OPTION>+"); out.println("<OPTION>-"); out.println("<OPTION>*"); out.println("<OPTION>/"); out.println("</SELECT></font></td><tr>"); out.println("<tr><td colspan=2><input type=submit value='Calculate'></td><tr>"); out.println("</table>"); out.println("</form>"); out.println("</body>"); out.println("</html>"); if ( out != null ) out.close(); System.out.println("Process Request Completed."); }

private String callAxisWebService(String firstOper, String secondOper, String operator) throws Exception { Object ret = null; String endpointURL = "http://localhost:8080/axis/services/CalculatorService"; try { Integer first = new Integer(firstOper); Integer second = new Integer(secondOper); String oper = new String(operator);

Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(new java.net.URL(endpointURL)); call.setOperationName(new QName("CalculatorService", "getCalculate")); ret = call.invoke( new Object[] { first, second, oper } ); System.out.println("Object = " + ret.getClass().getName()); System.out.println("Number Returned : " + ret.toString()); } catch(Exception e) { e.printStackTrace(); } return ret.toString(); } }

Page 79: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 79

Understanding the Client Code

// Endpoint is used for making the call String

endpointURL="http://localhost:8080/axis/services/CalculatorService"; // The Service object is the starting point for accessing the web service. Service service = new Service (); //The call object is used to actually invoke the web service. Call call = (Call)service.createCall(); // Sets the call objects endpoint address call.setTargetEndpointAddress(new java.net.URL(endpointURL));

// Sets the operation name associated with this Call object. call.setOperationName(new QName("CalculatorService", "getCalculate")); // Calls the object, passing in the String objects for the operands & Operators. //The return value is stored as an object named “ret”. ret = call.invoke (new Object [] {first, second, oper } );

Page 80: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 80

Testing the Web Service

Compile the Web Service client Use this URL:

http://localhost:8080/axis/servlet/CalculatorClient

Screen: Next slide

Page 81: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 81

Web Service Client Screen

Page 82: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 82

Un-deploying the Web Service Create undeploy.wsdd in the same

folder <undeployment

xmlns="http://xml.apache.org/axis/wsdd/"> <service name="CalculatorService"/> </undeployment>

Run the following command: java org.apache.axis.client.AdminClient

undeploy.wsdd

Page 83: AK 3   web services using apache axis

SOAP Monitor

Page 84: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 84

What is SOAP Monitor? Web service developers often have the

need to see the SOAP messages being used to invoke web services along with the results of those messages.

The goal of the SOAP Monitor utility is to provide a way for these developers to monitor the SOAP messages being used without requiring any special configuration or restarting of the server.

Page 85: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 85

Enabling SOAP Monitor – 1

By default, SOAP Monitor is not enabled

We need to explicitly enable it Step 1: Compile the SOAP monitor

applet javac SOAPMonitorApplet.java

Page 86: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 86

Enabling SOAP Monitor – 2

Step 2: Deploy the SOAPMonitorService web service with the admin client and the deploy-monitor.wsdd file

See next page

Page 87: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 87

Enabling SOAP Monitor – 3 Deploy-monitor.wsdd file

<?xml version="1.0"?> <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <handler name="soapmonitor" type="java:org.apache.axis.handlers.SOAPMonitorHandler"> <parameter name="wsdlURL" value="/axis/SOAPMonitorService-impl.wsdl"/> <parameter name="namespace" value="http://tempuri.org/wsdl/2001/12/SOAPMonitorService-impl.wsdl"/> <parameter name="serviceName" value="SOAPMonitorService"/> <parameter name="portName" value="Demo"/> </handler> <service name="SOAPMonitorService" provider="java:RPC"> <parameter name="allowedMethods" value="publishMessage"/> <parameter name="className" value="org.apache.axis.monitor.SOAPMonitorService"/> <parameter name="scope" value="Application"/> </service> </deployment>

Page 88: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 88

Enabling SOAP Monitor – 4

Deploy SOAP monitor java

org.apache.axis.client.AdminClient -lhttp://localhost:8080/axis/services/AdminService deploy-monitor.wsdd

Page 89: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 89

Enabling SOAP Monitor – 5 Step 3: For each service that we need to

monitor, add the following entries to its deploy.wsdd file: <requestFlow> <handler

type="soapmonitor"/> </requestFlow> <responseFlow> <handler type="soapmonitor"/> </responseFlow>

In this case, add these to the C:\tomcat\webapps\axis\WEB-INF\classes\deploy.wsdd file – Modified file on next slide

Page 90: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 90

Enabling SOAP Monitor – 6 <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <service name="CalculatorService" provider="java:RPC"> <parameter name="className" value="CalculatorService"/> <parameter name="allowedMethods" value="*"/> <requestFlow> <handler type="soapmonitor"/> </requestFlow> <responseFlow> <handler type="soapmonitor"/> </responseFlow>

</service> </deployment>

Page 91: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 91

Enabling SOAP Monitor – 7 Un-deploy and re-deploy the

Calculator Web Service java org.apache.axis.client.AdminClient

undeploy.wsdd java org.apache.axis.client.AdminClient

deploy.wsdd Now rerun the client:

http://localhost:8080/axis/servlet/CalculatorClient

Page 92: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 92

Page 93: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 93

SOAP Monitor

Page 94: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 94

SOAP Monitor FAQ http://wiki.perfsonar.net/jra1-wiki/index.php/Installing_SOAPMonitor_for_Axish

Installing SOAPMonitor for Axis From GEANT2-JRA1 Wiki Step 1: Make sure you have the necessary files for soap monitor Checklist: SOAPMonitorApplet.java (usually present in the tomcat/webapps/axis directory) deploy-monitor.wsdd -download undeploy-monitor.wsdd - download

Step 2: Make sure your classpath contains the following jars You can do this by either modifying CLASSPATH variable in .bash_profile (for unix) or by modifying it in Windows Environment Variables or by including the complete path to each of the following jar files in the javac and java commands (-cp option) xercesImpl.jar xml-apis.jar xalan.jar xmlrpc-1.2-patched.jar xmlParserAPIs.jar axis-1.4.jar commons-discovery-0.2.jar commons-logging-1.0.4.jar jaxrpc.jar saaj.jar wsdl4j-1.5.1.jar resolver.jar log4j-1.2.8.jar mail.jar Note: The above list probably contains more jars than actually required. Also, the version numbers are the ones that have been tested with. You might be able to use a different version than the one specified above. Make sure that the axis.jar version is the same as the axis servlet that you are trying to deploy your service

onto. Or else, you will get an IncompatibleClassChange error.

Step 3: Compile SOAPMonitorApplet.java You can compile the java file with the help of the following command. javac SOAPMonitorApplet.java Of if you want to use the -cp option (if the axis.jar file is not in your classpath) javac -cp %AXIS_HOME%\lib\axis.jar SOAPMonitorApplet.java

Step 4: Make sure resulting SOAPMonitoringApplet*.class (around 7 classes) are in your axis directory (ex: /home/loukik/tomcat/webapps/axis)

Step 5: Uncomment entries in WEB.xml You will need uncomment the following lines in WEB.xml. Also, the SOAP monitor requires to run on a port (in the example its 5001). If the default port is already in use by some other application or some other SOAP monitor, you will need to set a different port number. <servlet> <servlet-name>SOAPMonitorService</servlet-name> <display-name>SOAPMonitorService</display-name> <servlet-class> org.apache.axis.monitor.SOAPMonitorService </servlet-class> <init-param> <param-name>SOAPMonitorPort</param-name> <param-value>5001</param-value> </init-param> <load-on-

startup>100</load-on-startup> </servlet> Step 6: Deploy

The following command was used to deploy the service. You will need to substitute localhost:8080 with your machine's hostname/ip address and port. (This is the machine which is running tomcat and where the service is installed) java org.apache.axis.client.AdminClient -lhttp://localhost:8080/axis/services/AdminService deploy-monitor.wsdd or if you want to include the classpath here java -cp %CLASSPATH% org.apache.axis.client.AdminClient -lhttp://localhost:8080/axis/services/AdminService deploy-monitor.wsdd Step 7: Modify .wsdd file for your service and Re-deploy them By default, the .wsdd files that perfSONAR uses for deploying services currently does not support flows via a SOAP Monitor. It needs to be modified as below in order to monitor the flows using the SOAP monitor. The modification is quite simple. The following lines need to be added to the deploy.wsdd file (ma-service-deploy.wsdd or mp-service-deploy.wsdd or ls-service-deploy.wsdd, etc). You can find this file in $PERFSONAR/build/org/perfsonar/service/web/wsdd directory. <requestFlow> <handler name="soapmonitor" type="java:org.apache.axis.handlers.SOAPMonitorHandler"/> </requestFlow>   <responseFlow> <handler name="soapmonitor" type="java:org.apache.axis.handlers.SOAPMonitorHandler"/> </responseFlow> An example file showing these lines inserted in the wsdd can

be found here.

Step 8: Re-deploy your service, restart Tomcat After making changes to the wsdd file, you will need to undeploy the service and then re-deploy it. Check your service's installation guide for instructions on how to undeploy and deploy a service. After redeploying, tomcat will need to be restarted.

Step 9: Check your SOAP Monitor You can check if your SOAP Monitor is working by clicking on the SOAPMonitor link on the axis page (usually http://localhost:8080/axis/SOAPMonitor). If the SOAPMonitor page loads, it means that the web.xml entry has been properly uncommented. If the SOAPMonitor page shows that the SOAPMonitor has started (see page

here) it means all is ok. If not, check the port number (try making changes), restart tomcat and test again.

How to undeploy SOAP monitor Ok, so you had enough of the SOAP Monitor :).Here is how you can undeploy it. Undeploy SOAPMonitorService (make sure you have the undeploy-monitor.wsdd file - see step 1 if not) java org.apache.axis.client.AdminClient -lhttp://localhost:8080/axis/services/AdminService undeploy-monitor.wsdd or java -cp %CLASSPATH% org.apache.axis.client.AdminClient -lhttp://localhost:8080/axis/services/AdminService undeploy-monitor.wsdd Comment out the entries in web.xml <!-- <servlet> <servlet-name>SOAPMonitorService</servlet-name> <display-name>SOAPMonitorService</display-name> <servlet-class> org.apache.axis.monitor.SOAPMonitorService </servlet-class> <init-param> <param-name>SOAPMonitorPort</param-name> <param-value>5001</param-value> </init-param> <load-

on-startup>100</load-on-startup> </servlet> --> Modify your deploy.wsdd file If you do not modify your deploy.wsdd as specified below and re-deploy your service, your service will not work. Remove the following lines from it. <requestFlow> <handler name="soapmonitor" type="java:org.apache.axis.handlers.SOAPMonitorHandler"/> </requestFlow>   <responseFlow> <handler name="soapmonitor" type="java:org.apache.axis.handlers.SOAPMonitorHandler"/> </responseFlow> Undeploy and then deploy your service Look into your installation guide to find out how to undeploy and deploy your service Restart Tomcat Make sure you restart tomcat so that the changes can be applied. Other pages: Apache pages on SOAP Monitor installation

Page 95: AK 3   web services using apache axis

The Axis Client APIs

Page 96: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 96

Service Object

Package org.apache.axis.client.Service

Acts as a factory for the Call objects

Contains type mappings for XML<->Java bindings Service service = new Service();

Page 97: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 97

Call Object Call object is the main entry point into a

Web service Package org.apache.axis.client.Call This class should be used to actually

invoke a Web Service Call call = (Call) service.createCall();

Factory method creates a JAX-RPC Call object – returns a javax.xml.rpc.Call, which we need to type cast to the Axis Call type

Page 98: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 98

Endpoint URL Call setTargetEndpointAddress ()

method to pass the endpoint URL of the target Web Service to the newly created Call object String endpointURL =

"http://localhost:8080/axis/services/CalculatorService";

… call.setTargetEndpointAddress(new

java.net.URL(endpointURL));

Page 99: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 99

Operation

Set the method name (i.e. operation name) to be called in the Web Service class

Expects a Qname call.setOperationName (new Qname

("CalculatorService", "getCalculate"));

Page 100: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 100

Call the Web Service

Use the invoke method ret = call.invoke (new Object[] { first,

second, oper} ); Signature

java.lang.Object invoke (java.lang.Object [] params)

Expects an array of Objects, and returns an Object

Page 101: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 101

Display the Value Returned

System.out.println ("Number Returned: " + ret.toString ()); ret was an object

Page 102: AK 3   web services using apache axis

Java Web Services (JWS)

Page 103: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 103

What are JWS?

JAX-WS stands for Java API for XML Web Services. JAX-WS is a technology for building web services and clients that communicate using XML

Page 104: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 104

JWS Details JAX-WS allows developers to write

message-oriented as well as RPC-oriented web services

In JAX-WS, a web service operation invocation is represented by an XML-based protocol such as SOAP

The SOAP specification defines the envelope structure, encoding rules, and conventions for representing web service invocations and responses. These calls and responses are transmitted as SOAP messages (XML files) over HTTP

Page 105: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 105

Programmer’s View – 1 Although SOAP messages are complex, the

JAX-WS API hides this complexity from the application developer

On the server side, the developer specifies the web service operations by defining methods in an interface written in the Java programming language

The developer also codes one or more classes that implement those methods.

Client programs are also easy to code A client creates a proxy (a local object

representing the service) and then simply invokes methods on the proxy

Page 106: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 106

Programmer’s View – 2

With JAX-WS, the developer does not generate or parse SOAP messages

It is the JAX-WS runtime system that converts the API calls and responses to and from SOAP messages

Page 107: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 107

Client-Server Communication in JWS

Page 108: AK 3   web services using apache axis

Creating JWS on the Server-side

Page 109: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 109

Web Service Endpoint

The starting point for developing a JAX-WS web service is a Java class annotated with the javax.jws.WebService annotation

The @WebService annotation defines the class as a web service endpoint

Page 110: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 110

Service Endpoint Interface and Implementation (SEI) A service endpoint interface or service

endpoint implementation (SEI) is a Java interface or class, respectively, that declares the methods that a client can invoke on the service

An interface is not required when building a JAX-WS endpoint

The web service implementation class implicitly defines an SEI

Page 111: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 111

Web Service Examplepackage helloservice.endpoint;import javax.jws.WebService;

@WebService public class Hello { private String message = new String("Hello, "); public void Hello() {}

@WebMethod public String sayHello(String name) { return message + name + ".";}}

Page 112: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 112

Testing the Web Service Create a new Web Service client in

NetBeans and test it

-- OR -- Open browser, type URL as

http://localhost:4848 and provide ID as admin, password as adminadmin then choose Hello Web Service and click on Test

Page 113: AK 3   web services using apache axis

JWS Theory

Page 114: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 114

Java Web Services “New” Java APIs for Web Services

JAX-WS 2.0 – Java API for XML-based Web Services (Predecessor was JAX-RPC)

JAXB 2.0 – Java Architecture for XML Binding – Plumbing between Java objects and their XML representation

WS-Metadata – Web Services Metadata for the Java platform – Provides annotations

WSEE 1.2 – Web Services for Java EE – Defines the programming model and run-time behavior of Web Services in the Java EE container

Need Java SE 6 and Java EE 5

Page 115: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 115

Earlier Java Web Services Standards Carried Forward

SAAJ – SOAP with Attachments API for Java

JAXR – Java API for XML Registries

Page 116: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 116

Server-side of a Web Service1. Receive a SOAP message (e.g. HTTP or JMS end point)2. Invoke a handler that pre-processes the message (e.g. to

process headers or to persist the message)3. Determine the target service (i.e. WSDL operation) that

the message intends to invoke4. Based on the target WSDL operation, determine the Java

class/method (called as Java target) to invoke (This is called as dispatching)

5. Hand off the SOAP message to the Serialization subsystem to de-serialize it into a Java object that can be passed to the Java target as parameters

6. Invoke the Java target using the parameters generated by the Serialization subsystem and getting the Java object returned by the target method

7. Hand off the returned object to the Serialization subsystem to serialize it into an XML element conformant with the return message specified by the target WSDL operation

8. Wrap the returned XML element as a SOAP message response conforming to the target WSDL operation

9. Hand the SOAP message back to the transport for delivery

Page 117: AK 3   web services using apache axis

Web Services using Apache Axis | Atul Kahate 117

Client-side of a Web Service1. Create an instance of the Web Service end point

implementing a Java interface (called as Service Endpoint Interface or SEI)

2. Handle an invocation of the SEI instance3. Take the parameters passed to the SEI and pass them

to the Serialization subsystem to be serialized into XML elements that conform to the XML schema specified by the target service’s WSDL

4. Based on the target service’s WSDL, wrap the parameter elements in a SOAP message

5. Invoke handlers that post-process the message (e.g. add SOAP headers, persist the message)

6. Hand off the message to the transport for delivery to the target Web Service

7. Receive the SOAP message response from the transport8. Hand off the SOAP message to the Serialization

subsystem to de-serialize it into a Java object that is an instance of the class specified by the SEI’s return type

9. Complete the invocation of the SEI by returning the de-serialized SOAP response

Page 118: AK 3   web services using apache axis

Thank you!

Questions and Comments Welcome!