What is SOAP?
Simple Object Access Protocol (SOAP) is a cross-platform, language-independent, xml-based RPC protocol, usually (but not necessarily) HTTP.
It uses XML to encode information to make remote procedure calls, HTTP to transfer information from client to server on the network, and vice versa.
SOAP has several advantages over other technologies, such as Com,corba, for example, its relatively inexpensive deployment and debugging costs, its scalability and ease of use, and the existence of several different language and platform implementations.
See a simple tutorial to learn about SOAP
This tutorial will familiarize you with the SOAP implementation Ruby (SOAP4R). This is a basic tutorial, so if you need in-depth details, you need to refer to other resources.
Install soap4r:
SOAP4R is set up by Hiroshi Nakamura to download the soap implementation of Ruby's development directly from the Web:
Note: It is possible that this component has been installed.
Download SOAP
If you know the Gem utility, you can use the following command to install the SOAP4R and related packages.
$ gem Install soap4r--include-dependencies
If you are working on Windows, you need to download a compressed file from the above location and need to install it using standard installation methods to run Ruby's install.rb.
To write a soap4r server:
SOAP4R supports two different types of servers:
- CGI/FASTCGI based (soap::rpc::cgistub)
- Standalone (Soap::rpc:standaloneserver)
This tutorial will write a separate server in detail. The following steps are involved in writing a SOAP server:
Step 1th-Inherit the Soap::rpc::standaloneserver class:
To implement your own stand-alone server, you need to write a new class that will soap::standaloneserver the subclass of the class, as follows:
Copy Code code as follows:
Class MyServer < Soap::rpc::standaloneserver
...............
End
Note: If you want to write a fastcgi server, you need to inherit the Soap::rpc::cgistub class, and the remaining steps will remain the same.
Step 2nd-Define handler methods:
The second step is to write Web service methods that you want to expose to the outside world.
They can be written as simple ruby methods. For example, let's write a method of dividing two two two numbers and two numbers:
Class MyServer < Soap::rpc::standaloneserver
......... # Handler Methods
def add (A, b) return
A + b
-
def div (A, b)
return
Step 3rd-Exposure handler method:
The next step is the method we define to add to our server. The Initialize method is used to expose a service in one of the following two ways:
Class MyServer < Soap::rpc::standaloneserver
def initialize (*args)
Add_method (receiver, MethodName, * Paramarg)
End
The following parameter description:
To understand the usage of inout or out parameters, consider the following service method this takes two parameters (Inpar Am and Inoutparam), returns one normal return value (RetVal) and two further parameters:inoutparam and Outparam:
def ameth (Inparam, inoutparam)
retVal = Inparam + inoutparam outparam
= inparam. Inoutparam Inoutparam
= Inpa RAM * Inoutparam return
retVal, Inoutparam, Outparam
End
Now, we can disclose this method as follows:
Add_method (self, ' ameth ', [
%w (in Inparam),
%w (inout inoutparam),
%w (out outparam),%w
(retval return )
])
Step 4th-Start the server:
The last step is to start the server with an instance of the derived class of the instance and call the Start method.
MyServer = myserver.new (' ServerName ',
' urn:ruby:ServiceName ', hostname, port)
Myserver.start
This is a description of the required parameters:
For example:
Now using the steps above, let's write a separate server:
Require "Soap/rpc/standaloneserver"
begin
Class MyServer < Soap::rpc::standaloneserver
# expose our Services
def initialize (*args)
add_method (self, ' add ', ' a ', ' B ')
Add_method (self, ' div ', ' a ', ' B
') End
# Handler methods
def add (A, b) return
A + b
-
def div (A, b)
return End
Server = Myserver.new ("MyServer",
' urn:ruby:calculation ', ' localhost ', 8080)
trap (' INT ') {
Server.shutdown
}
server.start
rescue => err
puts Err.message
end
At execution time, the server application begins a separate soap service on the localhost to listen for port 8080 requests. It exposes a service method: Add and Div, which takes two parameters and returns the result.
You can now run this server backstage as follows:
Write SOAP4R client:
SOAP::RPC::D River class is used for writing to SOAP client applications for support. This tutorial will introduce this class to show the basics of the application that it uses.
The following is the minimum required information to invoke the SOAP service:
- SOAP Service (SOAP endpoint URL)
- Service method (method namespace URI)
- Name of service method and its parameters
Now we will write a SOAP client to invoke the service definition method in the example above named Add and Div.
The following are the main steps to create a SOAP client:
Step 1-Create a SOAP driver instance:
We create an instance Soap::rpc::D River by calling the new method as follows:
SOAP::RPC::D river.new (EndPoint, NameSpace, SOAPAction)
This is a description of the required parameters:
Step 2nd-Ways to add a service:
To add to the SOAP Soap service method to Soap::rpc::D River We can invoke the following method to use Soap::rpc::D River instance:
Driver.add_method (name, *paramarg)
The following parameter description:
Step 3rd-Invoke the SOAP service:
The final step is to invoke the SOAP service to use SOAP::RPC::D River Instance as follows:
result = Driver.servicemethod (Paramarg ...)
Here Servicemethod is the actual Web service method and Paramarg ... Is the list parameter that needs to be passed in the service method.
For example:
Based on the above steps, we will write a SOAP client as follows:
#!/usr/bin/ruby-w
require ' soap/rpc/driver '
NAMESPACE = ' urn:ruby:calculation '
URL = ' http://localhost : 8080/'
begin
Driver = soap::rpc::D river.new (URL, NAMESPACE)
# ADD Remote sevice methods
Method (' Add ', ' a ', ' B ')
# Call Remote service methods
puts Driver.add
rescue => err
puts E Rr.message End