บทที่ 3 SOAP-Based Web Services 3_1.pdf · SOAP: Simple Object Access Protocol...

8
1 ดร.ธีระยุทธ ทองเครือ ภาควิชาวิทยาการคอมพิวเตอร์ คณะวิทยาศาสตร์ มหาวิทยาลัยขอนแก่น บทที3 SOAP-Based Web Services SOAP-Based Web Services SOAP-Based Web Services เว็บเซอร์วิสแบบ SOAP (SOAP-Based Web Services) คือ ระบบทีออกแบบเพื่อการทํางานร วมก นระหว างเครื่อง (machine-to-machine interaction) บนเครือข าย ซึ ่งมีการอธิบายการ interface บระบบใน รูปแบบที่เครื่องเข้าใจด้วยเอกสาร WSDL และโต้ตอบก นด้วย SOAP- message บนโพรโตคอล HTTP อาจเรียกเว็บเซอร์วิสแบบนี าเป็น Classical Web Services หรือ Big Web Services และใช้สัญลักษณ์ย อๆว WS-* 2 องค์กรที่ใช้ WS-* ในประเทศไทย องค์กรที่ใช้ WS-* ในประเทศไทย กรมสรรพากร http://www.rd.go.th/publish/42546.0.html บริษัท ปตท. จํากัด (มหาชน) http://www.pttplc.com/webservice/pttinfo.asmx บริษัท ไปรษณีย์ไทย จํากัด http://track.thailandpost.co.th/TTPOSTWebService/TrackandTrace.asmx 3 ปแบบของเว็บเซอร์วิส ปแบบของเว็บเซอร์วิส Functional Service เว็บเซอร์วิสที่ให้บริการประมวลผลงานเฉพาะด้าน คํานวณภาษี ตรวจสอบความถูกต้องของหมายเลขบัตรประชาชน Generate Barcode แปลงค่าหน่วยต่างๆ Data Service เว็บเซอร์วิสที่ให้บริการข้อมูล เช่น บริการข้อมูลที่มีการเปลี่ยนแปลงตลอดเวลา เช่น ข่าว ค่าเงินสกุลต่างๆ ราคา นํ ามัน ราคาทอง บริการเพิ ลบ แก้ไขข้อมูลในฐานข้อมูล มักใช้ภายในองค์กร และต้องมีการ Authentication 4

Transcript of บทที่ 3 SOAP-Based Web Services 3_1.pdf · SOAP: Simple Object Access Protocol...

Page 1: บทที่ 3 SOAP-Based Web Services 3_1.pdf · SOAP: Simple Object Access Protocol ใชในการแลกเปล้ ี่ยนขอมู้ล WSDL: Web Services

1

ดร.ธีระยุทธ ทองเครือภาควชิาวทิยาการคอมพิวเตอร ์ คณะวทิยาศาสตร์

มหาวทิยาลยัขอนแกน่

บทที่ 3SOAP-Based Web Services

SOAP-Based Web ServicesSOAP-Based Web Servicesเวบ็เซอร์วสิแบบ SOAP (SOAP-Based Web Services) คือ ระบบที่

ออกแบบเพื่อการทาํงานร่วมกนัระหวา่งเครื่อง (machine-to-machine interaction) บนเครือข่าย ซึ่งมีการอธิบายการ interface กบัระบบในรูปแบบที่เครื่องเขา้ใจดว้ยเอกสาร WSDL และโตต้อบกนัดว้ย SOAP-message บนโพรโตคอล HTTP

อาจเรียกเวบ็เซอร์วิสแบบนี้วา่เป็น Classical Web Services หรือ Big Web Services และใชส้ญัลกัษณ์ยอ่ๆวา่ WS-*

2

องค์กรทีใ่ช้ WS-* ในประเทศไทยองค์กรทีใ่ช้ WS-* ในประเทศไทยกรมสรรพากร

http://www.rd.go.th/publish/42546.0.html

บริษทั ปตท. จาํกดั (มหาชน)http://www.pttplc.com/webservice/pttinfo.asmx

บริษทั ไปรษณียไ์ทย จาํกดั http://track.thailandpost.co.th/TTPOSTWebService/TrackandTrace.asmx

3

รูปแบบของเวบ็เซอร์วสิรูปแบบของเวบ็เซอร์วสิFunctional Service – เวบ็เซอร์วสิที่ใหบ้ริการประมวลผลงานเฉพาะดา้น คาํนวณภาษี ตรวจสอบความถูกตอ้งของหมายเลขบตัรประชาชน Generate Barcode แปลงค่าหน่วยต่างๆ

Data Service – เวบ็เซอร์วสิที่ใหบ้ริการขอ้มูล เช่น บริการขอ้มูลที่มีการเปลี่ยนแปลงตลอดเวลา เช่น ข่าว ค่าเงินสกลุต่างๆ ราคา

นํ้ามนั ราคาทอง บริการเพิ่ม ลบ แกไ้ขขอ้มูลในฐานขอ้มูล มกัใชภ้ายในองคก์ร และตอ้งมีการ

Authentication

4

Page 2: บทที่ 3 SOAP-Based Web Services 3_1.pdf · SOAP: Simple Object Access Protocol ใชในการแลกเปล้ ี่ยนขอมู้ล WSDL: Web Services

มาตรฐานหลกัมาตรฐานหลกัSOAP: Simple Object Access Protocol ใชใ้นการแลกเปลี่ยนขอ้มูล

WSDL: Web Services Description Language ใชใ้นการอธิบายการ interface กบัเวบ็เซอร์วสิ

UDDI: Universal Description, Discovery, and Integration ใชใ้นการคน้หาเวบ็เซอร์วสิ ปัจจุบนั UDDI ไม่เป็นที่นิยมแลว้

5

สถาปัตยกรรมสถาปัตยกรรมService consumer: ผูใ้ช ้serviceService provider: ผูส้ร้าง service (=server)Service registry: ศูนยก์ลางในการลงประกาศและคน้หา service

6

Service consumer คน้หา service ที่

ตอ้งการดว้ยรูปแบบ UDDI และ SOAP

Service provider ประกาศ service ที่สร้างดว้ยรูปแบบ UDDI และ SOAP

Service consumer เชื่องโยงกบั Serviceprovider ดว้ย SOAP

การประมวลผลการประมวลผล

7

1. เตรียมขอ้มูลที่ Service ตอ้งการ2. สร้างขอ้ความตามรูปแบบที่ Service กาํหนด3. ส่งคาํร้องไปยงั Service

1. รับขอ้ความที่ผูใ้ชส้่งมา2. ดึงขอ้มูลออกจากขอ้ความ3. ประมวลผล4. สร้างขอ้ความตอบกลบั5. ส่งขอ้ความตอบกลบั

ServiceConsumer

ServiceProvider

Request

Response

Consumer Processing Service Processing

SOAPSOAPSOAP (Simple Object Access Protocol) คือ มาตรฐานขอ้ความ ที่ใชส้าํหรับการ

ติดต่อสื่อสารและแลกเปลี่ยนขอ้มูลระหวา่ง Service provider และ consumer รูปแบบการส่งขอ้ความของ SOAP มี 2 รูปแบบ แบบทางเดียว (one-way หรือ stateless) Service Consumer ส่งขอ้ความส่งไปยงั Service

Provider โดยไม่ตอ้งรอการตอบกลบั

แบบสองทาง (request-response) Service Consumer และ Service Provider สามารถเป็นทั้งผูร้ับและผูส้่งขอ้ความตอบกลบัระหวา่งกนัได้

8

Page 3: บทที่ 3 SOAP-Based Web Services 3_1.pdf · SOAP: Simple Object Access Protocol ใชในการแลกเปล้ ี่ยนขอมู้ล WSDL: Web Services

ส่วนประกอบของ SOAP Messageส่วนประกอบของ SOAP Message SOAP Envelope คือ Root Element ของ SOAP Message ซึ่ง

ประกอบดว้ยส่วนหวั และส่วนเนื้อหา SOAP Header คือ ส่วนที่เกบ็ขอ้มูลเกี่ยวกบัการควบคุม ที่จะใชใ้น

การประมวลผลหรือใชบ้ริการเสริม เช่น การประมวลผล transaction, การกาํหนดความปลอดภยั ส่วน Header จะมีหรือไม่มีกไ็ด้

SOAP Body คือ ส่วนที่เกบ็ขอ้มูลจริง ที่ใชใ้นการเรียกใชเ้วบ็เซอร์วิส และเกบ็ผลลพัธ์ที่ไดจ้ากเวบ็เซอร์วิส ซึ่งกาํหนดโครงสร้างขอ้มูลดว้ย XML Schema จากเอกสาร WSDL ส่วน Body จะตอ้งมีเสมอ

SOAP Fault คือ คาํอธิบายขอ้ผดิพลาด ใชส้าํหรับส่งกลบัจากเวบ็เซอร์วสิเท่านั้น

SOAP Attachment คือ ขอ้มูลแบบ binary ที่แนบมากบัขอ้ความ

9

การแลกเปลีย่นข้อมูลของ SOAP Messageการแลกเปลีย่นข้อมูลของ SOAP Message

10

โครงสร้างของ SOAP Messageโครงสร้างของ SOAP Message

11

<soapenv:Envelopexmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">

<soapenv:Header><!‐‐ Transactions, priorites, etc. ‐‐>

</soapenv:Header> 

<soapenv:Body><!‐‐ Some content ‐‐>

</soapenv:Body>

</soapenv:Envelope> 

<Envelope>

<Header>

<Body>

<Fault>

โครงสร้างของ SOAP Messageโครงสร้างของ SOAP Message

12

Page 4: บทที่ 3 SOAP-Based Web Services 3_1.pdf · SOAP: Simple Object Access Protocol ใชในการแลกเปล้ ี่ยนขอมู้ล WSDL: Web Services

ข้อมูลทีอ่ยู่ใน SOAP Messageข้อมูลทีอ่ยู่ใน SOAP MessageSOAP Request – ใชส้่งคาํร้องขอใชเ้วบ็เซอร์วสิ ประกอบดว้ยชื่อ Service และ

ขอ้มูลนาํเขา้ ผูท้ี่สร้างคือผูใ้ชเ้วบ็เซอร์วสิ (Service consumer)

SOAP Response – ใชใ้นการส่งผลลพัธ์จากเวบ็เซอร์วสิ ซึ่งจะบรรจุขอ้มูลที่ไดจ้ากการประมวลผล ผูท้ี่สร้างคือผูใ้หบ้ริการเวบ็เซอร์วสิ (Service provider)

SOAP Fault – ใชใ้นการส่งขอ้ผดิพลาดจากการประมวล ผูท้ี่สร้างคือผูใ้หบ้ริการเวบ็เซอร์วสิ

13

ตวัอย่าง SOAP Requestตวัอย่าง SOAP Request

14

<soap:Envelopexmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"xmlns:web="http://www.webserviceX.NET/">

 <soap:Body>

<web:ConversionRate><web:FromCurrency>USD</web:FromCurrency><web:ToCurrency>THB</web:ToCurrency>

</web:ConversionRate></soap:Body>

</soap:Envelope> 

ชื่อ Serviceชื่อและค่า Parameter ที่จะส่งไปใหเ้วบ็เซอร์วสิ

ConversionRateFromCurrency

ToCurrencyConversionRateResult

ตวัอย่าง SOAP Responseตวัอย่าง SOAP Response

15

<soap:Envelopexmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema‐instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<soap:Body><ConversionRateResponse>

<ConversionRateResult>32.623</ConversionRateResult></ConversionRateResponse>

</soap:Body>

</soap:Envelope> 

ผลลพัธ์จากเวบ็เซอร์วสิ

ConversionRateFromCurrency

ToCurrencyConversionRateResult

SOAP Message บน HTTP RequestSOAP Message บน HTTP Request

16

POST /StockQuote HTTP/1.1Host: www.stockquoteserver.comContent‐Type: text/xml; charset="utf‐8"Content‐Length: nnnnSOAPAction: "Some‐URI"

<SOAP‐ENV:Envelopexmlns:SOAP‐ENV="http://schemas.xmlsoap.org/soap/envelope/"SOAP‐ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP‐ENV:Body>

<m:GetLastTradePrice xmlns:m="Some‐URI"><symbol>DIS</symbol>

</m:GetLastTradePrice></SOAP‐ENV:Body>

</SOAP‐ENV:Envelope> 

Page 5: บทที่ 3 SOAP-Based Web Services 3_1.pdf · SOAP: Simple Object Access Protocol ใชในการแลกเปล้ ี่ยนขอมู้ล WSDL: Web Services

SOAP Message บน HTTP ResponseSOAP Message บน HTTP Response

17

HTTP/1.1 200 OKContent‐Type: text/xml; charset="utf‐8"Content‐Length: nnnn

<SOAP‐ENV:Envelopexmlns:SOAP‐ENV="http://schemas.xmlsoap.org/soap/envelope/"SOAP‐ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/><SOAP‐ENV:Body>

<m:GetLastTradePriceResponse xmlns:m="Some‐URI"><Price>34.5</Price>

</m:GetLastTradePriceResponse></SOAP‐ENV:Body>

</SOAP‐ENV:Envelope> 

SOAP Header AttributeSOAP Header Attribute

Attribute คาํอธิบาย ค่าทีเ่ป็นไปได้env:role ระบุเสน้ทางที่จะไปต่อ ใชก้บัการส่ง

SOAP ไปยงัปลายทาง ซึ่งตอ้งผา่นหลายจุด

next none ultimateReceiver

env:mustUnderstand ระบุความตอ้งการใหเ้วบ็เซอร์วสิประมวลผลส่วน SOAP Header หรือไม่

1 – ใหป้ระมวลผล0 – ไม่ตอ้งประมวลผล

env:encodingStyle ระบุ Namespace สาํหรับแปลผลส่วน SOAP Header

URI

18

ตวัอย่าง SOAP Headerตวัอย่าง SOAP Header

19

<SOAP‐ENV:Envelopexmlns:SOAP‐ENV="http://schemas.xmlsoap.org/soap/envelope/"/>

<SOAP‐ENV:Header><t:Transaction xmlns:t="ws‐transactions‐URI" 

SOAP‐ENV:mustUnderstand="1">      57539 

</t:Transaction></SOAP‐ENV:Header>

<SOAP‐ENV:Body><m:GetLastTradePrice xmlns:m="Some‐URI">

<symbol>DEF</symbol></m:GetLastTradePrice>

</SOAP‐ENV:Body>

</SOAP‐ENV:Envelope> 

SOAP Header ทีม่กีาร AuthenticationSOAP Header ทีม่กีาร Authentication

20

<SOAP‐ENV:Envelopexmlns:SOAP‐ENV="http://schemas.xmlsoap.org/soap/envelope/"/>

<SOAP‐ENV:Header><wsse:Security SOAP‐ENV:mustUnderstand="1"><wsse:UsernameToken wsu:Id="User"><wsse:Username>engelen</wsse:Username><wsse:Password

Type="http://docs.oasis‐open.org/wss/2004/01/oasis‐200401‐wss‐username‐token‐profile‐1.0#PasswordDigest">

/u5faawcfIeve1yHCsdXAWyIlbU=</wsse:Password><wsse:Nonce>NDU0MGE5YjljYTUzYzAzZjA2MTc=</wsse:Nonce><wsu:Created>2006‐10‐26T12:27:37Z</wsu:Created>

</wsse:UsernameToken></wsse:Security>

</SOAP‐ENV:Header>

<SOAP‐ENV:Body> …   </SOAP‐ENV:Body></SOAP‐ENV:Envelope> 

Page 6: บทที่ 3 SOAP-Based Web Services 3_1.pdf · SOAP: Simple Object Access Protocol ใชในการแลกเปล้ ี่ยนขอมู้ล WSDL: Web Services

SOAP FaultSOAP FaultSOAP Fault ประกอบดว้ย Element ดงันี้ <faultcode> - ชื่อขอ้ผดิพลาด <faultstring> - ขอ้ความอธิบายขอ้ผดิพลาด <faultactor> - ระบุแหล่งที่เป็นตน้เหตุของขอ้ผดิพลาด (optional) <detail> - ขอ้ความผดิพลาดซึ่งเป็นรายละเอียดที่ไดจ้ากโปรแกรม

21

ค่าของ faultcodeค่าของ faultcode

22

Code ความหมายVersionMismatch Namespace ที่ระบุใน SOAP Message ใช ้version ไม่ตรงกนัMustUnderstand เกิดเมื่อมีการระบุส่วน SOAP Header โดยกาํหนด mustUnderstand

มีค่าเป็น 1 ซึ่งหมายถึงเวบ็ซอร์วสิตอ้งเขา้ใจส่วน Header แต่เวบ็เซอร์วสิไม่เขา้ใจ

DataEncodingUnknown ไม่เขา้ใจการเขา้รหสัของ SOAP MessageClient SOAP Message ไม่ถูกตอ้งตามรูปแบบ หรือไม่ไดใ้ส่ขอ้มูลตามที่

ระบุเอาไว ้เช่น ไม่ระบุรายละเอียดการรับรอง (authentication)Server เวบ็เซอร์วสิไม่สามารถประมวลผล SOAP Message ได้

ตวัอย่าง SOAP Fault: VersionMismatchตวัอย่าง SOAP Fault: VersionMismatch

23

<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap‐envelope">

<env:Header><env:Upgrade>

<env:SupportedEnvelope qname="soap1:Envelope" xmlns:soap1="http://www.w3.org/2003/05/soap‐envelope"/>

</env:Upgrade></env:Header>

<env:Body><env:Fault>

<env:Code><env:Value>env:VersionMismatch</env:Value></env:Code></env:Fault>

</env:Body>

</env:Envelope>

ตวัอย่าง SOAP Fault: MustUnderstandตวัอย่าง SOAP Fault: MustUnderstand

24

<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap‐envelope">

<env:Header><env:NotUnderstood qname="abc:ExtensionABC" 

xmlns:abc="http://example.org/2011/abc"/><env:NotUnderstood qname="xyz:ExtensionXYZ" 

xmlns:xyz="http://example.org/Martin"/></env:Header>

<env:Body><env:Fault><env:Code><env:Value>env:MustUnderstand</env:Value></env:Code></env:Fault>

</env:Body>

</env:Envelope>

Page 7: บทที่ 3 SOAP-Based Web Services 3_1.pdf · SOAP: Simple Object Access Protocol ใชในการแลกเปล้ ี่ยนขอมู้ล WSDL: Web Services

ตวัอย่าง SOAP Fault: Clientตวัอย่าง SOAP Fault: Client

25

<?xml version="1.0" encoding="UTF‐8"?><SOAP‐ENV:Envelope

xmlns:SOAP‐ENV="http://schemas.xmlsoap.org/soap/envelope/"xmlns:xsi="http://www.w3.org/1999/XMLSchema‐instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema">

<SOAP‐ENV:Body><SOAP‐ENV:Fault><faultcode xsi:type="xsd:string">SOAP‐ENV:Client</faultcode><faultstring xsi:type="xsd:string">

Failed to locate method (getWeatherUpdte) in class(WeatherUpdate) at /usr/local/weatherUpdate.py line 143.

</faultstring></SOAP‐ENV:Fault>

</SOAP‐ENV:Body>

</SOAP‐ENV:Envelope> 

ขั้นตอนการเรียกเวบ็เซอร์วสิขั้นตอนการเรียกเวบ็เซอร์วสิ

26

Service Consumer

client implementation

HTTP engine

client stub

SOAP engine

เรียก service เหมือนกบัฟังกช์นับนเครื่องปกติ

เรียก SOAP engine ใหส้ร้างSOAP message

บรรจุ SOAP Message ลงใน HTTP Message และส่งไปยงั Service Provider

Service Provider

service implementation

HTTP server

server stub

SOAP router

เรียกฟังกช์นัที่สร้างไว้

แปลขอ้ความ และเลือก stub ที่ตรงกบัความตอ้งการของผูใ้ช ้แลว้ส่งขอ้ความไปให้

รับขอ้มูลจาก HTTP message

รูปแบบของ SOAPรูปแบบของ SOAPRPC-style ใชส้่งขอ้ความเพื่อเรียกฟังกช์นับนเวบ็เซอร์วสิ ซึ่งมกัจะอยูใ่นรูปแบบ

synchronous โดยจะส่ง SOAP Request ที่มีชื่อฟังกช์นั และ Parameter ไปยงัเวบ็เซอร์วสิ และรอผลลพัธ์ในการประมวลผลเป็น SOAP Response

Document-style ใชส้่งขอ้ความที่มีลกัษณะเป็นข่าวสาร มีโครงสร้างขอ้มูลที่ซบัซอ้น การส่งอาจอยูใ่นรูปแบบ Synchronous หรือ Asynchronous กไ็ด้

27

รูปแบบของ SOAPรูปแบบของ SOAP

28

SOAP envelope

SOAP body

PurchaseOrderdocument-product item-quantity

SOAP envelope

SOAP body

Acknowledgementdocument-order id

SOAP envelope

SOAP bodymethod nameorderGoodsinput parameter 1product item

input parameter 2quantity

SOAP envelope

SOAP body

method return

return valueorder id

Document-style interaction

RPC-style interaction

Page 8: บทที่ 3 SOAP-Based Web Services 3_1.pdf · SOAP: Simple Object Access Protocol ใชในการแลกเปล้ ี่ยนขอมู้ล WSDL: Web Services

ชนิดข้อมูลของ SOAPชนิดข้อมูลของ SOAPขอ้มูลแบบ Struct – ใชช้นิดขอ้มูลเดียวกบั XML Schema

ขอ้มูลแบบ Array

29

ข้อมูลแบบ Arrayข้อมูลแบบ Arrayขอ้มูลที่เป็น Array จะระบุดว้ย Attribute ชื่อ arrayType เช่น

30

<myFavoriteNumbers SOAP-ENC:arrayType="xsd:int[2]"><number>3</number><number>4</number>

</myFavoriteNumbers>

<SOAP-ENC:Array SOAP-ENC:arrayType="xyz:Order[2]"><Order>

<Product>Apple</Product><Price>1.56</Price>

</Order><Order>

<Product>Peach</Product><Price>1.48</Price>

</Order></SOAP-ENC:Array>