26
SOAP: Simple Object Access Protocol 2007/10/30 최최최

2007/10/30 최석훈. 목차 I. Distributed System II. SOAP 의 장점 III. SOAP 메시지 구조 I. Envelope II. Header III. Body IV. SOAP 예제 – 달력 얻기 I. SOAP server

Embed Size (px)

Citation preview

Page 1: 2007/10/30 최석훈. 목차 I. Distributed System II. SOAP 의 장점 III. SOAP 메시지 구조 I. Envelope II. Header III. Body IV. SOAP 예제 – 달력 얻기 I. SOAP server

SOAP:Simple Object Access Protocol

2007/10/30최석훈

Page 2: 2007/10/30 최석훈. 목차 I. Distributed System II. SOAP 의 장점 III. SOAP 메시지 구조 I. Envelope II. Header III. Body IV. SOAP 예제 – 달력 얻기 I. SOAP server

목차I. Distributed SystemII. SOAP 의 장점III. SOAP 메시지 구조

I. EnvelopeII. HeaderIII. Body

IV. SOAP 예제 – 달력 얻기I. SOAP serverII. SOAP clientIII. Fault element

Page 3: 2007/10/30 최석훈. 목차 I. Distributed System II. SOAP 의 장점 III. SOAP 메시지 구조 I. Envelope II. Header III. Body IV. SOAP 예제 – 달력 얻기 I. SOAP server

Distributed System

JAVA RMIJAVA RMI

CORBACORBA

DCOMDCOM

• 특정 포트 사용으로 방화벽에서 차단될 수 있음• 일반적으로 위 기술 간 호출 불가능• 특정 포트 사용으로 방화벽에서 차단될 수 있음• 일반적으로 위 기술 간 호출 불가능

여러 서버에 분산된 컴포넌트간 상호 호출 가능

Page 4: 2007/10/30 최석훈. 목차 I. Distributed System II. SOAP 의 장점 III. SOAP 메시지 구조 I. Envelope II. Header III. Body IV. SOAP 예제 – 달력 얻기 I. SOAP server

SOAP 의 장점

• 플랫폼 독립적• 인간 친화적

플랫폼 및 언어에 종속되지 않고 쉽게 구현 및 사용 가능

방화벽 등의 방해 없이 거의 모든 시스템과 통신 가능

비동기식 처리로한 방향 액션 ( 퍼블리싱 등 )처리 유용

XML 기반 SMTP 사용

HTTP 사용

Page 5: 2007/10/30 최석훈. 목차 I. Distributed System II. SOAP 의 장점 III. SOAP 메시지 구조 I. Envelope II. Header III. Body IV. SOAP 예제 – 달력 얻기 I. SOAP server

SOAP 메시지 구조<?XML VERSION="1.0" ENCODING="EUC-KR" ?><Envelope> <!-- Header 부분 : 생략 가능 --> <Header> … </Header> <!-- Body 부분 : 필수 --> <Body> … </Body></Envelope>

Page 6: 2007/10/30 최석훈. 목차 I. Distributed System II. SOAP 의 장점 III. SOAP 메시지 구조 I. Envelope II. Header III. Body IV. SOAP 예제 – 달력 얻기 I. SOAP server

Envelope

Page 7: 2007/10/30 최석훈. 목차 I. Distributed System II. SOAP 의 장점 III. SOAP 메시지 구조 I. Envelope II. Header III. Body IV. SOAP 예제 – 달력 얻기 I. SOAP server

Envelope element루트 엘리먼트SOAP 메시지의 네임스페이스를 지정

http://schemas.xmlsoap.org/soap/envelope/http://www.w3.org/2003/05/soap-envelope/

encodingStyle attribute어떤 식으로 메시지를 구성할 것인지 명시http://schemas.xmlsoap.org/soap/encoding/http://www.w3.org/2003/05/soap-encoding

Page 8: 2007/10/30 최석훈. 목차 I. Distributed System II. SOAP 의 장점 III. SOAP 메시지 구조 I. Envelope II. Header III. Body IV. SOAP 예제 – 달력 얻기 I. SOAP server

Header

Page 9: 2007/10/30 최석훈. 목차 I. Distributed System II. SOAP 의 장점 III. SOAP 메시지 구조 I. Envelope II. Header III. Body IV. SOAP 예제 – 달력 얻기 I. SOAP server

Header elementHeader 의 사용은 선택 사항SOAP 메시지를 받는 쪽에서 이를 어떻게 처리할

것인지를 기술여러 개의 자식 엘리먼트를 포함할 수 있음받는 쪽에서는 해석 가능한 Header 의 자식

엘리먼트만 해석해 수행mustUnderstand 속성이 1 로 설정되어 있으면

반드시 해석해야 하고 , 못하면 에러를 발생해야 함

Page 10: 2007/10/30 최석훈. 목차 I. Distributed System II. SOAP 의 장점 III. SOAP 메시지 구조 I. Envelope II. Header III. Body IV. SOAP 예제 – 달력 얻기 I. SOAP server

Header elementactor attribute (role attribute in SOAP 1.2)

해당 헤더를 어떤 수신자가 처리할 것인지 기술<?XML VERSION="1.0" ENCODING="EUC-KR" ?><en:Envelope …><en:Header> <au:Authentication xmlns:au="http://..." en:actor="http://actor.com/auth" en:mustUnderstand="1"> <id>foo</id> <pw>1q2w3e</pw> </au:authentication></en:Header> …</Envelope>

<?XML VERSION="1.0" ENCODING="EUC-KR" ?><en:Envelope …><en:Header> <ao:AuthenticationOk xmlns:ao="http://..."> <name>Gildong, Hong.</name></ao:AuthenticationOk></en:Header> …</Envelope><actor.com 에서 처리한 결과 메시지 >

<actor.com 에서 처리될 메시지 >

Page 11: 2007/10/30 최석훈. 목차 I. Distributed System II. SOAP 의 장점 III. SOAP 메시지 구조 I. Envelope II. Header III. Body IV. SOAP 예제 – 달력 얻기 I. SOAP server

Body

Page 12: 2007/10/30 최석훈. 목차 I. Distributed System II. SOAP 의 장점 III. SOAP 메시지 구조 I. Envelope II. Header III. Body IV. SOAP 예제 – 달력 얻기 I. SOAP server

Body element메시지 수신자에게 전달되어야 할 목적 정보프로시저 요청 / 실행 결과 / 에러 등을 포함최종 수신자가 정의한 네임스페이스를 써서 기술

WSDL <binding …><soap:body … namespace="http://ns">

SOAP:Body<en:Envelope … xmlns:ns="http://ns"><ns:SomeMethod> … </ns:SomeMethod>

Page 13: 2007/10/30 최석훈. 목차 I. Distributed System II. SOAP 의 장점 III. SOAP 메시지 구조 I. Envelope II. Header III. Body IV. SOAP 예제 – 달력 얻기 I. SOAP server

SOAP 예제 – 달력 얻기

Page 14: 2007/10/30 최석훈. 목차 I. Distributed System II. SOAP 의 장점 III. SOAP 메시지 구조 I. Envelope II. Header III. Body IV. SOAP 예제 – 달력 얻기 I. SOAP server

SOAP 서버1. #!/usr/bin/env python2. 3. import sys, calendar4. 5. #Import the SOAP.py machinery6. from SOAPpy import SOAP7. 8. CAL_NS = "http://uche.ogbuji.net/eg/ws/simple-

cal"9. 10. class Calendar:11. def getMonth(self, year, month):12. return calendar.month(year, month)13. 14. def getYear(self, year):15. return calendar.calendar(year)16. 17. server = SOAP.SOAPServer(("localhost", 8080))18. cal = Calendar()19. server.registerObject(cal, CAL_NS)20. 21. print "Starting server..."22. server.serve_forever()

Page 15: 2007/10/30 최석훈. 목차 I. Distributed System II. SOAP 의 장점 III. SOAP 메시지 구조 I. Envelope II. Header III. Body IV. SOAP 예제 – 달력 얻기 I. SOAP server

SOAP 클라이언트1. import sys, httplib2. 3. SERVER_ADDR = '127.0.0.1'4. SERVER_PORT = 80805. CAL_NS = "http://uche.ogbuji.net/ws/eg/simple-cal"6. 7. BODY_TEMPLATE = """<SOAP-ENV:Envelope8. xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"9. xmlns:s="http://uche.ogbuji.net/eg/ws/simple-cal"10. xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"11. xmlns:xsd="http://www.w3.org/1999/XMLSchema"12. SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">13. <SOAP-ENV:Body>14. <s:getMonth>15. <year xsi:type="xsd:integer">%s</year>16. <month xsi:type="xsd:integer">%s</month>17. </s:getMonth>18. </SOAP-ENV:Body>19. </SOAP-ENV:Envelope>"""

Page 16: 2007/10/30 최석훈. 목차 I. Distributed System II. SOAP 의 장점 III. SOAP 메시지 구조 I. Envelope II. Header III. Body IV. SOAP 예제 – 달력 얻기 I. SOAP server

SOAP 클라이언트 ( 계속 )20. def GetMonth():21. year = 200122. month = 1223. body = BODY_TEMPLATE % (year, month)24. 25. blen = len(body)26. requestor = httplib.HTTP(SERVER_ADDR, SERVER_PORT)27. requestor.putrequest("POST", "cal-server")28. requestor.putheader("Host", SERVER_ADDR)29. requestor.putheader("Content-Type", "text/plain; charset=\"utf-8\"")30. requestor.putheader("Content-Length", str(blen))31. requestor.putheader("SOAPAction", "http://uche.ogbuji.net/eg/ws/simple-cal")32. requestor.endheaders()33. 34. print 'SENDED MESSAGE---'35. print body36. print '-----------------\n'37. 38. requestor.send(body)

Page 17: 2007/10/30 최석훈. 목차 I. Distributed System II. SOAP 의 장점 III. SOAP 메시지 구조 I. Envelope II. Header III. Body IV. SOAP 예제 – 달력 얻기 I. SOAP server

SOAP 클라이언트 ( 계속 )

Page 18: 2007/10/30 최석훈. 목차 I. Distributed System II. SOAP 의 장점 III. SOAP 메시지 구조 I. Envelope II. Header III. Body IV. SOAP 예제 – 달력 얻기 I. SOAP server

HTTP headersAccept

Specifies which Internet media types are acceptableContent-Length

Indicates the size (in octets) of the entity-body Content-Type

Specifies the Internet media type of the entity-body Host

Specifies the Internet host and port numberUser-Agent

Contains information about the user agent (client)SOAPAction

URI or empty string("") or NULL

Page 19: 2007/10/30 최석훈. 목차 I. Distributed System II. SOAP 의 장점 III. SOAP 메시지 구조 I. Envelope II. Header III. Body IV. SOAP 예제 – 달력 얻기 I. SOAP server

SOAP 클라이언트 ( 계속 )20. def GetMonth():21. year = 200122. month = 1223. body = BODY_TEMPLATE % (year, month)24. 25. blen = len(body)26. requestor = httplib.HTTP(SERVER_ADDR, SERVER_PORT)27. requestor.putrequest("POST", "cal-server")28. requestor.putheader("Host", SERVER_ADDR)29. requestor.putheader("Content-Type", "text/xml; charset=\"utf-8\"")30. requestor.putheader("Content-Length", str(blen))31. requestor.putheader("SOAPAction", "http://uche.ogbuji.net/eg/ws/simple-cal")32. requestor.endheaders()33. 34. print 'SENDED MESSAGE---'35. print body36. print '-----------------\n'37. 38. requestor.send(body)

Page 20: 2007/10/30 최석훈. 목차 I. Distributed System II. SOAP 의 장점 III. SOAP 메시지 구조 I. Envelope II. Header III. Body IV. SOAP 예제 – 달력 얻기 I. SOAP server

SOAP 클라이언트 ( 계속 )39. (status_code, message, reply_headers) = requestor.getreply()40. 41. reply_body = requestor.getfile().read()42. 43. print 'RECEIVED MESSAGE---'44. print "status code:", status_code45. print "status message:", message46. print "HTTP reply header:"47. for header in reply_headers.headers:48. print header,49. print "HTTP reply body:\n", reply_body50. print '-------------------'51. 52. if __name__ == "__main__":53. GetMonth()

Page 21: 2007/10/30 최석훈. 목차 I. Distributed System II. SOAP 의 장점 III. SOAP 메시지 구조 I. Envelope II. Header III. Body IV. SOAP 예제 – 달력 얻기 I. SOAP server

HTTP status codeSuccessful 2xx

200 – OK / 204 – No ContentRedirection 3xx

301 – Moved Permanently / 302 – FoundClient Error 4xx

400 – Bad Request / 403 – Forbidden / 404 – Not Found

Server Error 5xx500 – Internal Server Error /

503 – Service Unavailable

Page 22: 2007/10/30 최석훈. 목차 I. Distributed System II. SOAP 의 장점 III. SOAP 메시지 구조 I. Envelope II. Header III. Body IV. SOAP 예제 – 달력 얻기 I. SOAP server

SOAP 클라이언트 ( 계속 )

Page 23: 2007/10/30 최석훈. 목차 I. Distributed System II. SOAP 의 장점 III. SOAP 메시지 구조 I. Envelope II. Header III. Body IV. SOAP 예제 – 달력 얻기 I. SOAP server

Fault

Page 24: 2007/10/30 최석훈. 목차 I. Distributed System II. SOAP 의 장점 III. SOAP 메시지 구조 I. Envelope II. Header III. Body IV. SOAP 예제 – 달력 얻기 I. SOAP server

Fault elementSOAP 메시지 처리 때 생기는 에러 정보를 처리<Body> 엘리먼트 안에 하나만 나타날 수 있음다음 엘리먼트를 가질 수 있음

<faultcode> - 오류 코드<faultstring> - 오류 설명<faultactor> - 누가 오류를 발생시켰는지에 대한

정보<detail> - 오류에 대한 자세한 설명

Page 25: 2007/10/30 최석훈. 목차 I. Distributed System II. SOAP 의 장점 III. SOAP 메시지 구조 I. Envelope II. Header III. Body IV. SOAP 예제 – 달력 얻기 I. SOAP server

SOAP fault

Page 26: 2007/10/30 최석훈. 목차 I. Distributed System II. SOAP 의 장점 III. SOAP 메시지 구조 I. Envelope II. Header III. Body IV. SOAP 예제 – 달력 얻기 I. SOAP server

Reference / Q&A석광진 외 역 , 자바를 이용한 웹 서비스 구축 .안진만 역 , .NET XML 웹 서비스 .김종민 외 저 , POWER XML.송은지 외 저 , 웹 서비스 컴퓨팅 .Quick reference to HTTP headersThe Python Web services developer: Python S

OAP librariesSOAP Version 1.2 Part 1: Messaging Framewor

k (Second Edition)images from gettyimages RF section