260 likes | 517 Views
SOAP: Simple Object Access Protocol. 2007/10/30 최석훈. 목차. Distributed System SOAP 의 장점 SOAP 메시지 구조 Envelope Header Body SOAP 예제 – 달력 얻기 SOAP server SOAP client Fault element. 특정 포트 사용으로 방화벽에서 차단될 수 있음 일반적으로 위 기술 간 호출 불가능. JAVA RMI. DCOM. CORBA. Distributed System.
E N D
SOAP:Simple Object Access Protocol 2007/10/30 최석훈
목차 • Distributed System • SOAP의 장점 • SOAP 메시지 구조 • Envelope • Header • Body • SOAP 예제 – 달력 얻기 • SOAP server • SOAP client • Fault element
특정 포트 사용으로 방화벽에서 차단될 수 있음 • 일반적으로 위 기술 간 호출 불가능 JAVA RMI • DCOM CORBA Distributed System 여러 서버에 분산된 컴포넌트간 상호 호출 가능
플랫폼 및 언어에 종속되지 않고 쉽게 구현 및 사용 가능 XML 기반 SMTP 사용 HTTP 사용 • 플랫폼 독립적 • 인간 친화적 • 비동기식 처리로 • 한 방향 액션(퍼블리싱 등) • 처리 유용 방화벽 등의 방해 없이 거의 모든 시스템과 통신 가능 SOAP의 장점
SOAP 메시지 구조 <?XML VERSION="1.0" ENCODING="EUC-KR" ?> <Envelope> <!-- Header 부분: 생략 가능 --> <Header> … </Header> <!-- Body 부분: 필수 --> <Body> … </Body> </Envelope>
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
Header element • Header의 사용은 선택 사항 • SOAP 메시지를 받는 쪽에서 이를 어떻게 처리할 것인지를 기술 • 여러 개의 자식 엘리먼트를 포함할 수 있음 • 받는 쪽에서는 해석 가능한 Header의 자식 엘리먼트만 해석해 수행 • mustUnderstand 속성이 1로설정되어 있으면 반드시 해석해야 하고, 못하면 에러를 발생해야 함
Header element • actor attribute (role attribute in SOAP 1.2) • 해당 헤더를 어떤 수신자가 처리할 것인지 기술 <?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> <?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> <actor.com에서 처리한 결과 메시지> <actor.com에서 처리될 메시지>
Body element • 메시지 수신자에게 전달되어야 할 목적 정보 • 프로시저 요청 / 실행 결과 / 에러 등을 포함 • 최종 수신자가 정의한 네임스페이스를 써서 기술 • WSDL <binding …> <soap:body … namespace="http://ns"> • SOAP:Body <en:Envelope … xmlns:ns="http://ns"> <ns:SomeMethod> … </ns:SomeMethod>
SOAP 서버 • #!/usr/bin/env python • import sys, calendar • #Import the SOAP.py machinery • from SOAPpy import SOAP • CAL_NS = "http://uche.ogbuji.net/eg/ws/simple-cal" • class Calendar: • def getMonth(self, year, month): • return calendar.month(year, month) • def getYear(self, year): • return calendar.calendar(year) • server = SOAP.SOAPServer(("localhost", 8080)) • cal = Calendar() • server.registerObject(cal, CAL_NS) • print "Starting server..." • server.serve_forever()
SOAP 클라이언트 • import sys, httplib • SERVER_ADDR = '127.0.0.1' • SERVER_PORT = 8080 • CAL_NS = "http://uche.ogbuji.net/ws/eg/simple-cal" • BODY_TEMPLATE = """<SOAP-ENV:Envelope • xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" • xmlns:s="http://uche.ogbuji.net/eg/ws/simple-cal" • xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" • xmlns:xsd="http://www.w3.org/1999/XMLSchema" • SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> • <SOAP-ENV:Body> • <s:getMonth> • <year xsi:type="xsd:integer">%s</year> • <month xsi:type="xsd:integer">%s</month> • </s:getMonth> • </SOAP-ENV:Body> • </SOAP-ENV:Envelope>"""
SOAP 클라이언트(계속) • def GetMonth(): • year = 2001 • month = 12 • body = BODY_TEMPLATE % (year, month) • blen = len(body) • requestor = httplib.HTTP(SERVER_ADDR, SERVER_PORT) • requestor.putrequest("POST", "cal-server") • requestor.putheader("Host", SERVER_ADDR) • requestor.putheader("Content-Type", "text/plain; charset=\"utf-8\"") • requestor.putheader("Content-Length", str(blen)) • requestor.putheader("SOAPAction", "http://uche.ogbuji.net/eg/ws/simple-cal") • requestor.endheaders() • print 'SENDED MESSAGE---' • print body • print '-----------------\n' • requestor.send(body)
HTTP headers • Accept • Specifies which Internet media types are acceptable • Content-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 number • User-Agent • Contains information about the user agent (client) • SOAPAction • URI or empty string("") or NULL
SOAP 클라이언트(계속) • def GetMonth(): • year = 2001 • month = 12 • body = BODY_TEMPLATE % (year, month) • blen = len(body) • requestor = httplib.HTTP(SERVER_ADDR, SERVER_PORT) • requestor.putrequest("POST", "cal-server") • requestor.putheader("Host", SERVER_ADDR) • requestor.putheader("Content-Type", "text/xml; charset=\"utf-8\"") • requestor.putheader("Content-Length", str(blen)) • requestor.putheader("SOAPAction", "http://uche.ogbuji.net/eg/ws/simple-cal") • requestor.endheaders() • print 'SENDED MESSAGE---' • print body • print '-----------------\n' • requestor.send(body)
SOAP 클라이언트(계속) • (status_code, message, reply_headers) = requestor.getreply() • reply_body = requestor.getfile().read() • print 'RECEIVED MESSAGE---' • print "status code:", status_code • print "status message:", message • print "HTTP reply header:" • for header in reply_headers.headers: • print header, • print "HTTP reply body:\n", reply_body • print '-------------------' • if __name__ == "__main__": • GetMonth()
HTTP status code • Successful 2xx • 200 – OK / 204 – No Content • Redirection 3xx • 301 – Moved Permanently / 302 – Found • Client Error 4xx • 400 – Bad Request / 403 – Forbidden / 404 – Not Found • Server Error 5xx • 500 – Internal Server Error / 503 – Service Unavailable
Fault element • SOAP 메시지 처리 때 생기는 에러 정보를 처리 • <Body> 엘리먼트 안에 하나만 나타날 수 있음 • 다음 엘리먼트를 가질 수있음 • <faultcode> - 오류 코드 • <faultstring> - 오류 설명 • <faultactor> - 누가 오류를 발생시켰는지에 대한 정보 • <detail> - 오류에 대한 자세한 설명
Reference / Q&A • 석광진 외 역, 자바를 이용한 웹 서비스 구축. • 안진만 역, .NET XML 웹 서비스. • 김종민 외 저, POWER XML. • 송은지 외 저, 웹 서비스 컴퓨팅. • Quick reference to HTTP headers • The Python Web services developer: Python SOAP libraries • SOAP Version 1.2 Part 1: Messaging Framework (Second Edition) • images from gettyimages RF section