MDL-29279 REST server can return json format

This commit is contained in:
Jerome Mouneyrac 2011-09-19 13:54:09 +08:00
parent 9cfaebbd0e
commit a08b4cc8eb
2 changed files with 63 additions and 16 deletions

View File

@ -30,12 +30,17 @@ require_once("$CFG->dirroot/webservice/lib.php");
* @author Petr Skoda (skodak)
*/
class webservice_rest_server extends webservice_base_server {
/** @property string $alt return method (XML / JSON) */
protected $restformat;
/**
* Contructor
*/
public function __construct($authmethod) {
public function __construct($authmethod, $restformat = 'xml') {
parent::__construct($authmethod);
$this->wsname = 'rest';
$this->restformat = ($restformat != 'xml' && $restformat != 'json')?'xml':$restformat; //sanity check, we accept only xml or json
}
/**
@ -78,12 +83,28 @@ class webservice_rest_server extends webservice_base_server {
*/
protected function send_response() {
$this->send_headers();
if ($this->restformat == 'json') {
try {
$response = external_api::clean_returnvalue($this->function->returns_desc, $this->returns);
} catch (Exception $ex) {
$error = new stdClass;
$error->exception = get_class($ex);
$error->message = $ex->getMessage();
if (debugging() and isset($ex->debuginfo)) {
$error->debuginfo = $ex->debuginfo;
}
echo json_encode($error);
}
$json = json_encode($response);
echo $json;
} else {
$xml = '<?xml version="1.0" encoding="UTF-8" ?>'."\n";
$xml .= '<RESPONSE>'."\n";
$xml .= self::xmlize_result($this->returns, $this->function->returns_desc);
$xml .= '</RESPONSE>'."\n";
echo $xml;
}
}
/**
* Send the error information to the WS client
@ -93,6 +114,15 @@ class webservice_rest_server extends webservice_base_server {
*/
protected function send_error($ex=null) {
$this->send_headers();
if ($this->restformat == 'json') {
$error = new stdClass;
$error->exception = get_class($ex);
$error->message = $ex->getMessage();
if (debugging() and isset($ex->debuginfo)) {
$error->debuginfo = $ex->debuginfo;
}
echo json_encode($error);
} else {
$xml = '<?xml version="1.0" encoding="UTF-8" ?>'."\n";
$xml .= '<EXCEPTION class="'.get_class($ex).'">'."\n";
$xml .= '<MESSAGE>'.htmlentities($ex->getMessage(), ENT_COMPAT, 'UTF-8').'</MESSAGE>'."\n";
@ -102,14 +132,19 @@ class webservice_rest_server extends webservice_base_server {
$xml .= '</EXCEPTION>'."\n";
echo $xml;
}
}
/**
* Internal implementation - sending of page headers.
* @return void
*/
protected function send_headers() {
if ($this->restformat == 'json') {
header('Content-type: application/json');
} else {
header('Content-Type: application/xml; charset=utf-8');
header('Content-Disposition: inline; filename="response.xml"');
}
header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
header('Pragma: no-cache');

View File

@ -34,7 +34,19 @@ if (!webservice_protocol_is_enabled('rest')) {
die;
}
$server = new webservice_rest_server(WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN);
$restformat = optional_param('moodlewsrestformat', 'xml', PARAM_ALPHA);
//remove the alt from the request
if(isset($_REQUEST['moodlewsrestformat'])) {
unset($_REQUEST['moodlewsrestformat']);
}
if(isset($_GET['moodlewsrestformat'])) {
unset($_GET['moodlewsrestformat']);
}
if(isset($_POST['moodlewsrestformat'])) {
unset($_POST['moodlewsrestformat']);
}
$server = new webservice_rest_server(WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN, $restformat);
$server->run();
die;