Class: XMLRPC::BasicServer

Inherits:
Object
  • Object
show all
Includes:
ParseContentType, ParserWriterChooseMixin
Defined in:
lib/xmlrpc/server.rb

Direct Known Subclasses

CGIServer, ModRubyServer, WEBrickServlet

Constant Summary collapse

ERR_METHOD_MISSING =
1
ERR_UNCAUGHT_EXCEPTION =
2
ERR_MC_WRONG_PARAM =
3
ERR_MC_MISSING_PARAMS =
4
ERR_MC_MISSING_METHNAME =
5
ERR_MC_RECURSIVE_CALL =
6
ERR_MC_WRONG_PARAM_PARAMS =
7
ERR_MC_EXPECTED_STRUCT =
8

Instance Method Summary collapse

Methods included from ParseContentType

#parse_content_type

Methods included from ParserWriterChooseMixin

#set_parser, #set_writer

Constructor Details

#initialize(class_delim = ".")) ⇒ BasicServer

Returns a new instance of BasicServer.



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/xmlrpc/server.rb', line 170

def initialize(class_delim=".")
  @handler = []
  @default_handler = nil 
  @service_hook = nil

  @class_delim = class_delim
  @create = nil
  @parser = nil

  add_multicall     if Config::ENABLE_MULTICALL
  add_introspection if Config::ENABLE_INTROSPECTION
end

Instance Method Details

#add_handler(prefix, obj_or_signature = nil, help = nil, &block) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/xmlrpc/server.rb', line 183

def add_handler(prefix, obj_or_signature=nil, help=nil, &block)
  if block_given?
    # proc-handler
    @handler << [prefix, block, obj_or_signature, help]   
  else
    if prefix.kind_of? String
      # class-handler
      raise ArgumentError, "Expected non-nil value" if obj_or_signature.nil?
      @handler << [prefix + @class_delim, obj_or_signature]
    elsif prefix.kind_of? XMLRPC::Service::BasicInterface
      # class-handler with interface
      # add all methods
      @handler += prefix.get_methods(obj_or_signature, @class_delim)
    else
      raise ArgumentError, "Wrong type for parameter 'prefix'"
    end
  end
  self
end

#add_introspectionObject



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/xmlrpc/server.rb', line 263

def add_introspection
  add_handler("system.listMethods",%w(array), "List methods available on this XML-RPC server") do
    methods = []
    @handler.each do |name, obj|
      if obj.kind_of? Proc
        methods << name
      else
        obj.class.public_instance_methods(false).each do |meth|
          methods << "#{name}#{meth}"
        end
      end
    end
    methods
  end

  add_handler("system.methodSignature", %w(array string), "Returns method signature") do |meth|
    sigs = []
    @handler.each do |name, obj, sig|
      if obj.kind_of? Proc and sig != nil and name == meth
        if sig[0].kind_of? Array
          # sig contains multiple signatures, e.g. [["array"], ["array", "string"]]
          sig.each {|s| sigs << s}
        else
          # sig is a single signature, e.g. ["array"]
          sigs << sig 
        end
      end
    end
    sigs.uniq! || sigs  # remove eventually duplicated signatures
  end

  add_handler("system.methodHelp", %w(string string), "Returns help on using this method") do |meth|
    help = nil 
    @handler.each do |name, obj, sig, hlp|
      if obj.kind_of? Proc and name == meth 
        help = hlp
        break      
      end
    end
    help || ""
  end

  self
end

#add_multicallObject



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/xmlrpc/server.rb', line 221

def add_multicall
  add_handler("system.multicall", %w(array array), "Multicall Extension") do |arrStructs|
    unless arrStructs.is_a? Array 
      raise XMLRPC::FaultException.new(ERR_MC_WRONG_PARAM, "system.multicall expects an array")
    end

    arrStructs.collect {|call|
      if call.is_a? Hash
        methodName = call["methodName"]
        params     = call["params"]  

        if params.nil?
          multicall_fault(ERR_MC_MISSING_PARAMS, "Missing params")
        elsif methodName.nil?
          multicall_fault(ERR_MC_MISSING_METHNAME, "Missing methodName")
        else
          if methodName == "system.multicall"
            multicall_fault(ERR_MC_RECURSIVE_CALL, "Recursive system.multicall forbidden")
          else
            unless params.is_a? Array
              multicall_fault(ERR_MC_WRONG_PARAM_PARAMS, "Parameter params have to be an Array")
            else
              ok, val = call_method(methodName, *params)
              if ok
                # correct return value
                [val]
              else
                # exception
                multicall_fault(val.faultCode, val.faultString) 
              end
            end
          end
        end  
         
      else
        multicall_fault(ERR_MC_EXPECTED_STRUCT, "system.multicall expected struct")
      end
    } 
  end # end add_handler
  self
end

#get_default_handlerObject



212
213
214
# File 'lib/xmlrpc/server.rb', line 212

def get_default_handler
  @default_handler
end

#get_service_hookObject



203
204
205
# File 'lib/xmlrpc/server.rb', line 203

def get_service_hook
  @service_hook
end

#process(data) ⇒ Object



310
311
312
313
# File 'lib/xmlrpc/server.rb', line 310

def process(data)
  method, params = parser().parseMethodCall(data) 
  handle(method, *params)
end

#set_default_handler(&handler) ⇒ Object



216
217
218
219
# File 'lib/xmlrpc/server.rb', line 216

def set_default_handler (&handler)
  @default_handler = handler
  self
end

#set_service_hook(&handler) ⇒ Object



207
208
209
210
# File 'lib/xmlrpc/server.rb', line 207

def set_service_hook(&handler)
  @service_hook = handler
  self
end