Class: HttpReactor::RequestExecutionHandler
- Inherits:
-
Object
- Object
- HttpReactor::RequestExecutionHandler
- 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
- #finalize_context(context) ⇒ Object
- #handle_response(response, context) ⇒ Object
- #initalize_context(context, attachment) ⇒ Object
-
#initialize(request_count, handler_proc) ⇒ RequestExecutionHandler
constructor
A new instance of RequestExecutionHandler.
- #submit_request(context) ⇒ Object
Constructor Details
#initialize(request_count, handler_proc) ⇒ RequestExecutionHandler
Returns a new instance of RequestExecutionHandler.
17 18 19 20 |
# File 'lib/http_reactor/client.rb', line 17 def initialize(request_count, handler_proc) @request_count = request_count @handler_proc = handler_proc end |
Instance Method Details
#finalize_context(context) ⇒ Object
30 31 32 33 |
# File 'lib/http_reactor/client.rb', line 30 def finalize_context(context) flag = context.get_attribute(RESPONSE_RECEIVED) @request_count.count_down() unless flag end |
#handle_response(response, context) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/http_reactor/client.rb', line 59 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 request = context.getAttribute(HTTP_TARGET_REQUEST) request.uri.merge!(URI.parse(redirect_to)) redirect_to = request.uri puts "Redirecting to #{redirect_to}" redirect_history << redirect_to context.setAttribute(REDIRECT_HISTORY, redirect_history) io_reactor = context.getAttribute(IO_REACTOR) HttpReactor::Client.process_requests([request], 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.}" puts e.backtrace.map{|t| "\t#{t}" }.join("\n") end end |
#initalize_context(context, attachment) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/http_reactor/client.rb', line 22 def initalize_context(context, ) context.set_attribute(ExecutionContext.HTTP_TARGET_HOST, [:host]) context.set_attribute(HTTP_TARGET_PATH, [:path]) context.set_attribute(HTTP_TARGET_REQUEST, [:request]) context.set_attribute(IO_REACTOR, [:io_reactor]) context.set_attribute(REDIRECT_HISTORY, [:redirect_history] || []) end |
#submit_request(context) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/http_reactor/client.rb', line 35 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? begin # Stick some object into the context context.set_attribute(REQUEST_SENT, true); request = org.apache.http..BasicHttpEntityEnclosingRequest.new( target_request.method, target_path ) request.headers = target_request.headers.map { |k, v| BasicHeader.new(k, v) }.to_java(Header) request rescue Exception => e puts "Error submitting request: #{e.}" raise e end else # No new request to submit end end |