You are on page 1of 14

Calling Web Services from

Oracle
Presenter Raymond Jones
Company Intermountain Healthcare
Background We are implementing a
new ERP system (Peoplesoft) and have
decided to do all interfaces using web
services. Oracle SOA Suite will be used
to develop the web services. My team
will be calling these web services from
Oracle E-Business Suite.

Steps to call a web service


Get WSDL
Use soapUI to read WSDL
Create Oracle Wallet if calling secure
website
Use UTL_HTTP package to call web
service
Parse web service response using
oracle functions and xpath

WSDL
Web Services Description Language XML
document that is used to describe how a service
is called.
Tools such as soapUI are available that will read
the WSDL and provide the details needed to call
the service.
Example:
http://www.webservicex.net/stockquote.asmx?
WSDL

soapUI

A tool for processing WSDLs


Download version 4.5.1 (free version, not Pro version)
www.soapui.org
File -> New soapUI Project
Enter WSDL and click OK
Double click request
Save URL and XML to use when calling the web service
Configure proxy server if needed: File -> Preferences -> Proxy

Server
Note: Proxy server did not work for me because soapUI uses
NTLMv1 authentication and our proxy is using NTLMv2. A
workaround is to access WSDL in browser and then save to
local machine and then access that file from soapUI

Create Oracle Wallet


On Database server create Oracle
Wallet
Need to add a certificate for each
secure web server ie (https
connections)
See the following Oracle support
Document for steps needed to setup
Wallet: Configuring Wallet Manager
To Enable HTTPS Connections Via
UTL_HTTP.REQUEST [ID 169768.1]

Call web service using UTL_HTTP


CREATE OR REPLACE PACKAGE ihchr_web AS
PROCEDURE call_service
(p_url
IN
,p_xml_send_text
,p_xml_response
,p_error_message
END ihchr_web;

VARCHAR2
IN
XMLTYPE
OUT XMLTYPE
OUT VARCHAR2);

CREATE OR REPLACE PACKAGE BODY ihchr_web AS


FUNCTION call_service
(p_url
IN
VARCHAR2
,p_xml_send_text
IN
XMLTYPE
,p_xml_response
OUT XMLTYPE
,p_error_message OUT VARCHAR2
)
IS
v_http_req
UTL_HTTP.REQ;
v_http_resp
UTL_HTTP.RESP;
v_response_clob
CLOB;
v_text
VARCHAR2(32767);
v_location
VARCHAR2(255);
BEGIN
v_location := 'Set Oracle Wallet';
UTL_HTTP.SET_WALLET('file:/etc/ORACLE/WALLETS/oracle',password');
v_location := 'Set Proxy';
UTL_HTTP.SET_PROXY(user:password@server:port');
v_location := 'Establish connection to web server';
v_http_req := UTL_HTTP.BEGIN_REQUEST(p_url, 'POST', 'HTTP/1.0');
v_location := 'Set http request header';
UTL_HTTP.SET_HEADER(v_http_req, 'Content-Type', 'text/xml');
UTL_HTTP.SET_HEADER(v_http_req, 'Content-Length',
LENGTH(p_xml_send_text.GETSTRINGVAL()));

v_location := 'Writes text data in the HTTP request body';


UTL_HTTP.WRITE_TEXT(v_http_req, p_xml_send_text.GETSTRINGVAL());
v_location := 'Read the HTTP response';
v_http_resp := UTL_HTTP.GET_RESPONSE(v_http_req);
v_location := 'Put HTTP response into CLOB';
BEGIN
-- Initialize the CLOB.
DBMS_LOB.createtemporary(v_response_clob, FALSE);
LOOP --read in a chunk at a time so large responses will be handled properly
UTL_HTTP.READ_TEXT(v_http_resp, v_text, 32766);
DBMS_LOB.writeappend(v_response_clob, LENGTH(v_text), v_text);
END LOOP;
EXCEPTION
WHEN UTL_HTTP.end_of_body THEN -- this gets us out of above loop.
NULL;
END;
v_location := 'closing network connection';
UTL_HTTP.END_RESPONSE(v_http_resp);
v_location := 'convert response to XML';
IF DBMS_LOB.getlength(v_response_clob) > 0 THEN
p_xml_response := XMLTYPE(v_response_clob);
END IF;
DBMS_LOB.freetemporary(v_response_clob);

EXCEPTION
WHEN OTHERS THEN
p_error_message := 'Location=ihchr_web.call_service-'||v_location||chr(10)||
SUBSTR(SQLERRM,1,240);
END;
END ihchr_web;

Parsing XML
xpath
Namespace
Oracle functions

XPATH

XML Path Language is a query language for selecting nodes from


an XML document.
http://www.w3.org/TR/xpath has xpath documentation
http://www.w3schools.com/xpath/xpath_syntax.asp has xpath
examples
/* selects everything from the root node down
/bookstore/book/* select everything from book node down
/bookstore/book[1]/text() selects the value for the 1st book.
/stockquote/stock/price/text() selects the price value for the stock

XML namespace

Provide uniquely named elements and attributes in an XML


document
Not well documented for Oracle functions but are mandatory if
namespaces are included in XML documents such as SOAP
documents.
Example: xmlns:soap="http://www.w3.org/2003/05/soapenvelope" xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" xmlns:xsd=http://www.w3.org/2001/XMLSchema

Oracle functions to Parse


XML

EXTRACT
Syntax: xml_doc.EXTRACT(xpath,namespace).getvalue
Example:
v_xml_response.EXTRACT(/soap:store/book[1]/text(),xmlsn:soap
=http://www.w3.org).getstringval()
XMLSEQUENCE
Syntax: table(xmlsequence(extract(xml_doc,xpath,namespace)))
Example select count(*) from
table(xmlsequence(extract(xml_doc,xpath,namespace))) from
dual;
XMLTABLE
Syntax: xmltable(xmlnamespaces(url as namespace 1),xpath
PASSING xml_doc COLUMNS type PATH xpath

XMLTABLE example:
SELECT
seq, company,old_dept,bus_unit,new_dept
FROM
XMLTABLE(XMLNamespaces('http://schemas.xmlsoap.org/soap/envelope/'
as "env
,'http://intermountain.org/soa/utility/v1/IfsCrosswalk' as "ns0")
,'env:Envelope/env:Body/ns0:IfsCrosswalkResponse/ns0:Crossw
alkValues'
PASSING v_xml_response
COLUMNS
seq FOR ORDINALITY
, company VARCHAR2(100) PATH 'ns0:Source1'
, old_dept VARCHAR2(100) PATH 'ns0:Source2'
, bus_unit VARCHAR2(100) PATH 'ns0:Target1'
, new_dept VARCHAR2(100) PATH 'ns0:Target2'
) AS my_table
WHERE company = '105'
ORDER BY old_dept

You might also like