Class: Manticore::Response

Inherits:
Object
  • Object
show all
Includes:
Callable, ResponseHandler
Defined in:
lib/manticore/response.rb

Overview

Implementation of ResponseHandler which serves as a Ruby proxy for HTTPClient responses.

Direct Known Subclasses

StubbedResponse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, request, context, &block) ⇒ Response

Creates a new Response

Parameters:

  • client (Manticore::Client)

    The client that was used to create this response

  • request (HttpRequestBase)

    The underlying request object

  • context (HttpContext)

    The underlying HttpContext


31
32
33
34
35
36
37
38
39
40
41
# File 'lib/manticore/response.rb', line 31

def initialize(client, request, context, &block)
  @client = client
  @request = request
  @context = context
  @handlers = {
    success: block || Proc.new { |resp| resp.body },
    failure: Proc.new { |ex| raise ex },
    cancelled: Proc.new { },
    complete: [],
  }
end

Instance Attribute Details

#backgroundObject

Returns the value of attribute background


23
24
25
# File 'lib/manticore/response.rb', line 23

def background
  @background
end

#callback_resultObject (readonly)

Returns Value returned from any given on_success/response block.

Returns:

  • Value returned from any given on_success/response block


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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/manticore/response.rb', line 13

class Response
  include_package "org.apache.http.client"
  include_package "org.apache.http.util"
  include_package "org.apache.http.protocol"
  java_import "org.apache.http.client.protocol.HttpClientContext"
  java_import "java.util.concurrent.Callable"

  include ResponseHandler
  include Callable

  attr_accessor :background
  attr_reader :context, :request, :callback_result, :called, :future

  # Creates a new Response
  #
  # @param  client             [Manticore::Client] The client that was used to create this response
  # @param  request            [HttpRequestBase] The underlying request object
  # @param  context            [HttpContext] The underlying HttpContext
  def initialize(client, request, context, &block)
    @client = client
    @request = request
    @context = context
    @handlers = {
      success: block || Proc.new { |resp| resp.body },
      failure: Proc.new { |ex| raise ex },
      cancelled: Proc.new { },
      complete: [],
    }
  end

  # Implementation of Callable#call
  # Used by Manticore::Client to invoke the request tied to this response.
  def call
    return background! if @background
    raise "Already called" if @called
    @called = true
    begin
      @client.client.execute @request, self, @context
    rescue Java::JavaNet::SocketTimeoutException => e
      ex = Manticore::SocketTimeout
    rescue Java::OrgApacheHttpConn::ConnectTimeoutException => e
      ex = Manticore::ConnectTimeout
    rescue Java::JavaNet::SocketException => e
      ex = Manticore::SocketException
    rescue Java::OrgApacheHttpClient::ClientProtocolException, Java::JavaxNetSsl::SSLHandshakeException, Java::OrgApacheHttpConn::HttpHostConnectException,
           Java::OrgApacheHttp::NoHttpResponseException, Java::OrgApacheHttp::ConnectionClosedException => e
      ex = Manticore::ClientProtocolException
    rescue Java::JavaNet::UnknownHostException => e
      ex = Manticore::ResolutionFailure
    rescue Java::JavaLang::IllegalArgumentException => e
      ex = Manticore::InvalidArgumentException
    rescue Java::JavaLang::IllegalStateException => e
      if e.message.match(/Connection pool shut down/)
        ex = Manticore::ClientStoppedException
      else
        @exception = e
      end
    rescue Java::JavaLang::Exception => e # Handle anything we may have missed from java
      ex = Manticore::UnknownException
    rescue StandardError => e
      @exception = e
    end

    # TODO: If calling async, execute_complete may fail and then silently swallow exceptions. How do we fix that?
    if ex || @exception
      @exception ||= ex.new(e.cause || e.message)
      @handlers[:failure].call @exception
      execute_complete
      nil
    else
      execute_complete
      self
    end
  end

  # Fetch the final resolved URL for this response. Will call the request if it has not been called yet.
  #
  # @return [String]
  def final_url
    call_once
    last_request = context.get_attribute ExecutionContext.HTTP_REQUEST
    last_host = context.get_attribute ExecutionContext.HTTP_TARGET_HOST
    host = last_host.to_uri
    url = last_request.get_uri
    URI.join(host, url.to_s)
  end

  # Fetch the body content of this response. Will call the request if it has not been called yet.
  # This fetches the input stream in Ruby; this isn't optimal, but it's faster than
  # fetching the whole thing in Java then UTF-8 encoding it all into a giant Ruby string.
  #
  # This permits for streaming response bodies, as well.
  #
  # @example Streaming response
  #
  #     client.get("http://example.com/resource").on_success do |response|
  #       response.body do |chunk|
  #         # Do something with chunk, which is a parsed portion of the returned body
  #       end
  #     end
  #
  # @return [String] Reponse body
  def body(&block)
    call_once
    @body ||= begin
                if entity = @response.get_entity
                  EntityConverter.new.read_entity(entity, &block)
                end
              rescue Java::JavaIo::IOException, Java::JavaNet::SocketException, IOError => e
                raise StreamClosedException.new("Could not read from stream: #{e.message}")
                # ensure
                #   @request.release_connection
              end
  end

  alias_method :read_body, :body

  # Returns true if this response has been called (requested and populated) yet
  def called?
    !!@called
  end

  # Return a hash of headers from this response. Will call the request if it has not been called yet.
  #
  # @return [Array<string, obj>] Hash of headers. Keys will be lower-case.
  def headers
    call_once
    @headers
  end

  # Return the value of a single response header. Will call the request if it has not been called yet.
  # If the header was returned with multiple values, will only return the first value. If you need to get
  # multiple values, use response#headers[lowercase_key]
  #
  # @param  key [String] Case-insensitive header key
  # @return     [String] Value of the header, or nil if not present
  def [](key)
    v = headers[key.downcase]
    v.is_a?(Array) ? v.first : v
  end

  # Return the response code from this request as an integer. Will call the request if it has not been called yet.
  #
  # @return [Integer] The response code
  def code
    call_once
    @code
  end

  # Return the response text for a request as a string (Not Found, Ok, Bad Request, etc). Will call the request if it has not been called yet.
  #
  # @return [String] The response code text
  def message
    call_once
    @message
  end

  # Returns the length of the response body. Returns -1 if content-length is not present in the response.
  #
  # @return [Integer]
  def length
    (headers["content-length"] || -1).to_i
  end

  # Returns an array of {Manticore::Cookie Cookies} associated with this request's execution context
  #
  # @return [Array<Manticore::Cookie>]
  def cookies
    call_once
    @cookies ||= begin
      @context.get_cookie_store.get_cookies.inject({}) do |all, java_cookie|
        c = Cookie.from_java(java_cookie)
        all[c.name] ||= []
        all[c.name] << c
        all
      end
    end
  end

  # Set handler for success responses
  # @param block Proc which will be invoked on a successful response. Block will receive |response, request|
  #
  # @return self
  def on_success(&block)
    @handlers[:success] = block
    self
  end

  alias_method :success, :on_success

  # Set handler for failure responses
  # @param block Proc which will be invoked on a on a failed response. Block will receive an exception object.
  #
  # @return self
  def on_failure(&block)
    @handlers[:failure] = block
    self
  end

  alias_method :failure, :on_failure
  alias_method :fail, :on_failure

  # Set handler for cancelled requests. NB: Not actually used right now?
  # @param block Proc which will be invoked on a on a cancelled response.
  #
  # @return self
  def on_cancelled(&block)
    @handlers[:cancelled] = block
    self
  end

  alias_method :cancelled, :on_cancelled
  alias_method :cancellation, :on_cancelled
  alias_method :on_cancellation, :on_cancelled

  # Set handler for completed requests
  # @param block Proc which will be invoked on a on a completed response. This handler will be called
  #                   regardless of request success/failure.
  # @return self
  def on_complete(&block)
    @handlers[:complete] = Array(@handlers[:complete]).compact + [block]
    self
  end

  alias_method :complete, :on_complete
  alias_method :completed, :on_complete
  alias_method :on_completed, :on_complete

  def times_retried
    @context.get_attribute("retryCount") || 0
  end

  private

  def background!
    @background = false
    @future ||= @client.executor.java_method(:submit, [java.util.concurrent.Callable.java_class]).call(self)
  end

  # Implementation of {http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/ResponseHandler.html#handleResponse(org.apache.http.HttpResponse) ResponseHandler#handleResponse}
  # @param  response [Response] The underlying Java Response object
  def handleResponse(response)
    @response = response
    @code = response.get_status_line.get_status_code
    @message = response.get_status_line.get_reason_phrase
    @headers = response.get_all_headers.each_with_object({}) do |h, o|
      key = h.get_name.downcase
      if o.key?(key)
        o[key] = Array(o[key]) unless o[key].is_a?(Array)
        o[key].push h.get_value
      else
        o[key] = h.get_value
      end
    end

    @callback_result = @handlers[:success].call(self)
    nil
  end

  def call_once
    is_background = @background
    call unless called?
    # If this is a background request, then we don't want to allow the usage of sync methods, as it's probably a semantic error. We could resolve the future
    # but that'll probably result in blocking foreground threads unintentionally. Fail loudly.
    raise RuntimeError.new("Cannot call synchronous methods on a background response. Use an on_success handler instead.") if is_background && @future
    @called = true
  end

  def execute_complete
    @handlers[:complete].each { |h| h.call(self) }
  end
end

#calledObject (readonly)

Returns the value of attribute called


24
25
26
# File 'lib/manticore/response.rb', line 24

def called
  @called
end

#codeInteger (readonly)

Return the response code from this request as an integer. Will call the request if it has not been called yet.

Returns:

  • (Integer)

    The response code


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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/manticore/response.rb', line 13

class Response
  include_package "org.apache.http.client"
  include_package "org.apache.http.util"
  include_package "org.apache.http.protocol"
  java_import "org.apache.http.client.protocol.HttpClientContext"
  java_import "java.util.concurrent.Callable"

  include ResponseHandler
  include Callable

  attr_accessor :background
  attr_reader :context, :request, :callback_result, :called, :future

  # Creates a new Response
  #
  # @param  client             [Manticore::Client] The client that was used to create this response
  # @param  request            [HttpRequestBase] The underlying request object
  # @param  context            [HttpContext] The underlying HttpContext
  def initialize(client, request, context, &block)
    @client = client
    @request = request
    @context = context
    @handlers = {
      success: block || Proc.new { |resp| resp.body },
      failure: Proc.new { |ex| raise ex },
      cancelled: Proc.new { },
      complete: [],
    }
  end

  # Implementation of Callable#call
  # Used by Manticore::Client to invoke the request tied to this response.
  def call
    return background! if @background
    raise "Already called" if @called
    @called = true
    begin
      @client.client.execute @request, self, @context
    rescue Java::JavaNet::SocketTimeoutException => e
      ex = Manticore::SocketTimeout
    rescue Java::OrgApacheHttpConn::ConnectTimeoutException => e
      ex = Manticore::ConnectTimeout
    rescue Java::JavaNet::SocketException => e
      ex = Manticore::SocketException
    rescue Java::OrgApacheHttpClient::ClientProtocolException, Java::JavaxNetSsl::SSLHandshakeException, Java::OrgApacheHttpConn::HttpHostConnectException,
           Java::OrgApacheHttp::NoHttpResponseException, Java::OrgApacheHttp::ConnectionClosedException => e
      ex = Manticore::ClientProtocolException
    rescue Java::JavaNet::UnknownHostException => e
      ex = Manticore::ResolutionFailure
    rescue Java::JavaLang::IllegalArgumentException => e
      ex = Manticore::InvalidArgumentException
    rescue Java::JavaLang::IllegalStateException => e
      if e.message.match(/Connection pool shut down/)
        ex = Manticore::ClientStoppedException
      else
        @exception = e
      end
    rescue Java::JavaLang::Exception => e # Handle anything we may have missed from java
      ex = Manticore::UnknownException
    rescue StandardError => e
      @exception = e
    end

    # TODO: If calling async, execute_complete may fail and then silently swallow exceptions. How do we fix that?
    if ex || @exception
      @exception ||= ex.new(e.cause || e.message)
      @handlers[:failure].call @exception
      execute_complete
      nil
    else
      execute_complete
      self
    end
  end

  # Fetch the final resolved URL for this response. Will call the request if it has not been called yet.
  #
  # @return [String]
  def final_url
    call_once
    last_request = context.get_attribute ExecutionContext.HTTP_REQUEST
    last_host = context.get_attribute ExecutionContext.HTTP_TARGET_HOST
    host = last_host.to_uri
    url = last_request.get_uri
    URI.join(host, url.to_s)
  end

  # Fetch the body content of this response. Will call the request if it has not been called yet.
  # This fetches the input stream in Ruby; this isn't optimal, but it's faster than
  # fetching the whole thing in Java then UTF-8 encoding it all into a giant Ruby string.
  #
  # This permits for streaming response bodies, as well.
  #
  # @example Streaming response
  #
  #     client.get("http://example.com/resource").on_success do |response|
  #       response.body do |chunk|
  #         # Do something with chunk, which is a parsed portion of the returned body
  #       end
  #     end
  #
  # @return [String] Reponse body
  def body(&block)
    call_once
    @body ||= begin
                if entity = @response.get_entity
                  EntityConverter.new.read_entity(entity, &block)
                end
              rescue Java::JavaIo::IOException, Java::JavaNet::SocketException, IOError => e
                raise StreamClosedException.new("Could not read from stream: #{e.message}")
                # ensure
                #   @request.release_connection
              end
  end

  alias_method :read_body, :body

  # Returns true if this response has been called (requested and populated) yet
  def called?
    !!@called
  end

  # Return a hash of headers from this response. Will call the request if it has not been called yet.
  #
  # @return [Array<string, obj>] Hash of headers. Keys will be lower-case.
  def headers
    call_once
    @headers
  end

  # Return the value of a single response header. Will call the request if it has not been called yet.
  # If the header was returned with multiple values, will only return the first value. If you need to get
  # multiple values, use response#headers[lowercase_key]
  #
  # @param  key [String] Case-insensitive header key
  # @return     [String] Value of the header, or nil if not present
  def [](key)
    v = headers[key.downcase]
    v.is_a?(Array) ? v.first : v
  end

  # Return the response code from this request as an integer. Will call the request if it has not been called yet.
  #
  # @return [Integer] The response code
  def code
    call_once
    @code
  end

  # Return the response text for a request as a string (Not Found, Ok, Bad Request, etc). Will call the request if it has not been called yet.
  #
  # @return [String] The response code text
  def message
    call_once
    @message
  end

  # Returns the length of the response body. Returns -1 if content-length is not present in the response.
  #
  # @return [Integer]
  def length
    (headers["content-length"] || -1).to_i
  end

  # Returns an array of {Manticore::Cookie Cookies} associated with this request's execution context
  #
  # @return [Array<Manticore::Cookie>]
  def cookies
    call_once
    @cookies ||= begin
      @context.get_cookie_store.get_cookies.inject({}) do |all, java_cookie|
        c = Cookie.from_java(java_cookie)
        all[c.name] ||= []
        all[c.name] << c
        all
      end
    end
  end

  # Set handler for success responses
  # @param block Proc which will be invoked on a successful response. Block will receive |response, request|
  #
  # @return self
  def on_success(&block)
    @handlers[:success] = block
    self
  end

  alias_method :success, :on_success

  # Set handler for failure responses
  # @param block Proc which will be invoked on a on a failed response. Block will receive an exception object.
  #
  # @return self
  def on_failure(&block)
    @handlers[:failure] = block
    self
  end

  alias_method :failure, :on_failure
  alias_method :fail, :on_failure

  # Set handler for cancelled requests. NB: Not actually used right now?
  # @param block Proc which will be invoked on a on a cancelled response.
  #
  # @return self
  def on_cancelled(&block)
    @handlers[:cancelled] = block
    self
  end

  alias_method :cancelled, :on_cancelled
  alias_method :cancellation, :on_cancelled
  alias_method :on_cancellation, :on_cancelled

  # Set handler for completed requests
  # @param block Proc which will be invoked on a on a completed response. This handler will be called
  #                   regardless of request success/failure.
  # @return self
  def on_complete(&block)
    @handlers[:complete] = Array(@handlers[:complete]).compact + [block]
    self
  end

  alias_method :complete, :on_complete
  alias_method :completed, :on_complete
  alias_method :on_completed, :on_complete

  def times_retried
    @context.get_attribute("retryCount") || 0
  end

  private

  def background!
    @background = false
    @future ||= @client.executor.java_method(:submit, [java.util.concurrent.Callable.java_class]).call(self)
  end

  # Implementation of {http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/ResponseHandler.html#handleResponse(org.apache.http.HttpResponse) ResponseHandler#handleResponse}
  # @param  response [Response] The underlying Java Response object
  def handleResponse(response)
    @response = response
    @code = response.get_status_line.get_status_code
    @message = response.get_status_line.get_reason_phrase
    @headers = response.get_all_headers.each_with_object({}) do |h, o|
      key = h.get_name.downcase
      if o.key?(key)
        o[key] = Array(o[key]) unless o[key].is_a?(Array)
        o[key].push h.get_value
      else
        o[key] = h.get_value
      end
    end

    @callback_result = @handlers[:success].call(self)
    nil
  end

  def call_once
    is_background = @background
    call unless called?
    # If this is a background request, then we don't want to allow the usage of sync methods, as it's probably a semantic error. We could resolve the future
    # but that'll probably result in blocking foreground threads unintentionally. Fail loudly.
    raise RuntimeError.new("Cannot call synchronous methods on a background response. Use an on_success handler instead.") if is_background && @future
    @called = true
  end

  def execute_complete
    @handlers[:complete].each { |h| h.call(self) }
  end
end

#contextHttpContext (readonly)

Returns Context associated with this request/response.

Returns:

  • (HttpContext)

    Context associated with this request/response


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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/manticore/response.rb', line 13

class Response
  include_package "org.apache.http.client"
  include_package "org.apache.http.util"
  include_package "org.apache.http.protocol"
  java_import "org.apache.http.client.protocol.HttpClientContext"
  java_import "java.util.concurrent.Callable"

  include ResponseHandler
  include Callable

  attr_accessor :background
  attr_reader :context, :request, :callback_result, :called, :future

  # Creates a new Response
  #
  # @param  client             [Manticore::Client] The client that was used to create this response
  # @param  request            [HttpRequestBase] The underlying request object
  # @param  context            [HttpContext] The underlying HttpContext
  def initialize(client, request, context, &block)
    @client = client
    @request = request
    @context = context
    @handlers = {
      success: block || Proc.new { |resp| resp.body },
      failure: Proc.new { |ex| raise ex },
      cancelled: Proc.new { },
      complete: [],
    }
  end

  # Implementation of Callable#call
  # Used by Manticore::Client to invoke the request tied to this response.
  def call
    return background! if @background
    raise "Already called" if @called
    @called = true
    begin
      @client.client.execute @request, self, @context
    rescue Java::JavaNet::SocketTimeoutException => e
      ex = Manticore::SocketTimeout
    rescue Java::OrgApacheHttpConn::ConnectTimeoutException => e
      ex = Manticore::ConnectTimeout
    rescue Java::JavaNet::SocketException => e
      ex = Manticore::SocketException
    rescue Java::OrgApacheHttpClient::ClientProtocolException, Java::JavaxNetSsl::SSLHandshakeException, Java::OrgApacheHttpConn::HttpHostConnectException,
           Java::OrgApacheHttp::NoHttpResponseException, Java::OrgApacheHttp::ConnectionClosedException => e
      ex = Manticore::ClientProtocolException
    rescue Java::JavaNet::UnknownHostException => e
      ex = Manticore::ResolutionFailure
    rescue Java::JavaLang::IllegalArgumentException => e
      ex = Manticore::InvalidArgumentException
    rescue Java::JavaLang::IllegalStateException => e
      if e.message.match(/Connection pool shut down/)
        ex = Manticore::ClientStoppedException
      else
        @exception = e
      end
    rescue Java::JavaLang::Exception => e # Handle anything we may have missed from java
      ex = Manticore::UnknownException
    rescue StandardError => e
      @exception = e
    end

    # TODO: If calling async, execute_complete may fail and then silently swallow exceptions. How do we fix that?
    if ex || @exception
      @exception ||= ex.new(e.cause || e.message)
      @handlers[:failure].call @exception
      execute_complete
      nil
    else
      execute_complete
      self
    end
  end

  # Fetch the final resolved URL for this response. Will call the request if it has not been called yet.
  #
  # @return [String]
  def final_url
    call_once
    last_request = context.get_attribute ExecutionContext.HTTP_REQUEST
    last_host = context.get_attribute ExecutionContext.HTTP_TARGET_HOST
    host = last_host.to_uri
    url = last_request.get_uri
    URI.join(host, url.to_s)
  end

  # Fetch the body content of this response. Will call the request if it has not been called yet.
  # This fetches the input stream in Ruby; this isn't optimal, but it's faster than
  # fetching the whole thing in Java then UTF-8 encoding it all into a giant Ruby string.
  #
  # This permits for streaming response bodies, as well.
  #
  # @example Streaming response
  #
  #     client.get("http://example.com/resource").on_success do |response|
  #       response.body do |chunk|
  #         # Do something with chunk, which is a parsed portion of the returned body
  #       end
  #     end
  #
  # @return [String] Reponse body
  def body(&block)
    call_once
    @body ||= begin
                if entity = @response.get_entity
                  EntityConverter.new.read_entity(entity, &block)
                end
              rescue Java::JavaIo::IOException, Java::JavaNet::SocketException, IOError => e
                raise StreamClosedException.new("Could not read from stream: #{e.message}")
                # ensure
                #   @request.release_connection
              end
  end

  alias_method :read_body, :body

  # Returns true if this response has been called (requested and populated) yet
  def called?
    !!@called
  end

  # Return a hash of headers from this response. Will call the request if it has not been called yet.
  #
  # @return [Array<string, obj>] Hash of headers. Keys will be lower-case.
  def headers
    call_once
    @headers
  end

  # Return the value of a single response header. Will call the request if it has not been called yet.
  # If the header was returned with multiple values, will only return the first value. If you need to get
  # multiple values, use response#headers[lowercase_key]
  #
  # @param  key [String] Case-insensitive header key
  # @return     [String] Value of the header, or nil if not present
  def [](key)
    v = headers[key.downcase]
    v.is_a?(Array) ? v.first : v
  end

  # Return the response code from this request as an integer. Will call the request if it has not been called yet.
  #
  # @return [Integer] The response code
  def code
    call_once
    @code
  end

  # Return the response text for a request as a string (Not Found, Ok, Bad Request, etc). Will call the request if it has not been called yet.
  #
  # @return [String] The response code text
  def message
    call_once
    @message
  end

  # Returns the length of the response body. Returns -1 if content-length is not present in the response.
  #
  # @return [Integer]
  def length
    (headers["content-length"] || -1).to_i
  end

  # Returns an array of {Manticore::Cookie Cookies} associated with this request's execution context
  #
  # @return [Array<Manticore::Cookie>]
  def cookies
    call_once
    @cookies ||= begin
      @context.get_cookie_store.get_cookies.inject({}) do |all, java_cookie|
        c = Cookie.from_java(java_cookie)
        all[c.name] ||= []
        all[c.name] << c
        all
      end
    end
  end

  # Set handler for success responses
  # @param block Proc which will be invoked on a successful response. Block will receive |response, request|
  #
  # @return self
  def on_success(&block)
    @handlers[:success] = block
    self
  end

  alias_method :success, :on_success

  # Set handler for failure responses
  # @param block Proc which will be invoked on a on a failed response. Block will receive an exception object.
  #
  # @return self
  def on_failure(&block)
    @handlers[:failure] = block
    self
  end

  alias_method :failure, :on_failure
  alias_method :fail, :on_failure

  # Set handler for cancelled requests. NB: Not actually used right now?
  # @param block Proc which will be invoked on a on a cancelled response.
  #
  # @return self
  def on_cancelled(&block)
    @handlers[:cancelled] = block
    self
  end

  alias_method :cancelled, :on_cancelled
  alias_method :cancellation, :on_cancelled
  alias_method :on_cancellation, :on_cancelled

  # Set handler for completed requests
  # @param block Proc which will be invoked on a on a completed response. This handler will be called
  #                   regardless of request success/failure.
  # @return self
  def on_complete(&block)
    @handlers[:complete] = Array(@handlers[:complete]).compact + [block]
    self
  end

  alias_method :complete, :on_complete
  alias_method :completed, :on_complete
  alias_method :on_completed, :on_complete

  def times_retried
    @context.get_attribute("retryCount") || 0
  end

  private

  def background!
    @background = false
    @future ||= @client.executor.java_method(:submit, [java.util.concurrent.Callable.java_class]).call(self)
  end

  # Implementation of {http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/ResponseHandler.html#handleResponse(org.apache.http.HttpResponse) ResponseHandler#handleResponse}
  # @param  response [Response] The underlying Java Response object
  def handleResponse(response)
    @response = response
    @code = response.get_status_line.get_status_code
    @message = response.get_status_line.get_reason_phrase
    @headers = response.get_all_headers.each_with_object({}) do |h, o|
      key = h.get_name.downcase
      if o.key?(key)
        o[key] = Array(o[key]) unless o[key].is_a?(Array)
        o[key].push h.get_value
      else
        o[key] = h.get_value
      end
    end

    @callback_result = @handlers[:success].call(self)
    nil
  end

  def call_once
    is_background = @background
    call unless called?
    # If this is a background request, then we don't want to allow the usage of sync methods, as it's probably a semantic error. We could resolve the future
    # but that'll probably result in blocking foreground threads unintentionally. Fail loudly.
    raise RuntimeError.new("Cannot call synchronous methods on a background response. Use an on_success handler instead.") if is_background && @future
    @called = true
  end

  def execute_complete
    @handlers[:complete].each { |h| h.call(self) }
  end
end

#futureObject (readonly)

Returns the value of attribute future


24
25
26
# File 'lib/manticore/response.rb', line 24

def future
  @future
end

#headersArray<string, obj> (readonly)

Return a hash of headers from this response. Will call the request if it has not been called yet.

Returns:

  • (Array<string, obj>)

    Hash of headers. Keys will be lower-case.


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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/manticore/response.rb', line 13

class Response
  include_package "org.apache.http.client"
  include_package "org.apache.http.util"
  include_package "org.apache.http.protocol"
  java_import "org.apache.http.client.protocol.HttpClientContext"
  java_import "java.util.concurrent.Callable"

  include ResponseHandler
  include Callable

  attr_accessor :background
  attr_reader :context, :request, :callback_result, :called, :future

  # Creates a new Response
  #
  # @param  client             [Manticore::Client] The client that was used to create this response
  # @param  request            [HttpRequestBase] The underlying request object
  # @param  context            [HttpContext] The underlying HttpContext
  def initialize(client, request, context, &block)
    @client = client
    @request = request
    @context = context
    @handlers = {
      success: block || Proc.new { |resp| resp.body },
      failure: Proc.new { |ex| raise ex },
      cancelled: Proc.new { },
      complete: [],
    }
  end

  # Implementation of Callable#call
  # Used by Manticore::Client to invoke the request tied to this response.
  def call
    return background! if @background
    raise "Already called" if @called
    @called = true
    begin
      @client.client.execute @request, self, @context
    rescue Java::JavaNet::SocketTimeoutException => e
      ex = Manticore::SocketTimeout
    rescue Java::OrgApacheHttpConn::ConnectTimeoutException => e
      ex = Manticore::ConnectTimeout
    rescue Java::JavaNet::SocketException => e
      ex = Manticore::SocketException
    rescue Java::OrgApacheHttpClient::ClientProtocolException, Java::JavaxNetSsl::SSLHandshakeException, Java::OrgApacheHttpConn::HttpHostConnectException,
           Java::OrgApacheHttp::NoHttpResponseException, Java::OrgApacheHttp::ConnectionClosedException => e
      ex = Manticore::ClientProtocolException
    rescue Java::JavaNet::UnknownHostException => e
      ex = Manticore::ResolutionFailure
    rescue Java::JavaLang::IllegalArgumentException => e
      ex = Manticore::InvalidArgumentException
    rescue Java::JavaLang::IllegalStateException => e
      if e.message.match(/Connection pool shut down/)
        ex = Manticore::ClientStoppedException
      else
        @exception = e
      end
    rescue Java::JavaLang::Exception => e # Handle anything we may have missed from java
      ex = Manticore::UnknownException
    rescue StandardError => e
      @exception = e
    end

    # TODO: If calling async, execute_complete may fail and then silently swallow exceptions. How do we fix that?
    if ex || @exception
      @exception ||= ex.new(e.cause || e.message)
      @handlers[:failure].call @exception
      execute_complete
      nil
    else
      execute_complete
      self
    end
  end

  # Fetch the final resolved URL for this response. Will call the request if it has not been called yet.
  #
  # @return [String]
  def final_url
    call_once
    last_request = context.get_attribute ExecutionContext.HTTP_REQUEST
    last_host = context.get_attribute ExecutionContext.HTTP_TARGET_HOST
    host = last_host.to_uri
    url = last_request.get_uri
    URI.join(host, url.to_s)
  end

  # Fetch the body content of this response. Will call the request if it has not been called yet.
  # This fetches the input stream in Ruby; this isn't optimal, but it's faster than
  # fetching the whole thing in Java then UTF-8 encoding it all into a giant Ruby string.
  #
  # This permits for streaming response bodies, as well.
  #
  # @example Streaming response
  #
  #     client.get("http://example.com/resource").on_success do |response|
  #       response.body do |chunk|
  #         # Do something with chunk, which is a parsed portion of the returned body
  #       end
  #     end
  #
  # @return [String] Reponse body
  def body(&block)
    call_once
    @body ||= begin
                if entity = @response.get_entity
                  EntityConverter.new.read_entity(entity, &block)
                end
              rescue Java::JavaIo::IOException, Java::JavaNet::SocketException, IOError => e
                raise StreamClosedException.new("Could not read from stream: #{e.message}")
                # ensure
                #   @request.release_connection
              end
  end

  alias_method :read_body, :body

  # Returns true if this response has been called (requested and populated) yet
  def called?
    !!@called
  end

  # Return a hash of headers from this response. Will call the request if it has not been called yet.
  #
  # @return [Array<string, obj>] Hash of headers. Keys will be lower-case.
  def headers
    call_once
    @headers
  end

  # Return the value of a single response header. Will call the request if it has not been called yet.
  # If the header was returned with multiple values, will only return the first value. If you need to get
  # multiple values, use response#headers[lowercase_key]
  #
  # @param  key [String] Case-insensitive header key
  # @return     [String] Value of the header, or nil if not present
  def [](key)
    v = headers[key.downcase]
    v.is_a?(Array) ? v.first : v
  end

  # Return the response code from this request as an integer. Will call the request if it has not been called yet.
  #
  # @return [Integer] The response code
  def code
    call_once
    @code
  end

  # Return the response text for a request as a string (Not Found, Ok, Bad Request, etc). Will call the request if it has not been called yet.
  #
  # @return [String] The response code text
  def message
    call_once
    @message
  end

  # Returns the length of the response body. Returns -1 if content-length is not present in the response.
  #
  # @return [Integer]
  def length
    (headers["content-length"] || -1).to_i
  end

  # Returns an array of {Manticore::Cookie Cookies} associated with this request's execution context
  #
  # @return [Array<Manticore::Cookie>]
  def cookies
    call_once
    @cookies ||= begin
      @context.get_cookie_store.get_cookies.inject({}) do |all, java_cookie|
        c = Cookie.from_java(java_cookie)
        all[c.name] ||= []
        all[c.name] << c
        all
      end
    end
  end

  # Set handler for success responses
  # @param block Proc which will be invoked on a successful response. Block will receive |response, request|
  #
  # @return self
  def on_success(&block)
    @handlers[:success] = block
    self
  end

  alias_method :success, :on_success

  # Set handler for failure responses
  # @param block Proc which will be invoked on a on a failed response. Block will receive an exception object.
  #
  # @return self
  def on_failure(&block)
    @handlers[:failure] = block
    self
  end

  alias_method :failure, :on_failure
  alias_method :fail, :on_failure

  # Set handler for cancelled requests. NB: Not actually used right now?
  # @param block Proc which will be invoked on a on a cancelled response.
  #
  # @return self
  def on_cancelled(&block)
    @handlers[:cancelled] = block
    self
  end

  alias_method :cancelled, :on_cancelled
  alias_method :cancellation, :on_cancelled
  alias_method :on_cancellation, :on_cancelled

  # Set handler for completed requests
  # @param block Proc which will be invoked on a on a completed response. This handler will be called
  #                   regardless of request success/failure.
  # @return self
  def on_complete(&block)
    @handlers[:complete] = Array(@handlers[:complete]).compact + [block]
    self
  end

  alias_method :complete, :on_complete
  alias_method :completed, :on_complete
  alias_method :on_completed, :on_complete

  def times_retried
    @context.get_attribute("retryCount") || 0
  end

  private

  def background!
    @background = false
    @future ||= @client.executor.java_method(:submit, [java.util.concurrent.Callable.java_class]).call(self)
  end

  # Implementation of {http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/ResponseHandler.html#handleResponse(org.apache.http.HttpResponse) ResponseHandler#handleResponse}
  # @param  response [Response] The underlying Java Response object
  def handleResponse(response)
    @response = response
    @code = response.get_status_line.get_status_code
    @message = response.get_status_line.get_reason_phrase
    @headers = response.get_all_headers.each_with_object({}) do |h, o|
      key = h.get_name.downcase
      if o.key?(key)
        o[key] = Array(o[key]) unless o[key].is_a?(Array)
        o[key].push h.get_value
      else
        o[key] = h.get_value
      end
    end

    @callback_result = @handlers[:success].call(self)
    nil
  end

  def call_once
    is_background = @background
    call unless called?
    # If this is a background request, then we don't want to allow the usage of sync methods, as it's probably a semantic error. We could resolve the future
    # but that'll probably result in blocking foreground threads unintentionally. Fail loudly.
    raise RuntimeError.new("Cannot call synchronous methods on a background response. Use an on_success handler instead.") if is_background && @future
    @called = true
  end

  def execute_complete
    @handlers[:complete].each { |h| h.call(self) }
  end
end

#requestObject (readonly)

Returns the value of attribute request


24
25
26
# File 'lib/manticore/response.rb', line 24

def request
  @request
end

Instance Method Details

#[](key) ⇒ String

Return the value of a single response header. Will call the request if it has not been called yet. If the header was returned with multiple values, will only return the first value. If you need to get multiple values, use response#headers

Parameters:

  • key (String)

    Case-insensitive header key

Returns:

  • (String)

    Value of the header, or nil if not present


149
150
151
152
# File 'lib/manticore/response.rb', line 149

def [](key)
  v = headers[key.downcase]
  v.is_a?(Array) ? v.first : v
end

#body(&block) ⇒ String Also known as: read_body

Fetch the body content of this response. Will call the request if it has not been called yet. This fetches the input stream in Ruby; this isn't optimal, but it's faster than fetching the whole thing in Java then UTF-8 encoding it all into a giant Ruby string.

This permits for streaming response bodies, as well.

Examples:

Streaming response


client.get("http://example.com/resource").on_success do |response|
  response.body do |chunk|
    # Do something with chunk, which is a parsed portion of the returned body
  end
end

Returns:

  • (String)

    Reponse body


115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/manticore/response.rb', line 115

def body(&block)
  call_once
  @body ||= begin
              if entity = @response.get_entity
                EntityConverter.new.read_entity(entity, &block)
              end
            rescue Java::JavaIo::IOException, Java::JavaNet::SocketException, IOError => e
              raise StreamClosedException.new("Could not read from stream: #{e.message}")
              # ensure
              #   @request.release_connection
            end
end

#callObject

Implementation of Callable#call Used by Manticore::Client to invoke the request tied to this response.


45
46
47
48
49
50
51
52
53
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
83
84
85
86
# File 'lib/manticore/response.rb', line 45

def call
  return background! if @background
  raise "Already called" if @called
  @called = true
  begin
    @client.client.execute @request, self, @context
  rescue Java::JavaNet::SocketTimeoutException => e
    ex = Manticore::SocketTimeout
  rescue Java::OrgApacheHttpConn::ConnectTimeoutException => e
    ex = Manticore::ConnectTimeout
  rescue Java::JavaNet::SocketException => e
    ex = Manticore::SocketException
  rescue Java::OrgApacheHttpClient::ClientProtocolException, Java::JavaxNetSsl::SSLHandshakeException, Java::OrgApacheHttpConn::HttpHostConnectException,
         Java::OrgApacheHttp::NoHttpResponseException, Java::OrgApacheHttp::ConnectionClosedException => e
    ex = Manticore::ClientProtocolException
  rescue Java::JavaNet::UnknownHostException => e
    ex = Manticore::ResolutionFailure
  rescue Java::JavaLang::IllegalArgumentException => e
    ex = Manticore::InvalidArgumentException
  rescue Java::JavaLang::IllegalStateException => e
    if e.message.match(/Connection pool shut down/)
      ex = Manticore::ClientStoppedException
    else
      @exception = e
    end
  rescue Java::JavaLang::Exception => e # Handle anything we may have missed from java
    ex = Manticore::UnknownException
  rescue StandardError => e
    @exception = e
  end

  # TODO: If calling async, execute_complete may fail and then silently swallow exceptions. How do we fix that?
  if ex || @exception
    @exception ||= ex.new(e.cause || e.message)
    @handlers[:failure].call @exception
    execute_complete
    nil
  else
    execute_complete
    self
  end
end

#called?Boolean

Returns true if this response has been called (requested and populated) yet

Returns:

  • (Boolean)

131
132
133
# File 'lib/manticore/response.rb', line 131

def called?
  !!@called
end

#cookiesArray<Manticore::Cookie>

Returns an array of Cookies associated with this request's execution context

Returns:


180
181
182
183
184
185
186
187
188
189
190
# File 'lib/manticore/response.rb', line 180

def cookies
  call_once
  @cookies ||= begin
    @context.get_cookie_store.get_cookies.inject({}) do |all, java_cookie|
      c = Cookie.from_java(java_cookie)
      all[c.name] ||= []
      all[c.name] << c
      all
    end
  end
end

#final_urlString

Fetch the final resolved URL for this response. Will call the request if it has not been called yet.

Returns:

  • (String)

91
92
93
94
95
96
97
98
# File 'lib/manticore/response.rb', line 91

def final_url
  call_once
  last_request = context.get_attribute ExecutionContext.HTTP_REQUEST
  last_host = context.get_attribute ExecutionContext.HTTP_TARGET_HOST
  host = last_host.to_uri
  url = last_request.get_uri
  URI.join(host, url.to_s)
end

#lengthInteger

Returns the length of the response body. Returns -1 if content-length is not present in the response.

Returns:

  • (Integer)

173
174
175
# File 'lib/manticore/response.rb', line 173

def length
  (headers["content-length"] || -1).to_i
end

#messageString

Return the response text for a request as a string (Not Found, Ok, Bad Request, etc). Will call the request if it has not been called yet.

Returns:

  • (String)

    The response code text


165
166
167
168
# File 'lib/manticore/response.rb', line 165

def message
  call_once
  @message
end

#on_cancelled(&block) ⇒ Object Also known as: cancelled, cancellation, on_cancellation

Set handler for cancelled requests. NB: Not actually used right now?

Parameters:

  • block

    Proc which will be invoked on a on a cancelled response.

Returns:

  • self


219
220
221
222
# File 'lib/manticore/response.rb', line 219

def on_cancelled(&block)
  @handlers[:cancelled] = block
  self
end

#on_complete(&block) ⇒ Object Also known as: complete, completed, on_completed

Set handler for completed requests

Parameters:

  • block

    Proc which will be invoked on a on a completed response. This handler will be called regardless of request success/failure.

Returns:

  • self


232
233
234
235
# File 'lib/manticore/response.rb', line 232

def on_complete(&block)
  @handlers[:complete] = Array(@handlers[:complete]).compact + [block]
  self
end

#on_failure(&block) ⇒ Object Also known as: failure, fail

Set handler for failure responses

Parameters:

  • block

    Proc which will be invoked on a on a failed response. Block will receive an exception object.

Returns:

  • self


207
208
209
210
# File 'lib/manticore/response.rb', line 207

def on_failure(&block)
  @handlers[:failure] = block
  self
end

#on_success(&block) ⇒ Object Also known as: success

Set handler for success responses

Parameters:

  • block

    Proc which will be invoked on a successful response. Block will receive |response, request|

Returns:

  • self


196
197
198
199
# File 'lib/manticore/response.rb', line 196

def on_success(&block)
  @handlers[:success] = block
  self
end

#times_retriedObject


241
242
243
# File 'lib/manticore/response.rb', line 241

def times_retried
  @context.get_attribute("retryCount") || 0
end