Presentation is loading. Please wait.

Presentation is loading. Please wait.

Back to Basics: Simple Database Web Services How to consume and publish web services with an Oracle database without getting a headache using an Application.

Similar presentations


Presentation on theme: "Back to Basics: Simple Database Web Services How to consume and publish web services with an Oracle database without getting a headache using an Application."— Presentation transcript:

1 Back to Basics: Simple Database Web Services How to consume and publish web services with an Oracle database without getting a headache using an Application Server, SOA etc SAGE Computing Services Customised Oracle Training Workshops and Consulting Chris Muir Oracle Consultant and Trainer Oracle ACE Director - Fusion Middleware

2 Photo thanks to Flickr.com under CC In three two easy parts (and a hard bit)

3 Agenda Part I: Understanding web services Part II: Consuming web services from the database Part III: Publishing web services from 11g

4 Agenda Part I: Understanding web services Part II: Consuming web services from the database Part III: Publishing web services from 11g

5 Agenda Part I: Understanding web services Part II: Consuming web services from the database Part III: Publishing web services from 11g

6 Part I: Understanding Web Services SOAP, WSDL, RPC Style, Document Style, XML, XML Namespaces, HTTP request/response.... blah blah blah blah blah blah Photo thanks to Flickr.com under CC

7 Photo thanks to B.G. - Oodwin & Andrea Flickr.com under CC SOAP vs REST Web Services

8 SOAP Web Services Defined Host: Web Web Service/bookings /employees /timesheets Internet HTTP Request/Response Endpoints Server Web Service

9 SOAP Web Services Defined Endpoint: Web Service Employees getAddress getName updateAddress XML Namespace: Operation Uniquely identified in an XML Namespace

10 SOAP Web Service API Styles: 1.Remote Procedure Call (RPC) 2.Document Photo thanks to Phil Flickr.com under CC

11 Remote Procedure Call (RPC) Style Host: Endpoint: Namespace: Operation: getName In: integeremployeeNumber Return: stringname In: stringnameCase Request: 1234 U Response: CHRISTOPHER MUIR Photo thanks to Flickr.com under CC

12 Remote Procedure Call (RPC) Style Request: 1234 L Photo thanks to Flickr.com under CC Response: christopher muir

13 Document Style Host: Endpoint: Namespace: Operation: getName In: XML-Schema getNameInput Return: XML-Schema getNameOutput Photo thanks to ARS

14 Document Style "In" XML Schema XML Schema: Photo thanks to Flickr.com under CC Request: 1234 M

15 Document Style "In + Return" XML Schema XML Schema: Photo thanks to Flickr.com under CC Response: Christopher Muir

16 SOAP Envelope Request:... Request XML payload... Response:... Response XML payload...

17 Web Service Description Language (WSDL) Photo thanks to Flickr.com under CC De facto standard for describing web services XML based + W3C standards based Publish web service API Assists consumer in assembling request Not actually required in web service calls Port types = grouped operations Bindings = data exchange protocol (eg. SOAP over HTTP) Ports = endpoints/services

18 WSDL example

19 01 02 23

20 Photo thanks to Flickr.com under CC Part II: Consuming Web Services from the Database utl_http utl_dbws

21 Consuming Web Services from the Database Database provides 2 packages for accessing web services – utl_http – low level http assembler – utl_dbws – high level web service package 3 rd (alternative) method: – Load Apache Common's Java HttpClient into db – Write Java program in db – Generate PL/SQL wrapper

22 utl_http Available as of Send/receive raw HTTP request/responses to external servers Advantages: – Simplistic – Installed (completely) in the database – Passed and returns a VARCHAR2 XML payload – Very easy if you know the XML payload structures – Doesn't require a WSDL at publisher's site – Good examples available on the internet – 3 rd party PL/SQL wrappers available (Tim Hall: soap_api) Disadvantages: – Low level with no smarts to support web services – Cryptic HTTP error messages – Oracle documentation is less than useful

23 utl_http example

24 01 PROCEDURE call_web_service(i_payload IN VARCHAR2, o_response OUT VARCHAR2) IS 02 v_http_req utl_http.req; 03 v_http_resp utl_http.resp; 04 v_part_response VARCHAR2(32767); 04 BEGIN 05 '); 06 utl_http.set_wallet('file:/oracle/owallets/', 'pwd'); v_http_req := utl_http.begin_request( 09 'http://www.sagecomputing.com.au/employees', 'POST', 'HTTP/1.1'); 10 utl_http.set_authentication(v_http_req, 'cmuir', 'pwd', 'Basic', TRUE); utl_http.set_header(v_http_req, 'Content-Type','text/xml'); 13 utl_http.set_header(v_http_req, 'Content-Length',length(i_payload)); 14 utl_http.set_header(v_http_req, 'SOAPAction','getName'); utl_http.write_text(v_http_req, i_payload); v_http_resp := utl_http.get_response(v_http_req); Add handler for HTTP error v_http_resp.status_code >= 500 <= BEGIN 21 LOOP 22 utl_http.read_text(v_http_resp, v_part_response); 23 o_response := o_response || v_part_response; 24 END LOOP; 25 EXCEPTION WHEN utl_http.end_of_body THEN 26 NULL; 27 END; 28 utl_http.end_response(v_http_resp); Add exception handler 30 END call_web_service;

25 01 DECLARE 02 v_request VARCHAR2(5000); 03 v_response VARCHAR2(5000); v_soap_req VARCHAR2(5000); 06 v_soap_resp VARCHAR2(5000); v_xml_resp XMLType; BEGIN 11 v_request := ' ' 12 || ' 1234 ' 13 || ' M ' 14 || ' '; v_soap_req := ' ' 17 || ' ' 18 || ' ' 19 || ' ' 20 || v_request 21 || ' ' 22 || ' '; call_web_service(v_soap_req, v_soap_resp); Strip the SOAP XML envelope from the response 27 v_xml_resp := XMLType.createXml(v_soap_resp); 28 v_response := v_xml_resp.extract( 29 '/soap:Envelope/soap:Body/child::node()' 30,'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"').getStringVal(); END;

26 Oracle 11g utl_http Security Caveats New: Network Access Control Lists (ACLs) Purpose: Fine grained host access Affects: utl_tcp, utl_http, utl_smtp, utl_mail, utl_inaddr Documentation: Oracle Database Security Guide 11gR1 Chapter 4 Simple Workaround: Metalink Note Photo thanks to |m Le' Flickr.com under CC

27 utl_dbws Available as of 10gR1 PL/SQL Java wrapper on oracle.jpub.runtime.dbws.DbwsProxy Part of JPublisher Advantages: – High(er) level web service handler Disadvantages: – Not completely installed by default (bah!) – Poor error reporting (in particular HTTP) – Queries external WSDL each request – Oracle documentation is dismal – Use of HttpUriType does not support wallets or proxies – Minor bugs in 10gR2 version with external authentication – Minor issues on calling.Net web services – Uses database JVM

28 Source: Tim Hall's blog Download: tech/java/jsp/dbws-callout-utility zip Extract: dbwsclientws.jar + dbwsclientdb11.jar -> ORACLE_HOME/sqlj/lib Execute: ORACLE_HOME/sql/lib/loadjava -u sys/password -r -v -f -genmissing -s -grant public dbwsclientws.jar dbwsclientdb11.jar utl_dbws Installation Photo thanks to Flickr.com under CC Slow to install & will produce class loading errors at end Consider reinstalling in separate schema from sys as per Metalink note:

29 utl_dbws example

30 01 DECLARE 02 v_namespace VARCHAR2(1000) := 'http://www.sagecomputing.com.au/emp'; 03 v_service_qname utl_dbws.qname := utl_dbws.to_qname(v_namespace,'employees'); 04 v_port_qname utl_dbws.qname := utl_dbws.to_qname(v_namespace,'employeesSoapHttpPort'); 05 v_operation_qname utl_dbws.qname := utl_dbws.to_qname(v_namespace,'getName'); v_service utl_dbws.service; 08 v_call utl_dbws.call; v_int_type utl_dbws.qname; 11 v_string_type utl_dbws.qname; v_request_params utl_dbws.anydata_list; -- RPC style only 14 v_response_anydata AnyData; -- RPC style only v_request_xmltype XmlType; -- Document style only 17 v_response_xmltype XmlType; -- Document style only BEGIN 20 v_service := utl_dbws.create_service( 21 HttpUriType('http://www.sage.com.au/employees?wsdl'), v_service_qname); v_call := utl_dbws.create_call(v_service, v_port_qname, v_operation_qname); utl_dbws.set_property(v_call, 'SOAPACTION_USE', 'TRUE'); 26 utl_dbws.set_property(v_call, 'SOAPACTION_URI', 'getName'); 27 utl_dbws.set_property(v_call, 'ENCODINGSTYLE_URI', 28 'http://schemas.xmlsoap.org/soap/encoding/'); 29 utl_dbws.set_property(v_call, 'OPERATION_STYLE', 'rpc'); 29 utl_dbws.set_property(v_call, 'OPERATION_STYLE', 'document'); Pick one

31 29 utl_dbws.set_property(v_call, 'OPERATION_STYLE', 'rpc'); v_int_type := utl_dbws.to_qname('http://www.w3.org/2001/XMLSchema', 'int'); 32 v_string_type := utl_dbws.to_qname('http://www.w3.org/2001/XMLSchema', 'string'); utl_dbws.add_parameter(v_call, 'employeeNumber', v_int_type, 'ParameterMode.IN'); 35 utl_dbws.add_parameter(v_call, 'nameCase', v_string_type, 'ParameterMode.IN'); 36 utl_dbws.set_return_type(v_call, v_string_type); v_request_params(0) := AnyData.convertNumber(1234); 39 v_request_params(1) := AnyData.convertVarchar('M'); v_response_anydata := utl_dbws.invoke(v_call, v_request_params); 42 dbms_output.put_line('Result = ' || v_response_anydata.accessVarchar2); utl_dbws.release_call(v_call); 45 utl_dbws.release_service(v_service); 46 END; 47 / Result = Christopher Muir

32 29 utl_dbws.set_property(v_call, 'OPERATION_STYLE', 'document'); v_string_type := utl_dbws.to_qname('http://www.w3.org/2001/XMLSchema', 'string'); utl_dbws.add_parameter(v_call, 'Request', v_string_type, 'ParameterMode.IN'); 34 utl_dbws.set_return_type(v_call, v_string_type); v_request_xmltype := XmlType(' ' 37 || ' ' 38 || ' 1234 ' 39 || ' U ' 40 || ' '); v_response_xmltype := utl_dbws.invoke(v_call, v_request_xmltype); dbms_output.put_line('Result = ' || v_response_xmltype.getStringVal()); utl_dbws.release_call(v_call); 47 utl_dbws.release_service(v_service); 48 END; 49 / Result = CHRISTOPHER MUIR

33 Photo thanks to Nicki's Flickr.com under CC

34 Photo thanks to Daquella Flickr.com under CC

35 Part III: Publishing Web Services from the Database Native Web Services Photo thanks to The Dilla Flickr.com under CC

36 11g Native Web Services Available as of RDBMS 11gR1 Provided through Oracle XML DB feature set Publishes: – SQL & XQuery query facility – Stored PL/SQL procedures and functions including packages SOAP 1.1 compliant, WSDL automatically generated Advantages: – Simple, no application server required Disadvantages: – No control over WSDL naming conventions and payload structures – Security concerns as web services are exposed directly from database layer

37 11g Native Web Services Installation Not enabled by default Requires: Oracle XML DB HTTP Server running (running by default) Servlet "orawsv" configuration - Oracle XML DB Developer's Guide 11gR1 chapter 33 Grant roles to schema exposing objects GRANT XDB_WEBSERVICES TO sage; GRANT XDB_WEBSERVICES_OVER_HTTP TO sage; Photo thanks to Flickr.com under CC

38 Native PL/SQL Web Services Publish any procedure, function or package module Support for parameters using most primitive datatypes and object types, not %rowtype Endpoint format: : /orawsv/ / : /orawsv/ / / WSDL format: : /orawsv/ / ?wsdl : /orawsv/ / / ?wsdl Example: Schema/package/module names must match database case name Requires HTTP basic authentication with schema username/pwd

39 Native PL/SQL Web Service "Example" FUNCTION get_name(employeeNumber IN NUMBER, nameCase IN VARCHAR2) RETURN VARCHAR2 AS CURSOR c_name IS SELECT decode(nameCase,'U',upper(name),'L',lower(name),initcap(name)) name FROM employees WHERE emp_no = employeeNumber; v_name VARCHAR2(100); BEGIN OPEN c_name; FETCH c_name INTO v_name; CLOSE c_name; RETURN v_name; END get_name;

40 Native PL/SQL Web Service "Request" Request 1234 U

41 Native PL/SQL Web Service "Response" Response CHRISTOPHER MUIR

42 Native Query Web Services Queries & DML supported on any objects accessible via schema Endpoint format: : /orawsv WSDL format: : /orawsv?wsdl Example: Requires HTTP basic authentication with schema username/pwd Takes a basic SQL string with bind parameters Returns XML structure containing data Number of parameters to influence result

43 Native Query Web Service "Request" Request 1234

44 Native Query Web Service "Response" Response 1234 Christoper Muir

45 Summary

46 SAGE Computing Services Customised Oracle Training Workshops and Consulting Questions and Answers? Presentations are available from our website:

47 utl_http References Tim Hall's blog base.com/articles/9i/ConsumingWebServices9i.php miscellaneous&file=soap_api.sql Oracle documentation 8419/u_http.htm#CHDIAFFA

48 utl_dbws References Tim Hall's blog Marc Kelderman's blog with-utldbws.html Stellan's blog net-web-services-from.html Henry Cortez on the OraFAQ forum Oracle 10g documentation 4258/u_dbws.htm#i

49 Native Web Services References Tim Hall's blog NativeOracleXmlDbWebServices_11gR1.php Paul Gallagher's blog native-web-services.html Marc Thompson's blog installation-and-native.html Oracle 11g documentation 8369/xdb_web_services.htm


Download ppt "Back to Basics: Simple Database Web Services How to consume and publish web services with an Oracle database without getting a headache using an Application."

Similar presentations


Ads by Google