160 likes | 359 Views
Service Computing. Prof. Dr. Ramin Yahyapour IT & Medien Centrum 24. November 2009. SOAP Example Request. <SOAP-ENV:Envelope xmlns:SOAP-ENV="SoapEnvelopeURI" SOAP-ENV:encodingStyle="SoapEncodingURI"> <SOAP-ENV:Header> </SOAP-ENV:Header> <SOAP-ENV:Body>
E N D
Service Computing Prof. Dr. Ramin YahyapourIT & Medien Centrum24. November 2009
SOAP Example Request <SOAP-ENV:Envelope xmlns:SOAP-ENV="SoapEnvelopeURI" SOAP-ENV:encodingStyle="SoapEncodingURI"> <SOAP-ENV:Header> </SOAP-ENV:Header> <SOAP-ENV:Body> <m:GetLastTradePrice xmlns:m="ServiceURI"> <tickerSymbol>SUNW</tickerSymbol> </m:GetLastTradePrice> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
header Envelope body
SOAP Response Format header Envelope body
Generate WSDL (sqs-interface.wsdl) message portType
Generate WSDL (sqs-interface.wsdl) Binding (SOAP)
Building a Web Service public class StockQuoteService { if ( symbol.equals("XXX") ) return( (float) 55.25 ); URL url = new URL( "http://www.xmltoday.com/examples/" + "stockquote/getxmlquote.vep?s="+symbol ); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse( url.toExternalForm() ); Element elem = doc.getDocumentElement(); NodeList list = elem.getElementsByTagName( "stock_quote"); if ( list != null && list.getLength() != 0 ) { elem = (Element) list.item(0); list = elem.getElementsByTagName( "price" ); elem = (Element) list.item(0); String quoteStr = elem.getAttribute("value"); try { return Float.valueOf(quoteStr).floatValue(); } catch (NumberFormatException e1) {//Exception handling} }
WSDL Definition <?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:interface="http://www.getquote.com/StockQuoteService-interface" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="StockQuoteService" targetNamespace="http://www.getquote.com/StockQuoteService"> <import location="http://ZHANGLJ:80/stockquote/sqs-interface.wsdl" namespace="http://www.getquote.com/StockQuoteService-interface"/> <service name="StockQuoteService"> <documentation>Stock Quote Service</documentation> <port binding="interface:StockQuoteServiceBinding" name="Demo"> <soap:address location="http://yamato:80/stockquote/services/StockQuoteService"/> </port> </service> </definitions>
Service Endpoint Interface File: HelloIF.java package helloservice; import java.rmi.Remote; import java.rmi.RemoteException; public interface HelloIF extends Remote { public String sayHello(String s) throws RemoteException; }
Service Endpoint Implementation File: HelloImpl.java package helloservice; public class HelloImpl implements HelloIF { public String message ="Hello"; public String sayHello(String s) { return message + s; } }
Static Web Service Client File: HelloClient.java package staticstub; import javax.xml.rpc.Stub; public class HelloClient { private String endpointAddress; public static void main(String[] args) { System.out.println("Endpoint address = " + args[0]); try { Stub stub = createProxy(); stub._setProperty (javax.xml.rpc.Stub.ENDPOINT_ADDRESS_PROPERTY, args[0]); HelloIF hello = (HelloIF)stub; System.out.println(hello.sayHello("Duke!")); } catch (Exception ex) { ex.printStackTrace(); } } private static Stub createProxy() { // Note: MyHelloService_Impl is implementation-specific. return (Stub) (new MyHelloService_Impl().getHelloIFPort()); } }
File: HelloClient.java J2EE Application Client package appclient; import javax.xml.rpc.Stub; import javax.naming.*; public class HelloClient { private String endpointAddress; public static void main(String[] args) { System.out.println("Endpoint address = " + args[0]); try { Context ic = new InitialContext(); MyHelloServicemyHelloService = (MyHelloService) ic.lookup("java:comp/env/service/MyJAXRPCHello"); appclient.HelloIFhelloPort = myHelloService.getHelloIFPort(); ((Stub)helloPort)._setProperty (Stub.ENDPOINT_ADDRESS_PROPERTY,args[0]); System.out.println(helloPort.sayHello("Jake!")); System.exit(0); } catch (Exception ex) { ex.printStackTrace(); System.exit(1); } } }