Class: XCAPClient::Client
- Inherits:
-
Object
- Object
- XCAPClient::Client
- Defined in:
- lib/xcapclient/client.rb
Overview
The base class of the library. A program using this library must instantiate it.
Common notes for methods get*
, put*
and delete*
Shared parameters
auid: String, the auid of the application. Example:“pres-rules”.
document: nil by default. It can be:
* String, so the application must contain a document called as it.
* XCAPClient::Document object.
* nil, so the application default document is selected.
check_etag: true by default. If true the client adds the header “If-None-Match” or “If-Match” to the HTTP request containing the last ETag received.
selector: Document/node selector. The path that identifies the XML document or node within the XCAP root URL. It’s automatically converted to ASCII and percent encoded if needed. Example:
'/cp:ruleset/cp:rule/cp:conditions/cp:identity/cp:one[@id="sip:[email protected]"]'
xml_namespaces: nil by default. It’s a hash containing the prefixes and namespaces used in the query. Example:
{"pr"=>"urn:ietf:params:xml:ns:pres-rules", "cp"=>"urn:ietf:params:xml:ns:common-policy"}
Exceptions
If these methods receive a non HTTP 2XX response they generate an exception. Check ERRORS.rdoc for detailed info.
Access to the full response
In any case, the HTTP response is stored in the document last_response attribute as an :Message object. To get it:
@xcapclient.application("pres-rules").document.last_response
Constant Summary collapse
- USER_AGENT =
"Ruby-XCAPClient"
- COMMON_HEADERS =
{ "User-Agent" => "#{USER_AGENT}/#{VERSION}", "Connection" => "close" }
- XCAP_CAPS_XMLNS =
"urn:ietf:params:xml:ns:xcap-caps"
- HTTP_TIMEOUT =
6
- CONF_PARAMETERS =
[:xcap_root, :user, :auth_user, :password, :identity_header, :identity_user, :ssl_verify_cert]
Instance Attribute Summary collapse
-
#auth_user ⇒ Object
readonly
Returns the value of attribute auth_user.
-
#identity_header ⇒ Object
readonly
Returns the value of attribute identity_header.
-
#identity_user ⇒ Object
readonly
Returns the value of attribute identity_user.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#ssl_verify_cert ⇒ Object
readonly
Returns the value of attribute ssl_verify_cert.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
-
#xcap_root ⇒ Object
readonly
Returns the value of attribute xcap_root.
Instance Method Summary collapse
-
#application(auid) ⇒ Object
Returns the XCAPClient::Application whose auid mathes the auid parameter.
-
#applications ⇒ Object
Returns an Array with all the applications configured in the client.
-
#check_connection ⇒ Object
Checks the TCP connection with the XCAP server.
-
#delete(auid, document = nil, check_etag = true) ⇒ Object
Delete a document in the server.
-
#delete_attribute(auid, document, selector, attribute_name, xml_namespaces = nil, check_etag = true) ⇒ Object
Delete a node attribute in the document stored in the server.
-
#delete_node(auid, document, selector, xml_namespaces = nil, check_etag = true) ⇒ Object
Delete a node in the document stored in the server.
-
#get(auid, document = nil, check_etag = true) ⇒ Object
Fetch a document from the server.
-
#get_attribute(auid, document, selector, attribute_name, xml_namespaces = nil, check_etag = true) ⇒ Object
Fetch a node attribute from the document stored in the server.
-
#get_node(auid, document, selector, xml_namespaces = nil, check_etag = true) ⇒ Object
Fetch a node from the document stored in the server.
-
#get_node_namespaces(auid, document, selector, xml_namespaces = nil, check_etag = true) ⇒ Object
Fetch the namespace prefixes of a node.
-
#get_xcap_auids ⇒ Object
Fetch the XCAP applications (auids) supported by the server.
-
#get_xcap_extensions ⇒ Object
Fetch the XCAP extensions supported by the server.
-
#get_xcap_namespaces ⇒ Object
Fetch the XCAP namespaces supported by the server.
-
#initialize(conf = {}, applications = {}) ⇒ Client
constructor
Create a new XCAP client.
-
#put(auid, document = nil, check_etag = true) ⇒ Object
Create/replace a document in the server.
-
#put_attribute(auid, document, selector, attribute_name, attribute_value, xml_namespaces = nil, check_etag = true) ⇒ Object
Create/replace a node attribute in the document stored in the server.
-
#put_node(auid, document, selector, selector_body, xml_namespaces = nil, check_etag = true) ⇒ Object
Create/replace a node in the document stored in the server.
Constructor Details
#initialize(conf = {}, applications = {}) ⇒ Client
Create a new XCAP client. It requires two parameters:
-
conf: A hash containing settings related to the server:
-
xcap_root: The URL where the documents hold.
-
user: The client username. Depending on the server it could look like “sip:[email protected]”, “[email protected]”, “alice”, “tel:+12345678”…
-
auth_user: Username, SIP URI or TEL URI for HTTP Digest authentication (if required). If not set it takes the value of user field.
-
identity_header: Header required in some XCAP networks containing the user identity (i.e. “X-XCAP-Preferred-Identity”).
-
identity_user: Value for the identity_header. It could be a SIP or TEL URI. If not set it takes teh value of user field.
-
ssl_verify_cert: If true and the server uses SSL, the certificate is inspected (expiration time, signature…).
-
-
applications: A hash of hashes containing each XCAP application available for the client. Each application is an entry of the hash containing a key whose value is the “auid” of the application and whose value is a hast with the following fields:
-
xmlns: The XML namespace uri of the application.
-
Example:
xcap_conf = {
:xcap_root => "https://xcap.domain.org/xcap-root",
:user => "sip:[email protected]",
:auth_user => "alice",
:password => "1234",
:ssl_verify_cert => false
}
xcap_apps = {
"pres-rules" => {
:xmlns => "urn:ietf:params:xml:ns:pres-rules",
:mime_type => "application/auth-policy+xml"
},
"rls-services" => {
:xmlns => "urn:ietf:params:xml:ns:rls-services",
:mime_type => "application/rls-services+xml"
}
}
@client = Client.new(xcap_conf, xcap_apps)
Example:
xcap_conf = {
:xcap_root => "https://xcap.domain.net",
:user => "tel:+12345678",
:auth_user => "tel:+12345678",
:password => "1234",
:identity_header => "X-XCAP-Preferred-Identity",
:ssl_verify_cert => true
}
A XCAP application called “xcap-caps” is automatically added to the list of applications of the new client. This application is defined in the RFC 4825.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/xcapclient/client.rb', line 100 def initialize(conf={}, applications={}) # Check conf hash. raise ConfigError, "`conf' must be a hash" unless (Hash === conf) # Check non existing parameter names. conf.each_key do |key| raise ConfigError, "Uknown parameter name '#{key}' in `conf' hash" unless CONF_PARAMETERS.include?(key) end # Check xcap_root parameter. @xcap_root = ( conf[:xcap_root] =~ /\/$/ ) ? URI.parse(conf[:xcap_root][0..-2]) : URI.parse(conf[:xcap_root]) raise ConfigError, "`xcap_root' must be http or https URI" unless [URI::HTTP, URI::HTTPS].include?(@xcap_root.class) # Check user. @user = conf[:user].freeze raise ConfigError, "`user' must be a non empty string" unless (String === @user && ! @user.empty?) @auth_user = conf[:auth_user].freeze || @user @password = conf[:password].freeze @identity_header = conf[:identity_header].freeze @identity_user = ( conf[:identity_user].freeze || @user ) if @identity_header COMMON_HEADERS[@identity_header] = '"' + @identity_user + '"' if @identity_header # Initialize the HTTP client. @http_client = HTTPClient.new @http_client.set_auth(@xcap_root, @auth_user, @password) @http_client.protocol_retry_count = 3 ### TODO: Set an appropiate value (min 2 for 401). @http_client.connect_timeout = HTTP_TIMEOUT @http_client.send_timeout = HTTP_TIMEOUT @http_client.receive_timeout = HTTP_TIMEOUT @xcap_root.freeze # Freeze now as it has been modified in @http_client.set_auth. # Check ssl_verify_cert parameter. if URI::HTTPS === @xcap_root @ssl_verify_cert = conf[:ssl_verify_cert] || false raise ConfigError, "`ssl_verify_cert' must be true or false" unless [TrueClass, FalseClass].include?(@ssl_verify_cert.class) @http_client.ssl_config.verify_mode = ( @ssl_verify_cert ? 3 : 0 ) end # Generate applications. @applications = {} # Add the "xcap-caps" application with a document "index". @applications["xcap-caps"] = Application.new("xcap-caps", { :xmlns => "urn:ietf:params:xml:ns:xcap-caps", :mime_type => "application/xcap-caps+xml" }) @applications["xcap-caps"].add_document("index", :global) # Add custom applications. applications.each do |auid, data| @applications[auid] = Application.new(auid, data) end @applications.freeze end |
Instance Attribute Details
#auth_user ⇒ Object (readonly)
Returns the value of attribute auth_user.
50 51 52 |
# File 'lib/xcapclient/client.rb', line 50 def auth_user @auth_user end |
#identity_header ⇒ Object (readonly)
Returns the value of attribute identity_header.
50 51 52 |
# File 'lib/xcapclient/client.rb', line 50 def identity_header @identity_header end |
#identity_user ⇒ Object (readonly)
Returns the value of attribute identity_user.
50 51 52 |
# File 'lib/xcapclient/client.rb', line 50 def identity_user @identity_user end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
50 51 52 |
# File 'lib/xcapclient/client.rb', line 50 def password @password end |
#ssl_verify_cert ⇒ Object (readonly)
Returns the value of attribute ssl_verify_cert.
50 51 52 |
# File 'lib/xcapclient/client.rb', line 50 def ssl_verify_cert @ssl_verify_cert end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
50 51 52 |
# File 'lib/xcapclient/client.rb', line 50 def user @user end |
#xcap_root ⇒ Object (readonly)
Returns the value of attribute xcap_root.
50 51 52 |
# File 'lib/xcapclient/client.rb', line 50 def xcap_root @xcap_root end |
Instance Method Details
#application(auid) ⇒ Object
Returns the XCAPClient::Application whose auid mathes the auid parameter.
Example:
@xcapclient.application("pres-rules")
183 184 185 |
# File 'lib/xcapclient/client.rb', line 183 def application(auid) @applications[auid] end |
#applications ⇒ Object
Returns an Array with all the applications configured in the client.
189 190 191 |
# File 'lib/xcapclient/client.rb', line 189 def applications @applications end |
#check_connection ⇒ Object
Checks the TCP connection with the XCAP server.
164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/xcapclient/client.rb', line 164 def check_connection begin Timeout.timeout(HTTP_TIMEOUT) do TCPSocket.open @xcap_root.host, @xcap_root.port end rescue Timeout::Error raise Timeout::Error, "cannot connect the XCAP server within #{HTTP_TIMEOUT} seconds" rescue => e raise e.class, "cannot connect the XCAP server (#{e.})" end end |
#delete(auid, document = nil, check_etag = true) ⇒ Object
Delete a document in the server.
Example:
@xcapclient.delete("pres-rules")
If success:
-
The method returns true.
-
Local plain document and ETag are deleted.
258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/xcapclient/client.rb', line 258 def delete(auid, document=nil, check_etag=true) application, document = get_app_doc(auid, document) response = send_request(:delete, application, document, nil, nil, nil, nil, check_etag) # Reset the local document. document.plain = nil document.etag = nil return true end |
#delete_attribute(auid, document, selector, attribute_name, xml_namespaces = nil, check_etag = true) ⇒ Object
Delete a node attribute in the document stored in the server.
Example, deleting the “name” attribute of the node with “id” = “sip:[email protected]”:
@xcapclient.delete_attribute("pres-rules", nil,
'cp:ruleset/cp:rule[@id="pres_whitelist"]/cp:conditions/cp:identity/cp:one[@id="sip:[email protected]"]',
"name",
{"cp" => "urn:ietf:params:xml:ns:common-policy"})
If success:
-
The method returns true.
-
Local plain document and ETag are deleted (as the document has changed).
423 424 425 426 427 428 429 430 431 432 433 434 |
# File 'lib/xcapclient/client.rb', line 423 def delete_attribute(auid, document, selector, attribute_name, xml_namespaces=nil, check_etag=true) application, document = get_app_doc(auid, document) response = send_request(:delete, application, document, selector + "/@#{attribute_name}", nil, xml_namespaces, nil, check_etag) # Reset local plain document and ETag as we have modified it document.plain = nil document.etag = nil return true end |
#delete_node(auid, document, selector, xml_namespaces = nil, check_etag = true) ⇒ Object
Delete a node in the document stored in the server.
Example, deleting the node with “id” = “sip:[email protected]”:
@xcapclient.delete_node("pres-rules", nil,
'cp:ruleset/cp:rule[@id="pres_whitelist"]/cp:conditions/cp:identity/cp:one[@id="sip:[email protected]"]',
{"cp" => "urn:ietf:params:xml:ns:common-policy"})
If success:
-
The method returns true.
-
Local plain document is deleted (as the document has changed).
-
Received ETag is stored in
@xcapclient.application("pres-rules").document.etag
.
338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/xcapclient/client.rb', line 338 def delete_node(auid, document, selector, xml_namespaces=nil, check_etag=true) application, document = get_app_doc(auid, document) response = send_request(:delete, application, document, selector, nil, xml_namespaces, nil, check_etag) # Reset local plain document as we have modified it. document.plain = nil # Update ETag. document.etag = response.header["ETag"].first return true end |
#get(auid, document = nil, check_etag = true) ⇒ Object
Fetch a document from the server.
Example:
@xcapclient.get("pres-rules")
If success:
-
The method returns true.
-
Received XML plain document is stored in
@xcapclient.application("pres-rules").document.plain
(the default document). -
Received ETag is stored in
@xcapclient.application("pres-rules").document.etag
.
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/xcapclient/client.rb', line 205 def get(auid, document=nil, check_etag=true) application, document = get_app_doc(auid, document) response = send_request(:get, application, document, nil, nil, nil, nil, check_etag) # Check Content-Type. check_content_type(response, application.mime_type) # Store the plain document. document.plain = response.body.content # Update ETag. document.etag = response.header["ETag"].first return true end |
#get_attribute(auid, document, selector, attribute_name, xml_namespaces = nil, check_etag = true) ⇒ Object
Fetch a node attribute from the document stored in the server.
Example, fetching the “name” attribute of the node with “id” = “sip:[email protected]”:
@xcapclient.get_attribute("pres-rules", nil,
'cp:ruleset/cp:rule[@id="pres_whitelist"]/cp:conditions/cp:identity/cp:one[@id="sip:[email protected]"]',
"name",
{"cp" => "urn:ietf:params:xml:ns:common-policy"})
If success:
-
The method returns the attribute as a String.
367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/xcapclient/client.rb', line 367 def get_attribute(auid, document, selector, attribute_name, xml_namespaces=nil, check_etag=true) application, document = get_app_doc(auid, document) response = send_request(:get, application, document, selector + "/@#{attribute_name}", nil, xml_namespaces, nil, check_etag) # Check Content-Type. check_content_type(response, "application/xcap-att+xml") return response.body.content end |
#get_node(auid, document, selector, xml_namespaces = nil, check_etag = true) ⇒ Object
Fetch a node from the document stored in the server.
Example, fetching the node with “id” = “sip:[email protected]”:
@xcapclient.get_node("pres-rules", nil,
'cp:ruleset/cp:rule[@id="pres_whitelist"]/cp:conditions/cp:identity/cp:one[@id="sip:[email protected]"]',
{"cp" => "urn:ietf:params:xml:ns:common-policy"})
If success:
-
The method returns the node as a String.
283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/xcapclient/client.rb', line 283 def get_node(auid, document, selector, xml_namespaces=nil, check_etag=true) application, document = get_app_doc(auid, document) response = send_request(:get, application, document, selector, nil, xml_namespaces, nil, check_etag) # Check Content-Type. check_content_type(response, "application/xcap-el+xml") return response.body.content end |
#get_node_namespaces(auid, document, selector, xml_namespaces = nil, check_etag = true) ⇒ Object
Fetch the namespace prefixes of a node.
If the client wants to create/replace a node, the body of the PUT request must use the same namespaces and prefixes as those used in the document stored in the server. This methods allows the client to fetch these namespaces and prefixes.
Related documentation: RFC 4825 section 7.10, RFC 4825 section 10
Example:
@xcapclient.get_node_namespaces("pres-rules", nil,
'ccpp:ruleset/ccpp:rule[@id="pres_whitelist"]',
{ "ccpp" => "urn:ietf:params:xml:ns:common-policy" })
Assuming the server uses “cp” for that namespace, it would reply:
HTTP/1.1 200 OK
Content-Type: application/xcap-ns+xml
<cp:identity xmlns:pr="urn:ietf:params:xml:ns:pres-rules"
xmlns:cp="urn:ietf:params:xml:ns:common-policy" />
If Nokogiri is available the method returns a hash:
{"pr"=>"urn:ietf:params:xml:ns:pres-rules", "cp"=>"urn:ietf:params:xml:ns:common-policy"}
If not, the method returns the response body as a String and the application must parse it.
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
# File 'lib/xcapclient/client.rb', line 465 def get_node_namespaces(auid, document, selector, xml_namespaces=nil, check_etag=true) application, document = get_app_doc(auid, document) response = send_request(:get, application, document, selector + "/namespace::*", nil, xml_namespaces, nil, check_etag) # Check Content-Type. check_content_type(response, "application/xcap-ns+xml") return case NOKOGIRI_INSTALLED when true Nokogiri::XML.parse(response.body.content).namespaces when false response.body.content end end |
#get_xcap_auids ⇒ Object
Fetch the XCAP applications (auids) supported by the server.
Related documentation: RFC 4825 section 12
If Nokogiri is available the method returns an Array containing the auids. If not, the response body is returned as a String.
490 491 492 493 494 495 496 497 498 499 500 501 |
# File 'lib/xcapclient/client.rb', line 490 def get_xcap_auids body = get_node("xcap-caps", nil, 'xcap-caps/auids') return case NOKOGIRI_INSTALLED when true parse(body).xpath("auids/auid", {"xmlns" => XCAP_CAPS_XMLNS}).map {|auid| auid.content} when false body end end |
#get_xcap_extensions ⇒ Object
Fetch the XCAP extensions supported by the server.
Same as XCAPClient::Client::get_xcap_auids but fetching the supported extensions.
508 509 510 511 512 513 514 515 516 517 518 519 |
# File 'lib/xcapclient/client.rb', line 508 def get_xcap_extensions body = get_node("xcap-caps", nil, 'xcap-caps/extensions') return case NOKOGIRI_INSTALLED when true parse(body).xpath("extensions/extension", {"xmlns" => XCAP_CAPS_XMLNS}).map {|extension| extension.content} when false body end end |
#get_xcap_namespaces ⇒ Object
Fetch the XCAP namespaces supported by the server.
Same as XCAPClient::Client::get_xcap_auids but fetching the supported namespaces.
526 527 528 529 530 531 532 533 534 535 536 537 |
# File 'lib/xcapclient/client.rb', line 526 def get_xcap_namespaces body = get_node("xcap-caps", nil, 'xcap-caps/namespaces') return case NOKOGIRI_INSTALLED when true parse(body).xpath("namespaces/namespace", {"xmlns" => XCAP_CAPS_XMLNS}).map {|namespace| namespace.content} when false body end end |
#put(auid, document = nil, check_etag = true) ⇒ Object
Create/replace a document in the server.
Example:
@xcapclient.put("pres-rules")
If success:
-
The method returns true.
-
Local plain document in
@xcapclient.application("pres-rules").document.plain
is uploaded to the server. -
Received ETag is stored in
@xcapclient.application("pres-rules").document.etag
.
235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/xcapclient/client.rb', line 235 def put(auid, document=nil, check_etag=true) application, document = get_app_doc(auid, document) response = send_request(:put, application, document, nil, nil, nil, application.mime_type, check_etag) # Update ETag. document.etag = response.header["ETag"].first return true end |
#put_attribute(auid, document, selector, attribute_name, attribute_value, xml_namespaces = nil, check_etag = true) ⇒ Object
Create/replace a node attribute in the document stored in the server.
Example, creating/replacing the “name” attribute of the node with “id” = “sip:[email protected]” with new value “Alice Yeah”:
@xcapclient.put_attribute("pres-rules", nil,
'cp:ruleset/cp:rule[@id="pres_whitelist"]/cp:conditions/cp:identity/cp:one[@id="sip:[email protected]"]',
"name",
"Alice Yeah",
{"cp" => "urn:ietf:params:xml:ns:common-policy"})
If success:
-
The method returns true.
-
Local plain document and ETag are deleted (as the document has changed).
395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/xcapclient/client.rb', line 395 def put_attribute(auid, document, selector, attribute_name, attribute_value, xml_namespaces=nil, check_etag=true) application, document = get_app_doc(auid, document) response = send_request(:put, application, document, selector + "/@#{attribute_name}", attribute_value, xml_namespaces, "application/xcap-att+xml", check_etag) # Reset local plain document and ETag as we have modified it. document.plain = nil document.etag = nil return true end |
#put_node(auid, document, selector, selector_body, xml_namespaces = nil, check_etag = true) ⇒ Object
Create/replace a node in the document stored in the server.
Example, creating/replacing the node with “id” = “sip:[email protected]”:
@xcapclient.put_node("pres-rules", nil,
'cp:ruleset/cp:rule[@id="pres_whitelist"]/cp:conditions/cp:identity/cp:one[@id="sip:[email protected]"]',
'<cp:one id="sip:[email protected]"/>',
{"cp"=>"urn:ietf:params:xml:ns:common-policy"})
If success:
-
The method returns true.
-
Local plain document is deleted (as the document has changed).
-
Received ETag is stored in
@xcapclient.application("pres-rules").document.etag
.
309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/xcapclient/client.rb', line 309 def put_node(auid, document, selector, selector_body, xml_namespaces=nil, check_etag=true) application, document = get_app_doc(auid, document) response = send_request(:put, application, document, selector, selector_body, xml_namespaces, "application/xcap-el+xml", check_etag) # Reset local plain document as we have modified it. document.plain = nil # Update ETag. document.etag = response.header["ETag"].first return true end |