Pages

Tuesday 25 September 2012

PL/SQL client to consume the GSIS (ΓΓΠΣ) web service

This is the code for a plsql client, using the UTL_DBWS package, that consumes the web service for legal entities' details offered by Greece's Ministry of Finance General Secretariat of Information Systems (GSIS-ΓΓΠΣ).

I understand that you might wonder why use plsql to consume a web service instead of using a more convenient / classic approach (e.g. java, php). Well , for those that either feel better with Oracle plsql or are just fans of Oracle forms or even better Oracle APEX, this could be an interesting alternative :

The following example has been successfully implemented on Oracle 10g ( 10.2.0.3.0) database , on windows 2003 server.

The first step is the appropriate setup in database in order to be able to call web services as described in http://www.oracle-base.com/articles/10g/utl_dbws-10g.php.

Note: When tested on 11g db I had to load the jar file into sys schema as well (or else i got error: oracle/jpub/runtime/dbws/DbwsProxy does not exist):
   loadjava -u sys/<password> -r -v -f -genmissing -s -grant public dbwsclientws.jar dbwsclientdb11.jar

If test code in above example works (SELECT add_numbers(1, 5) FROM dual;) then we know that the db has a working connection to internet and to http web services.

Create function gsis_user_data (p_afm IN varchar2, p_field IN varchar2) which returns the value the web service returned for field name "p_field" for the specific legal entity with AFM "p_afm".

CREATE OR REPLACE FUNCTION gsis_user_data (p_afm IN varchar2, p_field IN varchar2)
RETURN varchar2
AS
l_service UTL_DBWS.service;
l_call UTL_DBWS.call;

l_wsdl_url VARCHAR2(32767);
l_namespace VARCHAR2(32767);
l_service_qname UTL_DBWS.qname;
l_port_qname UTL_DBWS.qname;
l_operation_qname UTL_DBWS.qname;

l_xmltype_in SYS.XMLTYPE;
l_xmltype_out SYS.XMLTYPE;
l_return VARCHAR2(32767);

BEGIN
  l_wsdl_url        := 'http://www.gsis.gr/wsnp/RgWsBasStoixN_version2.wsdl'; 
 
  l_namespace       := 'http://gr/gsis/rgwsbasstoixn/RgWsBasStoixN.wsdl';

  l_service_qname   := UTL_DBWS.to_qname(l_namespace, 'RgWsBasStoixN'); 
  l_port_qname      := UTL_DBWS.to_qname(l_namespace, 'RgWsBasStoixNSoapHttpPort');
  l_operation_qname := UTL_DBWS.to_qname(l_namespace, 'rgWsBasStoixEpit'); 

  l_service := UTL_DBWS.create_service (
    wsdl_document_location => URIFACTORY.getURI(l_wsdl_url),
    service_name           => l_service_qname);

  l_call := UTL_DBWS.create_call (
    service_handle => l_service,
    port_name      => l_port_qname,
    operation_name => l_operation_qname);

    --in order to find the correct xml you can try the apex version that automatically generates soapenv, and adjust accordingly
  l_xmltype_in := SYS.XMLTYPE('<?xml version="1.0" encoding="utf-8"?>
    <ns1:rgWsBasStoixEpit xmlns:ns1="' || l_namespace || '"
                      xmlns:typ= "http://gr/gsis/rgwsbasstoixn/RgWsBasStoixN.wsdl/types/"
    >
      <pAfm>' || p_afm || '</pAfm>    
      <pBasStoixEpitRec_out>
            <typ:residenceParDescription></typ:residenceParDescription>
            <typ:assTxpActualAfm></typ:assTxpActualAfm>
            <typ:actLongDescr></typ:actLongDescr>
            <typ:postalZipCode></typ:postalZipCode>
            <typ:INiFlagDescr></typ:INiFlagDescr>
            <typ:registDate>2011-01-01</typ:registDate>
            <typ:stopDate>2011-01-01</typ:stopDate>
            <typ:parDescription></typ:parDescription>
            <typ:doyDescr></typ:doyDescr>
            <typ:residenceZipCode></typ:residenceZipCode>
            <typ:deactivationFlagDescr></typ:deactivationFlagDescr>
            <typ:legalStatusDescr></typ:legalStatusDescr>
            <typ:firmPhone></typ:firmPhone>
            <typ:firmFax></typ:firmFax>
            <typ:afm></typ:afm>
            <typ:facActivity>0</typ:facActivity>
            <typ:countOfBranches>0</typ:countOfBranches>
            <typ:deactivationFlag>1</typ:deactivationFlag>
            <typ:postalAddressNo></typ:postalAddressNo>
            <typ:firmFlagDescr></typ:firmFlagDescr>
            <typ:postalAddress></typ:postalAddress>
            <typ:doy></typ:doy>
            <typ:onomasia></typ:onomasia>
            <typ:commerTitle></typ:commerTitle>
         </pBasStoixEpitRec_out>
       <pCallSeqId_out>0</pCallSeqId_out>
       <pErrorRec_out>
            <typ:errorDescr></typ:errorDescr>
            <typ:errorCode></typ:errorCode>
       </pErrorRec_out>
    </ns1:rgWsBasStoixEpit>');

  l_xmltype_out := UTL_DBWS.invoke(call_Handle => l_call,
                                   request     => l_xmltype_in);
 
  UTL_DBWS.release_call (call_handle => l_call);
  UTL_DBWS.release_service (service_handle => l_service);

  select
     extractValue(value(t),'/*/' || p_field,'xmlns="http://gr/gsis/rgwsbasstoixn/RgWsBasStoixN.wsdl/types/"')
    into l_return
   from table(xmlsequence(extract(l_xmltype_out,'//pBasStoixEpitRec_out'))) t;  

  RETURN l_return;
END;
/

Call example:
SELECT gsis_user_data('099324100','onomasia') FROM dual;

Update for 11g :

Note: all commands of type: call dbms_java.grant_permission(...) , were NOT NEEDED. As stated in:
http://www.oracle-base.com/articles/11g/fine-grained-access-to-network-services-11gr1.php
Oracle 11g introduces fine grained access to network services using access control lists (ACL) in the XML DB repository, allowing control over which users access which network resources, regardless of package grants.
so:

    BEGIN
      DBMS_NETWORK_ACL_ADMIN.CREATE_ACL('local-access-users.xml',
                 'ACL that lets power users to connect to everywhere',   'GSIS', TRUE, 'connect');
      DBMS_NETWORK_ACL_ADMIN.ASSIGN_ACL('local-access-users.xml','*');
     COMMIT; --do not forget
    end;

    BEGIN
      DBMS_NETWORK_ACL_ADMIN.ADD_PRIVILEGE(acl       => 'local-access-users.xml',
                                           principal => 'GSIS',
                                           is_grant  => true,
                                           privilege => 'connect');
      COMMIT;
    END;
    /

    BEGIN
      DBMS_NETWORK_ACL_ADMIN.ADD_PRIVILEGE(acl       => 'local-access-users.xml',
                                           principal => 'GSIS',
                                           is_grant  => true,
                                           privilege => 'resolve');
      COMMIT;
    END;
    /

Update for  https/ssl call:
The instructions above were OK when the gsis service was plain http. For https the following additional steps were needed, according to metalink note 443438.1:

Import the CA certificate in it's cacerts certificate stores. To achieve this, perform the following :
    List the trusted certificates stored in the JVM's keystore:
        keytool -list -v -keystore $ORACLE_HOME/javavm/lib/security/cacerts -storepass changeit

    Get the CA's Certificate file from the server hosting the Web Service , with firefox as: export x509 certificate with chain (PEM)
        Saved e.g. in: /home/oracle/pz_web_services/www.gsis.gr_chain_pem   
    Attention:
        a) keytool does not acccept chain certificates of type PKCS#7
        b) The certificate files must be retrieved for both following urls :
            -https://www1.gsis.gr/wsgsis/RgWsBasStoixN/RgWsBasStoixNSoapHttpPort
            -http://www.gsis.gr/wsnp/RgWsBasStoixN_version2.wsdl
    Import all certificates in database keystore with keytool:
        keytool -import -trustcacerts -alias gsis -file /home/oracle/pz_web_services/www.gsis.gr_chain_pem -keystore $ORACLE_HOME/javavm/lib/security/cacerts -storepass changeit
        keytool -import -trustcacerts -alias verg3gsis1 -file /home/oracle/pz_web_services/VeriSignClass3InternationalServerCA-G3_pem -keystore $ORACLE_HOME/javavm/lib/security/cacerts -storepass changeit   
        keytool -import -trustcacerts -alias gsis1 -file /home/oracle/pz_web_services/www1.gsis.gr_single_pem -keystore $ORACLE_HOME/javavm/lib/security/cacerts -storepass changeit   
    Also, add the certificate to JPublisher's JRE and/or JDK keystores using the same command.
        keytool -import -trustcacerts -alias gsis -file /home/oracle/pz_web_services/www.gsis.gr_chain_pem -keystore $ORACLE_HOME/jre/1.4.2/lib/security/cacerts -storepass changeit
        as root user : keytool -import -trustcacerts -alias gsis -file /home/oracle/pz_web_services/www.gsis.gr_chain_pem -keystore $ORACLE_HOME/jdk/jre/lib/security/cacerts -storepass changeit
        as root user : keytool -import -trustcacerts -alias verg3gsis1 -file /home/oracle/pz_web_services/VeriSignClass3InternationalServerCA-G3_pem -keystore $ORACLE_HOME/jdk/jre/lib/security/cacerts -storepass changeit
        as root user : keytool -import -trustcacerts -alias gsis1 -file /home/oracle/pz_web_services/www1.gsis.gr_single_pem -keystore $ORACLE_HOME/jdk/jre/lib/security/cacerts -storepass changeit
       
    Once the certificates have been successfully added to the keystores, restart your database.
    Now test with "Call example" created above.

References:
http://www.gsis.gr/wsnp/wsnp_ver2.html
https://www.gsis.gr/wsnp/RgWsBasStoixN_version2.wsdl
https://www1.gsis.gr/wsgsis/RgWsBasStoixN/RgWsBasStoixNSoapHttpPort
http://serafeimk.blogspot.gr/2009/05/call-web-services-plsql.html
http://www.oracle-base.com/articles/misc/utl_http-and-ssl.php
http://www.oracle-base.com/articles/11g/fine-grained-access-to-network-services-11gr1.php
Metalink note: How To Invoke A Web Service from the DBWS Callout Utility over SSL / HTTPS [ID 443438.1]:

Σύνδεση με Oracle Apex σε HTTPS (SSL) web service με self-signed πιστοποιητικά

Ακολουθούν τα βήματα που χρειάστηκε να ακολουθήσω σε Oracle 10g ( 10.2.0.3.0) database , σε windows 2003 server, με APEX 4.0.1.00.03 και Oracle HTTP Server (Apache 2.0) (10.1.3.3.0):

Προφανώς πρέπει να επιβεβαιώσουμε, πρώτα, ότι ο server που έχει τη βάση και το APEX επικοινωνεί με το internet και το https url του web service χωρίς πρόβλημα.


Δημιουργία wallet και self-signed πιστοποιητικών για τον server srv01, χρησιμοποιώντας το oracle wallet από το home του Oracle HTTP Server (λογικά δε θα έχει διαφορά αν χρησιμοποιηθεί το αντίστοιχο home της βάσης) ως εξής: 

D:\oracle\oias1013\BIN\orapki wallet create -wallet D:\oracle\wallet -auto_login

D:\oracle\WALLET> D:\oracle\oias1013\BIN\orapki wallet add -wallet D:\oracle\wallet -dn "CN=srv01, OU=a1, O=a11, L=a111, ST=a1111, C=GR" -keysize 1024 -self_signed -validity 3650

orapki wallet display -wallet D:\oracle\wallet

Προσοχή: a) κρατήστε το password που θα σας ζητηθεί γιατί θα χρειαστεί αργότερα, έστω: password123.
b) το CN στην πρώτη εντολή να είναι το fully qualified name του server που έχετε το Wallet και τη βάση του APEX.

Για να επικοινωνήσει σωστά το APEX με το web service που θέλετε θα πρέπει , το wallet, να περιέχει όλη την αλυσίδα πιστοποιητικών της σελίδας που θα καλέσετε. Για ένα ενδεικτικό web service (σε επόμενο post θα περιγράψω συγκεκριμένο παράδειγμα για το web service της ΓΓΠΣ, για μη φυσικά πρόσωπα) https://mywebservice.gr/test.wsdl θα αποθηκεύσετε τοπικά στο δίσκο τα πιστοποιητικά ως εξής :
  1. θα πρέπει να πάτε με τον browser στη σελίδα https://mywebservice.gr/test.wsdl 
  2. Εικονίδιο «Αναφορά ασφάλειας» -> Προβολή Πιστοποιητικών
  3. Διαδρομή πιστοποίησης
  4. Επιλογή: 
    •  VeriSign  -> Προβολή Πιστοποιητικού -> Λεπτομέρειες -> Αντιγραφή σε αρχείο: 1_ver.cer . Χρησιμοποιήστε την επιλογή  [Base-64 encoded X.509 (.CER)] .
    •        ... : 2_ver_class3.cer 
    •        : 3_test.cer
 Όλα τα πιστοποιητικά αυτά θα πρέπει να εισαχθούν στο στο wallet π.χ. χρησιμοποιώντας το gui

Start Menu\Programs\Oracle - oias1013\Integrated Management Tools\Wallet Manager :
    1. Ανοίγετε το wallet που δημιουργήσατε παραπάνω στο D:\oracle\wallet
    2. Right click : Trusted Certificates -> Import Trusted Certificate -> Select the file you saved earlier
    3. Επαναλαμβάνετε για κάθε ένα από τα πιστοποιητικά της αλυσίδας
    4. Αποθήκευση
      Ένας πρώτος έλεγχος (as system , sys) για το αν λειτουργεί το wallet είναι:
      select utl_http.request('https://support.oracle.com', NULL,'file:d:\oracle\wallet','password123') from dual;

      Ρύθμιση του wallet in apex:
      • Login internal (default user: admin)
      • Manage service -> Instance settings
      • Wallet path: file:d:\oracle\wallet
      • Apply
      • Επανεκκίνηση βάσης (όχι απαραίτητο)
      Τεστ web service in apex: Δημιουργία web service reference ως εξής:
      • Δημιουργήστε ένα νέο workspace (ή κάντε login σε αυτό που ήδη χρησιμοποιείτε)
      • Δημιουργήστε νέο application
      • Επιλέξτε το application και πατήστε Shared Components
      • Επιλέξτε Web Service References 
      • Πατήστε Create
      • -> Next 
      • search a UDDI registry to find the WSDL? : ΝΟ   -> Next 
      • WSDL Location: http://www.oracle-base.com/webservices/server.php?wsdl (αν είναι https βάζετε το αντίστοιχο url) -> Create reference (για το συγκεκριμένο δε χρειάζεται να φτιάξετε ξεχωριστή φόρμα/report)
      • Επιλέγετε ξανά Web Service References . Προσοχή , επιλέξτε report view για να δείτε τη λίστα με τα service references.
      • Στο Reference που μόλις δημιουργήστε , πατήστε το εικονίδιο στη στήλη "Test" 
      • Select operation "ws_add" -> Δώστε τους αριθμούς -> Πατήστε "Τεστ " και δείτε την απάντηση του web service

      Αναφορές:
      http://blog.whitehorses.nl/2010/05/27/access-to-https-via-utl_http-using-the-orapki-wallet-command/
      http://serafeimk.blogspot.gr/2009/05/call-web-services-plsql.html