aeden-jruby-http-reactor 0.3.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -29,6 +29,7 @@ begin
29
29
  gemspec.description = ""
30
30
  gemspec.authors = ["Anthony Eden"]
31
31
  gemspec.files.exclude 'docs/**/*'
32
+ gemspec.files.exclude '.gitignore'
32
33
  end
33
34
  rescue LoadError
34
35
  puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.5.0
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{jruby-http-reactor}
5
- s.version = "0.3.1"
5
+ s.version = "0.5.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Anthony Eden"]
9
- s.date = %q{2009-09-15}
9
+ s.date = %q{2009-09-16}
10
10
  s.description = %q{}
11
11
  s.email = %q{anthonyeden@gmail.com}
12
12
  s.extra_rdoc_files = [
@@ -14,8 +14,7 @@ Gem::Specification.new do |s|
14
14
  "README.textile"
15
15
  ]
16
16
  s.files = [
17
- ".gitignore",
18
- "README.rdoc",
17
+ "README.rdoc",
19
18
  "README.textile",
20
19
  "Rakefile",
21
20
  "VERSION",
@@ -9,6 +9,8 @@ module HttpReactor #:nodoc:
9
9
 
10
10
  HTTP_TARGET_PATH = 'http_target_path'
11
11
  HTTP_TARGET_REQUEST = 'http_target_request'
12
+ IO_REACTOR = "io_reactor"
13
+ REDIRECT_HISTORY = "redirect_history"
12
14
 
13
15
  def initialize(request_count, handler_proc)
14
16
  @request_count = request_count
@@ -19,6 +21,8 @@ module HttpReactor #:nodoc:
19
21
  context.set_attribute(ExecutionContext.HTTP_TARGET_HOST, attachment[:host])
20
22
  context.set_attribute(HTTP_TARGET_PATH, attachment[:path])
21
23
  context.set_attribute(HTTP_TARGET_REQUEST, attachment[:request])
24
+ context.set_attribute(IO_REACTOR, attachment[:io_reactor])
25
+ context.set_attribute(REDIRECT_HISTORY, attachment[:redirect_history] || [])
22
26
  end
23
27
 
24
28
  def finalize_context(context)
@@ -49,12 +53,29 @@ module HttpReactor #:nodoc:
49
53
 
50
54
  def handle_response(response, context)
51
55
  begin
52
- @handler_proc.call(HttpReactor::Response.new(response), context)
53
-
54
- context.setAttribute(RESPONSE_RECEIVED, true)
55
-
56
- # Signal completion of the request execution
57
- @request_count.count_down()
56
+ res = HttpReactor::Response.new(response)
57
+ case res.code
58
+ when 301, 302
59
+ redirect_to = res.headers['Location']
60
+ redirect_history = context.getAttribute(REDIRECT_HISTORY)
61
+ if redirect_history.include?(redirect_to)
62
+ puts "Too many redirects"
63
+ context.setAttribute(RESPONSE_RECEIVED, true)
64
+ @request_count.count_down()
65
+ else
66
+ puts "Redirecting to #{redirect_to}"
67
+ redirect_history << redirect_to
68
+ context.setAttribute(REDIRECT_HISTORY, redirect_history)
69
+ requests = [HttpReactor::Request.new(URI.parse(redirect_to))]
70
+ io_reactor = context.getAttribute(IO_REACTOR)
71
+ HttpReactor::Client.process_requests(requests, io_reactor, @request_count)
72
+ end
73
+ else
74
+ @handler_proc.call(res, context)
75
+ context.setAttribute(RESPONSE_RECEIVED, true)
76
+ # Signal completion of the request execution
77
+ @request_count.count_down()
78
+ end
58
79
  rescue => e
59
80
  puts "Error handling response: #{e.message}"
60
81
  end
@@ -135,7 +156,6 @@ module HttpReactor #:nodoc:
135
156
  def initialize(requests=[], handler_proc=nil, options={}, &block)
136
157
  handler_proc = block if block_given?
137
158
  handler_proc ||= default_handler_proc
138
- session_request_callback = SessionRequestCallback
139
159
 
140
160
  initialize_options(options)
141
161
 
@@ -152,11 +172,11 @@ module HttpReactor #:nodoc:
152
172
 
153
173
  # We are going to use this object to synchronize between the
154
174
  # I/O event and main threads
155
- request_count = java.util.concurrent.CountDownLatch.new(requests.length);
175
+ request_counter = java.util.concurrent.CountDownLatch.new(requests.length);
156
176
 
157
177
  handler = BufferingHttpClientHandler.new(
158
178
  httpproc,
159
- RequestExecutionHandler.new(request_count, handler_proc),
179
+ RequestExecutionHandler.new(request_counter, handler_proc),
160
180
  org.apache.http.impl.DefaultConnectionReuseStrategy.new,
161
181
  params
162
182
  )
@@ -178,30 +198,39 @@ module HttpReactor #:nodoc:
178
198
  puts "Shutdown"
179
199
  end
180
200
 
201
+ process_requests(requests, io_reactor, request_counter)
202
+
203
+ # Block until all connections signal
204
+ # completion of the request execution
205
+ request_counter.await()
206
+
207
+ puts "Shutting down I/O reactor"
208
+
209
+ io_reactor.shutdown()
210
+
211
+ puts "Done"
212
+ end
213
+
214
+ def process_requests(requests, io_reactor, request_counter)
215
+ HttpReactor::Client.process_requests(requests, io_reactor, request_counter)
216
+ end
217
+
218
+ def self.process_requests(requests, io_reactor, request_counter)
181
219
  requests.each do |request|
182
220
  uri = request.uri
183
221
  attachment = {
184
222
  :host => HttpHost.new(uri.host),
185
223
  :path => uri.request_uri,
186
- :request => request
224
+ :request => request,
225
+ :io_reactor => io_reactor
187
226
  }
188
227
  io_reactor.connect(
189
228
  java.net.InetSocketAddress.new(uri.host, uri.port),
190
229
  nil,
191
230
  attachment,
192
- session_request_callback.new(request_count)
231
+ SessionRequestCallback.new(request_counter)
193
232
  )
194
233
  end
195
-
196
- # Block until all connections signal
197
- # completion of the request execution
198
- request_count.await()
199
-
200
- puts "Shutting down I/O reactor"
201
-
202
- io_reactor.shutdown()
203
-
204
- puts "Done"
205
234
  end
206
235
 
207
236
  private
@@ -23,23 +23,39 @@ module HttpReactor #:nodoc:
23
23
 
24
24
  # Get the response content type
25
25
  def content_type
26
- @response_impl.entity.content_type.value
26
+ @content_type ||= @response_impl.entity.content_type.value
27
27
  end
28
28
 
29
29
  # Get the response content length
30
30
  def content_length
31
- @response_impl.entity.content_length
31
+ @content_length ||= @response_impl.entity.content_length
32
+ end
33
+
34
+ def headers
35
+ @headers ||= begin
36
+ h = Hash.new
37
+ @response_impl.all_headers.each do |header|
38
+ if h[header.name]
39
+ h[header.name] = [h[header.name], header.value]
40
+ else
41
+ h[header.name] = header.value
42
+ end
43
+ end
44
+ h
45
+ end
32
46
  end
33
47
 
34
48
  # Get the body text
35
49
  def body
36
- begin
37
- io = Java.java_to_ruby(
38
- org.jruby.RubyIO.new(JRuby.runtime, entity.content).java_object
39
- )
40
- io.read
41
- rescue Exception => e
42
- puts "Error in Response#body: #{e.message}"
50
+ @body ||= begin
51
+ begin
52
+ io = Java.java_to_ruby(
53
+ org.jruby.RubyIO.new(JRuby.runtime, entity.content).java_object
54
+ )
55
+ io.read
56
+ rescue Exception => e
57
+ puts "Error in Response#body: #{e.message}"
58
+ end
43
59
  end
44
60
  end
45
61
  end
data/test/client_test.rb CHANGED
@@ -5,7 +5,7 @@ require 'mime/types'
5
5
  class ClientTest < Test::Unit::TestCase
6
6
  def requests
7
7
  @requests ||= [
8
- 'http://www.yahoo.com/',
8
+ 'http://yahoo.com/',
9
9
  'http://www.google.com/',
10
10
  'http://www.apache.org/',
11
11
  'http://anthony.mp/about_me',
@@ -38,14 +38,11 @@ class ClientTest < Test::Unit::TestCase
38
38
  puts "request ur: #{context.getAttribute('http_target_request').uri}"
39
39
  puts "content-length: #{response.content_length}"
40
40
  assert response.body.length > 0
41
- end
42
- end
43
-
44
- def test_body
45
- HttpReactor::Client.new(requests) do |response, context|
46
- if response.code == 200
47
- puts response.body
48
- end
41
+ puts "=== HEADERS ==="
42
+ puts response.headers.inspect
43
+ # puts "===== BODY ===="
44
+ # puts response.body
45
+ # puts "==============="
49
46
  end
50
47
  end
51
48
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aeden-jruby-http-reactor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anthony Eden
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-15 00:00:00 -07:00
12
+ date: 2009-09-16 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -23,7 +23,6 @@ extra_rdoc_files:
23
23
  - README.rdoc
24
24
  - README.textile
25
25
  files:
26
- - .gitignore
27
26
  - README.rdoc
28
27
  - README.textile
29
28
  - Rakefile
data/.gitignore DELETED
@@ -1,2 +0,0 @@
1
- rdoc
2
- pkg