Changeset 430
- Timestamp:
- 03/28/07 17:53:40 (2 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/websites/processors/stubJsonRpcProcessor.php
r427 r430 37 37 38 38 39 p ublicfunction getReflectionObject($class) {39 protected function getReflectionObject($class) { 40 40 $fqClass = $this->classMap[$class]; 41 41 $classObj = new stubReflectionClass($fqClass); … … 43 43 } 44 44 45 public function fillResponse($requestId, $result) { 46 if (isset($result)) { 45 protected function fillResponse($requestId, $result) { 46 $error = $this->getError(); 47 if (isset($result) && !isset($error)) { 47 48 $this->setJsonResponseObject($requestId, $result, null); 48 49 } else { 49 $this->setJsonResponseObject($requestId, null, $ this->getError());50 } 51 } 52 53 p ublicfunction setError($msg) {50 $this->setJsonResponseObject($requestId, null, $error); 51 } 52 } 53 54 protected function setError($msg) { 54 55 $this->jsonRpcResponse['error'] = $msg; 55 56 } 56 57 57 p ublicfunction getError() {58 protected function getError() { 58 59 return $this->jsonRpcResponse['error']; 59 60 } 60 61 61 p ublicfunction setJsonResponseObject($id, $result, $error) {62 private function setJsonResponseObject($id, $result, $error) { 62 63 $this->jsonRpcResponse['id'] = $id; 63 64 if (isset($result)) { … … 69 70 } 70 71 71 p ublicfunction getJsonResponseObject() {72 protected function getJsonResponseObject() { 72 73 return $this->jsonRpcResponse; 73 74 } 74 75 75 p ublic function isClassRegistered($className) {76 protected function isRegisteredClass($className) { 76 77 $registeredServices = key($this->classMap); 77 78 for ($i = 0; $i < count($registeredServices); $i++) { … … 79 80 $this->classObj = $this->getReflectionObject($className); 80 81 return true; 81 } else {82 $this->setError('class is not registered as web service');83 return false;84 82 } 85 83 } 86 } 87 88 public function hasWebServiceMethod($methodName) { 84 $this->setError('class is not registered as web service'); 85 return false; 86 } 87 88 protected function hasWebServiceMethod($methodName) { 89 89 $methods = $this->classObj->getMethods(); 90 90 foreach ($methods as $method) { … … 96 96 $this->setError('no such method availible as web service'); 97 97 return false; 98 } 99 100 protected function retrieveGETParams(){ 101 $paramValues = array(); 102 $paramPattern = new stubRegexValidator(stubJsonRpcProcessor::paramPattern); 103 foreach ($this->methodObj->getParameters() as $param) { 104 $paramName = $param->getName(); 105 $currentRequest = $this->request->getValidatedValue($paramPattern, $paramName); 106 if (!isset($currentRequest)) { 107 $this->setError('GET-Parameter: '.$paramName.' fehlt.'); 108 } 109 array_push($paramValues, $currentRequest); 110 } 111 return $paramValues; 112 } 113 114 protected function invokeWebServiceMethod($params) { 115 $result = null; 116 $obj = $this->classObj->newInstance(); 117 if ($this->hasRequiredNumberOfParams($params)) { 118 $result = $this->methodObj->invokeArgs($obj, $params); 119 } else { 120 $this->setError('number of required parameters overshooted or undershooted'); 121 $result = null; 122 } 123 return $result; 124 } 125 126 private function hasRequiredNumberOfParams($params){ 127 return $this->methodObj->getNumberOfRequiredParameters() == count($params) ? true : false; 128 } 129 130 protected function checkRequestId($id) { 131 if (!isset($id)) { 132 $this->setError('id has to have 6-7 digits'); 133 } 98 134 } 99 135 … … 105 141 * [&<paramName>=3]* 106 142 * &method=<classname>.<methodname> 107 * &id= 2143 * &id=186252 108 144 * 109 145 */ … … 115 151 list($className, $methodName) = explode('.', $classAndMethod); 116 152 117 118 if ($this->isClassRegistered($className) && $this->hasWebServiceMethod($methodName)) { 119 // GET-Params 153 if ($this->isRegisteredClass($className) && $this->hasWebServiceMethod($methodName)) { 120 154 $paramValues = array(); 121 $paramPattern = new stubRegexValidator(stubJsonRpcProcessor::paramPattern); 122 foreach ($this->methodObj->getParameters() as $param) { 123 $paramName = $param->getName(); 124 $currentRequest = $this->request->getValidatedValue($paramPattern, $paramName); 125 if (!isset($currentRequest)) { 126 $this->setError('GET-Parameter: '.$paramName.' fehlt.'); 127 } 128 array_push($paramValues, $currentRequest); 129 } 155 $paramValues = $this->retrieveGETParams(); 130 156 131 $obj = $this->classObj->newInstance(); 132 $result = $this->methodObj->invokeArgs($obj, $paramValues); 157 $result = $this->invokeWebServiceMethod($paramValues); 133 158 } 134 159 } else { … … 137 162 138 163 $idPattern = new stubRegexValidator(stubJsonRpcProcessor::idPattern); 139 $requestId = ($this->request->getValidatedValue($idPattern, 'id')); 164 $requestId = $this->request->getValidatedValue($idPattern, 'id'); 165 $this->checkRequestId($requestId); 140 166 141 167 $this->fillResponse($requestId, $result); 142 143 168 } 144 169 … … 159 184 list($className, $methodName) = explode('.', $classAndMethod); 160 185 161 if ($this->isClassRegistered($className) && $this->hasWebServiceMethod($methodName)) { 162 $obj = $this->classObj->newInstance(); 163 if ($this->methodObj->getNumberOfRequiredParameters() == count($phpJsonObj->params)) { 164 $result = $this->methodObj->invokeArgs($obj, $phpJsonObj->params); 165 } else { 166 $this->setError('number of required parameters overshooted or undershooted'); 167 } 186 if ($this->isRegisteredClass($className) && $this->hasWebServiceMethod($methodName)) { 187 $result = $this->invokeWebServiceMethod($phpJsonObj->params); 168 188 } 169 189 … … 172 192 } 173 193 194 $this->checkRequestId($phpJsonObj->id); 174 195 $this->fillResponse($phpJsonObj->id, $result); 175 196 } trunk/src/test/php/net/stubbles/websites/WebsitesTestSuite.php
r344 r430 33 33 $this->addTestFile($dir . '/processors/stubDefaultProcessorResolverTestCase.php'); 34 34 $this->addTestFile($dir . '/processors/stubSimpleProcessorResolverTestCase.php'); 35 $this->addTestFile($dir . '/processors/stubJsonRpcProcessorTestCase.php'); 35 36 36 37 // xml tests
