spiegela-jruby-httpclient 0.1.0-java

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :development do
4
+ gem "bundler", "~> 1.0.0"
5
+ gem "jeweler", "~> 1.6.2"
6
+ gem "rcov", ">= 0"
7
+ gem "rdoc"
8
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.6.2)
6
+ bundler (~> 1.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.9.2)
10
+ rcov (0.9.9-java)
11
+ rdoc (3.6.1)
12
+
13
+ PLATFORMS
14
+ java
15
+
16
+ DEPENDENCIES
17
+ bundler (~> 1.0.0)
18
+ jeweler (~> 1.6.2)
19
+ rcov
20
+ rdoc
data/LICENSE.txt ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/README.rdoc ADDED
@@ -0,0 +1,27 @@
1
+ = jruby-httpclient
2
+
3
+ jruby-httpclient is a thin wrapper around Apache's HttpClient (version 4.1). I found that Net::HTTP was
4
+ not threadsafe in JRuby. This project is an attempt to make a threadsafe HTTP client for JRuby.
5
+
6
+ == Usage
7
+
8
+ client = HTTP::Client.new(:host => "localhost", :port => 8080)
9
+ client.get("/src", :param => "value")
10
+
11
+ client.post("/create", :param => "value")
12
+
13
+ == Contributing to jruby-httpclient
14
+
15
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
16
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
17
+ * Fork the project
18
+ * Start a feature/bugfix branch
19
+ * Commit and push until you are happy with your contribution
20
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
21
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
22
+
23
+ == Copyright
24
+
25
+ Copyright (c) 2011 Adam Esterline. See LICENSE.txt for
26
+ further details.
27
+
data/Rakefile ADDED
@@ -0,0 +1,54 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "spiegela-jruby-httpclient"
18
+ gem.homepage = "http://github.com/spiegela/jruby-httpclient"
19
+ gem.license = "Apache 2.0"
20
+ gem.summary = %Q{A thin wrapper around the Apache HttpClient}
21
+ gem.description = %Q{An HTTP client designed for use with JRuby in a threaded environment}
22
+ gem.email = "adam@esterlines.com"
23
+ gem.authors = ["Adam Esterline", "Aaron Spiegel"]
24
+ gem.platform = Gem::Platform::JAVA
25
+ # dependencies defined in Gemfile
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ test.rcov_opts << '--exclude "gems/*"'
42
+ end
43
+
44
+ task :default => :test
45
+
46
+ require 'rdoc/task'
47
+ RDoc::Task.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "http_client #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,16 @@
1
+ HTTP_CLIENT_DIR = File.join(File.dirname(__FILE__), 'http_client')
2
+ VENDOR_DIR = File.expand_path(File.join(File.dirname(__FILE__), '..', 'vendor'))
3
+
4
+ $LOAD_PATH.unshift(HTTP_CLIENT_DIR) unless $LOAD_PATH.include?(HTTP_CLIENT_DIR)
5
+ $LOAD_PATH.unshift(VENDOR_DIR) unless $LOAD_PATH.include?(VENDOR_DIR)
6
+
7
+ require 'java'
8
+ require 'commons-logging-1.1.1'
9
+ require 'commons-codec-1.4'
10
+ require 'httpcore-4.1'
11
+ require 'httpmime-4.1.1'
12
+ require 'httpclient-4.1.1'
13
+ require 'httpclient-cache-4.1.1'
14
+
15
+ require 'client'
16
+ require 'methods'
@@ -0,0 +1,206 @@
1
+ require 'uri'
2
+ module HTTP
3
+ DefaultHttpClient = org.apache.http.impl.client.DefaultHttpClient
4
+ BasicResponseHandler = org.apache.http.impl.client.BasicResponseHandler
5
+ BasicHttpParams = org.apache.http.params.BasicHttpParams
6
+ HttpHost = org.apache.http.HttpHost
7
+ CoreProtocolPNames = org.apache.http.params.CoreProtocolPNames
8
+ CoreConnectionPNames = org.apache.http.params.CoreConnectionPNames
9
+ ConnRoutePNames = org.apache.http.conn.params.ConnRoutePNames
10
+ CookieSpecPNames = org.apache.http.cookie.params.CookieSpecPNames
11
+ AuthPNames = org.apache.http.auth.params.AuthPNames
12
+ ClientPNames = org.apache.http.client.params.ClientPNames
13
+ SocketTimeoutException = java.net.SocketTimeoutException
14
+ class Client
15
+ CLIENT_PARAMETERS = {
16
+ :protocol_version => HTTP::CoreProtocolPNames::PROTOCOL_VERSION,
17
+ :strict_transfer_encoding => HTTP::CoreProtocolPNames::STRICT_TRANSFER_ENCODING,
18
+ :http_element_charset => HTTP::CoreProtocolPNames::HTTP_ELEMENT_CHARSET,
19
+ :use_expect_continue => HTTP::CoreProtocolPNames::USE_EXPECT_CONTINUE,
20
+ :wait_for_continue => HTTP::CoreProtocolPNames::WAIT_FOR_CONTINUE,
21
+ :user_agent => HTTP::CoreProtocolPNames::USER_AGENT,
22
+ :tcp_nodelay => HTTP::CoreConnectionPNames.TCP_NODELAY,
23
+ :so_timeout => HTTP::CoreConnectionPNames.SO_TIMEOUT,
24
+ :so_linger => HTTP::CoreConnectionPNames.SO_LINGER,
25
+ :so_reuseaddr => HTTP::CoreConnectionPNames.SO_REUSEADDR,
26
+ :socket_buffer_size => HTTP::CoreConnectionPNames.SOCKET_BUFFER_SIZE,
27
+ :connection_timeout => HTTP::CoreConnectionPNames.CONNECTION_TIMEOUT,
28
+ :max_line_length => HTTP::CoreConnectionPNames.MAX_LINE_LENGTH,
29
+ :max_header_count => HTTP::CoreConnectionPNames.MAX_HEADER_COUNT,
30
+ :stale_connection_check => HTTP::CoreConnectionPNames.STALE_CONNECTION_CHECK,
31
+ # :forced_route => HTTP::ConnRoutePNames::FORCED_ROUTE, # not implemented
32
+ :local_address => HTTP::ConnRoutePNames::LOCAL_ADDRESS,
33
+ :default_proxy => HTTP::ConnRoutePNames::DEFAULT_PROXY,
34
+ :date_patterns => HTTP::CookieSpecPNames::DATE_PATTERNS,
35
+ :single_cookie_header => HTTP::CookieSpecPNames::SINGLE_COOKIE_HEADER,
36
+ :credential_charset => HTTP::AuthPNames::CREDENTIAL_CHARSET,
37
+ :cookie_policy => HTTP::ClientPNames::COOKIE_POLICY,
38
+ :handle_authentication => HTTP::ClientPNames::HANDLE_AUTHENTICATION,
39
+ :handle_redirects => HTTP::ClientPNames::HANDLE_REDIRECTS,
40
+ :max_redirects => HTTP::ClientPNames::MAX_REDIRECTS,
41
+ :allow_circular_redirects => HTTP::ClientPNames::ALLOW_CIRCULAR_REDIRECTS,
42
+ :virtual_host => HTTP::ClientPNames::VIRTUAL_HOST,
43
+ :default_host => HTTP::ClientPNames::DEFAULT_HOST
44
+ # :default_headers => HTTP::ClientPNames::DEFAULT_HEADERS, # not implemented
45
+ # :connection_manager_factory_class_name => HTTP::ClientPNames::CONNECTION_MANAGER_FACTORY_CLASS_NAME # not implemented
46
+ }
47
+
48
+ INTEGER_PARAMETER_SETTERS = [
49
+ :so_timeout, :so_linger, :socket_buffer_size, :connection_timeout,
50
+ :max_line_length, :max_header_count, :max_redirects
51
+ ]
52
+
53
+ SIMPLE_PARAMETER_SETTERS = [
54
+ :strict_transfer_encoding, :http_element_charset, :use_expect_continue,
55
+ :wait_for_continue, :user_agent, :tcp_nodelay, :so_reuseaddr,
56
+ :stale_connection_check, :date_patterns, :single_cookie_header,
57
+ :credential_charset, :cookie_policy, :handle_authentication,
58
+ :handle_redirects, :allow_circular_redirects
59
+ ]
60
+
61
+ def initialize(*params)
62
+ self.class.class_eval do
63
+ # Setup dynamic getters (setters can be more complex)
64
+ CLIENT_PARAMETERS.each do |method_name, param_class|
65
+ define_method method_name do
66
+ @params.get_parameter param_class
67
+ end
68
+ end
69
+
70
+ INTEGER_PARAMETER_SETTERS.each do |method_name|
71
+ define_method "#{method_name}=" do |arg|
72
+ @params.set_int_parameter CLIENT_PARAMETERS[method_name], arg
73
+ end
74
+ end
75
+
76
+ # For those params that our simple, we'll create the setters
77
+ SIMPLE_PARAMETER_SETTERS.each do |method_name|
78
+ define_method "#{method_name}=" do |arg|
79
+ @params.set_parameter CLIENT_PARAMETERS[method_name], arg
80
+ end
81
+ end
82
+ end
83
+
84
+ # Parse the remaining options
85
+ options = parse_options(params)
86
+ host = options[:host] || "localhost"
87
+ port = options[:port] || 8080
88
+ protocol = options[:scheme] || "http"
89
+ base_path = options[:base_path] || ""
90
+
91
+ if options[:disable_response_handler]
92
+ @response_handler = nil
93
+ elsif options[:response_handler]
94
+ @response_handler = options[:response_handler]
95
+ else
96
+ BasicResponseHandler.new
97
+ end
98
+
99
+ @uri_builder = URIBuilder.new(protocol, host, port, base_path)
100
+ @encoding = options[:encoding] || "UTF-8"
101
+ @params = BasicHttpParams.new
102
+ DefaultHttpClient.set_default_http_params(@params)
103
+
104
+ # Handle a custom setting for "timemout_in_seconds" and or "so_timeout"
105
+ if options[:timeout_in_seconds]
106
+ self.so_timeout = options[:timeout_in_seconds] * 1000
107
+ elsif options[:so_timeout]
108
+ self.so_timeout = options[:so_timeout]
109
+ else
110
+ self.so_timeout = 30000
111
+ end
112
+
113
+ # Set options from the rest of the options-hash
114
+ CLIENT_PARAMETERS.each do |method_name, param_class|
115
+ self.send("#{method_name}=", options[method_name]) if options[method_name]
116
+ end
117
+ end
118
+
119
+ # Advanced Setters
120
+
121
+ def protocol_version=(version_string)
122
+ protocol, major_version, minor_version = version_string.split(/[\.|\s|\/]/)
123
+ @params.set_parameter HTTP::CoreProtocolPNames::PROTOCOL_VERSION, org.apache.http.ProtocolVersion.new(protocol, major_version.to_i, minor_version.to_i)
124
+ end
125
+
126
+ def local_address=(local_addr_str)
127
+ @params.set_parameter HTTP::ConnRoutePNames::LOCAL_ADDRESS, java.net.InetAddress.getByName(local_addr_str)
128
+ end
129
+
130
+ def default_proxy=(host)
131
+ uri = URI.parse host
132
+ @params.set_parameter HTTP::ConnRoutePNames::DEFAULT_PROXY, HTTP::HttpHost.new(uri.host, uri.port, uri.scheme)
133
+ end
134
+
135
+ def virtual_host=(host)
136
+ uri = URI.parse host
137
+ @params.set_parameter HTTP::ClientPNames::VIRTUAL_HOST, HTTP::HttpHost.new(uri.host, uri.port, uri.scheme)
138
+ end
139
+
140
+ def default_host=(host)
141
+ uri = URI.parse host
142
+ @params.set_parameter HTTP::ClientPNames::DEFAULT_HOST, HTTP::HttpHost.new(uri.host, uri.port, uri.scheme)
143
+ end
144
+
145
+ # Request Methods
146
+
147
+ def get(params, options = {})
148
+ execute(Get.new(params, options))
149
+ end
150
+
151
+ def post(params, options = {})
152
+ execute(Post.new(params, options))
153
+ end
154
+
155
+ def delete(path)
156
+ execute(Delete.new(path))
157
+ end
158
+
159
+ def put(path)
160
+ execute(Put.new(path))
161
+ end
162
+
163
+ def execute(request)
164
+ native_request = request.create_native_request(@uri_builder, @encoding)
165
+ client = DefaultHttpClient.new(@params)
166
+ if @response_handler
167
+ client.execute(native_request, @response_handler)
168
+ else
169
+ client.execute(native_request)
170
+ end
171
+ rescue SocketTimeoutException
172
+ raise Timeout::Error, "timed out after #{so_timeout} ms"
173
+ ensure
174
+ client.connection_manager.shutdown
175
+ end
176
+
177
+ private
178
+ def parse_options(params)
179
+ options = {}
180
+
181
+ params.reverse.each do |param|
182
+ if param.kind_of?(Hash)
183
+ options = param.merge(options)
184
+ else
185
+ uri = URI.parse(param)
186
+ options = {:host => uri.host, :port => uri.port, :scheme => uri.scheme, :base_path => uri.path}.merge(options)
187
+ end
188
+ end
189
+
190
+ options
191
+ end
192
+ end
193
+
194
+ class URIBuilder
195
+ def initialize(protocol, host, port, base_path)
196
+ @protocol = protocol
197
+ @host = host
198
+ @port = port
199
+ @base_path = base_path
200
+ end
201
+
202
+ def create_uri(path, query_string = nil)
203
+ URIUtils.create_uri(@protocol, @host, @port, "#{@base_path}#{path}", query_string, nil)
204
+ end
205
+ end
206
+ end
@@ -0,0 +1,56 @@
1
+ module HTTP
2
+ class Request
3
+ def self.create_type(&native_request_factory)
4
+ Class.new do
5
+ def initialize(path, params = {})
6
+ @path = path
7
+ @params = params
8
+ @headers = {}
9
+ end
10
+
11
+ def add_headers(headers)
12
+ @headers.merge!(headers)
13
+ end
14
+
15
+ define_method(:create_native_request) do |uri_builder, encoding|
16
+ params = @params.collect { |key, value| BasicNameValuePair.new(key.to_s, value.to_s) }
17
+ request = native_request_factory.call(uri_builder, @path, params, encoding)
18
+
19
+ @headers.each { |name, value| request.add_header(name.to_s, value.to_s) }
20
+
21
+ request
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ Post = Request.create_type do |uri_builder, path, params, encoding|
28
+ post = HttpPost.new(uri_builder.create_uri(path))
29
+ post.entity = UrlEncodedFormEntity.new(params, encoding)
30
+ post
31
+ end
32
+
33
+ Get = Request.create_type do |uri_builder, path, params, encoding|
34
+ query_string = URLEncodedUtils.format(params, encoding)
35
+ get = HttpGet.new(uri_builder.create_uri(path, query_string))
36
+ get
37
+ end
38
+
39
+ Delete = Request.create_type do |uri_builder, path|
40
+ HttpDelete.new(uri_builder.create_uri(path))
41
+ end
42
+
43
+ Put = Request.create_type do |uri_builder, path|
44
+ HttpPut.new(uri_builder.create_uri(path))
45
+ end
46
+
47
+ private
48
+ HttpGet = org.apache.http.client.methods.HttpGet
49
+ HttpPost = org.apache.http.client.methods.HttpPost
50
+ HttpDelete = org.apache.http.client.methods.HttpDelete
51
+ HttpPut = org.apache.http.client.methods.HttpPut
52
+ BasicNameValuePair = org.apache.http.message.BasicNameValuePair
53
+ URIUtils = org.apache.http.client.utils.URIUtils
54
+ URLEncodedUtils = org.apache.http.client.utils.URLEncodedUtils
55
+ UrlEncodedFormEntity = org.apache.http.client.entity.UrlEncodedFormEntity
56
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,23 @@
1
+ TEST_DIR = File.dirname(__FILE__)
2
+ LIB_DIR = File.join(TEST_DIR, '..', 'lib')
3
+
4
+ require 'rubygems'
5
+ require 'bundler'
6
+
7
+ begin
8
+ Bundler.setup(:default, :development)
9
+ rescue Bundler::BundlerError => e
10
+ $stderr.puts e.message
11
+ $stderr.puts "Run `bundle install` to install missing gems"
12
+ exit e.status_code
13
+ end
14
+ require 'test/unit'
15
+
16
+ $LOAD_PATH.unshift(TEST_DIR) unless $LOAD_PATH.include?(TEST_DIR)
17
+ $LOAD_PATH.unshift(LIB_DIR) unless $LOAD_PATH.include?(LIB_DIR)
18
+
19
+ require 'http_client'
20
+ require 'webrick'
21
+ require 'http_test_server'
22
+
23
+ HTTP::TestServer.start_server
@@ -0,0 +1,31 @@
1
+ require 'helper'
2
+
3
+ class TestBasicClientOperations < Test::Unit::TestCase
4
+ def test_simple_get
5
+ result = @client.get("/echo", :content => "hello")
6
+
7
+ assert_equal("hello", result)
8
+ end
9
+
10
+ def test_simple_post
11
+ result = @client.post("/echo", :content => "world")
12
+
13
+ assert_equal("world", result)
14
+ end
15
+
16
+ def test_simple_delete
17
+ result = @client.delete("/echo")
18
+
19
+ assert_equal("delete", result)
20
+ end
21
+
22
+ def test_simple_put
23
+ result = @client.put("/echo")
24
+
25
+ assert_equal("put", result)
26
+ end
27
+
28
+ def setup
29
+ @client = HTTP::Client.new(:host => "localhost", :port => 8080)
30
+ end
31
+ end
@@ -0,0 +1,31 @@
1
+ require 'helper'
2
+
3
+ class TestClientCreation < Test::Unit::TestCase
4
+ def test_can_be_created_with_options
5
+ client = HTTP::Client.new(:host => "localhost", :port => 8080)
6
+ result = client.get("/echo", :content => "hi")
7
+
8
+ assert_equal("hi", result)
9
+ end
10
+
11
+ def test_can_be_created_with_base_url
12
+ client = HTTP::Client.new("http://localhost:8080")
13
+ result = client.get("/echo", :content => "hi there")
14
+
15
+ assert_equal("hi there", result)
16
+ end
17
+
18
+ def test_can_be_created_with_base_url_and_options
19
+ client = HTTP::Client.new("http://localhost", :port => 8080)
20
+ result = client.get("/echo", :content => "me too")
21
+
22
+ assert_equal("me too", result)
23
+ end
24
+
25
+ def test_can_create_with_url_that_has_path
26
+ client = HTTP::Client.new("http://localhost/echo", :port => 8080)
27
+ result = client.get("/", :content => "me three")
28
+
29
+ assert_equal("me three", result)
30
+ end
31
+ end
@@ -0,0 +1,48 @@
1
+ require 'helper'
2
+
3
+ class TestClientHeaders < Test::Unit::TestCase
4
+ def test_get_headers
5
+ get = HTTP::Get.new("/echo_test_header")
6
+ get.add_headers(:test_header => "get testing")
7
+
8
+ response = @client.execute(get)
9
+ assert_equal("get testing", response)
10
+ end
11
+
12
+ def test_post_headers
13
+ get = HTTP::Post.new("/echo_test_header")
14
+ get.add_headers(:test_header => "post testing")
15
+
16
+ response = @client.execute(get)
17
+ assert_equal("post testing", response)
18
+ end
19
+
20
+ def test_delete_headers
21
+ get = HTTP::Delete.new("/echo_test_header")
22
+ get.add_headers(:test_header => "post testing")
23
+
24
+ response = @client.execute(get)
25
+ assert_equal("post testing", response)
26
+ end
27
+
28
+ def test_put_headers
29
+ get = HTTP::Put.new("/echo_test_header")
30
+ get.add_headers(:test_header => "post testing")
31
+
32
+ response = @client.execute(get)
33
+ assert_equal("post testing", response)
34
+ end
35
+
36
+ def test_multiple_calls_to_add_headers_should_prefer_last_set_of_headers
37
+ get = HTTP::Get.new("/echo_test_header")
38
+ get.add_headers(:test_header => "get testing")
39
+ get.add_headers(:test_header => "should prefer this one")
40
+
41
+ response = @client.execute(get)
42
+ assert_equal("should prefer this one", response)
43
+ end
44
+
45
+ def setup
46
+ @client = HTTP::Client.new(:host => "localhost", :port => 8080)
47
+ end
48
+ end
@@ -0,0 +1,195 @@
1
+ require 'helper'
2
+
3
+ class TestClientParameters < Test::Unit::TestCase
4
+ def test_timeout
5
+ client = HTTP::Client.new(:host => "localhost", :port => 8080, :timeout_in_seconds => 2)
6
+
7
+ assert_raises(Timeout::Error) do
8
+ client.get("/slow", :sleep => "30")
9
+ end
10
+ end
11
+
12
+ def test_protocol_version
13
+ client = HTTP::Client.new(:host => "localhost", :port => 8080, :protocol_version => "HTTP 1.0")
14
+ result = client.get("/echo", :content => "hello")
15
+ # We did set the parameter, right?
16
+ assert_equal(client.protocol_version.to_s, "HTTP/1.0")
17
+ # Make sure the client is still working (badly set params would prevent this)
18
+ assert_equal("hello", result)
19
+ end
20
+
21
+ def test_strict_transfer_encodeing
22
+ client = HTTP::Client.new(:host => "localhost", :port => 8080, :strict_transfer_encoding => true)
23
+ result = client.get("/echo", :content => "hello")
24
+ assert_equal(client.strict_transfer_encoding, true)
25
+ assert_equal("hello", result)
26
+ end
27
+
28
+ def test_http_element_charset
29
+ client = HTTP::Client.new(:host => "localhost", :port => 8080, :http_element_charset => "ASCII")
30
+ result = client.get("/echo", :content => "hello")
31
+ assert_equal(client.http_element_charset, "ASCII")
32
+ assert_equal("hello", result)
33
+ end
34
+
35
+ def test_use_expect_continue
36
+ client = HTTP::Client.new(:host => "localhost", :port => 8080, :use_expect_continue => true)
37
+ result = client.get("/echo", :content => "hello")
38
+ assert_equal(client.use_expect_continue, true)
39
+ assert_equal("hello", result)
40
+ end
41
+
42
+ def test_wait_for_continue
43
+ client = HTTP::Client.new(:host => "localhost", :port => 8080, :wait_for_continue => true)
44
+ result = client.get("/echo", :content => "hello")
45
+ assert_equal(client.wait_for_continue, true)
46
+ assert_equal("hello", result)
47
+ end
48
+
49
+ def test_user_agent
50
+ client = HTTP::Client.new(:host => "localhost", :port => 8080, :user_agent => "Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10.5; en-US; rv:1.9.0.3) Gecko/2008092414 Firefox/3.0.3")
51
+ result = client.get("/echo", :content => "hello")
52
+ assert_equal(client.user_agent, "Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10.5; en-US; rv:1.9.0.3) Gecko/2008092414 Firefox/3.0.3")
53
+ assert_equal("hello", result)
54
+ end
55
+
56
+ def test_user_agent
57
+ client = HTTP::Client.new(:host => "localhost", :port => 8080, :tcp_nodelay => true)
58
+ result = client.get("/echo", :content => "hello")
59
+ assert_equal(client.tcp_nodelay, true)
60
+ assert_equal("hello", result)
61
+ end
62
+
63
+ def test_so_linger
64
+ client = HTTP::Client.new(:host => "localhost", :port => 8080, :so_linger => 3000)
65
+ result = client.get("/echo", :content => "hello")
66
+ assert_equal(client.so_linger, 3000)
67
+ assert_equal("hello", result)
68
+ end
69
+
70
+ def test_so_reuseaddr
71
+ client = HTTP::Client.new(:host => "localhost", :port => 8080, :so_reuseaddr => true)
72
+ result = client.get("/echo", :content => "hello")
73
+ assert_equal(client.so_reuseaddr, true)
74
+ assert_equal("hello", result)
75
+ end
76
+
77
+ def test_socket_buffer_size
78
+ client = HTTP::Client.new(:host => "localhost", :port => 8080, :socket_buffer_size => 10000)
79
+ result = client.get("/echo", :content => "hello")
80
+ assert_equal(client.socket_buffer_size, 10000)
81
+ assert_equal("hello", result)
82
+ end
83
+
84
+ def test_connection_timeout
85
+ client = HTTP::Client.new(:host => "localhost", :port => 8080, :connection_timeout => 2)
86
+ result = client.get("/echo", :content => "hello")
87
+ assert_equal(client.connection_timeout, 2)
88
+ assert_equal("hello", result)
89
+ end
90
+
91
+ def test_max_line_length
92
+ client = HTTP::Client.new(:host => "localhost", :port => 8080, :max_line_length => 2)
93
+ result = client.get("/echo", :content => "hello")
94
+ assert_equal(client.max_line_length, 2)
95
+ assert_equal("hello", result)
96
+ end
97
+
98
+ def test_max_header_count
99
+ client = HTTP::Client.new(:host => "localhost", :port => 8080, :max_header_count => 10)
100
+ result = client.get("/echo", :content => "hello")
101
+ assert_equal(client.max_header_count, 10)
102
+ assert_equal("hello", result)
103
+ end
104
+
105
+ def test_stale_connection_check
106
+ client = HTTP::Client.new(:host => "localhost", :port => 8080, :stale_connection_check => true)
107
+ result = client.get("/echo", :content => "hello")
108
+ assert_equal(client.stale_connection_check, true)
109
+ assert_equal("hello", result)
110
+ end
111
+
112
+ def test_local_address
113
+ client = HTTP::Client.new(:host => "localhost", :port => 8080, :local_address => "127.0.0.1")
114
+ result = client.get("/echo", :content => "hello")
115
+ assert_equal(client.local_address.get_host_address, "127.0.0.1")
116
+ assert_equal("hello", result)
117
+ end
118
+
119
+ def test_default_proxy
120
+ client = HTTP::Client.new(:host => "localhost", :port => 8080, :default_proxy => "http://127.0.0.1:8080")
121
+ result = client.get("/echo", :content => "hello")
122
+ assert_equal(client.default_proxy.to_s, "http://127.0.0.1:8080")
123
+ assert_equal("hello", result)
124
+ end
125
+
126
+ def test_date_patterns
127
+ client = HTTP::Client.new(:host => "localhost", :port => 8080, :date_patterns => ["MDy"])
128
+ result = client.get("/echo", :content => "hello")
129
+ assert(client.date_patterns.include? 'MDy')
130
+ assert_equal("hello", result)
131
+ end
132
+
133
+ def test_single_cookie_header
134
+ client = HTTP::Client.new(:host => "localhost", :port => 8080, :single_cookie_header => true)
135
+ result = client.get("/echo", :content => "hello")
136
+ assert_equal(client.single_cookie_header, true)
137
+ assert_equal("hello", result)
138
+ end
139
+
140
+ def test_credential_charset
141
+ client = HTTP::Client.new(:host => "localhost", :port => 8080, :credential_charset => "ASCII")
142
+ result = client.get("/echo", :content => "hello")
143
+ assert_equal(client.credential_charset, "ASCII")
144
+ assert_equal("hello", result)
145
+ end
146
+
147
+ def test_cookie_policy
148
+ client = HTTP::Client.new(:host => "localhost", :port => 8080, :cookie_policy => "RFC2109")
149
+ result = client.get("/echo", :content => "hello")
150
+ assert_equal(client.cookie_policy, "RFC2109")
151
+ assert_equal("hello", result)
152
+ end
153
+
154
+ def test_handle_authentication
155
+ client = HTTP::Client.new(:host => "localhost", :port => 8080, :handle_authentication => true)
156
+ result = client.get("/echo", :content => "hello")
157
+ assert_equal(client.handle_authentication, true)
158
+ assert_equal("hello", result)
159
+ end
160
+
161
+ def test_handle_redirects
162
+ client = HTTP::Client.new(:host => "localhost", :port => 8080, :handle_redirects => true)
163
+ result = client.get("/echo", :content => "hello")
164
+ assert_equal(client.handle_redirects, true)
165
+ assert_equal("hello", result)
166
+ end
167
+
168
+ def test_max_redirects
169
+ client = HTTP::Client.new(:host => "localhost", :port => 8080, :max_redirects => 1)
170
+ result = client.get("/echo", :content => "hello")
171
+ assert_equal(client.max_redirects, 1)
172
+ assert_equal("hello", result)
173
+ end
174
+
175
+ def test_allow_circular_redirects
176
+ client = HTTP::Client.new(:host => "localhost", :port => 8080, :allow_circular_redirects => true)
177
+ result = client.get("/echo", :content => "hello")
178
+ assert_equal(client.allow_circular_redirects, true)
179
+ assert_equal("hello", result)
180
+ end
181
+
182
+ def test_virtual_host
183
+ client = HTTP::Client.new(:host => "localhost", :port => 8080, :virtual_host => "http://127.0.0.1:80")
184
+ result = client.get("/echo", :content => "hello")
185
+ assert_equal(client.virtual_host.to_s, "http://127.0.0.1:80")
186
+ assert_equal("hello", result)
187
+ end
188
+
189
+ def test_default_host
190
+ client = HTTP::Client.new(:host => "localhost", :port => 8080, :default_host => "http://127.0.0.1:80")
191
+ result = client.get("/echo", :content => "hello")
192
+ assert_equal(client.default_host.to_s, "http://127.0.0.1:80")
193
+ assert_equal("hello", result)
194
+ end
195
+ end
@@ -0,0 +1,74 @@
1
+ module HTTP
2
+ module TestServer
3
+ SERVER = WEBrick::HTTPServer.new(:Port => 8080)
4
+
5
+ def self.start_server
6
+ SERVER.mount('/echo', EchoServlet)
7
+ SERVER.mount('/slow', SlowServlet)
8
+ SERVER.mount('/echo_test_header', HeaderEchoServlet)
9
+ Thread.new { SERVER.start }
10
+ end
11
+
12
+ class HeaderEchoServlet < WEBrick::HTTPServlet::AbstractServlet
13
+ def do_GET(request, response)
14
+ echo_header(request, response)
15
+ end
16
+
17
+ def do_POST(request, response)
18
+ echo_header(request, response)
19
+ end
20
+
21
+ def do_DELETE(request, response)
22
+ echo_header(request, response)
23
+ end
24
+
25
+ def do_PUT(request, response)
26
+ echo_header(request, response)
27
+ end
28
+
29
+ private
30
+ def echo_header(request, response)
31
+ response.status = 200
32
+ response['Content-Type'] = 'text/plain'
33
+ response.body = request.header["test_header"].first
34
+ end
35
+ end
36
+
37
+ class EchoServlet < WEBrick::HTTPServlet::AbstractServlet
38
+
39
+ def do_GET(request, response)
40
+ echo(request, response)
41
+ end
42
+
43
+ def do_POST(request, response)
44
+ echo(request, response)
45
+ end
46
+
47
+ def do_DELETE(request, response)
48
+ echo(request, response, "delete")
49
+ end
50
+
51
+ def do_PUT(request, response)
52
+ echo(request, response, "put")
53
+ end
54
+
55
+ private
56
+ def echo(request, response, canned_response = nil)
57
+ response.status = 200
58
+ response['Content-Type'] = 'text/plain'
59
+ response.body = canned_response || request.query['content']
60
+ end
61
+ end
62
+
63
+ class SlowServlet < WEBrick::HTTPServlet::AbstractServlet
64
+ def do_GET(request, response)
65
+ sleep_in_seconds = request.query["sleep"] || 30
66
+ sleep sleep_in_seconds.to_i
67
+
68
+ response.status = 200
69
+ response['Content-Type'] = 'text/plain'
70
+ response.body = "Done"
71
+ end
72
+ end
73
+ end
74
+ end
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spiegela-jruby-httpclient
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: java
7
+ authors:
8
+ - Adam Esterline
9
+ - Aaron Spiegel
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2011-06-27 00:00:00 -05:00
15
+ default_executable:
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: bundler
19
+ prerelease: false
20
+ requirement: &id001 !ruby/object:Gem::Requirement
21
+ none: false
22
+ requirements:
23
+ - - ~>
24
+ - !ruby/object:Gem::Version
25
+ version: 1.0.0
26
+ type: :development
27
+ version_requirements: *id001
28
+ - !ruby/object:Gem::Dependency
29
+ name: jeweler
30
+ prerelease: false
31
+ requirement: &id002 !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ~>
35
+ - !ruby/object:Gem::Version
36
+ version: 1.6.2
37
+ type: :development
38
+ version_requirements: *id002
39
+ - !ruby/object:Gem::Dependency
40
+ name: rcov
41
+ prerelease: false
42
+ requirement: &id003 !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ type: :development
49
+ version_requirements: *id003
50
+ - !ruby/object:Gem::Dependency
51
+ name: rdoc
52
+ prerelease: false
53
+ requirement: &id004 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ type: :development
60
+ version_requirements: *id004
61
+ description: An HTTP client designed for use with JRuby in a threaded environment
62
+ email: adam@esterlines.com
63
+ executables: []
64
+
65
+ extensions: []
66
+
67
+ extra_rdoc_files:
68
+ - LICENSE.txt
69
+ - README.rdoc
70
+ files:
71
+ - .document
72
+ - Gemfile
73
+ - Gemfile.lock
74
+ - LICENSE.txt
75
+ - README.rdoc
76
+ - Rakefile
77
+ - VERSION
78
+ - lib/http_client.rb
79
+ - lib/http_client/client.rb
80
+ - lib/http_client/methods.rb
81
+ - test/helper.rb
82
+ - test/http_client/test_basic_client_operations.rb
83
+ - test/http_client/test_client_creation.rb
84
+ - test/http_client/test_client_headers.rb
85
+ - test/http_client/test_client_parameters.rb
86
+ - test/http_test_server.rb
87
+ - vendor/commons-codec-1.4.jar
88
+ - vendor/commons-logging-1.1.1.jar
89
+ - vendor/httpclient-4.1.1.jar
90
+ - vendor/httpclient-cache-4.1.1.jar
91
+ - vendor/httpcore-4.1.jar
92
+ - vendor/httpmime-4.1.1.jar
93
+ has_rdoc: true
94
+ homepage: http://github.com/spiegela/jruby-httpclient
95
+ licenses:
96
+ - Apache 2.0
97
+ post_install_message:
98
+ rdoc_options: []
99
+
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: "0"
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: "0"
114
+ requirements: []
115
+
116
+ rubyforge_project:
117
+ rubygems_version: 1.5.1
118
+ signing_key:
119
+ specification_version: 3
120
+ summary: A thin wrapper around the Apache HttpClient
121
+ test_files: []
122
+