2006-06-25 10:44:22 -04:00
|
|
|
require 'net/https'
|
|
|
|
require 'date'
|
|
|
|
require 'time'
|
|
|
|
require 'uri'
|
2006-10-23 18:21:09 -04:00
|
|
|
require 'benchmark'
|
2006-06-25 10:44:22 -04:00
|
|
|
|
|
|
|
module ActiveResource
|
2007-06-23 13:29:54 -04:00
|
|
|
class ConnectionError < StandardError # :nodoc:
|
2006-06-25 10:44:22 -04:00
|
|
|
attr_reader :response
|
|
|
|
|
|
|
|
def initialize(response, message = nil)
|
|
|
|
@response = response
|
|
|
|
@message = message
|
|
|
|
end
|
2006-08-31 05:31:11 -04:00
|
|
|
|
2006-06-25 10:44:22 -04:00
|
|
|
def to_s
|
2007-08-09 15:22:04 -04:00
|
|
|
"Failed with #{response.code} #{response.message if response.respond_to?(:message)}"
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
|
|
|
end
|
2006-08-31 05:31:11 -04:00
|
|
|
|
2008-04-16 06:39:19 -04:00
|
|
|
# Raised when a Timeout::Error occurs.
|
|
|
|
class TimeoutError < ConnectionError
|
|
|
|
def initialize(message)
|
|
|
|
@message = message
|
|
|
|
end
|
|
|
|
def to_s; @message ;end
|
|
|
|
end
|
|
|
|
|
2007-08-09 15:22:04 -04:00
|
|
|
# 3xx Redirection
|
|
|
|
class Redirection < ConnectionError # :nodoc:
|
2008-08-18 23:38:58 -04:00
|
|
|
def to_s; response['Location'] ? "#{super} => #{response['Location']}" : super; end
|
|
|
|
end
|
2007-08-09 15:22:04 -04:00
|
|
|
|
2007-06-23 13:29:54 -04:00
|
|
|
# 4xx Client Error
|
|
|
|
class ClientError < ConnectionError; end # :nodoc:
|
2008-08-18 23:38:58 -04:00
|
|
|
|
2007-12-14 18:09:46 -05:00
|
|
|
# 400 Bad Request
|
|
|
|
class BadRequest < ClientError; end # :nodoc
|
2008-08-18 23:38:58 -04:00
|
|
|
|
2007-12-14 18:09:46 -05:00
|
|
|
# 401 Unauthorized
|
|
|
|
class UnauthorizedAccess < ClientError; end # :nodoc
|
2008-08-18 23:38:58 -04:00
|
|
|
|
2007-12-14 18:09:46 -05:00
|
|
|
# 403 Forbidden
|
|
|
|
class ForbiddenAccess < ClientError; end # :nodoc
|
2008-08-18 23:38:58 -04:00
|
|
|
|
2007-06-23 13:29:54 -04:00
|
|
|
# 404 Not Found
|
|
|
|
class ResourceNotFound < ClientError; end # :nodoc:
|
2008-08-18 23:38:58 -04:00
|
|
|
|
2007-06-23 13:29:54 -04:00
|
|
|
# 409 Conflict
|
|
|
|
class ResourceConflict < ClientError; end # :nodoc:
|
2006-06-25 10:44:22 -04:00
|
|
|
|
2007-06-23 13:29:54 -04:00
|
|
|
# 5xx Server Error
|
|
|
|
class ServerError < ConnectionError; end # :nodoc:
|
2006-08-31 05:31:11 -04:00
|
|
|
|
2007-05-26 16:57:08 -04:00
|
|
|
# 405 Method Not Allowed
|
2007-06-23 13:29:54 -04:00
|
|
|
class MethodNotAllowed < ClientError # :nodoc:
|
2007-05-26 16:57:08 -04:00
|
|
|
def allowed_methods
|
|
|
|
@response['Allow'].split(',').map { |verb| verb.strip.downcase.to_sym }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-06-23 13:29:54 -04:00
|
|
|
# Class to handle connections to remote web services.
|
|
|
|
# This class is used by ActiveResource::Base to interface with REST
|
|
|
|
# services.
|
2006-06-25 10:44:22 -04:00
|
|
|
class Connection
|
2008-04-13 02:40:30 -04:00
|
|
|
attr_reader :site, :user, :password, :timeout
|
2007-09-20 19:18:05 -04:00
|
|
|
attr_accessor :format
|
2006-08-31 05:31:11 -04:00
|
|
|
|
2006-06-25 10:44:22 -04:00
|
|
|
class << self
|
|
|
|
def requests
|
|
|
|
@@requests ||= []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-06-23 13:29:54 -04:00
|
|
|
# The +site+ parameter is required and will set the +site+
|
|
|
|
# attribute to the URI for the remote resource service.
|
2007-09-20 19:18:05 -04:00
|
|
|
def initialize(site, format = ActiveResource::Formats[:xml])
|
2006-12-05 14:12:51 -05:00
|
|
|
raise ArgumentError, 'Missing site URI' unless site
|
2008-02-17 19:21:18 -05:00
|
|
|
@user = @password = nil
|
2006-12-05 14:12:51 -05:00
|
|
|
self.site = site
|
2007-09-20 19:18:05 -04:00
|
|
|
self.format = format
|
2006-09-29 12:25:49 -04:00
|
|
|
end
|
2006-12-05 14:12:51 -05:00
|
|
|
|
2007-01-16 01:34:10 -05:00
|
|
|
# Set URI for remote service.
|
2006-09-29 12:25:49 -04:00
|
|
|
def site=(site)
|
|
|
|
@site = site.is_a?(URI) ? site : URI.parse(site)
|
2008-02-19 18:06:09 -05:00
|
|
|
@user = URI.decode(@site.user) if @site.user
|
|
|
|
@password = URI.decode(@site.password) if @site.password
|
2008-02-17 19:21:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Set user for remote service.
|
|
|
|
def user=(user)
|
|
|
|
@user = user
|
|
|
|
end
|
|
|
|
|
|
|
|
# Set password for remote service.
|
|
|
|
def password=(password)
|
|
|
|
@password = password
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
2006-08-31 05:31:11 -04:00
|
|
|
|
2008-04-13 02:40:30 -04:00
|
|
|
# Set the number of seconds after which HTTP requests to the remote service should time out.
|
|
|
|
def timeout=(timeout)
|
|
|
|
@timeout = timeout
|
|
|
|
end
|
|
|
|
|
2007-01-16 01:34:10 -05:00
|
|
|
# Execute a GET request.
|
|
|
|
# Used to get (find) resources.
|
2007-04-28 23:14:36 -04:00
|
|
|
def get(path, headers = {})
|
2007-09-20 19:18:05 -04:00
|
|
|
format.decode(request(:get, path, build_request_headers(headers)).body)
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
2006-08-31 05:31:11 -04:00
|
|
|
|
2007-01-16 01:34:10 -05:00
|
|
|
# Execute a DELETE request (see HTTP protocol documentation if unfamiliar).
|
|
|
|
# Used to delete resources.
|
2007-04-28 23:14:36 -04:00
|
|
|
def delete(path, headers = {})
|
|
|
|
request(:delete, path, build_request_headers(headers))
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
2006-08-31 05:31:11 -04:00
|
|
|
|
2007-01-16 01:34:10 -05:00
|
|
|
# Execute a PUT request (see HTTP protocol documentation if unfamiliar).
|
|
|
|
# Used to update resources.
|
2007-04-28 23:14:36 -04:00
|
|
|
def put(path, body = '', headers = {})
|
2007-06-19 14:40:28 -04:00
|
|
|
request(:put, path, body.to_s, build_request_headers(headers))
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
|
|
|
|
2007-01-16 01:34:10 -05:00
|
|
|
# Execute a POST request.
|
|
|
|
# Used to create new resources.
|
2007-04-28 23:14:36 -04:00
|
|
|
def post(path, body = '', headers = {})
|
2007-06-19 14:40:28 -04:00
|
|
|
request(:post, path, body.to_s, build_request_headers(headers))
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
2006-08-31 05:31:11 -04:00
|
|
|
|
2008-02-09 17:21:55 -05:00
|
|
|
# Execute a HEAD request.
|
2008-05-02 09:45:23 -04:00
|
|
|
# Used to obtain meta-information about resources, such as whether they exist and their size (via response headers).
|
|
|
|
def head(path, headers = {})
|
2008-02-09 17:21:55 -05:00
|
|
|
request(:head, path, build_request_headers(headers))
|
|
|
|
end
|
|
|
|
|
2007-04-19 18:18:03 -04:00
|
|
|
|
2006-06-25 10:44:22 -04:00
|
|
|
private
|
2007-01-16 01:34:10 -05:00
|
|
|
# Makes request to remote service.
|
2006-10-23 18:21:09 -04:00
|
|
|
def request(method, path, *arguments)
|
2006-12-05 14:12:51 -05:00
|
|
|
logger.info "#{method.to_s.upcase} #{site.scheme}://#{site.host}:#{site.port}#{path}" if logger
|
2006-10-23 18:21:09 -04:00
|
|
|
result = nil
|
|
|
|
time = Benchmark.realtime { result = http.send(method, path, *arguments) }
|
2008-08-15 10:27:07 -04:00
|
|
|
logger.info "--> %d %s (%d %.2fs)" % [result.code, result.message, result.body ? result.body.length : 0, time] if logger
|
2006-10-23 18:21:09 -04:00
|
|
|
handle_response(result)
|
2008-04-16 06:39:19 -04:00
|
|
|
rescue Timeout::Error => e
|
|
|
|
raise TimeoutError.new(e.message)
|
2006-08-31 05:31:11 -04:00
|
|
|
end
|
2006-06-25 10:44:22 -04:00
|
|
|
|
2007-01-16 01:34:10 -05:00
|
|
|
# Handles response and error codes from remote service.
|
2006-08-31 05:31:11 -04:00
|
|
|
def handle_response(response)
|
2006-06-25 10:44:22 -04:00
|
|
|
case response.code.to_i
|
2007-08-09 15:22:04 -04:00
|
|
|
when 301,302
|
|
|
|
raise(Redirection.new(response))
|
2006-08-31 05:31:11 -04:00
|
|
|
when 200...400
|
2006-06-25 10:44:22 -04:00
|
|
|
response
|
2007-12-14 18:09:46 -05:00
|
|
|
when 400
|
|
|
|
raise(BadRequest.new(response))
|
|
|
|
when 401
|
|
|
|
raise(UnauthorizedAccess.new(response))
|
|
|
|
when 403
|
|
|
|
raise(ForbiddenAccess.new(response))
|
2006-06-25 10:44:22 -04:00
|
|
|
when 404
|
|
|
|
raise(ResourceNotFound.new(response))
|
2007-05-26 16:57:08 -04:00
|
|
|
when 405
|
|
|
|
raise(MethodNotAllowed.new(response))
|
2006-09-08 20:11:12 -04:00
|
|
|
when 409
|
|
|
|
raise(ResourceConflict.new(response))
|
2007-01-16 19:46:32 -05:00
|
|
|
when 422
|
|
|
|
raise(ResourceInvalid.new(response))
|
2006-09-07 20:07:30 -04:00
|
|
|
when 401...500
|
2006-06-25 10:44:22 -04:00
|
|
|
raise(ClientError.new(response))
|
|
|
|
when 500...600
|
|
|
|
raise(ServerError.new(response))
|
|
|
|
else
|
|
|
|
raise(ConnectionError.new(response, "Unknown response code: #{response.code}"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-11-18 19:06:23 -05:00
|
|
|
# Creates new Net::HTTP instance for communication with
|
2007-01-16 01:34:10 -05:00
|
|
|
# remote service and resources.
|
2006-06-25 10:44:22 -04:00
|
|
|
def http
|
2007-11-18 19:06:23 -05:00
|
|
|
http = Net::HTTP.new(@site.host, @site.port)
|
|
|
|
http.use_ssl = @site.is_a?(URI::HTTPS)
|
|
|
|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl
|
2008-04-13 02:40:30 -04:00
|
|
|
http.read_timeout = @timeout if @timeout # If timeout is not set, the default Net::HTTP timeout (60s) is used.
|
2007-11-18 19:06:23 -05:00
|
|
|
http
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
2007-09-20 19:18:05 -04:00
|
|
|
|
|
|
|
def default_header
|
|
|
|
@default_header ||= { 'Content-Type' => format.mime_type }
|
|
|
|
end
|
2008-04-13 02:40:30 -04:00
|
|
|
|
2007-01-16 01:34:10 -05:00
|
|
|
# Builds headers for request to remote service.
|
2007-04-28 23:14:36 -04:00
|
|
|
def build_request_headers(headers)
|
2007-09-20 19:18:05 -04:00
|
|
|
authorization_header.update(default_header).update(headers)
|
2006-09-29 12:25:49 -04:00
|
|
|
end
|
2008-04-13 02:40:30 -04:00
|
|
|
|
2008-02-17 19:21:18 -05:00
|
|
|
# Sets authorization header
|
2006-09-29 12:25:49 -04:00
|
|
|
def authorization_header
|
2008-02-17 19:21:18 -05:00
|
|
|
(@user || @password ? { 'Authorization' => 'Basic ' + ["#{@user}:#{ @password}"].pack('m').delete("\r\n") } : {})
|
2006-09-29 12:25:49 -04:00
|
|
|
end
|
2006-10-23 18:21:09 -04:00
|
|
|
|
2007-01-16 01:34:10 -05:00
|
|
|
def logger #:nodoc:
|
2008-08-18 23:38:58 -04:00
|
|
|
Base.logger
|
2006-10-23 18:21:09 -04:00
|
|
|
end
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
2007-11-18 19:06:23 -05:00
|
|
|
end
|