Zimbra offers Open Source email server software and shared calendar for Linux and the Mac
 
Go Back   Zimbra - Forums > Zimbra Collaboration Suite > Developers

Welcome to the Zimbra - Forums!
Welcome, if you would like to post a comment in the forums, please register and review our posting policy & tips. We also encourage you to explore all things Zimbra with our team and members of the community.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-18-2005, 10:04 AM
Member
 
Posts: 25
Default Zimbra server/SOAP & ruby ... Help needed

To the ruby/soap experts around...

How do i control/model the unmarshalling behaviour of the Zimbra SOAP response in the code snippet bellow in order to get not an array with (only) the 'values' on zimbra, but an hash with pairs (variable, value) ?


Code:
#!/usr/bin/ruby
require 'soap/rpc/driver'
require 'soap/driver'
require 'soap/header/simplehandler'

class ClientAuthHeaderHandler < SOAP::Header::SimpleHandler
    MyHeaderName = XSD::QName.new("urn:zimbra", "context")
    
    def initialize(sessionid = nil, authtoken = nil)
      super(MyHeaderName)
      @sessionid = sessionid
      @authtoken = authtoken       
    end
    
    def on_simple_outbound
      if @sessionid
        { "sessionId" => @sessionid, "authToken" => @authtoken }
      end     
    end 
end

drv = SOAP::RPC::Driver.new('https://zimbra:7071/service/admin/soap/', 'urn:zimbraAdmin')

drv.wiredump_dev = STDERR  #if $DEBUG
drv.options["protocol.http.ssl_config.verify_mode"] = nil 
drv.add_method('GetAllAdminAccountsRequest')
drv.add_method('AuthRequest',"name", "password")

token, lifetime, sessionid = drv.AuthRequest('admin@zimbra,'zimbra')
drv.headerhandler << ClientAuthHeaderHandler.new(sessionid, token) 

response = drv.__send__(:GetAllAdminAccountsRequest)
p response
p response.a
Thanxs in advance and best regards... Have a nice weekend

Last edited by antonio.meireles : 11-18-2005 at 01:31 PM.
Reply With Quote
  #2 (permalink)  
Old 11-19-2005, 07:04 AM
Senior Member
 
Posts: 51
Default

I am using Ruby on Rails together with Zimbra and till now it's a great success. I already posted something about it, because I made a plugin for RoR that lets the webservice return JSON instead of SOAP. I have no problems with retrieving any key/value pairs of data. Also more complex data structure are no problem: array of structs and those structs contain structs . Maybe this is also an idea for you.

Next week I will probably post some articles about the communication between ZimbraTK and Ruby On Rails. You will find these articles on my fairly new technical blog: www.smies.com.
Reply With Quote
  #3 (permalink)  
Old 11-19-2005, 08:31 AM
Member
 
Posts: 25
Default

Quote:
Originally Posted by smies
I am using Ruby on Rails together with Zimbra and till now it's a great success. I already posted something about it, because I made a plugin for RoR that lets the webservice return JSON instead of SOAP. I have no problems with retrieving any key/value pairs of data. Also more complex data structure are no problem: array of structs and those structs contain structs . Maybe this is also an idea for you.

Next week I will probably post some articles about the communication between ZimbraTK and Ruby On Rails. You will find these articles on my fairly new technical blog: www.smies.com.
Could you please provide a quick exeample of rails/ror SOAP/JSON interaction with zimbra… Fetch a request/changing a value from/to zimbra server?

TIA

Have a nice weekend. All the best
Reply With Quote
  #4 (permalink)  
Old 11-20-2005, 07:40 AM
Senior Member
 
Posts: 51
Default

I would like to provide you with a quick example but I don't have the time right now. Tomorrow I am going to write the article about this topic, so you will have to wait till then.
Reply With Quote
  #5 (permalink)  
Old 11-21-2005, 11:22 AM
Zimbra Employee
 
Posts: 6
Default SOAP & ruby

One way to achieve this is to use soap document style encoding. This basically means that the code will get the raw xml structure back and has to do its own parsing.

Here is an example on how to do that, following the same code


Code:
...

def GetNamePair(response)
  response.account.each do |x| 
    class << x
      attr :configuration, true    
    end     
    x.configuration = Hash[*x.a.map do |y|
      [y.__xmlattr[XSD::QName.new(nil, 'n')], String.new(y)]
    end.flatten] 
  end
end

mNS = 'urn:zimbraAdmin'
drv.add_document_method('GetAllAdminAccountsRequest', mNS, [XSD::QName.new(mNS, 'GetAllAdminAccountsRequest')],  
  [XSD::QName.new(mNS, 'GetAllAdminAccountsResponse')] )  

puts YAML.dump(GetNamePair(drv.GetAllAdminAccountsRequest([]))

Last edited by bhwang : 11-21-2005 at 11:26 AM.
Reply With Quote
  #6 (permalink)  
Old 11-21-2005, 04:25 PM
Senior Member
 
Posts: 51
Default Done, finally

I am finally done with the article. I hope that it is readable, because my English grammer isn't really great. Take a look at www.smies.com. It also contains a complete RoR project with working example code.
Reply With Quote
  #7 (permalink)  
Old 02-16-2006, 07:10 PM
Junior Member
 
Posts: 1
Question Problem creating new accounts

I can create new accounts - using the code in this thread as a starting point. My problem is that I can only seem to specify the username and password using soap. I can't seem to figure out how to let zimbra know that I want to also set the display name, cos, first name, last name, and the other options.

I know from reading the soap.txt that the create account request needs the following format:
Code:
<CreateAccountRequest>
  <name>...</name>
  <password>...</password>*
  <a n="attr-name>...</a>+
</CreateAccountRequest>
I can't figure out how to use ruby to pass information in that format for the <a n="attr-name"> elements.

I've tried a hash, an array, but the information is just ignored (the account is created but with only the username and password).

I'm sure that I could just use ruby to generate the expected soap xml call - but that seems silly.

Many of the examples for using soap with ruby are based on accessing sites that have published a wsdl - so + on the need for a wsdl. (I know that was discussed in a previous post).

Any help would be appreciated.
Reply With Quote
  #8 (permalink)  
Old 02-17-2006, 02:15 AM
Zimbra Employee
 
Posts: 6
Default

Here is one way to do this.

Code:
require 'soap/rpc/driver'  

 
class A < SOAP::SOAPRawString 
  attr :extraattr, true      
  def initialize(name, value)
    super(value)
    @extraattr = {"n" => name}
  end
end

class CreateAccountRequest
   @@schema_element = [["name", "SOAP::SOAPString"], ["password", "SOAP::SOAPString"], ["a", "A[]"]]
  
  attr_accessor :name
  attr_accessor :password
  attr_accessor :a 
 
  
  def initialize(name = nil, password = nil, a = [])
    @name = name
    @password = password
    @a = a  
  end  
end


s = SOAP::RPC::Driver.new('https://zimbra:7071/service/admin/soap/', 
'urn:zimbraAdmin') 
s.options["protocol.http.ssl_config.verify_mode"] = nil 
mNS = 'urn:zimbraAdmin'
s.add_document_method('CreateAccountRequest', 'CreateAccountRequest', 
   [XSD::QName.new(mNS, 'CreateAccountRequest')], 
   [XSD::QName.new(mNS, 'CreateAccoutnResponse')])
s.wiredump_dev = $stdout

puts YAML.dump(s.CreateAccountRequest(
    CreateAccountRequest.new('a','b',[A.new('23','4'), A.new('45','4')])
This will produce soap request

Code:
<?xml version="1.0" encoding="us-ascii" ?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <env:Body>
    <CreateAccountRequest xmlns="urn:zimbraAdmin">
      <name>a</name>
      <a n="23">4</a>
      <a n="45">4</a>
      <password>b</password>
    </CreateAccountRequest>
  </env:Body>
</env:Envelope>

Last edited by bhwang : 02-17-2006 at 02:12 PM.
Reply With Quote
  #9 (permalink)  
Old 08-01-2006, 02:57 PM
Member
 
Posts: 21
Default

Hey Guys,

I've started work on a ruby client for talking to the Zimbra soap api. Has anyone done anymore work on this area since this last post? I've successfully got admin login and account creation working but I'm not getting soap4r to return a useful response that I can look into (such as confirm details about the newly created account). I'll also be venturing into interfacing with the contact lists and mail sending api.

When I'm done, I'll be happy to bundle up what I've written for others to use.

Cheers,
Marty
Reply With Quote
  #10 (permalink)  
Old 08-10-2006, 08:51 PM
Zimbra Employee
 
Posts: 4,784
Default

Sounds cool Marty. If you want you can setup a spot on the wiki to post details and code drops.
__________________
Subscribe to our Blog - Bugzilla - Wiki - Downloads
Reply With Quote
Reply


Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Jar For Soap Client wcameron Developers 19 06-03-2009 02:04 PM
XSD/WSDL Files for the SOAP Server? photoadrian Developers 3 08-17-2006 09:08 AM
Bug with preauth mechanism and SOAP headers? Coilcore Developers 3 07-20-2006 10:41 AM
Zimbra and Ruby On Rails JoshuaPrismon Developers 7 05-09-2006 05:01 PM
Ruby On Rails JSON webservice plugin smies Developers 2 12-22-2005 02:02 PM

Why Join?

Registering let's you ask questions, makes it easier to search, displays any files attached to posts, and notifies you about replies.

Zimbrablog.com

sourceforge.net



 

Search Engine Optimization by vBSEO 3.1.0