summaryrefslogtreecommitdiff
path: root/framework/Web/Services/TJsonRpcProtocol.php
blob: b3196b13b1d7a32430107af7ade98c30e24e37f3 (plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/**
 * @author Robin J. Rogge <rrogge@bigpoint.net>
 * @link http://www.pradosoft.com/
 * @copyright 2010 Bigpoint GmbH
 * @license http://www.pradosoft.com/license/
 * @version $Id$
 * @since 3.2
 * @package System.Web.Services
 */

/**
 * TJsonRpcProtocol class
 *
 * TJsonRpcProtocol is a class that implements JSON-Rpc protocol in {@link TRpcService}.
 * Both version 1.0 and 2.0 of the specification are implemented, and the server will try
 * to answer using the same version of the protocol used by the requesting client.
 *
 * @author Robin J. Rogge <rrogge@bigpoint.net>
 * @author Fabio Bas <ctrlaltca@gmail.com>
 * @version $Id$
 * @package System.Web.Services
 * @since 3.2
 */
class TJsonRpcProtocol extends TRpcProtocol
{
	protected $_id=null;
	protected $_specificationVersion=1.0;

	/**
	 * Handles the RPC request
	 * @param string $requestPayload
	 * @return string JSON RPC response
	 */
	public function callMethod($requestPayload)
	{
		try
		{
			$_request = $this->decode($requestPayload);

			if(isset($_request['jsonrpc']))
			{
				$this->_specificationVersion=$_request['jsonrpc'];
				if($this->_specificationVersion > 2.0)
					throw new TRpcException('Unsupported specification version', '-32600');
			}

			if(isset($_request['id']))
				$this->_id=$_request['id'];

			if(!isset($_request['method']))
					throw new TRpcException('Missing request method', '-32600');

			if(!isset($_request['params']))
				$parameters = array();
			else
				$parameters = $_request['params'];

			if(!is_array($parameters))
				$parameters = array($parameters);

			// a request without an id is a notification that doesn't need a response
			if($this->_id !== null)
			{
				if($this->_specificationVersion==2.0)
				{
					return $this->encode(array(
						'jsonrpc' => '2.0',
						'id' => $this->_id,
						'result' => $this->callApiMethod($_request['method'], $parameters),
					));
				} else {
					return $this->encode(array(
						'id' => $this->_id,
						'result' => $this->callApiMethod($_request['method'], $_request['params']),
						'error' => null
					));
				}
			}
		}
		catch(TRpcException $e)
		{
			return $this->createErrorResponse($e);
		}
		catch(THttpException $e)
		{
			throw $e;
		}
		catch(Exception $e)
		{
			return $this->createErrorResponse(new TRpcException('An internal error occured', '-32603'));
		}
	}

	/**
	 * Turns the given exception into an JSON RPC fault
	 * @param TRpcException $exception
	 * @return string JSON RPC fault
	 */
	public function createErrorResponse(TRpcException $exception)
	{
		if($this->_specificationVersion==2.0)
		{
			return $this->encode(array(
				'id' => $this->_id,
				'result' => null,
				'error'=> array(
					'code' => $exception->getCode(),
					'message'=> $exception->getMessage(),
					'data' => null,
					)
			));
		} else {
			return $this->encode(array(
				'id' => $this->_id,
				'error'=> array(
					'code' => $exception->getCode(),
					'message'=> $exception->getMessage(),
					'data' => null,
					)
			));			
		}
	}

	/**
	 * Sets the correct response headers
	 * @param THttpResponse $response
	 */
	public function createResponseHeaders($response)
	{
		$response->setContentType('application/json');
		$response->setCharset('UTF-8');
	}

	/**
	 * Decodes JSON encoded data into PHP data
	 * @param string $data in JSON format
	 * @return array PHP data
	 */
	public function decode($data)
	{
		$s = json_decode($data, true);
		self::checkJsonError();
		return $s;
	}

	/**
	 * Encodes PHP data into JSON data
	 * @param mixed PHP data
	 * @return string JSON encoded PHP data
	 */
	public function encode($data)
	{
		$s = json_encode($data);
		self::checkJsonError();
		return $s;		
	}

	private static function checkJsonError()
	{
		$errnum = json_last_error();
		if($errnum != JSON_ERROR_NONE)
			throw new Exception("JSON error: $msg", $err);
	}

	/**
	 * Calls the callback handler for the given method
	 * Overrides parent implementation to correctly handle error codes
	 * @param string $methodName of the RPC
	 * @param array $parameters for the callback handler as provided by the client
	 * @return mixed whatever the callback handler returns
	 */
	public function callApiMethod($methodName, $parameters)
	{
		if(!isset($this->rpcMethods[$methodName]))
			throw new TRpcException('Method "'.$methodName.'" not found', '-32601');

		return call_user_func_array($this->rpcMethods[$methodName]['method'], $parameters);
	}
}