CREATE OR REPLACE PROCEDURE FToCConverter( temperature FLOAT )
BEGIN
DECLARE hd_key LONG VARCHAR;
DECLARE hd_entry LONG VARCHAR;
DECLARE alias LONG VARCHAR;
DECLARE first_name LONG VARCHAR;
DECLARE last_name LONG VARCHAR;
DECLARE xpath LONG VARCHAR;
DECLARE authinfo LONG VARCHAR;
DECLARE namespace LONG VARCHAR;
DECLARE mustUnderstand LONG VARCHAR;
header_loop:
LOOP
SET hd_key = NEXT_SOAP_HEADER( hd_key );
IF hd_key IS NULL THEN
-- no more header entries
LEAVE header_loop;
END IF;
IF hd_key = 'Authentication' THEN
SET hd_entry = SOAP_HEADER( hd_key );
SET xpath = '/*:' || hd_key || '/*:userName';
SET namespace = SOAP_HEADER( hd_key, 1, '@namespace' );
SET mustUnderstand = SOAP_HEADER( hd_key, 1, 'mustUnderstand' );
BEGIN
-- parse the XML returned in the SOAP header
DECLARE crsr CURSOR FOR
SELECT * FROM OPENXML( hd_entry, xpath )
WITH ( alias LONG VARCHAR '@*:alias',
first_name LONG VARCHAR '*:first/text()',
last_name LONG VARCHAR '*:last/text()' );
OPEN crsr;
FETCH crsr INTO alias, first_name, last_name;
CLOSE crsr;
END;
-- build a response header
-- based on the pieces from the request header
SET authinfo =
XMLELEMENT( 'Authentication',
XMLATTRIBUTES(
namespace as xmlns,
alias,
mustUnderstand ),
XMLELEMENT( 'first', first_name ),
XMLELEMENT( 'last', last_name ) );
CALL SA_SET_SOAP_HEADER( 'authinfo', authinfo );
END IF;
END LOOP header_loop;
SELECT ROUND((temperature - 32.0) * 5.0 / 9.0, 5) AS answer;
END;
The NEXT_SOAP_HEADER function is used in a LOOP structure to iterate through all the header names in a SOAP request, and exits the loop when the NEXT_SOAP_HEADER function returns NULL.
Note:
This function does not necessarily iterate through the headers in the order that they appear in the SOAP request.
The SOAP_HEADER function returns the header value or NULL when the header name does not exist.
The FToCConverter procedure searches for a header named
Authentication and extracts the header structure,
including the @namespace and
mustUnderstand attributes. The
@namespace header attribute is a special
SAP Sybase IQ attribute used to access the namespace
(xmlns) of the given header entry.
The following is an XML string representation of a possible Authentication header structure, where the @namespace attribute has a value of "SecretAgent", and mustUnderstand has a value of 1:
<Authentication xmlns="SecretAgent" mustUnderstand="1">
<userName alias="99">
<first>Susan</first>
<last>Hilton</last>
</userName>
</Authentication>
The OPENXML system procedure in the SELECT statement parses the XML header using the XPath string "/*:Authentication/*:userName" to extract the alias attribute value and the contents of the first and last tags. The result set is processed using a cursor to fetch the three column values.
At this point, you have all the information of interest that was passed to the web service. You have the temperature in Fahrenheit degrees and you have some additional attributes that were passed to the web service in a SOAP header. You could look up the name and alias that were provided to see if the person is authorized to use the web service. However, this exercise is not shown in the example.
The SET statement is used to build a SOAP response in XML format to send to the client. The following is an XML string representation of a possible SOAP response. It is based on the above Authentication header structure example.
<Authentication xmlns="SecretAgent" alias="99" mustUnderstand="1">
<first>Susan</first>
<last>Hilton</last>
</Authentication>
The SA_SET_SOAP_HEADER system procedure is used to set the SOAP response header that will be sent to the client.
The final SELECT statement is used to convert the supplied Fahrenheit value to a Celsius value. This information is relayed back to the client.