forked from suitmedia/kirk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exchange.rb
65 lines (57 loc) · 1.76 KB
/
exchange.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
module Kirk
class Client
class Exchange < Jetty::ContentExchange
def initialize(session, handler)
@handler = handler
@session = session
@headers = {}
super()
end
def on_exception(ex)
puts ex.inspect
end
def on_response_complete
@session.queue.put(response)
handle(:on_response_complete, response)
super
end
def on_response_content(content)
handle(:on_response_content, content)
super
end
def on_response_header(name, value)
@headers[name.to_s] = value.to_s
handle(:on_response_header, {name.to_s => value.to_s})
super
end
def response
@response ||= begin
Response.new(get_response_content, get_response_status, @headers)
end
end
def self.from_request(request)
exchange = new(request.group, request.handler)
exchange.set_method(request.method)
exchange.set_url(request.url)
request.headers.each do |name, value|
exchange.set_request_header(name, value)
end if request.headers
if request.body && request.body.respond_to?(:read)
exchange.set_request_content_source(request.body.to_inputstream)
else
body = Jetty::ByteArrayBuffer.new(request.body.to_s)
exchange.set_request_content(body)
end
exchange
end
def handle(method, *args)
@handler.send(method, *args) if @handler && @handler.respond_to?(method)
end
# Implement Java API
alias onResponseComplete on_response_complete
alias onResponseContent on_response_content
alias onResponseHeader on_response_header
alias onException on_exception
end
end
end