Class: ActiveCMIS::Internal::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/active_cmis/internal/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ Connection

Returns a new instance of Connection.

Parameters:

  • Initialize (Logger)

    with a logger of your choice



10
11
12
# File 'lib/active_cmis/internal/connection.rb', line 10

def initialize(logger)
  @logger = logger || ActiveCMIS.default_logger
end

Instance Attribute Details

#loggerLogger (readonly)

Returns A logger used to send debug and info messages.

Returns:

  • (Logger)

    A logger used to send debug and info messages



7
8
9
# File 'lib/active_cmis/internal/connection.rb', line 7

def logger
  @logger
end

#userString? (readonly)

Returns The user that is used with the authentication to the server.

Returns:

  • (String, nil)

    The user that is used with the authentication to the server



5
6
7
# File 'lib/active_cmis/internal/connection.rb', line 5

def user
  @user
end

Instance Method Details

#authenticate(method, *params) ⇒ void

This method returns an undefined value.

Use authentication to access the CMIS repository

Examples:

Basic authentication

repo.authenticate(:basic, "username", "password")

Parameters:

  • method (Symbol)

    Currently only :basic is supported

  • params

    The parameters that need to be sent to the Net::HTTP authentication method used, username and password for basic authentication



21
22
23
24
25
26
27
28
# File 'lib/active_cmis/internal/connection.rb', line 21

def authenticate(method, *params)
  case method
  when :basic
    @authentication = {:method => :basic_auth, :params => params}
    @user = params.first
  else raise "Authentication method not supported"
  end
end

#delete(url) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/active_cmis/internal/connection.rb', line 81

def delete(url, headers = {})
  uri = normalize_url(url)

  req = Net::HTTP::Put.new(uri.request_uri)
  headers.each {|k,v| req.add_field k, v}
  handle_request(uri, req)
end

#get(url) ⇒ String

The return value is the unparsed body, unless an error occured If an error occurred, exceptions are thrown (see _ActiveCMIS::Exception

Returns:

  • (String)

    returns the body of the request, unless an error occurs



35
36
37
38
39
40
# File 'lib/active_cmis/internal/connection.rb', line 35

def get(url)
  uri = normalize_url(url)

  req = Net::HTTP::Get.new(uri.request_uri)
  handle_request(uri, req)
end

#get_atom_entry(url) ⇒ Nokogiri::XML::Node

Returns:

  • (Nokogiri::XML::Node)


65
66
67
68
# File 'lib/active_cmis/internal/connection.rb', line 65

def get_atom_entry(url)
  # FIXME: add validation that first child is really an entry
  get_xml(url).child
end

#get_response(url) ⇒ Net::HTTP::Response

Does not throw errors, returns the full response (includes status code and headers)

Returns:

  • (Net::HTTP::Response)


45
46
47
48
49
50
51
52
53
54
# File 'lib/active_cmis/internal/connection.rb', line 45

def get_response(url)
  logger.debug "GET (response) #{url}"
  uri = normalize_url(url)

  req = Net::HTTP::Get.new(uri.request_uri)
  http = authenticate_request(uri, req)
  response = http.request(req)
  logger.debug "GOT (#{response.code}) #{url}"
  response
end

#get_xml(url) ⇒ Nokogiri::XML::Document

Returns the parsed body of the result

Returns:

  • (Nokogiri::XML::Document)


59
60
61
# File 'lib/active_cmis/internal/connection.rb', line 59

def get_xml(url)
  Nokogiri::XML.parse(get(url), nil, nil, Nokogiri::XML::ParseOptions::STRICT)
end

#post(url, body, headers = {}) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/active_cmis/internal/connection.rb', line 90

def post(url, body, headers = {})
  uri = normalize_url(url)

  req = Net::HTTP::Post.new(uri.request_uri)
  headers.each {|k,v| req.add_field k, v}
  assign_body(req, body)
  handle_request(uri, req)
end

#post_response(url, body, headers = {}) ⇒ Object

Does not throw errors, returns the full response (includes status code and headers)



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/active_cmis/internal/connection.rb', line 101

def post_response(url, body, headers = {})
  logger.debug "POST (response) #{url}"
  uri = normalize_url(url)

  req = Net::HTTP::Post.new(uri.request_uri)
  headers.each {|k,v| req.add_field k, v}
  assign_body(req, body)

  http = authenticate_request(uri, req)
  response = http.request(req)
  logger.debug "POSTED (#{response.code}) #{url}"
  response
end

#put(url, body, headers = {}) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/active_cmis/internal/connection.rb', line 71

def put(url, body, headers = {})
  uri = normalize_url(url)

  req = Net::HTTP::Put.new(uri.request_uri)
  headers.each {|k,v| req.add_field k, v}
  assign_body(req, body)
  handle_request(uri, req)
end