You are on page 1of 15

Services over HTTP: Currency Converter

WebServices &
SOAP (Simple Object Access Protocol)
• Request • Response
POST /currency/convert HTTP/1.1 HTTP/1.1 200 OK
Accept: image/gif, image/x-xbitmap, Server: Apache-Coyote/1.1
image/jpeg, image/pjpeg,
Content application/x-shockwave-flash,
Content-Type: text/html;charset=ISO-8859-1
Content-Length: 159
application/vnd.ms-excel,
1. Introduction to SOAP application/vnd.ms-powerpoint,
application/msword, */*
Date: Thu, 08 Dec 2005 02:45:32 GMT

2. Axis Server Referer: http://localhost:8081/currency/ <html>


Accept-Language: de <head>
3. Demo Content-Type: application/x-www-form- <title>Currency Converter</title>
urlencoded </head>
Accept-Encoding: gzip, deflate <body bgcolor="white">
User-Agent: Mozilla/4.0 (compatible; MSIE <h1>Currency Converter</h1>
6.0; Windows NT 5.1; SV1; .NET CLR 100 CHF
Prof. Dr. Dominik Gruntz 1.1.4322; .NET CLR 2.0.40607)
<br>
Host: localhost:8081
76.0717 USD
University of Applied Sciences Content-Length: 38
</body>
Connection: Keep-Alive
dominik.gruntz@fhnw.ch Cache-Control: no-cache
</html>

http://www.cs.fh-aargau.ch/~gruntz amt=100&from=CHF&to=USD&submit=Convert

(C) Hochschule für Technik


1 April 2008 1 1 April 2008 Fachhochschule Nordwestschweiz 2

Services over HTTP: Currency Converter Services over HTTP: Currency Converter XML

• Request • Response • Request • Response


POST /currency/convert HTTP/1.1 HTTP/1.1 200 OK POST /currency/convert HTTP/1.1 HTTP/1.1 200 OK
Accept: image/gif, image/x-xbitmap, Server: Apache-Coyote/1.1 Content-Type: text/xml Content-Type: text/xml
image/jpeg, image/pjpeg, Content-Type: text/html;charset=ISO-8859-1 Content-Length: ... Content-Length: ...
application/x-shockwave-flash, Content-Length: 7
application/vnd.ms-excel,
application/vnd.ms-powerpoint, Date: Thu, 08 Dec 2005 02:45:32 GMT <methodCall> <methodResponse>
application/msword, */* <methodName> <result>
Referer: http://localhost:8081/currency/ 76.0717 Currency.convert 76.0717
Accept-Language: de </methodName> </result>
Content-Type: application/x-www-form- <params> </methodResponse>
urlencoded <param name=“amt”>
Accept-Encoding: gzip, deflate 100
User-Agent: Mozilla/4.0 (compatible; MSIE </param>
6.0; Windows NT 5.1; SV1; .NET CLR <param name=“from”>
1.1.4322; .NET CLR 2.0.40607) CHF
Host: localhost:8081 </param>
Content-Length: 38 <param name=“to”>
Connection: Keep-Alive USD
Cache-Control: no-cache </param>
</params>
amt=100&from=CHF&to=USD&submit=Convert </methodCall>

(C) Hochschule für Technik (C) Hochschule für Technik


1 April 2008 Fachhochschule Nordwestschweiz 3 1 April 2008 Fachhochschule Nordwestschweiz 4
XML-Remote Procedure Calls XML-RPC Request

• XML-RPC • Method Call


– Simple RPC protocol over HTTP <?xml version="1.0"?>
– Procedure parameters can be <methodCall>
ƒ Scalars: string, integer, double, boolean, date, base64 <methodName>hello</methodName>
<params>
ƒ Structures: records and lists <param>
– Author: Dave Winer <value><string>Hello World</string></value>
– http://www.xmlrpc.com/ </param>
</params>
</methodCall>
• Implementation
– Apache XML RPC (current Version: 3.1 [Aug 2007]) – params tag may contain several parameters
ƒ http://ws.apache.org/xmlrpc/
– Many other implementations available
ƒ http://www.xmlrpc.com/directory/1568/implementations

(C) Hochschule für Technik (C) Hochschule für Technik


1 April 2008 Fachhochschule Nordwestschweiz 5 1 April 2008 Fachhochschule Nordwestschweiz 6

XML-RPC Responses (1/2) XML-RPC Responses (2/2)

• Single Result • Fault Result


<?xml version="1.0"?> <?xml version="1.0"?>
<methodResponse> <methodResponse>
<params> <fault>
<param> <value>
<value><string>Hello World</string></value> <struct>
</param> <member><name>faultCode</name>
</params> <value><int>4711</int></value>
</methodResponse> </member>
<member><name>faultString</name>
– Params node may also be empty (=> void) <value>java.lang.IOException</value>
</member>
– Result type may also be of type struct or array </struct>
</value>
</fault>
</methodResponse>

(C) Hochschule für Technik (C) Hochschule für Technik


1 April 2008 Fachhochschule Nordwestschweiz 7 1 April 2008 Fachhochschule Nordwestschweiz 8
XML-RPC Types XML-RPC Types

• Simple Data Types • Structs


– int, i4 signed 32bit integer – Struct contains members with a name and a value
– string ASCII string (default type, no latin-1/unicode) – Structs can be recursive (i.e. may contain other structs or arrays)
– boolean either 0 (false) or 1 (true)
<struct>
– double double-precision floating point number <member>
– dateTime.iso8601 e.g. 20050717T14:08:14 (no time zone) <name>from</name>
– base64 raw binary data of any length, base64 encoded <value><i4>-5</i4></value>
</member>
<member>
• Example <name>to</name>
<value><i4>5</i4></value>
– <i4>4711</i4>
</member>
– <boolean>0</boolean> </struct>
– <base64>eW91IGNhbid0IHJ1YWQgdGhpcyE=</base64>

(C) Hochschule für Technik (C) Hochschule für Technik


1 April 2008 Fachhochschule Nordwestschweiz 9 1 April 2008 Fachhochschule Nordwestschweiz 10

XML-RPC Types XML-RPC Protocol

• Arrays • Request • Response


POST /xmlrpc HTTP/1.1 HTTP/1.1 200 OK
– Array contains a single data element which contains several values Content-Length: 152 Server: Apache-Coyote/1.1
– Arbitrary types for values (including arrays/structs) Content-Type: text/xml Content-Type: text/xml
Cache-Control: no-cache Content-Length: 161
– Types can be mixed Pragma: no-cache
– No indices User-Agent: Java/1.5.0_05 <?xml version="1.0" encoding="UTF-8"?>
Host: 127.0.0.1:88 <methodResponse>
<array> Connection: keep-alive <params>
<data> <param>
<?xml version="1.0"encoding="UTF-8"?> <value>[XML-RPC] Hello World,
<value><i4>-5</i4></value> <methodCall> welcome with XML-RPC</value>
<value><string>44</string></value> <methodName> </param>
<value><boolean>1</boolean></value> Sample.hello </params>
</data> </methodName> </methodResponse>
</array> <params>
<param>
<value>World</value>
</param>
</params>
</methodCall>
(C) Hochschule für Technik (C) Hochschule für Technik
1 April 2008 Fachhochschule Nordwestschweiz 11 1 April 2008 Fachhochschule Nordwestschweiz 12
XML-RPC Sample Client XML-RPC Sample Server

import java.util.*; import org.apache.xmlrpc.server.*;


import org.apache.xmlrpc.*; import org.apache.xmlrpc.webserver.WebServer;

public class HelloClient { public class HelloServer {


public static void main (String [] args) throws Exception { public static void main (String [] args) throws Exception {
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(); WebServer server = new WebServer(80);
config.setServerURL(new URL("http://localhost/xmlrpc")); XmlRpcServer xmlRpcServer = server.getXmlRpcServer();

XmlRpcClient client = new XmlRpcClient(); PropertyHandlerMapping phm = new PropertyHandlerMapping();


client.setConfig(config); phm.addHandler("Sample", Hello.class);
xmlRpcServer.setHandlerMapping(phm);
List params = new ArrayList();
params.add(args[0]); server.start();
System.out.println("Server started");
Object result = client.execute("Sample.hello", params ); }
System.out.println("The result is: "+result.toString()); }
}
}
(C) Hochschule für Technik (C) Hochschule für Technik
1 April 2008 Fachhochschule Nordwestschweiz 13 1 April 2008 Fachhochschule Nordwestschweiz 14

XML-RPC Sample Server XML-RPC Sample Server

public class Hello { import org.apache.xmlrpc.server.*;


public String hello(String name) { import org.apache.xmlrpc.webserver.WebServer;
return "[XML-RPC] Hello "+name+", welcome with WebServices";
} public class HelloServer {
} public static void main (String [] args) throws Exception {
WebServer server = new WebServer(80);
XmlRpcServer xmlRpcServer = server.getXmlRpcServer();
– Only instance methods are accessible
PropertyHandlerMapping phm = new PropertyHandlerMapping();
– Access works with reflection phm.load(Thread.currentThread().getContextClassLoader(),
"xmlrpc.properties");
xmlRpcServer.setHandlerMapping(phm);

server.start();
System.out.println("Server started");
}
}

(C) Hochschule für Technik (C) Hochschule für Technik


1 April 2008 Fachhochschule Nordwestschweiz 15 1 April 2008 Fachhochschule Nordwestschweiz 16
XML-RPC Sample Server XML-RPC Servlet Server

public class Hello { <web-app>


public String hello(String name) { <servlet>
return "[XML-RPC] Hello "+name+", welcome with WebServices"; <servlet-name>XmlRpcServlet</servlet-name>
} <servlet-class>
} org.apache.xmlrpc.webserver.XmlRpcServlet</servlet-class>
<init-param>
<param-name>enabledForExtensions</param-name>
xmlrpc.properties <param-value>true</param-value> <!-- vendor extensions -->
Sample=Hello </init-param>
</servlet>

<servlet-mapping>
<servlet-name>XmlRpcServlet</servlet-name>
<url-pattern>/xmlrpc</url-pattern>
</servlet-mapping>
</web-app>
<!-- org/apache/xmlrpc/webserver/XmlRpcServlet.properties -->

(C) Hochschule für Technik (C) Hochschule für Technik


1 April 2008 Fachhochschule Nordwestschweiz 17 1 April 2008 Fachhochschule Nordwestschweiz 18

XML-RPC Discussion Apache XML-RPC

• Simple protocol • Vendor extensions may be enabled


– Only one transport protocol (HTTP) – Support of all primitive Java types
– Only one operating mode (RPC) – Support of Calendar Objects (timezone setting / milliseconds may be sent)
• Simple data types – Serializable objects can be transmitted
– Coding is not completely specified (timezone for dateTime.iso8601) – Client and server can be operated in streaming mode
– Only ASCII 7bit => base64 for other strings – Support of compression mode
– Cyclic and Alias references cannot be modeled – Exception handling
• Meta description ƒ Server may convert exceptions into a byte array and send them to the client
using a faultCause message
– No formal interface description languages
– XML-RPC services have to be described in natural language

(C) Hochschule für Technik (C) Hochschule für Technik


1 April 2008 Fachhochschule Nordwestschweiz 19 1 April 2008 Fachhochschule Nordwestschweiz 20
SOAP SOAP

• SOAP = Simple Object Access Protocol • Web Services Architecture


(actually, today SOAP is no longer an acronym) enables cross-platform interoperability
– simple, lightweight protocol for exchanging structured and typed between loosely coupled applications
information on the Web – SOAP Simple Object Access Protocol Wire
ƒ Protocol: HTTP, SMTP, FTP, raw TCP/IP, ….. – WSDL Web Services Description Language Description
ƒ Format: XML ƒ XML grammer to describe SOAP enabled Web services
ƒ Messaging type: one-way or request/response – UDDI Universal Description, Discovery and Integration Discovery
ƒ Body: may contain additional (meta) information ƒ discovery of web services on the internet ( ~ DNS )

• Design Goal: KISS • SOAP


– stick to absolutely minimum of functionality – 12/1999 SOAP v1.0 Microsoft & Develop Mentor (D. Box)
– based on existing and adopted standards (HTTP / XML) – 05/2000 SOAP v1.1 + IBM
– 2003 SOAP v1.2 http://www.w3.org/TR/soap/

(C) Hochschule für Technik (C) Hochschule für Technik


1 April 2008 Fachhochschule Nordwestschweiz 21 1 April 2008 Fachhochschule Nordwestschweiz 22

Document vs RPC Style SOAP Example

• Styles • Book Store Service


– RCP-oriented
– Document-oriented service BookService {
boolean OrderBook(
• Encodings
/* in */ string title,
– Literal:
/* in */ string author,
ƒ Body contents conform to a specific XML Schema
/* out */ long daysToDeliver,
– Encoded /* out */ double cost
ƒ Data is encoded based on XML Schema datatypes );
ƒ Message doesn't conform to a particular schema };

literal encoded
=> 2 in-parameters
Document X => 2 out-parameters
RPC X => 1 result

(C) Hochschule für Technik (C) Hochschule für Technik


1 April 2008 Fachhochschule Nordwestschweiz 23 1 April 2008 Fachhochschule Nordwestschweiz 24
SOAP HTTP Request SOAP HTTP Response (Success)

POST /BookStoreServlet HTTP/1.1 HTTP/1.0 200 OK


Host: www.amazon.com
HTTP Request Content-Length: 162
Content-Length: 162 Content-Type: text/xml; charset="utf-8"
Content-Type: text/xml; charset="utf-8" SOAP HTTP Binding
SOAPAction: ”BookService" <Envelope>
<Body>
<Envelope> <OrderBookResponse>
<Body> <return>true</return>
<OrderBook> SOAP Payload <daysToDeliver>3</daysToDeliver>
<title>Understanding SOAP</title> <cost>62.50</cost>
<author>Kennard Scribner</author> </OrderBookResponse>
</OrderBook> </Body>
</Body> </Envelope>
</Envelope>

(C) Hochschule für Technik (C) Hochschule für Technik


1 April 2008 Fachhochschule Nordwestschweiz 25 1 April 2008 Fachhochschule Nordwestschweiz 26

SOAP HTTP Response (Fault) SOAP HTTP Request

POST /BookStoreServlet HTTP/1.1


HTTP/1.0 500 Internal Server Error Host: www.amazon.com
Content-Length: 339 with namespaces name
Content-Length: 520
Content-Type: text/xml; charset="utf-8" Content-Type: text/xml; charset="utf-8"
collisions are avoided
SOAPAction: ""
<Envelope> determines version
<Body> of SOAP (1.1)
<SOAP-ENV:Envelope
<Fault>
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
<faultcode>Server</faultcode>
<faultstring>Server Error</faultstring> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<detail> xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<myfaultdetails> <SOAP-ENV:Body>
<message>Book out of print</message> <ns1:OrderBook xmlns:ns1="BookService" SOAP-ENV:
<errorcode>4711</errorcode> encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<myfaultdetails> <title xsi:type="xsd:string">Understanding SOAP</title>
</detail> <author xsi:type="xsd:string">Kennard Scribner</author>
</Fault> </ns1:OrderBook>
</Body>
</SOAP-ENV:Body>
</Envelope>
</SOAP-ENV:Envelope>
(C) Hochschule für Technik (C) Hochschule für Technik
1 April 2008 Fachhochschule Nordwestschweiz 27 1 April 2008 Fachhochschule Nordwestschweiz 28
SOAP HTTP Response (Success) SOAP HTTP Response (Fault)

HTTP/1.0 200 OK HTTP/1.0 500 Internal Server Error


Content-Length: 580 Content-Length: 496
Content-Type: text/xml; charset="utf-8" Content-Type: text/xml; charset="utf-8"

<SOAP-ENV:Envelope <SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/”>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <SOAP-ENV:Body>
xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <SOAP-ENV:Fault>
<faultcode>SOAP-ENV:Server</faultcode>
<SOAP-ENV:Body>
<faultstring>Server Error</faultstring>
<ns1:OrderBookResponse xmlns:ns1="BookService" SOAP-ENV: <detail>
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <e:myfaultdetails xmlns:e=“BookService”>
<return xsi:type="xsd:boolean">true</return> <message>Book out of print</message>
<daysToDeliver xsi:type="xsd:integer">3</daysToDeliver> <errorcode>4711</errorcode>
<cost xsi:type="xsd:double">62.50</cost > </e:myfaultdetails>
</ns1:OrderBookResponse> </detail>
</SOAP-ENV:Body> </SOAP-ENV:Fault>
</SOAP-ENV:Envelope> </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
(C) Hochschule für Technik (C) Hochschule für Technik
1 April 2008 Fachhochschule Nordwestschweiz 29 1 April 2008 Fachhochschule Nordwestschweiz 30

SOAP Message SOAP Header

Envelope • Envelope • Header Data


• sdf – 0..1 Header – Has to be processed on the message path (from sender to receiver)
Header – 1 Body – May contain additional information;
xml xml
<…> <…> => allows for modular addition of features and services, e.g.
<…> <…> • SOAP does not limit the type of ƒ Transaction IDs
XML documents ƒ Routing information
Body => flexibility ƒ Security credentials (e.g. a digital signature)
ƒ Payment tokens
xml
<…> ƒ Debugging information
<…>

– SOAP intermediaries may


ƒ Process (and remove) header information
ƒ Add new header information

(C) Hochschule für Technik (C) Hochschule für Technik


1 April 2008 Fachhochschule Nordwestschweiz 31 1 April 2008 Fachhochschule Nordwestschweiz 32
SOAP Header Attributes SOAP Header (example)

• actor attribute <SOAP-ENV:Envelope


xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
– Specifies a role a node must play in order to process the header SOAP-ENV:encodingStyle=
(=> SOAP 1.2: role attribute) "http://schemas.xmlsoap.org/soap/encoding/">
– Roles are specified as URI
ƒ http://www.gruntz.ch/logger <SOAP-ENV:Header>
<t:Transaction xmlns:t=”urn:ubs" SOAP-ENV:mustUnderstand="1">
ƒ http://schemas.xmlsoap.org/soap/actor/next
5
ƒ Default: ultimate receiver </t:Transaction>
</SOAP-ENV:Header>
• mustUnderstand attribute ("1" = true, "0" = false)
<SOAP-ENV:Body>
– if this attribute is set then the server must understand and handle this <m:Deposit xmlns:m=”urn:bank#132-23-2003a">
header attribute, otherwise must fail processing the message <m:amount>200</m:amount>
– Default: 0 </m:Deposit>
</SOAP-ENV:Body>

</SOAP-ENV:Envelope>
(C) Hochschule für Technik (C) Hochschule für Technik
1 April 2008 Fachhochschule Nordwestschweiz 33 1 April 2008 Fachhochschule Nordwestschweiz 34

Built-in data types of


Data Types in SOAP XML Schema Part 2: Datatypes

• simple types (data types defined in XML Schema Spec part 2)


<m:method>
<para xsi:type="int">26</para>
</m:method>

• compound type
<m:method>
<book>
<title xsi:type="xsi:string">Inside C# </title>
<author xsi:type="xsi:string">Tom Archer</author>
</book>
</m:method>

(C) Hochschule für Technik (C) Hochschule für Technik


1 April 2008 Fachhochschule Nordwestschweiz 35 1 April 2008 Fachhochschule Nordwestschweiz 36
Data Types in SOAP Array

• references • Java Argument


<m:compare> int[] primes = new int[]{2,3,5,7,11,13,17};
<x href="#book1"/>
<y href="#book1"/>
</m:compare>
<m:book id="book1">
• XML Translation
<primes xsi:type="soapenc:Array"
<title xsi:type="xsi:string">Inside C#</title>
soapenc:arrayType="xsd:int[7]"
<author xsi:type="xsi:string">Tom Archer</author>
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
</m:book>
<item>2</item>
<item>3</item>
• circular list <item>5</item>
<m:method> <item>7</item>
<x href="#node1"/> <item>11</item>
</m:method> <item>13</item>
<m:node id="node1"><next href="#node2"></m:node> <item>17</item>
<m:node id="node2"><next href="#node1"></m:node> </primes>

(C) Hochschule für Technik (C) Hochschule für Technik


1 April 2008 Fachhochschule Nordwestschweiz 37 1 April 2008 Fachhochschule Nordwestschweiz 38

Example: Map SOAP Summary

• Java Argument • Simple means:


Map m = new HashMap();
m.put("Daffy Duck", "123-444"); – stateless objects (similar to internet)
m.put("Lucky Luck", "123-445"); – no call-backs (i.e. no bidirectional communications)
• XML Translation – no distributed garbage collection
<arg1 href="#id0"/> – no objects-by-reference
<multiRef id="id0" soapenc:root="0" soapenv:encodingStyle=
"http://schemas.xmlsoap.org/soap/encoding/" xsi:type= – no remote object activation
"ns2:Map" xmlns:ns2="http://xml.apache.org/xml-soap" – no session/state management
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" >
<item> – no distributed transactions
<key xsi:type="xsd:string">Daffy Duck</key> • Advantages
<value xsi:type="xsd:string">123-444</value>
</item> – easy to implement (HTTP/XML)
<item> – easy to install / administrate
<key xsi:type="xsd:string">Lucky Luck</key>
<value xsi:type="xsd:string">123-445</value> – firewall friendly
</item>
(C) Hochschule für Technik (C) Hochschule für Technik
</multiRef>
1 April 2008 Fachhochschule Nordwestschweiz 39 1 April 2008 Fachhochschule Nordwestschweiz 40
SOAP Summary Apache Axis

• Security • Key Design Features


– no security facility – Speed
ƒ custom security measures on top of SOAP ƒ SAX based
• WS-Security – Flexibility
ƒ custom security measures on top of HTTP ƒ design based around a chain of message handlers
• HTTPS ƒ extensions possible for custom header processing, system management, etc.
– Transport independence
• Efficiency ƒ the SOAP kernel is completely transport-independent (SMTP, FTP, MOM)
– performance loss of factor 10 (approx), measured with axis • Functionality
– most time is used in coding and parsing payload – Drop-In development
– WSDL support (WSDL <-> Java)
• Resources
– http://ws.apache.org/axis/

(C) Hochschule für Technik (C) Hochschule für Technik


1 April 2008 Fachhochschule Nordwestschweiz 41 1 April 2008 Fachhochschule Nordwestschweiz 42

Installing Axis Test Axis Installation

• Installation • Visit http://localhost:8080/axis


– download and expand axis (http://ws.apache.org/axis/)
ƒ Axis 1.1 [June 2003] – AdminServlet
ƒ Axis 1.2 [Nov 2004] ƒ Uncomment the
ƒ Axis 1.3 [Oct 2005] corresponding
ƒ Axis 1.4 [Apr 2006] declaration in
WEB-INF/web.xml
in the axis webapp
– copy axis folder from
– SoapMonitor
ƒ AXIS_HOME/webapps to
ƒ Compile SOAP-
ƒ CATALINA_HOME/webapps
MonitorApplet.java

(C) Hochschule für Technik (C) Hochschule für Technik


1 April 2008 Fachhochschule Nordwestschweiz 43 1 April 2008 Fachhochschule Nordwestschweiz 44
Example Service: Example Service:
1. Service Implementation 2. Client Implementation

import org.apache.axis.client.Call;
• Write Server
public class HelloServer {
public class HelloClient {
public String sayHelloTo(String s) {
return "[axis] Hello " + s + ", how are you?" public static void main(String[] args) throws Exception {
+ "["+ new java.util.Date() + "]"; String endpoint =
} "http://localhost:8080/axis/HelloServer.jws";
}
Call service = new Call(endpoint);
System.out.println(
• Store source as *.jws file (Java Web Service) in webapps/axis service.invoke("sayHelloTo", new Object[]{args[0]})
– webapps/axis/HelloServer.jws );
}
}
Restriction:
Packages cannot be used; JWS are
intended for simple services only

(C) Hochschule für Technik (C) Hochschule für Technik


1 April 2008 Fachhochschule Nordwestschweiz 45 1 April 2008 Fachhochschule Nordwestschweiz 46

Example Service: Example Service:


3. Test & Demo 3. Test & Demo

• Run Client • SOAP Request


– java HelloClient Dominik POST /axis/HelloServer.jws HTTP/1.0
[axis] Hello Dominik, how are you? [Tue Apr 01 09:19:09 2008] Content-Type: text/xml; charset=utf-8
SOAPAction: ""
Content-Length: 456

<?xml version="1.0" encoding="UTF-8"?>


<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<sayHelloTo soapenv:encodingStyle=
"http://schemas.xmlsoap.org/soap/encoding/">
<arg0 xsi:type="soapenc:string" xmlns:soapenc=
"http://schemas.xmlsoap.org/soap/encoding/">
Dominik</arg0>
</sayHelloTo>
</soapenv:Body>
</soapenv:Envelope>
(C) Hochschule für Technik (C) Hochschule für Technik
1 April 2008 Fachhochschule Nordwestschweiz 47 1 April 2008 Fachhochschule Nordwestschweiz 48
Custom Deployment using WSDD Custom Deployment using WSDD

• Web Service Deployment Descriptor (WSDD) • Admin Client


<deployment java org.apache.axis.client.AdminClient deploy.wsdd
xmlns="http://xml.apache.org/axis/wsdd/" - Processing file deploy.wsdd
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> - <Admin>Done processing</Admin>
<service name = "HelloServer" provider="java:RPC"> java org.apache.axis.client.AdminClient list
<parameter name="className"
value="samples.hello.HelloServer"/>
<parameter name="methodName" • Configuration
value="sayHelloTo"/> – stored in file webapps\axis\WEB-INF\server-config.wsdd
</service> – Inspect over http://localhost:8080/axis/servlet/AxisServlet
</deployment>

– Additional information: • Undeploy


ƒ additional handlers (e.g. logging) <undeployment xmlns="http://xml.apache.org/axis/wsdd/">
ƒ type mappings <service name="HelloServer"/>
</undeployment>
ƒ scope information (Application / Request)
(C) Hochschule für Technik (C) Hochschule für Technik
1 April 2008 Fachhochschule Nordwestschweiz 49 1 April 2008 Fachhochschule Nordwestschweiz 50

Client Client

package samples.hello; package samples.hello;

import ...
import org.apache.axis.client.Call;
public class HelloClient1 {
public class HelloClient {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
Call service = new Call( String endpoint = "http://localhost:8081/axis/servlet/AxisServlet";
"http://localhost:8080/axis/servlet/AxisServlet"); Service service = new Service();
Call call = (Call)service.createCall(); // new Call(new Service());
String result = "";
System.out.println( try {
service.invoke("HelloServer", "sayHelloTo", call.setTargetEndpointAddress( new java.net.URL(endpoint) );
new Object[]{args[0]}) call.setOperationName( new QName("HelloServer", "sayHelloTo") );
); call.addParameter( "name", Constants.XSD_STRING, ParameterMode.IN );
} call.setReturnType(XMLType.XSD_STRING );
} result = (String) call.invoke( new Object[] { args[0] } );
}
catch (AxisFault fault) {result = "Error : " + fault.toString();}
System.out.println(result);
}
}
(C) Hochschule für Technik (C) Hochschule für Technik
1 April 2008 Fachhochschule Nordwestschweiz 51 1 April 2008 Fachhochschule Nordwestschweiz 52
WSDL WSDL: Stub/Skeleton generation

• Web Service Description Language • WSDL2Java


– specification of Web Services (IBM / Microsoft) – java org.apache.axis.wsdl.WSDL2Java -p samples.hello.proxy
– used to access service from .NET / SOAP::Lite, … samples\hello\HelloServer.wsdl
– java org.apache.axis.wsdl.WSDL2Java -p samples.hello.proxy
http://localhost:8080/axis/services/HelloServer?wsdl
• WSDL for deployed services
– http://localhost:8080/axis/HelloServer.jws?wsdl
– http://localhost:8080/axis/services/HelloServer?wsdl
• Generated Files
– HelloServer_PortType service interface
– Java2WSDL – HelloServerService factory interface
ƒ java org.apache.axis.wsdl.Java2WSDL – HelloServerSoapBindingStub service proxy
– HelloServerServiceLocator factory implementation

(C) Hochschule für Technik (C) Hochschule für Technik


1 April 2008 Fachhochschule Nordwestschweiz 53 1 April 2008 Fachhochschule Nordwestschweiz 54

HelloServer: Generated Classes HelloServer: Generated Classes

• HelloServer_PortType • HelloServerSoapBindingStub
package samples.hello.proxy; package samples.hello.proxy;
import java.rmi.*;
public interface HelloServer_PortType extends Remote { public class HelloServerSoapBindingStub
public String sayHelloTo(String nameOfClient) extends org.apache.axis.client.Stub
throws RemoteException; implements samples.hello.proxy.HelloServerPortType {
}
public String sayHelloTo(String arg0)
throws java.rmi.RemoteException{...}
• HelloServerService
package samples.hello.proxy; // From javax.xml.rpc.Stub, baseclass of …Stub
import javax.xml.rpc.*; public void _setProperty(String name, Object value) {...}
public interface HelloServerService extends Service { public Object _getProperty(String name) {...}
public String getHelloServerAddress();
public void _setTargetEndpoint(java.net.URL address) {...}
public java.net.URL _getTargetEndpoint() {...}
public HelloServer_PortType getHelloServer()
public void setMaintainSession(boolean session) {...}
throws ServiceException;
public HelloServer_PortType getHelloServer( // From javax.naming.Referenceable
java.net.URL portAddress) throws ServiceException public javax.naming.Reference getReference() {...}
} }
(C) Hochschule für Technik (C) Hochschule für Technik
1 April 2008 Fachhochschule Nordwestschweiz 55 1 April 2008 Fachhochschule Nordwestschweiz 56
HelloServer: Client (using proxies) Further References

• Client • Sample WebServices


package samples.hello; – http://www.xmethods.com/
import samples.hello.proxy.HelloServerPortType; – Google Web Service
import samples.hello.proxy.HelloServerLocator; ƒ Search and spell checking
ƒ http://www.google.com/apis/
public class HelloClient2 {
– Amazon Web Service
public static void main(String[] args) throws Exception {
HelloServerPortType port = ƒ www.amazon.com/gp/aws/landing.html
new HelloServerLocator().getHelloServerPort();
System.out.println(port.sayHelloTo(args[0])); • SOAP Implementations
}
} – XFire http://xfire.codehaus.org/
– Axis2 http://ws.apache.org/axis2/

(C) Hochschule für Technik (C) Hochschule für Technik


1 April 2008 Fachhochschule Nordwestschweiz 57 1 April 2008 Fachhochschule Nordwestschweiz 58

You might also like