class Fedora::Connection

services.
This class is used by ActiveResource::Base to interface with REST
Class to handle connections to remote web services.

def authorization_header

Sets authorization header; authentication information is pulled from credentials provided with site URI.
def authorization_header
  (@site.user || @site.password ? { 'Authorization' => 'Basic ' + ["#{@site.user}:#{ @site.password}"].pack('m').delete("\r\n") } : {})
end

def build_request_headers(headers)

Builds headers for request to remote service.
def build_request_headers(headers)
  headers.merge!({"From"=>surrogate}) if @surrogate
  authorization_header.update(default_header).update(headers)
end

def default_header

def default_header
  @default_header ||= { 'Content-Type' => format.mime_type }
end

def delete(path, headers = {})

Used to delete resources.
Execute a DELETE request (see HTTP protocol documentation if unfamiliar).
def delete(path, headers = {})
  request(:delete, path, build_request_headers(headers))
end

def do_method(method, path, body = '', headers = {})

def do_method(method, path, body = '', headers = {})
  meth_map={:put=>Net::HTTP::Put::Multipart, :post=>Net::HTTP::Post::Multipart}
  raise "undefined method: #{method}" unless meth_map.has_key? method
  headers = build_request_headers(headers) 
  if body.respond_to?(:read)
    if body.respond_to?(:original_filename?)
      filename = File.basename(body.original_filename)
      io = UploadIO.new(body, mime_type,filename)
    elsif body.path
      filename = File.basename(body.path)
    else
      filename="NOFILE"
    end
    mime_types = MIME::Types.of(filename)
    mime_type ||= mime_types.empty? ? "application/octet-stream" : mime_types.first.content_type
    io = nil
    if body.is_a?(File)
      io = UploadIO.new(body.path,mime_type)
    else
      io =UploadIO.new(body, mime_type, filename)
    end
    req = meth_map[method].new(path, {:file=>io}, headers)
    multipart_request(req)
  else
    request(method, path, body.to_s, headers)
  end
end

def get(path, headers = {})

Used to get (find) resources.
Execute a GET request.
def get(path, headers = {})
  format.decode(request(:get, path, build_request_headers(headers)).body)
end

def handle_response(response)

Handles response and error codes from remote service.
def handle_response(response)
  case response.code.to_i
  when 301,302
    raise(Redirection.new(response))
  when 200...400
    response
  when 400
    raise(BadRequest.new(response))
  when 401
    raise(UnauthorizedAccess.new(response))
  when 403
    raise(ForbiddenAccess.new(response))
  when 404
    raise(ResourceNotFound.new(response))
  when 405
    raise(MethodNotAllowed.new(response))
  when 409
    raise(ResourceConflict.new(response))
  when 422
    raise(ResourceInvalid.new(response))
  when 401...500
    raise(ClientError.new(response))
  when 500...600
    raise(ServerError.new(response))
  else
    raise(ConnectionError.new(response, "Unknown response code: #{response.code}"))
  end
end

def http

remote service and resources.
Creates new Net::HTTP instance for communication with
def http
  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
  http
end

def initialize(site, format = ActiveResource::Formats[:xml], surrogate=nil)

attribute to the URI for the remote resource service.
The +site+ parameter is required and will set the +site+
def initialize(site, format = ActiveResource::Formats[:xml], surrogate=nil)
  raise ArgumentError, 'Missing site URI' unless site
  self.site = site
  self.format = format
  @surrogate=surrogate
end

def multipart_request(req)

def multipart_request(req)
  result = nil
  result = http.start do |conn|
    conn.read_timeout=60600 #these can take a while
    conn.request(req)
  end
  handle_response(result)
end

def post(path, body='', headers={})

def post(path, body='', headers={})
  do_method(:post, path, body, headers)
end

def put( path, body='', headers={})

def put( path, body='', headers={})
  do_method(:put, path, body, headers)
end

def raw_get(path, headers = {})

def raw_get(path, headers = {})
  request(:get, path, build_request_headers(headers))
end

def request(method, path, *arguments)

Makes request to remote service.
def request(method, path, *arguments)
  result = http.send(method, path, *arguments)
  handle_response(result)
end

def requests

def requests
  @@requests ||= []
end

def site=(site)

Set URI for remote service.
def site=(site)
  @site = site.is_a?(URI) ? site : URI.parse(site)
end