Class: RExchange::ExchangeRequest

Inherits:
Net::HTTPRequest
  • Object
show all
Defined in:
lib/rexchange/exchange_request.rb

Overview

Exchange Server’s WebDAV interface is non-standard, so we create this simple wrapper to extend the ‘net/http’ library and add the request methods we need.

Constant Summary collapse

REQUEST_HAS_BODY =
true
RESPONSE_HAS_BODY =
true

Class Method Summary collapse

Class Method Details

.authenticate(credentials) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rexchange/exchange_request.rb', line 12

def self.authenticate(credentials)
  owa_uri = credentials.owa_uri
  if owa_uri
    http = Net::HTTP.new(owa_uri.host, owa_uri.port)
    http.set_debug_output(RExchange::DEBUG_STREAM) if RExchange::DEBUG_STREAM
    req = Net::HTTP::Post.new(owa_uri.path)
    destination = owa_uri.scheme+"://"+owa_uri.host+(owa_uri.port ? ':'+owa_uri.port.to_s : '')
    req.body = "destination=#{destination}&username=#{credentials.user}&password=#{credentials.password}"
    http.use_ssl = owa_uri.scheme ? (owa_uri.scheme.downcase == 'https') : false
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    res = http.request(req)
    credentials.auth_cookie = res.header["set-cookie"].split(',').map(&:strip).map{|c| c.split(';')[0]}.reverse.join('; ') if res.header["set-cookie"]
  end
end

.exchange_request(credentials, options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rexchange/exchange_request.rb', line 27

def self.exchange_request(credentials, options = {})
  http = Net::HTTP.new(credentials.dav_uri.host, credentials.dav_uri.port)
  http.set_debug_output(RExchange::DEBUG_STREAM) if RExchange::DEBUG_STREAM
  request_path = options[:path] || credentials.dav_uri.path
  req = self.new(request_path)
  options[:request] = req
  req.basic_auth credentials.user, credentials.password #if !credentials.is_owa?
  req.content_type = 'text/xml'
  req.add_field 'host', credentials.dav_uri.host

  if options[:headers]
    options[:headers].each_pair do |k, v|
      req.add_field k, v
    end
  end

  req.body = options[:body] if REQUEST_HAS_BODY
  http.use_ssl = credentials.dav_use_ssl?
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  response = http.request(req) if RESPONSE_HAS_BODY

end

.execute(credentials, options = {}, &b) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rexchange/exchange_request.rb', line 50

def self.execute(credentials, options = {}, &b)
  begin
    headers = options[:headers] ||= {}
    headers['Cookie'] = credentials.auth_cookie if credentials.auth_cookie
    response = self.exchange_request(credentials, options)
    case response
    when Net::HTTPClientError then
      self.authenticate(credentials)
      #repeat exchange_request after authentication with the auth-cookies
      headers = options[:headers] ||= {}
      headers['Cookie'] = credentials.auth_cookie if credentials.auth_cookie
      response = self.exchange_request(credentials, options)
    end

    raise 'NOT 2xx NOR 3xx: ' + response.inspect.to_s unless (Net::HTTPSuccess === response || Net::HTTPRedirection === response)
    
    yield response if b
    response
  rescue RException => e
    raise e
  rescue Exception => e
    #puts e.backtrace.map{|e| e.to_s}.join("\n")
    raise RException.new(options[:request], response, e)
  end

end