Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 12.0.1 » SQL Anywhere Server - Programming » HTTP web services » HTTP web service examples » Tutorial: Using SQL Anywhere to access a SOAP/DISH service


Lesson 1: Setting up a web server to receive SOAP requests and send SOAP responses

In this lesson, you set up a new database server and create a SOAP service to handle incoming SOAP requests. The server anticipates SOAP requests that provide a Fahrenheit value that needs to be converted to Celsius.

 Set up a database server for receiving SOAP requests and sending SOAP responses
  1. Run the following command to create a SQL Anywhere database:

    dbinit ftc
  2. Start the network database server using the following command:

    dbsrv12 -xs http(port=8082) -n ftc ftc.db

    This command indicates that the HTTP web server should listen on port 8082 for requests. Use a different port number if 8082 is disallowed on your network.

  3. Connect to the database server in Interactive SQL using the following command:

    dbisql -c "UID=DBA;PWD=sql;SERVER=ftc"
  4. Create a new DISH service to accept incoming requests.

    Execute the following SQL statement in Interactive SQL:

    CREATE SERVICE soap_endpoint 
        TYPE 'DISH'
        SECURE OFF
        USER DBA;

    This statement creates a new DISH service named soap_endpoint that handles incoming SOAP service requests.

  5. Create a new SOAP service to handle Fahrenheit to Celsius conversions.

    Execute the following SQL statement in Interactive SQL:

        TYPE 'SOAP'
        FORMAT 'XML'
        USER DBA
        AS CALL FToCConvertor( :temperature );

    This statement creates a new SOAP service named FtoCService that generates XML-formatted strings as output. It calls a stored procedure named FToCConvertor when a web client sends a SOAP request to the service. You create the FToCConvertor procedure in the next step.

  6. Create the FToCConvertor procedure to handle incoming SOAP requests. This procedure performs the necessary calculations to convert a client-supplied Fahrenheit value to a Celsius value.

    Execute the following SQL statements in Interactive SQL:

    CREATE PROCEDURE FToCConvertor( temperature FLOAT )
        DECLARE hd_key LONG VARCHAR;
        DECLARE hd_entry LONG VARCHAR;
        DECLARE first_name LONG VARCHAR;
        DECLARE last_name LONG VARCHAR;
        DECLARE authinfo LONG VARCHAR;
        DECLARE namespace LONG VARCHAR;
        DECLARE mustUnderstand LONG VARCHAR; 
            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' );
                    -- 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;
                -- build a response header
                -- based on the pieces from the request header
                SET authinfo = 
                    XMLELEMENT( 'Authentication',
                            namespace as xmlns,
                            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;

    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.


    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 FToCConvertor 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 SQL Anywhere 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">

    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">

    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.

At this point, you now have a running SQL Anywhere web server that provides a service for converting temperatures from degrees Fahrenheit to degrees Celsius. This service processes a SOAP header from the client and sends a SOAP response back to the client. In the next section, you develop an example of a client that can send SOAP requests to the web server and receive SOAP responses from the web server.

 See also