Ruby
Use Ruby to invoke web services using SOAP clients such as Savon.
-
Create application, for example rails new
WsTest1
-
Create controller, for example rails generate
WsCallSample WsCall
controller -
Set the root. In this example, delete
public/index.html
and modify the root property inconfig/routes.rb
toWsCallSample#WsCall
-
Add Savon to the application, for example add
gem'savon'
toapp/Gemfile
The following is a sample code to create a HTML form to enter the
payload to be passed to the service, and invoke processing for Savon and net/https
call. In this example application, the HTML form
is created under views/ws_call_sample/WsCall.html.erb.
<HTML>
<HEAD>
<title>WsCallSample#WsCall</title>
</HEAD>
<BODY >
<form name="envelope" method="POST">
<input type="hidden" name="INPUT_PAYLOAD" >
<input type="hidden" name="OUTPUT_PAYLOAD" >
<%= submit_tag "Invoke Savon", :name => "invokeSavon" %>
<%= submit_tag "Invoke HttpRequest", :name => "invokeHttpRequest" %>
<br>
<table style="width:100%; font-size: small;" align=center>
<tr>
<td align="left"><b>Input Payload</b></td>
<td align="left"><b>Output Payload</b></td>
</tr>
<tr>
<td align="center"><textarea name="inputpayload" wrap="off" style="width:100%;height:100%" rows=30 ><%= params["inputpayload"] %></textarea></td>
<td align="center"><textarea name="outputpayload" wrap="off" style="width:100%;height:100%" rows=30 ><%= params["xmlbody"] %></textarea></td>
</tr>
</table>
<br>
</form>
</BODY>
</HTML>
The following is a sample code to update the controller to handle the
submissions from the HTML form. In this example application, the controller is under controllers/ws_call_sample_controller.rb
. This code
processes the envelope in the input, executes using Savon and basic net/https
call, and displays the response in the output
field.
require 'rubygems'
require 'nokogiri'
require 'rexml/document'
require "net/https"
require 'uri'
class WsCallSampleController < ApplicationController
def WsCall
username ="username";
password ="password";
input=params["inputpayload"];
if params['invokeSavon']
# Process submission when the invokeSavon button was pressed
url = "https://host:port/icCnSetupCreditRulesPublicService/CreditRuleService?wsdl";
# Create new instance of the Savon client using basic authentication
client = Savon.client(wsdl: url, basic_auth: [username, password], ssl_verify_mode: :none )
# Call the service with the envelope provided as input
response = client.call(:on_fault, xml: input)
# Parse the response to XML
doc = Nokogiri::XML(response.to_xml ,&:noblanks)
s = doc.to_xml( indent:2, indent_text:" " )
# Populate reponse content to a variable to be displayed on the output area
params["xmlbody"] = s
elsif params['invokeHttpRequest']
# Process submission when the invokeHttpRequest button was pressed
uri = URI.parse("https://host:port/icCnSetupCreditRulesPublicService/CreditRuleService")
# Create new instance of HTTP object
http = Net::HTTP.new(uri.host, uri.port)
# The request uses SSL, certificates are NOT verified
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
# Create new instance of HTTP POST request
request = Net::HTTP::Post.new(uri.request_uri)
# Configure the request content type to be xml and SOAP action
request.basic_auth(username, password)
request.add_field("SOAPAction", "http://xmlns.oracle.com/apps/incentiveCompensation/cn/creditSetup/creditRule/creditRuleService/findRule")
request.add_field("Content-Type", "text/xml;charset=UTF-8")
# Set the envelope content obtained from the input to the request
request.body = input
# Execute the request and obtain the response
response = http.request(request)
# Parse the response to XML
doc = Nokogiri::XML(response.body ,&:noblanks)
s = doc.to_xml( indent:2, indent_text:" " )
# Populate reponse content to a variable to be displayed on the output area
params["xmlbody"] = s
else
# For any other action than invokeSavon / invokeHttpRequest default the input SOAP enveloper
params["inputpayload"] = ""+
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"> " + "\n"+
" <soap:Body> " + "\n"+
" <findRule xmlns=\"http://xmlns.oracle.com/apps/incentiveCompensation/cn/creditSetup/creditRule/creditRuleService/types/\"> " + "\n"+
" <findCriteria> " + "\n"+
" <fetchStart xmlns=\"http://xmlns.oracle.com/adf/svc/types/\">0</fetchStart> " + "\n"+
" <fetchSize xmlns=\"http://xmlns.oracle.com/adf/svc/types/\">-1</fetchSize> " + "\n"+
" <filter xmlns=\"http://xmlns.oracle.com/adf/svc/types/\"> " + "\n"+
" <group> " + "\n"+
" <upperCaseCompare>false</upperCaseCompare> " + "\n"+
" <item> " + "\n"+
" <upperCaseCompare>false</upperCaseCompare> " + "\n"+
" <attribute>RuleId</attribute> " + "\n"+
" <operator>=</operator> " + "\n"+
" <value>300000000851162</value> " + "\n"+
" </item> " + "\n"+
" </group> " + "\n"+
" </filter> " + "\n"+
" <excludeAttribute xmlns=\"http://xmlns.oracle.com/adf/svc/types/\">false</excludeAttribute> " + "\n"+
" </findCriteria> " + "\n"+
" <findControl> " + "\n"+
" <retrieveAllTranslations xmlns=\"http://xmlns.oracle.com/adf/svc/types/\">false</retrieveAllTranslations> " + "\n"+
" </findControl> " + "\n"+
" </findRule> " + "\n"+
" </soap:Body> " + "\n"+
"</soap:Envelope> " ;
end
end
end