1
+ module HTTP
2
+ BasicHttpParams = org . apache . http . params . BasicHttpParams
3
+ HttpHost = org . apache . http . HttpHost
4
+ CoreProtocolPNames = org . apache . http . params . CoreProtocolPNames
5
+ CoreConnectionPNames = org . apache . http . params . CoreConnectionPNames
6
+ ConnRoutePNames = org . apache . http . conn . params . ConnRoutePNames
7
+ CookieSpecPNames = org . apache . http . cookie . params . CookieSpecPNames
8
+ AuthPNames = org . apache . http . auth . params . AuthPNames
9
+ ClientPNames = org . apache . http . client . params . ClientPNames
10
+ CookiePolicy = org . apache . http . client . params . CookiePolicy
11
+
12
+ CLIENT_CONFIGURATION_PARAMETERS = {
13
+ :protocol_version => HTTP ::CoreProtocolPNames ::PROTOCOL_VERSION ,
14
+ :strict_transfer_encoding => HTTP ::CoreProtocolPNames ::STRICT_TRANSFER_ENCODING ,
15
+ :http_element_charset => HTTP ::CoreProtocolPNames ::HTTP_ELEMENT_CHARSET ,
16
+ :use_expect_continue => HTTP ::CoreProtocolPNames ::USE_EXPECT_CONTINUE ,
17
+ :wait_for_continue => HTTP ::CoreProtocolPNames ::WAIT_FOR_CONTINUE ,
18
+ :user_agent => HTTP ::CoreProtocolPNames ::USER_AGENT ,
19
+ :tcp_nodelay => HTTP ::CoreConnectionPNames . TCP_NODELAY ,
20
+ :so_timeout => HTTP ::CoreConnectionPNames . SO_TIMEOUT ,
21
+ :so_linger => HTTP ::CoreConnectionPNames . SO_LINGER ,
22
+ :so_reuseaddr => HTTP ::CoreConnectionPNames . SO_REUSEADDR ,
23
+ :socket_buffer_size => HTTP ::CoreConnectionPNames . SOCKET_BUFFER_SIZE ,
24
+ :connection_timeout => HTTP ::CoreConnectionPNames . CONNECTION_TIMEOUT ,
25
+ :max_line_length => HTTP ::CoreConnectionPNames . MAX_LINE_LENGTH ,
26
+ :max_header_count => HTTP ::CoreConnectionPNames . MAX_HEADER_COUNT ,
27
+ :stale_connection_check => HTTP ::CoreConnectionPNames . STALE_CONNECTION_CHECK ,
28
+ # :forced_route => HTTP::ConnRoutePNames::FORCED_ROUTE, # not implemented
29
+ :local_address => HTTP ::ConnRoutePNames ::LOCAL_ADDRESS ,
30
+ :default_proxy => HTTP ::ConnRoutePNames ::DEFAULT_PROXY ,
31
+ :date_patterns => HTTP ::CookieSpecPNames ::DATE_PATTERNS ,
32
+ :single_cookie_header => HTTP ::CookieSpecPNames ::SINGLE_COOKIE_HEADER ,
33
+ :credential_charset => HTTP ::AuthPNames ::CREDENTIAL_CHARSET ,
34
+ :cookie_policy => HTTP ::ClientPNames ::COOKIE_POLICY ,
35
+ :handle_authentication => HTTP ::ClientPNames ::HANDLE_AUTHENTICATION ,
36
+ :handle_redirects => HTTP ::ClientPNames ::HANDLE_REDIRECTS ,
37
+ :max_redirects => HTTP ::ClientPNames ::MAX_REDIRECTS ,
38
+ :allow_circular_redirects => HTTP ::ClientPNames ::ALLOW_CIRCULAR_REDIRECTS ,
39
+ :virtual_host => HTTP ::ClientPNames ::VIRTUAL_HOST ,
40
+ :default_host => HTTP ::ClientPNames ::DEFAULT_HOST
41
+ # :default_headers => HTTP::ClientPNames::DEFAULT_HEADERS, # not implemented
42
+ # :connection_manager_factory_class_name => HTTP::ClientPNames::CONNECTION_MANAGER_FACTORY_CLASS_NAME # not implemented
43
+ }
44
+
45
+ class ClientConfiguration
46
+ DEFAULT_TIMEOUT = 30_000
47
+
48
+ CLIENT_CONFIGURATION_PARAMETERS . each do |method_name , param_class |
49
+ define_method ( method_name ) do
50
+ params . get_parameter param_class
51
+ end
52
+
53
+ define_method ( "#{ method_name } =" ) do |arg |
54
+ arg . add_to_params ( params , CLIENT_CONFIGURATION_PARAMETERS [ method_name ] )
55
+ end
56
+ end
57
+
58
+ def initialize ( options = { } )
59
+ self . so_timeout = DEFAULT_TIMEOUT
60
+
61
+ options . each do |parameter_name , parameter_value |
62
+ setter_name = "#{ parameter_name } ="
63
+ self . send ( setter_name , parameter_value ) if self . respond_to? ( setter_name )
64
+ end
65
+ end
66
+
67
+ def build_http_client
68
+ DefaultHttpClient . new ( params )
69
+ end
70
+
71
+ def protocol_version = ( version_string )
72
+ protocol , major_version , minor_version = version_string . split ( /[\. |\s |\/ ]/ )
73
+ params . set_parameter HTTP ::CoreProtocolPNames ::PROTOCOL_VERSION , org . apache . http . ProtocolVersion . new ( protocol , major_version . to_i , minor_version . to_i )
74
+ end
75
+
76
+ def local_address = ( local_addr_str )
77
+ params . set_parameter HTTP ::ConnRoutePNames ::LOCAL_ADDRESS , java . net . InetAddress . getByName ( local_addr_str )
78
+ end
79
+
80
+ def default_proxy = ( host )
81
+ set_host_parameter ( HTTP ::ConnRoutePNames ::DEFAULT_PROXY , host )
82
+ end
83
+
84
+ def virtual_host = ( host )
85
+ set_host_parameter ( HTTP ::ClientPNames ::VIRTUAL_HOST , host )
86
+ end
87
+
88
+ def default_host = ( host )
89
+ set_host_parameter ( HTTP ::ClientPNames ::DEFAULT_HOST , host )
90
+ end
91
+
92
+ def timeout_in_seconds = ( timeout )
93
+ self . so_timeout = timeout * 1000
94
+ end
95
+
96
+ private
97
+ def set_host_parameter ( parameter_name , host )
98
+ uri = URI . parse host
99
+ params . set_parameter ( parameter_name , HTTP ::HttpHost . new ( uri . host , uri . port , uri . scheme ) )
100
+ end
101
+
102
+ def params
103
+ @params ||= default_params
104
+ end
105
+
106
+ def default_params
107
+ params = BasicHttpParams . new
108
+ DefaultHttpClient . set_default_http_params ( params )
109
+ params
110
+ end
111
+ end
112
+ end
113
+
114
+ class Integer
115
+ def add_to_params ( params , param_name )
116
+ params . set_int_parameter ( param_name , self )
117
+ end
118
+ end
119
+
120
+ class TrueClass
121
+ def add_to_params ( params , param_name )
122
+ params . set_boolean_parameter ( param_name , self )
123
+ end
124
+ end
125
+
126
+ class FalseClass
127
+ def add_to_params ( params , param_name )
128
+ params . set_boolean_parameter ( param_name , self )
129
+ end
130
+ end
131
+
132
+ class Object
133
+ def add_to_params ( params , param_name )
134
+ params . set_parameter ( param_name , self )
135
+ end
136
+ end
0 commit comments