April 05 Prof. Ismael H. F. Santos - [email protected] 1 Modulo II WebServices Prof. Ismael...

26
April 05 Prof. Ismael H. F. Santos - [email protected] 1 Modulo II WebServices Prof. Ismael H F Santos

Transcript of April 05 Prof. Ismael H. F. Santos - [email protected] 1 Modulo II WebServices Prof. Ismael...

Page 1: April 05 Prof. Ismael H. F. Santos - ismael@tecgraf.puc-rio.br 1 Modulo II WebServices Prof. Ismael H F Santos.

April 05 Prof. Ismael H. F. Santos - [email protected] 1

Modulo II WebServices

Prof. Ismael H F Santos

Page 2: April 05 Prof. Ismael H. F. Santos - ismael@tecgraf.puc-rio.br 1 Modulo II WebServices Prof. Ismael H F Santos.

April 05 Prof. Ismael H. F. Santos - [email protected] 2

Bibliografia

Page 3: April 05 Prof. Ismael H. F. Santos - ismael@tecgraf.puc-rio.br 1 Modulo II WebServices Prof. Ismael H F Santos.

April 05 Prof. Ismael H. F. Santos - [email protected] 3

Ementa

WebServices em Java

Page 4: April 05 Prof. Ismael H. F. Santos - ismael@tecgraf.puc-rio.br 1 Modulo II WebServices Prof. Ismael H F Santos.

April 05 Prof. Ismael H. F. Santos - [email protected] 4

WebServices

Java

SOA

Page 5: April 05 Prof. Ismael H. F. Santos - ismael@tecgraf.puc-rio.br 1 Modulo II WebServices Prof. Ismael H F Santos.

April 05 Prof. Ismael H. F. Santos - [email protected] 5

WebServices com Java

A plataforma J2EE oferece as seguintes APIs: Document-oriented

Java API for XML Processing (JAXP) processes XML documents using various parsers

Java Architecture for XML Binding (JAXB) processes XML documents using schema-derived JavaBeans component classes

Procedure-oriented Java API for XML-based RPC (JAX-RPC) sends SOAP method

calls to remote parties over the Internet and receives the results Java API for XML Messaging (JAXM) sends SOAP messages

over the Internet in a standard way Java API for XML Registries (JAXR) provides a standard way

toaccess business registries and share information

Page 6: April 05 Prof. Ismael H. F. Santos - ismael@tecgraf.puc-rio.br 1 Modulo II WebServices Prof. Ismael H F Santos.

April 05 Prof. Ismael H. F. Santos - [email protected] 6

WebServices com Java

A tecnologia Java oferece tambem as seguintes ferramentas: Java Web Services Developer Pack (Java WSDP) SOAP with Attachments API for Java (SAAJ)

Com estas APIs você não precisa saber como criar o SOAP. Você só precisa saber utilizar as classes da API para criar e acessar os Web Services.

Page 7: April 05 Prof. Ismael H. F. Santos - ismael@tecgraf.puc-rio.br 1 Modulo II WebServices Prof. Ismael H F Santos.

April 05 Prof. Ismael H. F. Santos - [email protected] 7

URI HTML HTTP

UDDI WSDL SOAP

State of the Art

Page 8: April 05 Prof. Ismael H. F. Santos - ismael@tecgraf.puc-rio.br 1 Modulo II WebServices Prof. Ismael H F Santos.

April 05 Prof. Ismael H. F. Santos - [email protected] 8

Web Service : How They Work?

Components required Software which needs to be exposed as a Web service A SOAP Server (Apache Axis, SOAP::Lite, etc.) HTTP Server (if HTTP is used as the transport level protocol) SOAP Client (Apache Axis, SOAP::Lite etc.)

(http transport)Requestor

SOAP Messages

Web Service ProviderEndpoint

SOAP Client

Page 9: April 05 Prof. Ismael H. F. Santos - ismael@tecgraf.puc-rio.br 1 Modulo II WebServices Prof. Ismael H F Santos.

April 05 Prof. Ismael H. F. Santos - [email protected] 9

RemoteWeb ServiceRepository(Web Sites)

WriteClient Code

Service Requestor

Invoke Web Service

Manual Web Service

Lookup

SOAP Request

SOAP Response

WSDL - Web Service DescriptionSOAP - Web Service Message Protocol

WSDL File

Remote Web service

Publish Web Service

1

2

3

4

5

HTTP GET

Simple Web Service Invocation

Page 10: April 05 Prof. Ismael H. F. Santos - ismael@tecgraf.puc-rio.br 1 Modulo II WebServices Prof. Ismael H F Santos.

April 05 Prof. Ismael H. F. Santos - [email protected] 10

From S. Chandrasekaran’s Talk

Web Service Description

Why describe Web services? A service requestor needs to analyze a service for

his requirements A Web service needs to provide the following

information the operations it supports the transport and messaging protocols on which it supports those

operations the network endpoint of the Web service

Languages such as WSDL, DAML-S, RDF can be used for describing Web services WSDL – describes the syntactic information of a service DAML-S and RDF – describe the syntactic as well as the semantic

information

Page 11: April 05 Prof. Ismael H. F. Santos - ismael@tecgraf.puc-rio.br 1 Modulo II WebServices Prof. Ismael H F Santos.

April 05 Prof. Ismael H. F. Santos - [email protected] 11

Web Service Description (WSDL)

Abstract Description

ConcreteDescription

Page 12: April 05 Prof. Ismael H. F. Santos - ismael@tecgraf.puc-rio.br 1 Modulo II WebServices Prof. Ismael H F Santos.

April 05 Prof. Ismael H. F. Santos - [email protected] 12

WebService

Example

SOA

Page 13: April 05 Prof. Ismael H. F. Santos - ismael@tecgraf.puc-rio.br 1 Modulo II WebServices Prof. Ismael H F Santos.

April 05 Prof. Ismael H. F. Santos - [email protected] 13

A Web Service example in Java

SOAP-awareSOAP-awareServletServlet

(e.g. Apache Axis)(e.g. Apache Axis)

SOAP-awareSOAP-awareServletServlet

(e.g. Apache Axis)(e.g. Apache Axis)

Any classprocessing

the incomingrequests

(“business logic”

Any classprocessing

the incomingrequests

(“business logic”

Any classprocessing

the incomingrequests

(“business logic”

Any classprocessing

the incomingrequests

(“business logic”

Any classprocessing

the incomingrequests

(“business logic”

Any classprocessing

the incomingrequests

(“business logic”

Any classAny classprocessingprocessing

the incomingthe incomingrequestsrequests

(“business logic”(“business logic”

Any classAny classprocessingprocessing

the incomingthe incomingrequestsrequests

(“business logic”(“business logic”

HTTP ServerHTTP Server

Servlet engine (e.g. Apache Tomcat)Servlet engine (e.g. Apache Tomcat)

Sending requests,

getting results

Sending requests,

getting results

Page 14: April 05 Prof. Ismael H. F. Santos - ismael@tecgraf.puc-rio.br 1 Modulo II WebServices Prof. Ismael H F Santos.

April 05 Prof. Ismael H. F. Santos - [email protected] 14

Usual principles of Java toolkits

Writing server is easier than writing clients (but only regarding the toolkit, not the business logic)

Servers may be written independently on the used toolkit Always test interoperability with a non-Java client

(because of data serialization and de-serialization) Steps:

write your service implementation make all your classes available to the toolkit deploy your service (usually done just once) restart the whole servlet engine test it with a client request

Page 15: April 05 Prof. Ismael H. F. Santos - ismael@tecgraf.puc-rio.br 1 Modulo II WebServices Prof. Ismael H F Santos.

April 05 Prof. Ismael H. F. Santos - [email protected] 15

Java SOAP Toolkits

Apache SOAP (was IBM’s SOAP4J) Apache Axis (a follow-on to the Apache SOAP)

http://ws.apache.org/axis/

…and many others

Page 16: April 05 Prof. Ismael H. F. Santos - ismael@tecgraf.puc-rio.br 1 Modulo II WebServices Prof. Ismael H F Santos.

April 05 Prof. Ismael H. F. Santos - [email protected] 16

package hello;public interface HelloWorld {

String getHelloMessage();void setHelloMessage (String newHello);

}

hello/HelloWorld.java

package hello;public class HelloWorldService implements HelloWorld { String message = "Hello, world!"; public String getHelloMessage() {

return message; } public void setHelloMessage (String newMessage) {

message = newMessage; }}

hello/HelloWorldService.java

Page 17: April 05 Prof. Ismael H. F. Santos - ismael@tecgraf.puc-rio.br 1 Modulo II WebServices Prof. Ismael H F Santos.

April 05 Prof. Ismael H. F. Santos - [email protected] 17

import org.apache.axis.client.*;public class HelloWorldClient { public static void main (String [] args) { try { // prepare the call (the same for all called methods) Call call = (Call) new Service().createCall(); call.setTargetEndpointAddress (new java.net.URL( "http://localhost:8080/axis/services/Hello"));

// call "get message" if (args.length == 0) { call.setOperationName ("getHelloMessage"); String result = (String)call.invoke( new Object[]{} ); System.out.println (result); System.exit (0); }

HelloWorldClient.java

Page 18: April 05 Prof. Ismael H. F. Santos - ismael@tecgraf.puc-rio.br 1 Modulo II WebServices Prof. Ismael H F Santos.

April 05 Prof. Ismael H. F. Santos - [email protected] 18

// call "set message" and afterwards "get message" call.setMaintainSession (true); // TRY also without // this line... call.setOperationName ("setHelloMessage"); call.invoke ( new Object [] { args[0] } ); call.setOperationName ("getHelloMessage"); System.out.println (call.invoke ( new Object [] {} ));

} catch (Exception e) { System.err.println ("ERROR:\n" + e.toString()); } }}

HelloWorldClient.java

Page 19: April 05 Prof. Ismael H. F. Santos - ismael@tecgraf.puc-rio.br 1 Modulo II WebServices Prof. Ismael H F Santos.

April 05 Prof. Ismael H. F. Santos - [email protected] 19

Generated for HelloWorld

HelloWorldServiceLocator

implements

HelloWorldService

getHello()

1. Make an instance of this

3. Call methods on this proxy object

HelloSoapBindingStub

implements

HelloWorld

2. Use it to make an instance of this

Page 20: April 05 Prof. Ismael H. F. Santos - ismael@tecgraf.puc-rio.br 1 Modulo II WebServices Prof. Ismael H F Santos.

April 05 Prof. Ismael H. F. Santos - [email protected] 20

public class HelloWorldClientFromStubs { public static void main (String [] args) { try { // prepare the calls (the same for all called methods) hello.generated.HelloWorldService service = new hello.generated.HelloWorldServiceLocator(); hello.generated.HelloWorld myHelloProxy = service.getHello(); // call "get message" if (args.length == 0) { String result = myHelloProxy.getHelloMessage() System.out.println (result); System.exit (0); }

// call "set message" and afterwards "get message”

myHelloProxy.setHelloMessage (args[0]);

System.out.println (myHelloProxy.getHelloMessage());

} catch (Exception e) {

System.err.println ("ERROR:\n" + e.toString());

}

}

}

HelloWorldClientFromStubs.java

Page 21: April 05 Prof. Ismael H. F. Santos - ismael@tecgraf.puc-rio.br 1 Modulo II WebServices Prof. Ismael H F Santos.

April 05 Prof. Ismael H. F. Santos - [email protected] 21

Java <=> XML Data Mapping

How Java objects are converted to/from XML data (in order to be able to be put into SOAP messages)

Important especially for the non-basic data types

It’s easier if your non-basic data types are Java Beans (having set/get methods for members)

Page 22: April 05 Prof. Ismael H. F. Santos - ismael@tecgraf.puc-rio.br 1 Modulo II WebServices Prof. Ismael H F Santos.

April 05 Prof. Ismael H. F. Santos - [email protected] 22

Examples (Java Client)

URL endpointURL = new URL(endpoint);

Call call = new Call();

call.setSOAPTransport(m_httpconn);

call.setTargetObjectURI("MessageService");

call.setMethodName("setMessage");

call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);

Page 23: April 05 Prof. Ismael H. F. Santos - ismael@tecgraf.puc-rio.br 1 Modulo II WebServices Prof. Ismael H F Santos.

April 05 Prof. Ismael H. F. Santos - [email protected] 23

Examples (Java Client)

Vector params = new Vector();

params.addElement(

new Parameter("name", java.lang.String.class, name, null));

params.addElement(

new Parameter("colour", java.lang.String.class, colour, null));

call.setParams(params);

Response response = call.invoke(endpointURL, "");

Page 24: April 05 Prof. Ismael H. F. Santos - ismael@tecgraf.puc-rio.br 1 Modulo II WebServices Prof. Ismael H F Santos.

April 05 Prof. Ismael H. F. Santos - [email protected] 24

A Web Service example in Perl

This is a module implementingthe “business logic”

package HelloPerl;use strict;use vars qw( $Message );$Message = 'Hello, here is Perl.';sub getHelloMessage { $Message; }sub setHelloMessage { $Message = shift; }1;

This is a cgi-binscript

#!/usr/bin/perl -w -- Perl –use SOAP::Transport::HTTP;SOAP::Transport::HTTP::CGI -> dispatch_to('HelloPerl') -> handle;

#!/usr/bin/perl –wuse SOAP::Lite on_fault => sub {…};print SOAP::Lite -> uri ('HelloPerl') -> proxy ('http://localhost/cgi-bin/helloserver.cgi') -> getHelloMessage -> result;

This is a client

Page 25: April 05 Prof. Ismael H. F. Santos - ismael@tecgraf.puc-rio.br 1 Modulo II WebServices Prof. Ismael H F Santos.

April 05 Prof. Ismael H. F. Santos - [email protected] 25

SOAP::Lite

a collection of (many) modules but they are loaded automatically when needed

supports SOAP 1.1 specification all methods can be used for both setting and

retrieving values: if you provide no parameters, you will get current value, and if

parameters are provided, a new value will be assigned to the object

and the method in question will return the current object (if not stated otherwise) which is is suitable for stacking these calls like:

$lite = SOAP::Lite-> uri(’openBQS')-> proxy('http://industry.ebi.ac.uk/soap/openBQS');

Page 26: April 05 Prof. Ismael H. F. Santos - ismael@tecgraf.puc-rio.br 1 Modulo II WebServices Prof. Ismael H F Santos.

April 05 Prof. Ismael H. F. Santos - [email protected] 26

Using “wsdl” - directly

#!/usr/bin/perl -w

use SOAP::Lite on_fault => sub {…};print SOAP::Lite -> service ('file:/home/senger/ws-ws/perl/Hello.wsdl') -> setHelloMessage (123);

getting “.wsdl” file by using its URL then, you do not need to worry about autotyping

#!/usr/bin/perl -w

use SOAP::Lite on_fault => sub {…};my $service = SOAP::Lite -> service ('file:./Hello.wsdl');$service->setHelloMessage ($ARGV[0] or "Hello!!!");print $service->getHelloMessage, "\n";