Class: OpenfireAdmin::AdminClient

Inherits:
Object
  • Object
show all
Defined in:
lib/openfire_admin/admin_client.rb,
lib/openfire_admin/plugin.rb,
lib/openfire_admin/property_map.rb,
lib/openfire_admin/system_cache.rb

Overview

extend for system cache

Instance Method Summary collapse

Constructor Details

#initialize(loginurl) ⇒ AdminClient

Returns a new instance of AdminClient.

Parameters:



8
9
10
# File 'lib/openfire_admin/admin_client.rb', line 8

def initialize(loginurl)
  @http = HttpClient.new(URI.parse(loginurl))
end

Instance Method Details

#get(path) {|Net::HTTPResponse| ... } ⇒ Object

http get.

Parameters:

  • path (String)

    post url path

  • form_data (Hash<String,String>)

    post form data

Yields:

  • (Net::HTTPResponse)


21
# File 'lib/openfire_admin/admin_client.rb', line 21

def get(path, &proc); @http.get(path, &proc); end

#get_installed_pluginsObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/openfire_admin/plugin.rb', line 6

def get_installed_plugins
  get("/plugin-admin.jsp") do |res|
    doc = HtmlParser.new(res.body)
    doc.search("//h1/parent::*//table/tbody/tr[@valign='top']"){|tr|
      img = tr.at('//a[contains(@href,"reloadplugin=")]')
      if img
        {
          :key => img[:href].match(/\?reloadplugin=([^"'&>]*)/)[1],
          :name => tr.search('td')[1].text.strip,
          :description => tr.search('td')[3].text.strip,
          :version => tr.search('td')[4].text.strip
        }
      end
    }.collect
  end
end

#get_propertiesObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/openfire_admin/property_map.rb', line 24

def get_properties
  ret = {}
  get("/server-properties.jsp") do |res|
    raise ResponceException.new("can't read",res) unless res.code== "200"
    doc = HtmlParser.new(res.body)
    version = VersionString.new(/Openfire ([0-9.]*)/.match(doc.at("//div[@id='jive-userstatus']").text.to_s)[1])
    if version >= "3.9.2"
      def ret.strong_hidden?
        true
      end
    else
      def ret.strong_hidden?
        false
      end
    end
    doc.search('//h1/parent::node()//table/tbody/tr[@class=""]').each do |tr|
      v = tr.at('//td[2]//span')[:title]
      v = "" if v == NBSP
      ret[tr.at('//td//span')[:title]]= v
    end
  end
  ret
end

#get_property(name) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/openfire_admin/property_map.rb', line 17

def get_property(name)
  post("/server-properties.jsp", "edit"=>"true", "propName"=>name) do |res|
    ta = HtmlParser.new(res.body).at('//textarea[@name="propValue"]')
    raise ResponceException.new("not found textarea",res) unless ta
    ta.text.to_s
  end
end

#install_plugin(url) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/openfire_admin/plugin.rb', line 22

def install_plugin(url)
  post("/dwr/exec/downloader.installPlugin.dwr",
    "callCount"=>"1",
    "c0-scriptName"=>"downloader",
    "c0-methodName"=>"installPlugin",
    "c0-id"=>"0",
    "c0-param0"=>"string:#{url}",
    "c0-param1"=>"string:14867746",
    "xml"=>"true" ) do |res|
    raise ResponceException.new("plugin install fail",res) unless res.code == "200" and res.body =~ /s0\.successfull=/
  end
end

#login(username, pass) ⇒ Object

login

Parameters:

  • username (String)

    admin user name

  • pass (String)

    admin user password



26
27
28
29
30
31
32
33
34
# File 'lib/openfire_admin/admin_client.rb', line 26

def (username, pass)
  post( "/login.jsp" , {
      "login"=> "true",
      "password"=>pass,
      "url"=>"/index.jsp",
      "username"=>username}) do |res|
    raise ResponceException.new("can't login",res) unless res.code == "302"
  end
end

#post(path, form_data) {|Net::HTTPResponse| ... } ⇒ Object

http post.

Parameters:

  • path (String)

    post url path

  • form_data (Hash<String,String>)

    post form data

Yields:

  • (Net::HTTPResponse)


15
# File 'lib/openfire_admin/admin_client.rb', line 15

def post(path, form_data, &proc); @http.post(path, form_data, &proc); end

#reload_plugin(key) ⇒ Object



34
35
36
37
38
# File 'lib/openfire_admin/plugin.rb', line 34

def reload_plugin(key)
  get("/plugin-admin.jsp?reloadplugin=#{key}") do |res|
    raise ResponceException.new("cant reload",res) if res.code != "302" or res['location'] !~ /reloadsuccess=true/
  end
end

#remove_property(name) ⇒ Object



7
8
9
10
11
# File 'lib/openfire_admin/property_map.rb', line 7

def remove_property(name)
  post('/server-properties.jsp', 'propName' => name, 'del'=>'true') do |res|
    raise ResponceException.new("cant save",res) if res.code != "302" or res['location'] !~ /deletesuccess=true$/
  end
end

#set_property(name, value) ⇒ Object



12
13
14
15
16
# File 'lib/openfire_admin/property_map.rb', line 12

def set_property(name, value)
  post('/server-properties.jsp', 'propName' => name, 'propValue'=>value.to_s, 'save'=>'Save Property') do |res|
    raise ResponceException.new("cant save",res) if res.code != "302"
  end
end

#system_cacheObject



7
8
9
10
11
12
13
14
15
16
# File 'lib/openfire_admin/system_cache.rb', line 7

def system_cache
  get("/system-cache.jsp") do |res|
    HtmlParser.new(res.body).search('//input[@type="checkbox"][@name="cacheID"]'){|i|
      {
        :cacheID => i[:value],
        :name => i.at("ancestor::tr[1]//table[1]//td[2]").text.strip
      }
    }
  end
end

#system_cache_clear(cacheID) ⇒ Object



17
18
19
20
21
# File 'lib/openfire_admin/system_cache.rb', line 17

def system_cache_clear(cacheID)
  post("/system-cache.jsp","cacheID"=>cacheID, "clear"=>"Clear") do |res|
    ! HtmlParser.new(res.body).at("//div[@class='jive-success']").nil?
  end
end

#uninstall_plugin(key) ⇒ Object



39
40
41
42
43
# File 'lib/openfire_admin/plugin.rb', line 39

def uninstall_plugin(key)
  get("/plugin-admin.jsp?deleteplugin=#{key}") do |res|
    raise ResponceException.new("cant delete",res) if res.code != "302" or res['location'] !~ /deletesuccess=true/
  end
end