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, options) ⇒ Connection

Returns a new instance of Connection

Parameters:

  • Initialize (Logger)

    with a logger of your choice


13
14
15
16
# File 'lib/active_cmis/internal/connection.rb', line 13

def initialize(logger, options)
  @logger = logger || ActiveCMIS.default_logger
  @options = options || {}
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


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

def logger
  @logger
end

#optionsHash (readonly)

Returns Options to be used by the HTTP objects

Returns:

  • (Hash)

    Options to be used by the HTTP objects


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

def options
  @options
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


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

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")

NTLM authentication

repo.authenticate(:ntlm, "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


27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/active_cmis/internal/connection.rb', line 27

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

#delete(url) ⇒ Object


97
98
99
100
101
102
103
# File 'lib/active_cmis/internal/connection.rb', line 97

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

Raises:


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

def get(url)
  uri = normalize_url(url)

  # Ensure the parsed URL is an HTTP one
  raise HTTPError::ClientError.new("Invalid URL #{url}") unless uri.is_a?(URI::HTTP)

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

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

Returns:

  • (Nokogiri::XML::Node)

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

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)

58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/active_cmis/internal/connection.rb', line 58

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 = nil
  http.request(req) do |res|
    logger.debug "GOT (#{res.code}) #{url}"
    response = res
  end
  response
end

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

Returns the parsed body of the result

Returns:

  • (Nokogiri::XML::Document)

75
76
77
# File 'lib/active_cmis/internal/connection.rb', line 75

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

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


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

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)


117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/active_cmis/internal/connection.rb', line 117

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


87
88
89
90
91
92
93
94
# File 'lib/active_cmis/internal/connection.rb', line 87

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