]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-12886 refactored ws test client support and other minor tweaks
authorskodak <skodak>
Wed, 21 Oct 2009 18:25:00 +0000 (18:25 +0000)
committerskodak <skodak>
Wed, 21 Oct 2009 18:25:00 +0000 (18:25 +0000)
lang/en_utf8/webservice.php
lang/en_utf8/webservice_xmlrpc.php
webservice/lib.php
webservice/rest/locallib.php
webservice/rest/testclient/index.php [deleted file]
webservice/testclient.php [new file with mode: 0644]
webservice/testclient_forms.php
webservice/xmlrpc/locallib.php
webservice/xmlrpc/testclient/index.php [deleted file]

index 028ef711ad260baaf168fada4f7727a4cfac23dd..fadf7582fbba3d1753c7310a110d15f2d2b84654 100644 (file)
@@ -40,6 +40,6 @@ $string['serviceusers'] = 'Authorized users';
 $string['serviceusersmatching'] = 'Authorized users matching';
 $string['serviceuserssettings'] = 'Change settings for the Authorized users';
 $string['test'] = 'Test';
-$string['testclient'] = 'Test client';
+$string['testclient'] = 'Web service test client';
 $string['validuntil'] = 'Valid until';
 $string['webservices'] = 'Web services';
index 48d1636e92f59a669ac06dd38b494e83e23ee4a0..1800fd1d9ac9e245b4b604e0ffe052b0fbfdd62f 100644 (file)
@@ -1,3 +1,3 @@
 <?php
 
-$string['pluginname'] = 'XMLRPC protocol';
+$string['pluginname'] = 'XML-RPC protocol';
index f7780925b9e590ba435e2f6fec3e4e1e1806e872..5f983231eb8127ca3fadd3009b942cd66d2d77c2 100644 (file)
@@ -37,6 +37,11 @@ class webservice_access_exception extends moodle_exception {
     }
 }
 
+/**
+ * Is protocol enabled?
+ * @param string $protocol name of WS protocol
+ * @return bool
+ */
 function webservice_protocol_is_enabled($protocol) {
     global $CFG;
 
@@ -62,6 +67,20 @@ interface webservice_server {
     public function run($simple);
 }
 
+/**
+ * Mandatory test client interface.
+ */
+interface webservice_test_client_interface {
+    /**
+     * Execute test client WS request
+     * @param string $serverurl
+     * @param string $function
+     * @param array $params
+     * @return mixed
+     */
+    public function simpletest($serverurl, $function, $params);
+}
+
 /**
  * Special abstraction of our srvices that allows
  * interaction with stock Zend ws servers.
@@ -229,6 +248,7 @@ class '.$classname.' {
 '.$methods.'
 }
 ';
+
         // load the virtual class definition into memory
         eval($code);
         $this->service_class = $classname;
@@ -506,7 +526,6 @@ abstract class webservice_base_server implements webservice_server {
      */
     abstract protected function send_error($ex=null);
 
-
     /**
      * Process request from client.
      * @param bool $simple use simple authentication
index b1af6b7f5fbb51de8a11d9d20fb584ddc8e624c5..03e3a345daa7d628baa59e3a4b69724df6792da6 100644 (file)
@@ -122,8 +122,11 @@ class webservice_rest_server extends webservice_base_server {
             return '';
 
         } else if ($desc instanceof external_value) {
-            //TODO: there should be some way to indicate the real NULL value
-            return '<VALUE>'.htmlentities($returns, ENT_COMPAT, 'UTF-8').'</VALUE>'."\n";
+            if (is_null($returns)) {
+                return '<VALUE null="null"/>'."\n";
+            } else {
+                return '<VALUE>'.htmlentities($returns, ENT_COMPAT, 'UTF-8').'</VALUE>'."\n";
+            }
 
         } else if ($desc instanceof external_multiple_structure) {
             $mult = '<MULTIPLE>'."\n";
@@ -153,3 +156,19 @@ class webservice_rest_server extends webservice_base_server {
     }
 }
 
+
+/**
+ * REST test client class
+ */
+class webservice_rest_test_client implements webservice_test_client_interface {
+    /**
+     * Execute test client WS request
+     * @param string $serverurl
+     * @param string $function
+     * @param array $params
+     * @return mixed
+     */
+    public function simpletest($serverurl, $function, $params) {
+        return download_file_content($serverurl.'&wsfunction='.$function, null, $params);
+    }
+}
\ No newline at end of file
diff --git a/webservice/rest/testclient/index.php b/webservice/rest/testclient/index.php
deleted file mode 100644 (file)
index b5361d6..0000000
+++ /dev/null
@@ -1,100 +0,0 @@
-<?php
-
-// This file is part of Moodle - http://moodle.org/
-//
-// Moodle 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 3 of the License, or
-// (at your option) any later version.
-//
-// Moodle is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
-
-/**
- * REST web service test client.
- *
- * @package   webservice
- * @copyright 2009 Moodle Pty Ltd (http://moodle.com)
- * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-
-require('../../../config.php');
-require_once("$CFG->dirroot/webservice/testclient_forms.php");
-require_once("$CFG->dirroot/webservice/rest/locallib.php");
-
-$function = optional_param('function', '', PARAM_SAFEDIR);
-
-$PAGE->set_url('webservice/rest/testclient/index.php');
-
-require_login();
-require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM)); // TODO: do we need some new capability?
-
-$functions = array('moodle_group_get_groups');
-$functions = array_combine($functions, $functions);
-
-if (!isset($functions[$function])) {
-    $function = '';
-}
-
-if (!$function) {
-    $mform = new webservice_test_client_form(null, $functions);
-    echo $OUTPUT->header();
-    echo $OUTPUT->heading(get_string('pluginname', 'webservice_rest'));
-    $mform->display();
-    echo $OUTPUT->footer();
-    die;
-}
-
-$class = $function.'_form';
-
-$mform = new $class();
-
-if ($mform->is_cancelled()) {
-    redirect('index.php');
-
-} else if ($data = $mform->get_data()) {
-    unset($data->submitbutton);
-    $serverurl = "$CFG->wwwroot/webservice/rest/simpleserver.php";
-    $serverurl .= '?wsusername='.urlencode($data->wsusername);
-    unset($data->wsusername);
-    $serverurl .= '&wspassword='.urlencode($data->wspassword);
-    unset($data->wspassword);
-    $serverurl .= '&wsfunction='.urlencode($data->function);
-    unset($data->function);
-
-    if ($function === 'moodle_group_get_groups') {
-        //note: this could be placed into separate function lib file in the same dir
-        for ($i=0; $i<10; $i++) {
-            if (empty($data->groupids[$i])) {
-                continue;
-            }
-            $serverurl .= "&groupids[$i]=".urlencode($data->groupids[$i]);
-        }
-    }
-
-    echo $OUTPUT->header();
-    echo $OUTPUT->heading(get_string('pluginname', 'webservice_rest').': '.$function);
-
-    echo 'URL: '.s($serverurl);
-    echo $OUTPUT->box_start();
-    echo '<code>';
-    $response = download_file_content($serverurl);
-    echo str_replace("\n", '<br />', s($response));
-    echo '</code>';
-    echo $OUTPUT->box_end();
-    $mform->display();
-    echo $OUTPUT->footer();
-    die;
-
-} else {
-    echo $OUTPUT->header();
-    echo $OUTPUT->heading(get_string('pluginname', 'webservice_rest').': '.$function);
-    $mform->display();
-    echo $OUTPUT->footer();
-    die;
-}
\ No newline at end of file
diff --git a/webservice/testclient.php b/webservice/testclient.php
new file mode 100644 (file)
index 0000000..38a715c
--- /dev/null
@@ -0,0 +1,139 @@
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle 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 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Web service test client.
+ *
+ * @package   webservice
+ * @copyright 2009 Moodle Pty Ltd (http://moodle.com)
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+require('../config.php');
+require_once("$CFG->dirroot/webservice/testclient_forms.php");
+
+$function = optional_param('function', '', PARAM_SAFEDIR);
+$protocol = optional_param('protocol', '', PARAM_SAFEDIR);
+
+$PAGE->set_url('webservice/testclient.php');
+
+require_login();
+require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM)); // TODO: do we need some new capability?
+
+// list of all available functions for testing - please note there must be explicit
+// support for testing of each functions, the parameter conversion and form is hardcoded
+$functions = array('moodle_group_get_groups');
+$functions = array_combine($functions, $functions);
+if (!isset($functions[$function])) { // whitelisting security
+    $function = '';
+}
+
+// list all enabled webservices
+$available_protocols = get_plugin_list('webservice');
+$active_protocols = empty($CFG->webserviceprotocols) ? array() : explode(',', $CFG->webserviceprotocols);
+$protocols = array();
+foreach ($active_protocols as $p) {
+    if (empty($available_protocols[$p])) {
+        continue;
+    }
+    $protocols[$p] = get_string('pluginname', 'webservice_'.$p);
+}
+if (!isset($protocols[$protocol])) { // whitelisting security
+    $protocol = '';
+}
+
+if (!$function or !$protocol) {
+    $mform = new webservice_test_client_form(null, array($functions, $protocols));
+    echo $OUTPUT->header();
+    echo $OUTPUT->heading(get_string('testclient', 'webservice'));
+    $mform->display();
+    echo $OUTPUT->footer();
+    die;
+}
+
+$class = $function.'_form';
+
+$mform = new $class();
+$mform->set_data(array('function'=>$function, 'protocol'=>$protocol));
+
+if ($mform->is_cancelled()) {
+    redirect('testclient.php');
+
+} else if ($data = $mform->get_data()) {
+    // remove unused from form data
+    unset($data->submitbutton);
+    unset($data->protocol);
+    unset($data->function);
+
+    // first load lib of selected protocol
+    require_once("$CFG->dirroot/webservice/$protocol/locallib.php");
+
+    $testclientclass = "webservice_{$protocol}_test_client";
+    if (!class_exists($testclientclass)) {
+        throw new coding_exception('Missing WS test class in protocol '.$protocol);
+    }
+    $testclient = new $testclientclass();
+
+    $serverurl = "$CFG->wwwroot/webservice/$protocol/simpleserver.php";
+    $serverurl .= '?wsusername='.urlencode($data->wsusername);
+    unset($data->wsusername);
+    $serverurl .= '&wspassword='.urlencode($data->wspassword);
+    unset($data->wspassword);
+
+    // now get the function parameters - each functions processing must be hardcoded here
+    $params = array();
+    if ($function === 'moodle_group_get_groups') {
+        $params['groupids'] = array();
+        for ($i=0; $i<10; $i++) {
+            if (empty($data->groupids[$i])) {
+                continue;
+            }
+            $params['groupids'][] = $data->groupids[$i];
+        }
+
+    } else {
+        throw new coding_exception('Testing of function '.$function.' not implemented yet!');
+    }
+
+    echo $OUTPUT->header();
+    echo $OUTPUT->heading(get_string('pluginname', 'webservice_'.$protocol).': '.$function);
+
+    echo 'URL: '.s($serverurl);
+    echo $OUTPUT->box_start();
+    echo '<code>';
+
+    try {
+        $response = $testclient->simpletest($serverurl, $function, $params);
+        echo str_replace("\n", '<br />', s(var_export($response, true)));
+    } catch (Exception $ex) {
+        //TODO: handle exceptions and faults without exposing of the sensitive information such as debug traces!
+        echo str_replace("\n", '<br />', s($ex));
+    }
+
+    echo '</code>';
+    echo $OUTPUT->box_end();
+    $mform->display();
+    echo $OUTPUT->footer();
+    die;
+
+} else {
+    echo $OUTPUT->header();
+    echo $OUTPUT->heading(get_string('pluginname', 'webservice_'.$protocol).': '.$function);
+    $mform->display();
+    echo $OUTPUT->footer();
+    die;
+}
\ No newline at end of file
index e6e627a18d2b115a5fd2805a713f46306e04444f..ea0a6ca8d2618ba0077ecab8f14a8f270f8e025e 100644 (file)
@@ -8,10 +8,12 @@ class webservice_test_client_form extends moodleform {
         global $CFG;
 
         $mform = $this->_form;
-        $functions = $this->_customdata;
+        list($functions, $protocols) = $this->_customdata;
 
         $mform->addElement('header', 'wstestclienthdr', get_string('testclient', 'webservice'));
 
+        $mform->addElement('select', 'protocol', get_string('protocol', 'webservice'), $protocols);
+
         $mform->addElement('select', 'function', get_string('function', 'webservice'), $functions);
 
         $this->add_action_buttons(false, get_string('select'));
@@ -36,7 +38,9 @@ class moodle_group_get_groups_form extends moodleform {
 
         $mform->addElement('hidden', 'function');
         $mform->setType('function', PARAM_SAFEDIR);
-        $mform->setDefault('function', 'moodle_group_get_groups');
+
+        $mform->addElement('hidden', 'protocol');
+        $mform->setType('protocol', PARAM_SAFEDIR);
 
         $this->add_action_buttons(true, get_string('test', 'webservice'));
     }
index 75f8c9e76a2f8c879cbc330640339bd2ebbe263c..ea4e7865d2ec10061db62122c1a5fc78233cb517 100644 (file)
@@ -39,3 +39,24 @@ class webservice_xmlrpc_server extends webservice_zend_server {
     }
 }
 
+/**
+ * XML-RPC test client class
+ */
+class webservice_xmlrpc_test_client implements webservice_test_client_interface {
+    /**
+     * Execute test client WS request
+     * @param string $serverurl
+     * @param string $function
+     * @param array $params
+     * @return mixed
+     */
+    public function simpletest($serverurl, $function, $params) {
+        //zend expects 0 based array with numeric indexes
+        $params = array_values($params);
+
+        include "Zend/Loader.php";
+        Zend_Loader::registerAutoload();
+        $client = new Zend_XmlRpc_Client($serverurl);
+        return $client->call($function, $params);
+    }
+}
\ No newline at end of file
diff --git a/webservice/xmlrpc/testclient/index.php b/webservice/xmlrpc/testclient/index.php
deleted file mode 100644 (file)
index 3126ac7..0000000
+++ /dev/null
@@ -1,109 +0,0 @@
-<?php
-
-// This file is part of Moodle - http://moodle.org/
-//
-// Moodle 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 3 of the License, or
-// (at your option) any later version.
-//
-// Moodle is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
-
-/**
- * XML-RPC web service test client.
- *
- * @package   webservice
- * @copyright 2009 Moodle Pty Ltd (http://moodle.com)
- * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-
-require('../../../config.php');
-require_once("$CFG->dirroot/webservice/testclient_forms.php");
-require_once("$CFG->dirroot/webservice/xmlrpc/locallib.php");
-
-$function = optional_param('function', '', PARAM_SAFEDIR);
-
-$PAGE->set_url('webservice/xmlrpc/testclient/index.php');
-
-require_login();
-require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM)); // TODO: do we need some new capability?
-
-$functions = array('moodle_group_get_groups');
-$functions = array_combine($functions, $functions);
-
-if (!isset($functions[$function])) {
-    $function = '';
-}
-
-if (!$function) {
-    $mform = new webservice_test_client_form(null, $functions);
-    echo $OUTPUT->header();
-    echo $OUTPUT->heading(get_string('pluginname', 'webservice_xmlrpc'));
-    $mform->display();
-    echo $OUTPUT->footer();
-    die;
-}
-
-$class = $function.'_form';
-
-$mform = new $class();
-
-if ($mform->is_cancelled()) {
-    redirect('index.php');
-
-} else if ($data = $mform->get_data()) {
-    unset($data->submitbutton);
-    $serverurl = "$CFG->wwwroot/webservice/xmlrpc/simpleserver.php";
-    $serverurl .= '?wsusername='.urlencode($data->wsusername);
-    unset($data->wsusername);
-    $serverurl .= '&wspassword='.urlencode($data->wspassword);
-    unset($data->wspassword);
-    unset($data->function);
-
-    // now get the function parameters
-    $params = array();
-    if ($function === 'moodle_group_get_groups') {
-        $params[0] = array();
-        //note: this could be placed into separate function lib file in the same dir
-        for ($i=0; $i<10; $i++) {
-            if (empty($data->groupids[$i])) {
-                continue;
-            }
-            $params[0][] = $data->groupids[$i];
-        }
-    } else {
-        die('notimplemented');
-    }
-
-    echo $OUTPUT->header();
-    echo $OUTPUT->heading(get_string('pluginname', 'webservice_xmlrpc').': '.$function);
-
-    echo 'URL: '.s($serverurl);
-    echo $OUTPUT->box_start();
-    echo '<code>';
-
-    include "Zend/Loader.php";
-    Zend_Loader::registerAutoload();
-    $client = new Zend_XmlRpc_Client($serverurl);
-    $response = $client->call($function, $params);
-    echo str_replace("\n", '<br />', s(var_export($response, true)));
-
-    echo '</code>';
-    echo $OUTPUT->box_end();
-    $mform->display();
-    echo $OUTPUT->footer();
-    die;
-
-} else {
-    echo $OUTPUT->header();
-    echo $OUTPUT->heading(get_string('pluginname', 'webservice_xmlrpc').': '.$function);
-    $mform->display();
-    echo $OUTPUT->footer();
-    die;
-}
\ No newline at end of file