20
Distribution and Integration Technologies XML Web Services APM@FEUP Web Services

Distribution and Integration Technologiesapm/TDIN/docs/05xmlws.pdfAPM@FEUP Web Services 2 XML Web Services A web service is a set of methods, usually in a class, accessible and invoked

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Distribution and Integration Technologiesapm/TDIN/docs/05xmlws.pdfAPM@FEUP Web Services 2 XML Web Services A web service is a set of methods, usually in a class, accessible and invoked

Distribution and Integration

Technologies

XML Web Services

APM@FEUP Web Services

Page 2: Distribution and Integration Technologiesapm/TDIN/docs/05xmlws.pdfAPM@FEUP Web Services 2 XML Web Services A web service is a set of methods, usually in a class, accessible and invoked

APM@FEUP Web Services 22

XML Web Services

A web service is a set of methods, usually in a

class, accessible and invoked using web

technologies Can be used in an intranet or in the internet

Usually Web methods are invoked using an OO language like C# or Java

Invocations and results are encoded in SOAP (Simple Object

Access Protocol), which is a rich XML schema, over HTTP

Almost all development platforms support Web

Services nowadays (both as servers as well as

clients)

Page 3: Distribution and Integration Technologiesapm/TDIN/docs/05xmlws.pdfAPM@FEUP Web Services 2 XML Web Services A web service is a set of methods, usually in a class, accessible and invoked

APM@FEUP Web Services 33

Involved Technologies

Simple Object Access Protocol (SOAP)

A protocol for coding the invocation and response

messages using an XML schema

Web Services Description Language (WSDL)

A language for Web Services description and

specification, also using XML

• Methods, Types, Parameters, Responses, URLs

Universal Description, Discovery and Integration

(UDDI) server

Also called the Web Services yellow pages,

implemented as a Web Service (optional and seldom

used)

Page 4: Distribution and Integration Technologiesapm/TDIN/docs/05xmlws.pdfAPM@FEUP Web Services 2 XML Web Services A web service is a set of methods, usually in a class, accessible and invoked

APM@FEUP Web Services 44

General Architecture

Service

ConsumerService

Supplier

Service

Description

Services

Directory

registers

(SOAP protocol)

UDDI

Client Server

Document

WSDL

generates

supplies

Uses for

proxy

building

consults

(SOAP protocol)

Invocation and response

(SOAP over HTTP)

Page 5: Distribution and Integration Technologiesapm/TDIN/docs/05xmlws.pdfAPM@FEUP Web Services 2 XML Web Services A web service is a set of methods, usually in a class, accessible and invoked

APM@FEUP Web Services 55

Web Services – Discovery and Use

UDDI

Registry

Internet UDDI

RegistryApplication

WSDL Interface

Application

Application

SOAP

Page 6: Distribution and Integration Technologiesapm/TDIN/docs/05xmlws.pdfAPM@FEUP Web Services 2 XML Web Services A web service is a set of methods, usually in a class, accessible and invoked

APM@FEUP Web Services 66

The Web of Services (SOA)

Reservations

Application

Internet

Intranet

Seat Availability

Application

Pricing

Application

Airline

Reservation

System

Page 7: Distribution and Integration Technologiesapm/TDIN/docs/05xmlws.pdfAPM@FEUP Web Services 2 XML Web Services A web service is a set of methods, usually in a class, accessible and invoked

APM@FEUP Web Services 77

Invocation and response

Client

<Add>

<n1>2</n1>

<n2>4</n2>

</Add>

SOAP invocation

<AddResult>

<sum>6</sum>

</AddResult>

SOAP response

Web

Service

HTTP

HTTP

int Add(int a, int b)

Page 8: Distribution and Integration Technologiesapm/TDIN/docs/05xmlws.pdfAPM@FEUP Web Services 2 XML Web Services A web service is a set of methods, usually in a class, accessible and invoked

APM@FEUP Web Services 88

Description: WSDL

Document using an XML format Schema approved by the World Wide Web

Consortium (W3C)

Followed by all the players in the Web

Services communities The Web Services containers generate this document

The proxy build tools read it

Contains

Type descriptions for parameters and results

Invocation ports and addresses

Available methods

Format for invocation and response

Page 9: Distribution and Integration Technologiesapm/TDIN/docs/05xmlws.pdfAPM@FEUP Web Services 2 XML Web Services A web service is a set of methods, usually in a class, accessible and invoked

APM@FEUP Web Services 99

Encoding: SOAP

The SOAP protocol is used in invocations

Uses an XML format for parameters and result values

The SOAP request/reply messages can be transported

using:

HTTP

• The most common transport protocol

SMTP or MSMQ

• Can be used in less interactive applications

Other protocols

Globally supported and implemented

Microsoft, IBM, Oracle, BEA, Iona, and many others …

Independent

From the operating system, languages, object models, etc

Supports synchronous and asynchronous calls

Page 10: Distribution and Integration Technologiesapm/TDIN/docs/05xmlws.pdfAPM@FEUP Web Services 2 XML Web Services A web service is a set of methods, usually in a class, accessible and invoked

APM@FEUP Web Services 1010

General Format of a SOAP Message

Headers

Envelope

Body

Method

Parameters

Headers are used in specifications that increase the basic Web Services functionalities

Some of those specifications (approved by W3C or other organizations) are implemented

by several framework suppliers, that make efforts to make them interoperable.

Some of those specifications are:

WS-Attachments WS-ReliableMessaging WS-Security

WS-Addressing WS-Referral WS-Trust

WS-Routing WS-AtomicTransaction WS-SecureConversation

Page 11: Distribution and Integration Technologiesapm/TDIN/docs/05xmlws.pdfAPM@FEUP Web Services 2 XML Web Services A web service is a set of methods, usually in a class, accessible and invoked

APM@FEUP Web Services 1111

SOAP Messages over HTTP

POST /AccountAccess HTTP/1.1

Host: www.qwickbank.com

Content-Type: text/xml; charset=“utf-8”

Content-Length: 305

SOAPAction:

<SOAP-ENV:Envelope

xmlns:SOAP-ENV =

“http://schemas.xmlsoap.org/soap/envelope/”

SOAP-ENV:encodingStyle =

“http://schemas.xmlsoap.org/soap/encoding/”>

<SOAP-ENV:Body>

<m:GetBalance

xmlns:m=“http://www.qwickbank.com/bank”>

<Account>729-1269-4785</Account>

</m:GetBalance>

</SOAP-ENV:Body >

</SOAP-ENV:Envelope>

HTTP/1.1 200 OK

Content-Type: text/xml; charset=“utf-8”

Content-Length: 304

<SOAP-ENV:Envelope

xmlns:SOAP-ENV =

“http://schemas.xmlsoap.org/soap/envelope/”

SOAP-ENV:encodingStyle =

“http://schemas.xmlsoap.org/soap/encoding/”>

<SOAP-ENV:Body>

<m:GetBalanceResponse

xmlns:m=“http://www.qwickbank.com/bank”>

<Balance>3,822.55</Balance>

</m:GetBalanceResponse>

</SOAP-ENV:Body ></SOAP-ENV:Envelope>

Request: Reply:

Page 12: Distribution and Integration Technologiesapm/TDIN/docs/05xmlws.pdfAPM@FEUP Web Services 2 XML Web Services A web service is a set of methods, usually in a class, accessible and invoked

APM@FEUP Web Services 1212

ASMX Web Services in .NET

Common Language Runtime

SOAP

ADO.NET

IIS

app.asmx

class X {

[WebMethod]

public int

Method1()

{ … }

}

DBMS

Page 13: Distribution and Integration Technologiesapm/TDIN/docs/05xmlws.pdfAPM@FEUP Web Services 2 XML Web Services A web service is a set of methods, usually in a class, accessible and invoked

APM@FEUP Web Services 13

Client Proxy and Container Intervention

Client

acmemath.comCalc c = new Calc();

int sum = c.Add(2, 4);

Proxy IISSOAP ASPNET_WP

Calc

ASMX

Handler

aspnet_isapi.dll

<Add>

<n1>2</n1>

<n2>4</n2>

</Add>

Add(2, 4)

Container

Page 14: Distribution and Integration Technologiesapm/TDIN/docs/05xmlws.pdfAPM@FEUP Web Services 2 XML Web Services A web service is a set of methods, usually in a class, accessible and invoked

APM@FEUP Web Services 14

Web Services in IIS

IIS

directory - URL

bin

calc.asmx

calc.dll

<%@ WebService class="CalcWS.Calculator" %>

using System.Web.Services;

using System;

namespace CalcWS {

[WebService(Namespace="http://fe.up.pt/apm/webservices/",

Name="Calculator Web Service",

Description="Provides simple remote methods for several calculations")]

public class Calculator {

[WebMethod(Description="Returns the square root of a number")]

public double Sqroot(double value) {

double res = Math.Sqrt(value);

return res;

}

[WebMethod(Description="Determines the maximum and minimun of an array of numbers")]

public int Maxmin(double[ ] vals, ref double mx, ref double mn) {

int k, len;

mx = mn = 0;

len = vals.Length;

if (len > 0) {

mx = mn = vals[0];

for(k=1; k<len; k++)

if (vals[k] > mx)

mx = vals[k];

else if (vals[k] < mn)

mn = vals[k];

}

return len;

}

}

}

Page 15: Distribution and Integration Technologiesapm/TDIN/docs/05xmlws.pdfAPM@FEUP Web Services 2 XML Web Services A web service is a set of methods, usually in a class, accessible and invoked

APM@FEUP Web Services 15

Presentation of a WS from IIS

Page 16: Distribution and Integration Technologiesapm/TDIN/docs/05xmlws.pdfAPM@FEUP Web Services 2 XML Web Services A web service is a set of methods, usually in a class, accessible and invoked

APM@FEUP Web Services 16

Client and Proxy

Command line tool for the generation of a proxy class: wsdl

> wsdl http://localhost/WS/Calc.asmx

class CalculatorWebService

string Url

int Timeout (default 100000 ms) ( = Timeout.Infinite)

using System;

class Client {

public static void Main ( ) {

double mx = 0, mn = 0;

double[ ] ar = new double[100];

CalculatorWebService cws = new CalculatorWebService();

double res = cws.Sqroot(2.0);

Console.WriteLine("Square root of 2.0 = {0}", res);

Random r = new Random();

for (int k=0; k<100; k++)

ar[k] = 100*r.NextDouble();

int nr = cws.Maxmin(ar, ref mx, ref mn);

Console.WriteLine("From an array of {0} doubles: max = {1}, min = {2}", nr, mx, mn);

}

}

Proxy class

csc /t:exe CalcClient.cs calc.cs

Page 17: Distribution and Integration Technologiesapm/TDIN/docs/05xmlws.pdfAPM@FEUP Web Services 2 XML Web Services A web service is a set of methods, usually in a class, accessible and invoked

APM@FEUP Web Services 1717

XML Web Services in Java EE

Hosted in a web server capable of executing the web

technologies defined in Java EE (web container)

Created in a “web project” (.war), configured with a

deployment descriptor and manifest (XML)

Can be created and made available from

Java classes defined as resources of a web application, using

‘annotations’ (after version Java EE 5 and JAX-WS)

session EJBs

From the WSDL document proxies can be created in

‘standalone’ Java SE applications

Enterprise Clients

Web applications

EJBs

(these run inside an application server)

Page 18: Distribution and Integration Technologiesapm/TDIN/docs/05xmlws.pdfAPM@FEUP Web Services 2 XML Web Services A web service is a set of methods, usually in a class, accessible and invoked

APM@FEUP Web Services 18

Available in Apache Tomcat

Also available in any application server (Glassfish,

JBoss, Websphere, Weblogic, etc)

Java EE web container

Web

Client

Web Container

JSP

Servlets

JSF

Web

Services

Java

Web Server

or

Java

Application ServerOther

Application

Name inside the

the web

application URL

Page 19: Distribution and Integration Technologiesapm/TDIN/docs/05xmlws.pdfAPM@FEUP Web Services 2 XML Web Services A web service is a set of methods, usually in a class, accessible and invoked

APM@FEUP Web Services 19

Java Tools

Netbeans

Has wizards aiding the creation of web services, methods, input

parameters and return type

Facilities to deploy in a web container supported by the IDE

Contains Apache Tomcat e Glassfish Application Server test

versions

Has a wizard for the proxy generation in applications

Has a wizard for generating the method calls in Web Services

Page 20: Distribution and Integration Technologiesapm/TDIN/docs/05xmlws.pdfAPM@FEUP Web Services 2 XML Web Services A web service is a set of methods, usually in a class, accessible and invoked

APM@FEUP Web Services 20

Web Service and Invocation in Java

@WebService()

public class WSAdder {

/**

* Web service operation

*/

@WebMethod( operationName = “add” )

public double add( @WebParam( name = “a”) double a,

@WebParam( name = “b” ) double b ) {

double c = a + b;

return c;

}

}

Web Service definition

try {

ws.WSAdderService service = new ws.WSAdderService( );

ws.WSAdder port = service.getWSAdderPort( );

double a = 2.3d;

double b = 5.5d;

double result = port.add( a, b );

System.out.println( “Result = ” + result );

} catch ( Exception ex ) {

ex.printStackTrace( );

}

Client invocation