Class: HttpReactor::RequestExecutionHandler

Inherits:
Object
  • Object
show all
Includes:
HttpRequestExecutionHandler
Defined in:
lib/http_reactor/client.rb

Overview

:nodoc:

Constant Summary collapse

REQUEST_SENT =
"request-sent"
RESPONSE_RECEIVED =
"response-received"
HTTP_TARGET_PATH =
'http_target_path'
HTTP_TARGET_REQUEST =
'http_target_request'
IO_REACTOR =
"io_reactor"
REDIRECT_HISTORY =
"redirect_history"

Instance Method Summary collapse

Constructor Details

#initialize(request_count, handler_proc) ⇒ RequestExecutionHandler

Returns a new instance of RequestExecutionHandler.



15
16
17
18
# File 'lib/http_reactor/client.rb', line 15

def initialize(request_count, handler_proc)
  @request_count = request_count
  @handler_proc = handler_proc
end

Instance Method Details

#finalize_context(context) ⇒ Object



28
29
30
31
# File 'lib/http_reactor/client.rb', line 28

def finalize_context(context)
  flag = context.get_attribute(RESPONSE_RECEIVED)
  @request_count.count_down() unless flag
end

#handle_response(response, context) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/http_reactor/client.rb', line 54

def handle_response(response, context)
  begin
    res = HttpReactor::Response.new(response)
    case res.code
    when 301, 302
      redirect_to = res.headers['Location']
      redirect_history = context.getAttribute(REDIRECT_HISTORY)
      if redirect_history.include?(redirect_to)
        puts "Too many redirects"
        context.setAttribute(RESPONSE_RECEIVED, true)
        @request_count.count_down()
      else
        puts "Redirecting to #{redirect_to}"
        redirect_history << redirect_to
        context.setAttribute(REDIRECT_HISTORY, redirect_history)
        requests = [HttpReactor::Request.new(URI.parse(redirect_to))]
        io_reactor = context.getAttribute(IO_REACTOR)
        HttpReactor::Client.process_requests(requests, io_reactor, @request_count)
      end
    else
      @handler_proc.call(res, context)
      context.setAttribute(RESPONSE_RECEIVED, true)
      # Signal completion of the request execution
      @request_count.count_down()
    end
  rescue => e
    puts "Error handling response: #{e.message}"
  end
end

#initalize_context(context, attachment) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/http_reactor/client.rb', line 20

def initalize_context(context, attachment)
  context.set_attribute(ExecutionContext.HTTP_TARGET_HOST, attachment[:host])
  context.set_attribute(HTTP_TARGET_PATH, attachment[:path])
  context.set_attribute(HTTP_TARGET_REQUEST, attachment[:request])
  context.set_attribute(IO_REACTOR, attachment[:io_reactor])
  context.set_attribute(REDIRECT_HISTORY, attachment[:redirect_history] || [])
end

#submit_request(context) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/http_reactor/client.rb', line 33

def submit_request(context)
  target_host = context.get_attribute(ExecutionContext.HTTP_TARGET_HOST);
  target_path = context.get_attribute(HTTP_TARGET_PATH)
  target_request = context.get_attribute(HTTP_TARGET_REQUEST)
  flag = context.get_attribute(REQUEST_SENT);
  if flag.nil?
    # Stick some object into the context
    context.set_attribute(REQUEST_SENT, true);

    puts "--------------"
    puts "Sending request to #{target_host}#{target_path}"
    puts "--------------"

    org.apache.http.message.BasicHttpEntityEnclosingRequest.new(
      target_request.method, target_path
    )
  else
    # No new request to submit
  end
end