# # Copyright (c) 2005-2006, John Mettraux, OpenWFE.org # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # . Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # . Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # . Neither the name of the "OpenWFE" nor the names of its contributors may be # used to endorse or promote products derived from this software without # specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # # $Id: restclient.rb 3454 2006-10-08 16:51:00Z jmettraux $ # # # "hecho en Costa Rica" # require 'base64' require 'net/http' require 'rexml/document' module OpenWFE class RestClient attr_reader :host, :port, :resource, :sessionId def initialize (url, username, password) splitUrl(url) @username = username connect(password) end # # Closes this REST client # def close () post('endWorkSession', nil, {}, nil) end protected def get (action, subResourceName, params) return @httpclient\ .get(computeResource(action, subResourceName, params)) end def post (action, subResourceName, params, data) data = data.to_s() return @httpclient\ .post(computeResource(action, subResourceName, params), data) end private def splitUrl (url) @host = nil @port = nil @resource = nil url = url[7..-1] if url[0..6] == 'http://' i = url.index('/') if i < 0 @resource = '/defaultrestresource' else @resource = url[i..-1] url = url[0..i] end @host, @port = url.split(':') if @port == nil @port = 5080 else @port = Integer(@port[0..-2]) end end def connect (password) @httpclient = Net::HTTP.new(@host, @port) hs = {} hs['Authorization'] = \ 'Basic ' + Base64.encode64(@username+":"+password) hs['RestClient'] = 'openwfe-ruby' r = @httpclient.get(@resource, hs) xml = REXML::Document.new(r.body) @sessionId = Integer(xml.root.attributes["id"]) end def computeResource (action, subResourceName, params) resource = @resource resource = resource+'/'+subResourceName if subResourceName resource = resource+\ '?session='+@sessionId.to_s()+'&action='+action if params params.keys.each do |k| resource = resource+'&'+k.to_s()+'='+params[k].to_s() end end return resource end end end # for test purposes # #client = RestClient::RestClient\ # .new("127.0.0.1:5080/worklist", "alice", "alice") #client = OpenWFE::RestClient\ # .new("http://127.0.0.1:5080/worklist", "alice", "alice") # #puts "sessionId : #{client.sessionId}" # #puts client.get('getstorenames', nil, nil) #puts client.get('getheaders', 'Store.alpha', nil)