The Cricket Location

Download Report

Transcript The Cricket Location

Beginning 자바 웹 서비스
웹 서비스 기술
강미란([email protected])
Cyber-Infrastructure Research Lab
Konkuk University
목차
• HTTP
• WSDL
– 웹 서비스를 사용하는 Client 측 개발자를 위
해 웹 서비스를 어떻게 기술할 것인가
– WSDL 파일로부터 Java Class 생성
The HTTP Protocol (1)
• HTTP 는 TCP/IP의 Application 계층에 속한다
• HTTP client 는 TCP 방식으로 HTTP 서버와
통신한다
• 서버와 연결된 후, client는 서버에 HTTP 요청
을 전송한다
POST /item HTTP/1.1
Host: 189.123.345.239
Content-Type: text/plain
Content-Length: 200
The HTTP Protocol (2)
• Client로 부터 HTTP 요청을 받으면, 서버는 요청내
용을 처리하고 HTTP 응답을 client에 보낸다
• HTTP 응답은 요청에 대한 처리결과를 나타내는
상태코드를 포함한다
200 OK
Content-Type: text/plain
Content-Length: 200
200 - 서버가 페이지를 표시했습니다.
404 - 요청한 페이지가 없습니다.
503 - 서버를 일시적으로 사용하실 수 없습니다.
• 위 예제에서 상태코드는 200 이다
• 이는 HTTP 요청이 정상 처리되었음을 나타낸다
The HTTP Protocol (3)
• HTTP 상태 코드
– http://support.microsoft.com/kb/943891
– 1 xx - 조건부 응답
• Client에게 계속 진행할 것을 요구하는 상태 코드입니다.
– 2 xx - 성공
• 서버가 요청을 성공적으로 처리했음을 의미합니다.
– 3 xx - 리디렉션
• 요청을 완료하려면 추가 조치가 필요합니다.
• 이 상태 코드는 일반적으로 리디렉션에 사용됩니다. 리디렉션 < 5
– 4 xx - 클라이언트 오류
•
요청에 오류가 발생하여 서버가 이 요청을 처리하지 못할 가능성이 높
음을 나타냅니다.
– 5 xx - 서버 오류
• 이 상태 코드는 서버가 요청을 처리하려 할 때 내부 오류가 발생했음을
나타냅니다. 이 오류는 주로 요청이 아니라 서버와 관련이 있습니다.
SOAP HTTP Binding
• HTTP + XML = SOAP
• SOAP 요청은 HTTP POST나 HTTP GET 방
식으로 전송된다
• The HTTP POST 는 두개 이상의 HTTP
headers를 정의한다
– Content-Type와 Content-Length
Content-Type
• SOAP 요청/응답에서의 Content-Type header
는 메시지의 MIME (Multipurpose Internet
Mail Extensions)타입과 XML body에서 사용
되는 character encoding (optional) 을 명시
한다
• 문법
Content-Type: MIMEType; charset=character-encoding
• 예제
POST /item HTTP/1.1
Content-Type: application/soap+xml; charset=utf-8
Content-Length
• SOAP 요청/응답에서의 Content-Length
header 는 body 의 byte 수를 명시한다
• 문법
Content-Length: bytes
• 예제
POST /item HTTP/1.1
Content-Type: application/soap+xml; charset=utf-8
Content-Length: 250
SOAP 예제
• StockQuote 예제에서, 서버에 전송되는
getQuote 서비스에 대한 요청
• SOAP 요청은 “ticker ”parameter를 전송하
고 서버로부터 그에 대한 Price를 받게 된
다
• getQuote 서비스에 대한 네임스페이스는
"http://www.stock.org/stock" 이다
The SOAP request
POST /InStock HTTP/1.1
Host: www.stock.org
Content-Type: application/soap+xml; charset=utf-8
Content-Length: nnn
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope“
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Body xmlns:m="http://www.stock.org/stock">
<m:getQuote>
<m:ticker>IBM</m:ticker>
</m:getQuote>
</soap:Body>
</soap:Envelope>
A SOAP response
HTTP/1.1 200 OK
Content-Type: application/soap; charset=utf-8
Content-Length: nnn
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope“
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Body xmlns:m="http://www.stock.org/stock">
<getQuoteReturn xsi:type="xsd:string">69.01</getQuoteReturn>
</soap:Body>
</soap:Envelope>
WSDL의 필요성 (1)
• XML 문서는 데이터에 대한 의미를 포함한다
• XML 형식의 SOAP 메시지도 해석 가능하다
12
WSDL의 필요성 (2)
 서비스 요청자 관점에서,
 예제:
◦ 사용자가 POSubmission 서비스를 호출하고자 한
다
◦ 어떤 메시지를 보내야 할까?
 SOAP 에 대해 이미 알고 있을 수는 있지만 이는 메시지
전송 프로토콜일뿐, 실질적인 메시지에 대해서는 알지 못
한다
 SOAP envelop 내부 body에 포함할 데이터에 대해서 알
지 못한다
 SOAP Envelope 내 body에 들어갈 내용에 대한 포맷에
대해서도 알수 없다
13
WSDL 개요
• WSDL
– Web Services Description Language
• WSDL 은 XML문서이다
• WSDL 는 Web services를 설명한다
WSDL 문서 구조
<definitions>
<types>
definition of
</types>
<message>
definition of
</message>
<portType>
definition of
</portType>
<binding>
definition of
</binding>
<service>
definition of
</service>
</definitions>
types........
a message....
a port.......
a binding....
a service....
WSDL 구조
• 추상적 정의와 구체적인 바인딩이 분리되어 기술
– 추상적 요소는 binding 요소를 통해 SOAP과 같은 실제 프로토콜에
연결
• 이런 구조는 WSDL의 재사용을 목적
– 한 WSDL 정의에서 다른 WSDL 정의를 사용할 수 있다.
– 추상적 요소와 바인딩 요소가 다른 WSDL 요소와 매핑됨
• 추상적 정의(abstract definition)
–
–
–
–
types : WSDL 안에서 쓰이는 데이터 타입
message : 메소드 호출을 위한 메소드의 매개변수
operation : 서비스 호출을 위한 메소드 용법
portType : operation의 집합
• 구체적인 바인딩(concrete binding)
– binding : 메소드 호출을 위한 전송 프로토콜
– port : 웹 서비스 위치
– service : port의 집합
<definitions> 요소
• WSDL 정의에서 최상위 요소
• 참조되는 요소를 구별하기 위해 네임스페이
스를 지정
• 전형적인 <definitions> 요소의 예
<wsdl:definitions
targetNamespace="http://localhost:8080/axis/services/
StockQuote"
xmlns:apachesoap="http://xml.apache.org/xml-soap"
xmlns:impl="http://localhost:8080/axis/services/StockQ
uote"
xmlns:intf="http://localhost:8080/axis/services/StockQu
ote"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/enco
ding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soa
p/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
WSDL 접두사와 네임스페이스
Prefix
네임스페이스 URI
정의
wsdl
http://www.w3.org/2003/03/w
sdl
표준 XML 스키마
soap12
http://www.w3.org/2003/01/w
sdl/soap12
SOAP1.2 네임스페이스
http
http://www.w3.org/2003/01/w
sdl/http
WSDL HTTP GET & POST 바인
딩에 대한 WSDL 네임스페이스
mime
http://www.w3.org/2003/01/w
sdl/mime
WSDL MIME 바인딩에 대한
WSDL 네임스페이스
xsd
http://www.w3.org/2001/XML
Schema
XSD에서 정의한 스키마
네임스페이스
xsi
http://www.w3.org/2001/XML
Schema-instance
XSD에서 정의한 인스턴스
네임스페이스
tns
사용자 정의
현재 WSDL에서 정의한 요소를
위한 네임스페이스
<message> 요소
• <message> 요소는 웹 서비스에서 사용되는 메시지
에 대한 추상적인 요소를 표현한다.
• 실제 구현은 <binding> 요소를 사용하여 정의
• <message> 요소는 하나 이상의 자식 요소인
<part>를 사용한다.
• <part> 요소
– <types>에 정의한 데이터 타입을 참조
– 직접 XML 스키마의 데이터 타입을 참조할 수 있다.
– <part>의 속성
• element : XML 스키마를 이용하여 정의된 요소(element)를 참조
할 때 사용
• type : XML 스키마를 이용하여 정의된 타입(simple type,
complex type)을 참조할 때 사용
<portType> <operation>
• <portType>은 웹 서비스의 인터페이스를 정
의하는 부분이다.
• <portType>은 하나 이상의 <operation>을
포함한다.
• <operation>은 한 개의 메소드를 정의한다.
• <operation>의 <input>, <output>은 각각
메소드의 매개변수와 반환값을 정의한다.
– <message>에서 정의한 메시지를 매개변수와 반
환값으로 지정한다.
Operation의 4가지 형태
• One-way
– 오퍼레이션이 요청을 받을 수 있으나 응답을 보내지
않는다.
• Request-Response
– 오퍼레이션이 요청을 받고 응답을 보낸다.
– 가장 많이 사용
• Solicit-Response
– 오퍼레이션이 요청을 보내고 응답을 기다린다.
– CORBA나 RMI의 call-back에 해당
• Notification
– 오퍼레이션이 요청을 보내고 응답을 기다리지 않는다.
Request-Response의 예
•
Java의 다음 메소드를 WSDL로 표현
public String getQuote(String ticker); // Java
<wsdl:message name="getQuoteResponse">
<wsdl:part name="getQuoteReturn" type="xsd:string"/>
</wsdl:message>
<wsdl:message name="getQuoteRequest">
<wsdl:part name="ticker" type="xsd:string"/>
</wsdl:message>
<wsdl:portType name="StockQuote">
<wsdl:operation name="getQuote" parameterOrder="ticker">
<wsdl:input message="impl:getQuoteRequest" name="getQuoteRequest"/>
<wsdl:output message="impl:getQuoteResponse" name="getQuoteResponse"/>
</wsdl:operation>
</wsdl:portType>
바인딩(binding)
• 바인딩 : 인터페이스 정의와 실제 프로토콜을 매핑시키는 과
정
• <binding> 요소를 사용하여 <portType>에서 정의한
operation과 메시지를 실제 전송 프로토콜과 매핑시킨다.
• <binding>요소가 포함하는 요소
–
–
–
–
<soap:binding>
<soap:operation>
<input>, <output>
<soap:body>
• 바인딩 프로토콜
– SOAP
– HTTP
– MIME
SOAP 바인딩의 예
<wsdl:binding name="StockQuoteSoapBinding"
type="impl:StockQuote">
<wsdlsoap:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getQuote">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="getQuoteRequest">
<wsdlsoap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/
" namespace="http://ws.yust.edu" use="encoded"/>
</wsdl:input>
<wsdl:output name="getQuoteResponse">
<wsdlsoap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/
"
namespace="http://localhost:8080/axis/services/StockQuote"
use="encoded"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<SOAP:binding>
• SOAP 메시지 타입과 전송 프로토콜을 정의한다.
• SOAP 메시지 타입
– style 속성으로 정의
• rpc와 literal
– rpc 메시지 타입
• WSDL에서 정의한 메소드 이름과 매개변수는 RPC를 위한 SOAP
메시지 형태로 변환
– literal 메시지 타입
• SOAP 메시지가 문서 타입으로 기본값
• WSDL에서 정의한 메시지를 XML 형태 그대로 SOAP 바디에 담
는다.
• 전송 프로토콜
– transport 속성으로 전송 프로토콜 지정
– 앞의 예제에서 전송 프로토콜로 HTTP 지정
<soap:binding style=“rpc”
transport=“http://schemas.xmlsoap.org/soap/http”/>
<SOAP:operation>
• style과 soapAction 두 개의 속성이 있다.
• style 속성
– <SOAP:binding>의 style 속성과 동일
– <SOAP:operation>에 style 속성이 없으면
<SOAP:binding>의 style 속성을 물려받음
• soapAction 속성
– SOAP 메시지의 전송 프로토콜로 HTTP가 사용될 때 HTTP
헤더값인 SOAPAction 헤더 값을 정하기 위해 사용
– HTTP 이외에 다른 프로토콜이 사용되면 이 속성을 사용할
수 없다.
<soap:operation
soapAction=“http://ws.yust.edu/StockQuote”/>
<SOAP:input/output>
• WSDL에서 정의한 내용이 어떻게 인코딩되어
SOAP 바디에 나타날지 정의한다.
• use 속성
• encodingStyle 속성
<soap:body
encodingStyle=“http://schemas.xmlsoap.org/soa
p/encoding”
namespace=“http://ws.yust.edu/StockQuote”
use=“encoded”/>
<service>
• <service>의 자식 요소인 <port>는 웹 서비스가
제공되는 주소를 알려준다.
• <port> 요소는 여러 개 정의가 가능
– 한 웹 서비스가 여러 포트를 통해서 접근이 가능하다.
• 각각의 <port>는 <binding>에서 기술했던 바인
드 형에 해당한다.
<wsdl:service name="StockQuoteService">
<wsdl:port binding="impl:StockQuoteSoapBinding"
name="StockQuote">
<wsdlsoap:address
location="http://localhost:8080/axis/services/StockQuote"/>
</wsdl:port>
</wsdl:service>
WSDL - definitions
<xml version=“1.0”?>
<definitions name=“BookStore”
targetNamespace=“http://www.sungkyul.edu/bookstore.wsdl”
xmlns:tns=“http://www.sungkyul.edu/book-store.wsdl”
xmlns:xsd=“http://www.w3.org/2000/10/XMLSchema”
xmlns:xsd1=“http://www.sungkyul.edu/book-store.xsd”
xmlns:soap=“http://schemas.xmlsoap.org/wsdl/soap/”
xmlns:wsdl=“http:// schemas.xmlsoap. org/wsdl”>
WSDL – message, portType
<wsdl:message name="getQuoteResponse">
<wsdl:part name="getQuoteReturn" type="xsd:string"/>
</wsdl:message>
<wsdl:message name="getQuoteRequest">
<wsdl:part name="ticker" type="xsd:string"/>
</wsdl:message>
<wsdl:portType name="StockQuote">
<wsdl:operation name="getQuote"
parameterOrder="ticker">
<wsdl:input message="impl:getQuoteRequest"
name="getQuoteRequest"/>
<wsdl:output message="impl:getQuoteResponse"
name="getQuoteResponse"/>
</wsdl:operation>
</wsdl:portType>
WSDL - binding
<wsdl:binding name="StockQuoteSoapBinding" type="impl:StockQuote">
<wsdlsoap:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getQuote">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="getQuoteRequest">
<wsdlsoap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://ws.yust.edu" use="encoded"/>
</wsdl:input>
<wsdl:output name="getQuoteResponse">
<wsdlsoap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://localhost:8080/axis/services/StockQuote"
use="encoded"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
WSDL – service
<wsdl:service name="StockQuoteService">
<wsdl:port binding="impl:StockQuoteSoapBinding"
name="StockQuote">
<wsdlsoap:address
location="http://localhost:8080/axis/services/StockQuot
e"/>
</wsdl:port>
</wsdl:service>