Class: HTTP::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/http_client/client.rb

Constant Summary collapse

CLIENT_PARAMETERS =
{
  :protocol_version         => HTTP::CoreProtocolPNames::PROTOCOL_VERSION,
  :strict_transfer_encoding => HTTP::CoreProtocolPNames::STRICT_TRANSFER_ENCODING,
  :http_element_charset     => HTTP::CoreProtocolPNames::HTTP_ELEMENT_CHARSET,
  :use_expect_continue      => HTTP::CoreProtocolPNames::USE_EXPECT_CONTINUE,
  :wait_for_continue        => HTTP::CoreProtocolPNames::WAIT_FOR_CONTINUE,
  :user_agent               => HTTP::CoreProtocolPNames::USER_AGENT,
  :tcp_nodelay              => HTTP::CoreConnectionPNames.TCP_NODELAY,
  :so_timeout               => HTTP::CoreConnectionPNames.SO_TIMEOUT,
  :so_linger                => HTTP::CoreConnectionPNames.SO_LINGER,
  :so_reuseaddr             => HTTP::CoreConnectionPNames.SO_REUSEADDR,
  :socket_buffer_size       => HTTP::CoreConnectionPNames.SOCKET_BUFFER_SIZE,
  :connection_timeout       => HTTP::CoreConnectionPNames.CONNECTION_TIMEOUT,
  :max_line_length          => HTTP::CoreConnectionPNames.MAX_LINE_LENGTH,
  :max_header_count         => HTTP::CoreConnectionPNames.MAX_HEADER_COUNT,
  :stale_connection_check   => HTTP::CoreConnectionPNames.STALE_CONNECTION_CHECK,
  # :forced_route             => HTTP::ConnRoutePNames::FORCED_ROUTE, # not implemented
  :local_address            => HTTP::ConnRoutePNames::LOCAL_ADDRESS,
  :default_proxy            => HTTP::ConnRoutePNames::DEFAULT_PROXY,
  :date_patterns            => HTTP::CookieSpecPNames::DATE_PATTERNS,
  :single_cookie_header     => HTTP::CookieSpecPNames::SINGLE_COOKIE_HEADER,
  :credential_charset       => HTTP::AuthPNames::CREDENTIAL_CHARSET,
  :cookie_policy            => HTTP::ClientPNames::COOKIE_POLICY,
  :handle_authentication    => HTTP::ClientPNames::HANDLE_AUTHENTICATION,
  :handle_redirects         => HTTP::ClientPNames::HANDLE_REDIRECTS,
  :max_redirects            => HTTP::ClientPNames::MAX_REDIRECTS,
  :allow_circular_redirects => HTTP::ClientPNames::ALLOW_CIRCULAR_REDIRECTS,
  :virtual_host             => HTTP::ClientPNames::VIRTUAL_HOST,
  :default_host             => HTTP::ClientPNames::DEFAULT_HOST
  # :default_headers          => HTTP::ClientPNames::DEFAULT_HEADERS, # not implemented
  # :connection_manager_factory_class_name => HTTP::ClientPNames::CONNECTION_MANAGER_FACTORY_CLASS_NAME # not implemented
}
INTEGER_PARAMETER_SETTERS =
[
  :so_timeout, :so_linger, :socket_buffer_size, :connection_timeout,
  :max_line_length, :max_header_count, :max_redirects
]
SIMPLE_PARAMETER_SETTERS =
[
  :strict_transfer_encoding, :http_element_charset, :use_expect_continue,
  :wait_for_continue, :user_agent, :tcp_nodelay, :so_reuseaddr,
  :stale_connection_check, :date_patterns, :single_cookie_header,
  :credential_charset, :cookie_policy, :handle_authentication,
  :handle_redirects, :allow_circular_redirects
]

Instance Method Summary collapse

Constructor Details

#initialize(*params) ⇒ Client

Returns a new instance of Client.



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
# File 'lib/http_client/client.rb', line 61

def initialize(*params)      
  self.class.class_eval do
    # Setup dynamic getters (setters can be more complex)
    CLIENT_PARAMETERS.each do |method_name, param_class|
      define_method method_name do
        @params.get_parameter param_class
      end
    end
    
    INTEGER_PARAMETER_SETTERS.each do |method_name|
      define_method "#{method_name}=" do |arg|
        @params.set_int_parameter CLIENT_PARAMETERS[method_name], arg
      end
    end
    
    # For those params that our simple, we'll create the setters
    SIMPLE_PARAMETER_SETTERS.each do |method_name|
      define_method "#{method_name}=" do |arg|
        @params.set_parameter CLIENT_PARAMETERS[method_name], arg
      end
    end
  end
  
  # Parse the remaining options
  options   = parse_options(params)      
  host      = options[:host] || "localhost"
  port      = options[:port] || 8080
  protocol  = options[:scheme] || "http"
  base_path = options[:base_path] || ""

  if options[:disable_response_handler]
    @response_handler = nil
  elsif options[:response_handler]
    @response_handler = options[:response_handler]
  else
    BasicResponseHandler.new
  end
  
  @uri_builder = URIBuilder.new(protocol, host, port, base_path)
  @encoding = options[:encoding] || "UTF-8"
  @params   = BasicHttpParams.new
  DefaultHttpClient.set_default_http_params(@params)
  
  # Handle a custom setting for "timemout_in_seconds" and or "so_timeout"
  if options[:timeout_in_seconds]
    self.so_timeout = options[:timeout_in_seconds] * 1000
  elsif options[:so_timeout]
    self.so_timeout = options[:so_timeout]
  else
    self.so_timeout = 30000
  end

  # Set options from the rest of the options-hash      
  CLIENT_PARAMETERS.each do |method_name, param_class|
    self.send("#{method_name}=", options[method_name]) if options[method_name]
  end
end

Instance Method Details

#default_host=(host) ⇒ Object



140
141
142
143
# File 'lib/http_client/client.rb', line 140

def default_host=(host)
  uri = URI.parse host
  @params.set_parameter HTTP::ClientPNames::DEFAULT_HOST, HTTP::HttpHost.new(uri.host, uri.port, uri.scheme)
end

#default_proxy=(host) ⇒ Object



130
131
132
133
# File 'lib/http_client/client.rb', line 130

def default_proxy=(host)
  uri = URI.parse host
  @params.set_parameter HTTP::ConnRoutePNames::DEFAULT_PROXY, HTTP::HttpHost.new(uri.host, uri.port, uri.scheme)
end

#delete(path) ⇒ Object



155
156
157
# File 'lib/http_client/client.rb', line 155

def delete(path)
  execute(Delete.new(path))
end

#execute(request) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/http_client/client.rb', line 163

def execute(request)
  native_request = request.create_native_request(@uri_builder, @encoding)
  client = DefaultHttpClient.new(@params)
  if @response_handler
    client.execute(native_request, @response_handler)
  else
    client.execute(native_request)
  end
rescue SocketTimeoutException
  raise Timeout::Error, "timed out after #{so_timeout} ms"
ensure
  client.connection_manager.shutdown
end

#get(params, options = {}) ⇒ Object

Request Methods



147
148
149
# File 'lib/http_client/client.rb', line 147

def get(params, options = {})
  execute(Get.new(params, options))
end

#local_address=(local_addr_str) ⇒ Object



126
127
128
# File 'lib/http_client/client.rb', line 126

def local_address=(local_addr_str)
  @params.set_parameter HTTP::ConnRoutePNames::LOCAL_ADDRESS, java.net.InetAddress.getByName(local_addr_str)
end

#post(params, options = {}) ⇒ Object



151
152
153
# File 'lib/http_client/client.rb', line 151

def post(params, options = {})
  execute(Post.new(params, options))
end

#protocol_version=(version_string) ⇒ Object

Advanced Setters



121
122
123
124
# File 'lib/http_client/client.rb', line 121

def protocol_version=(version_string)
  protocol, major_version, minor_version = version_string.split(/[\.|\s|\/]/)
  @params.set_parameter HTTP::CoreProtocolPNames::PROTOCOL_VERSION, org.apache.http.ProtocolVersion.new(protocol, major_version.to_i, minor_version.to_i)
end

#put(path) ⇒ Object



159
160
161
# File 'lib/http_client/client.rb', line 159

def put(path)
  execute(Put.new(path))
end

#virtual_host=(host) ⇒ Object



135
136
137
138
# File 'lib/http_client/client.rb', line 135

def virtual_host=(host)
  uri = URI.parse host
  @params.set_parameter HTTP::ClientPNames::VIRTUAL_HOST, HTTP::HttpHost.new(uri.host, uri.port, uri.scheme)
end