1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
|
<?php
/*
* jsonrpc_dispatcher.php - <short-description>
*
* Copyright (C) 2006 - Marcus Lunzenauer <mlunzena@uos.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*/
/**
* <ClassDescription>
*
* @package studip
* @subpackage ws
*
* @author mlunzena
* @copyright (c) Authors
* @version $Id: jsonrpc_dispatcher.php 3888 2006-09-06 13:27:19Z mlunzena $
*/
class Studip_Ws_JsonrpcDispatcher extends Studip_Ws_Dispatcher {
/**
* <MethodDescription>
*
* @param type <description>
*
* @return type <description>
*/
function dispatch($msg = NULL) {
# ensure correct invocation
if (is_null($msg) || !is_a($msg, 'jsonrpcmsg'))
return $this->throw_exception('functions_parameters_type must not be '.
'phpvals.');
# get decoded parameters
$len = $msg->getNumParams();
$argument_array = array();
for ($i = 0; $i < $len; ++$i)
$argument_array[] = php_jsonrpc_decode($msg->getParam($i));
# return result
return new jsonrpcresp(
php_jsonrpc_encode($this->invoke($msg->method(), $argument_array)));
}
/**
* <MethodDescription>
*
* @param type <description>
*
* @return type <description>
*/
function throw_exception($message/*, ...*/) {
$args = func_get_args();
return new jsonrpcresp(0, $GLOBALS['xmlrpcerruser'] + 1,
vsprintf(array_shift($args), $args));
}
/**
* Class method that composes the dispatch map from the available methods.
*
* @return array This service's dispatch map.
*
*/
function get_dispatch_map() {
$dispatch_map = array();
foreach ($this->api_methods as $method_name => $method)
$dispatch_map[$method_name] = $this->map_method($method);
return $dispatch_map;
}
/**
* <MethodDescription>
*
* @param type <description>
*
* @return type <description>
*/
function map_method($method) {
# TODO validate method
## 1. function
$function = array(&$this, 'dispatch');
## 2. signature
$signature = array(array());
# return value
$signature[0][] = $this->translate_type($method->returns);
# arguments
foreach ($method->expects as $type)
$signature[0][] = $this->translate_type($type);
## 3. docstring
$docstring = $method->description;
return compact('function', 'signature', 'docstring');
}
/**
* <MethodDescription>
*
* @param type <description>
*
* @return type <description>
*/
function translate_type($type0) {
switch ($type = Studip_Ws_Type::get_type($type0)) {
case STUDIP_WS_TYPE_INT:
return $GLOBALS['xmlrpcInt'];
case STUDIP_WS_TYPE_STRING:
return $GLOBALS['xmlrpcString'];
case STUDIP_WS_TYPE_BASE64:
return $GLOBALS['xmlrpcBase64'];
case STUDIP_WS_TYPE_BOOL:
return $GLOBALS['xmlrpcBoolean'];
case STUDIP_WS_TYPE_FLOAT:
return $GLOBALS['xmlrpcDouble'];
case STUDIP_WS_TYPE_NULL:
return $GLOBALS['xmlrpcBoolean'];
case STUDIP_WS_TYPE_ARRAY:
return $GLOBALS['xmlrpcArray'];
case STUDIP_WS_TYPE_STRUCT:
return $GLOBALS['xmlrpcStruct'];
}
trigger_error(sprintf('Type %s could not be found.',
var_export($type, TRUE)),
E_USER_ERROR);
return $GLOBALS['xmlrpcString'];
}
}
|