Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
http://www.cs.fh-aargau.ch/~gruntz amt=100&from=CHF&to=USD&submit=Convert
Services over HTTP: Currency Converter Services over HTTP: Currency Converter XML
server.start();
System.out.println("Server started");
}
}
<servlet-mapping>
<servlet-name>XmlRpcServlet</servlet-name>
<url-pattern>/xmlrpc</url-pattern>
</servlet-mapping>
</web-app>
<!-- org/apache/xmlrpc/webserver/XmlRpcServlet.properties -->
literal encoded
=> 2 in-parameters
Document X => 2 out-parameters
RPC X => 1 result
<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-ENV:Envelope>
(C) Hochschule für Technik (C) Hochschule für Technik
1 April 2008 Fachhochschule Nordwestschweiz 33 1 April 2008 Fachhochschule Nordwestschweiz 34
• compound type
<m:method>
<book>
<title xsi:type="xsi:string">Inside C# </title>
<author xsi:type="xsi:string">Tom Archer</author>
</book>
</m:method>
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
Client Client
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
• 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