34.5"> 34.5">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

SOAP 2. SOAP Example POST /StockQuote HTTP/1.1 Host: Content-Type: text/xml; charset="utf-8"Content-Length: nnnn SOAPAction:"Some-URI"

Similar presentations


Presentation on theme: "SOAP 2. SOAP Example POST /StockQuote HTTP/1.1 Host: Content-Type: text/xml; charset="utf-8"Content-Length: nnnn SOAPAction:"Some-URI""— Presentation transcript:

1 SOAP 2

2 SOAP Example POST /StockQuote HTTP/1.1 Host: www.stockquoteserver.com Content-Type: text/xml; charset="utf-8"Content-Length: nnnn SOAPAction:"Some-URI" <SOAP-ENV:Envelope xmlns:SOAP- ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> DIS

3 Response Document HTTP/1.1 200 OK Content-Type: text/xml; charset="utf-8"Content-Length: nnnn <SOAP-ENV:Envelope xmlns:SOAP- ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/ > 34.5

4 Set-ups http://ws.apache.org/soap/ http://www.alphaworks.ibm.com/tech/soap4j/ http://java.sun.com/webservices/jwsdp/index.j sp http://www.extreme.indiana.edu/xgws/xsoap/ (Java RMI)http://www.extreme.indiana.edu/xgws/xsoap/ http://www.soaplite.com/#TOOLKITS (Perl toolkits)http://www.soaplite.com/#TOOLKITS

5 Simple SOAP Service public class SoapService extends Object { /** Creates new SoapService */ public SoapService() { } /** This is the SOAP exposes method */ public String sayGreeting(String name) { return "Hello "+name; } As you see, the SOAP service does not necessarily have to use the Apache SOAP API. It can be any simple program to a complex database access program or a Java RMI program. Once deployed in the SOAP container, any SOAP client would be able to access the service in a seamless manner by using the Apache SOAP API. However, the SOAP service can also use Apache SOAP API to obtain fine-grained control over the SOAP messages over the wire.

6 Set-ups Tomcat and Apache SOAP Deploy through admin utility or command line Deployment Descriptor

7 <isd:service xmlns:isd="http://xml.apache.org/xml-soap/deployment" id="urn:greetingService"> java org.apache.soap.server.ServiceManagerClient http://localhost:8080/soap/servlet/rpcrouter deploy soap.xml

8 Client String urlString = args[0]; String name = args[1]; //build a Call object Call call = new Call(); call.setTargetObjectURI("urn:greetingService"); call.setMethodName("sayGreeting"); call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC); //creating a parameter list Vector params = new Vector(); params.addElement(new Parameter("name", String.class, name,null)); //adding the parameter(s) to the Call object call.setParams(params); //invoke the soap method Response res = call.invoke(new URL(urlString), "");

9 //if there is no fault in the method invocation, get the result try { if( res.generatedFault() ==false) { Parameter retValue = res.getReturnValue(); Object value = retValue.getValue(); System.out.println(value); }else { System.out.println("The fault is: "+ res.getFault().getFaultString()); } }catch(SOAPException soe) { System.out.println("The SOAP Exception is : "+soe.toString()); } java clientclass http://localhost:8080/soap/servlet/rpcrouter yourname

10 PERL Example # Hello.pm - simple Hello module package Hello; sub sayHello { shift;# remove class name return "Hello ". shift; } 1;

11 CGI Script #!/usr/bin/perl -w # hello.cgi - Hello SOAP handler use SOAP::Transport::HTTP; SOAP::Transport::HTTP::CGI -> dispatch_to('Hello::(?:sayHello)') -> handle ;

12 Client #!/usr/bin/perl -w # hw_client.pl - Hello client use SOAP::Lite; my $name = shift; print "\n\nCalling the SOAP Server to say hello\n\n"; print "The SOAP Server says: "; print SOAP::Lite -> uri('urn:Example1') -> proxy('http://localhost/cgi-bin/helloworld.cgi') -> sayHello($name) -> result. "\n\n";

13 Running % perl hw_client.pl James Calling the SOAP Server to say hello The SOAP Server says: Hello James %

14 Java w/ Apache Web Services public class Hello { public String sayHello(String name) { return "Hello " + name; } <dd:service xmlns:dd="http://xml.apache.org/xml-soap/deployment" id="urn:Example1"> <dd:provider type="java" scope="Application" methods="sayHello"> <dd:java class="samples.Hello" static="false" /> org.apache.soap.server.DOMFaultListener

15 Client import java.io.*; import java.net.*; import java.util.*; import org.apache.soap.*; import org.apache.soap.rpc.*; public class Example1_client { public static void main (String[] args) throws Exception { System.out.println("\n\nCalling the SOAP Server to say hello\n\n"); URL url = new URL (args[0]); String name = args[1]; Call call = new Call ( ); call.setTargetObjectURI("urn:Example1"); call.setMethodName("sayHello"); call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC;); Vector params = new Vector ( ); params.addElement (new Parameter("name", String.class, name, null)); call.setParams (params); System.out.print("The SOAP Server says: "); Response resp = call.invoke(url, "");

16 if (resp.generatedFault ( )) { Fault fault = resp.getFault ( ); System.out.println ("\nOuch, the call failed: "); System.out.println (" Fault Code = " + fault.getFaultCode ( )); System.out.println (" Fault String = " + fault.getFaultString ( )); } else { Parameter result = resp.getReturnValue ( ); System.out.print(result.getValue ( )); System.out.println( ); }

17 C# and.Net using System.Web.Services; [WebService(Namespace="urn:Example1")] public class Example1 { [ WebMethod ] public string sayHello(string name) { return "Hello " + name; } }

18 Client // HelloWorld.cs using System.Diagnostics; using System.Xml.Serialization; using System; using System.Web.Services.Protocols; using System.Web.Services; [System.Web.Services.WebServiceBindingAttribute( Name="Example1Soap", Namespace="urn:Example1")] public class Example1 : System.Web.Services.Protocols.SoapHttpClientProtocol { public Example1( ) { this.Url = "http://localhost/helloworld.asmx "; } [System.Web.Services.Protocols.SoapDocumentMethodAttribute( "urn:Example1/sayHello", RequestNamespace="urn:Example1", ResponseNamespace="urn:Example1", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)] public string sayHello(string name) { object[] results = this.Invoke("sayHello", new object[] {name}); return ((string)(results[0])); }

19 public static void Main(string[] args) { Console.WriteLine("Calling the SOAP Server to say hello"); Example1 example1 = new Example1( ); Console.WriteLine("The SOAP Server says: " + example1.sayHello(args[0])); }

20 SOAP Template <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">.........

21 public class MessageObject extends java.lang.Object { protected java.lang.String message; public MessageObject() { message = "Hello, World, from jbs MessageObject"; } public void setMessge(java.lang.String message) { this.message = message; } public java.lang.String getMessage() { return this.message; } public java.lang.String getMessageXML() { String xmlString = " \n"; xmlString += " "; xmlString += this.message; xmlString += " \n"; return xmlString; }

22 import org.apache.axis.client.Call; import org.apache.axis.client.Service; import javax.xml.namespace.QName; public class SOAP_Call { public static String callService() { String msg = null; try { String endpoint = "http://jbs.cs.unc.edu:8888/axis/MessageObject.jws"; //"http://jbs.cs.unc.edu:8888/axis/services/MessageObject"; Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress( new java.net.URL(endpoint) ); call.setOperationName(new QName("http://soapinterop.org/", "getMessage") ); msg = (String) call.invoke( new Object[]{} ); } catch (Exception e) { System.err.println(e.toString()); } return msg; } public SOAP_Call() { super(); }


Download ppt "SOAP 2. SOAP Example POST /StockQuote HTTP/1.1 Host: Content-Type: text/xml; charset="utf-8"Content-Length: nnnn SOAPAction:"Some-URI""

Similar presentations


Ads by Google