Changeset 430

Show
Ignore:
Timestamp:
03/28/07 17:53:40 (2 years ago)
Author:
richi
Message:

json-rpc id length checked / refactoring

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/websites/processors/stubJsonRpcProcessor.php

    r427 r430  
    3737     
    3838     
    39     public function getReflectionObject($class) { 
     39    protected function getReflectionObject($class) { 
    4040        $fqClass = $this->classMap[$class];      
    4141        $classObj = new stubReflectionClass($fqClass); 
     
    4343    } 
    4444 
    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)) { 
    4748            $this->setJsonResponseObject($requestId, $result, null); 
    4849        } else { 
    49             $this->setJsonResponseObject($requestId, null, $this->getError());     
    50         } 
    51     } 
    52      
    53     public function setError($msg) { 
     50            $this->setJsonResponseObject($requestId, null, $error);    
     51        } 
     52    } 
     53     
     54    protected function setError($msg) { 
    5455        $this->jsonRpcResponse['error'] = $msg; 
    5556    } 
    5657     
    57     public function getError() { 
     58    protected function getError() { 
    5859        return $this->jsonRpcResponse['error']; 
    5960    } 
    6061     
    61     public function setJsonResponseObject($id, $result, $error) { 
     62    private function setJsonResponseObject($id, $result, $error) { 
    6263        $this->jsonRpcResponse['id']          = $id;   
    6364        if (isset($result)) { 
     
    6970    } 
    7071     
    71     public function getJsonResponseObject() { 
     72    protected function getJsonResponseObject() { 
    7273        return $this->jsonRpcResponse; 
    7374    } 
    7475     
    75     public function isClassRegistered($className) { 
     76    protected function isRegisteredClass($className) { 
    7677        $registeredServices = key($this->classMap); 
    7778        for ($i = 0; $i < count($registeredServices); $i++) { 
     
    7980                $this->classObj = $this->getReflectionObject($className); 
    8081                return true; 
    81             } else { 
    82                 $this->setError('class is not registered as web service'); 
    83                 return false; 
    8482            } 
    8583        } 
    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) { 
    8989        $methods = $this->classObj->getMethods(); 
    9090        foreach ($methods as $method) { 
     
    9696        $this->setError('no such method availible as web service'); 
    9797        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        } 
    98134    } 
    99135     
     
    105141     * [&<paramName>=3]* 
    106142     * &method=<classname>.<methodname> 
    107      * &id=
     143     * &id=18625
    108144     * 
    109145     */ 
     
    115151            list($className, $methodName) = explode('.', $classAndMethod);   
    116152             
    117  
    118             if ($this->isClassRegistered($className) && $this->hasWebServiceMethod($methodName)) { 
    119                 // GET-Params 
     153            if ($this->isRegisteredClass($className) && $this->hasWebServiceMethod($methodName)) { 
    120154                $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(); 
    130156                 
    131                 $obj = $this->classObj->newInstance(); 
    132                 $result = $this->methodObj->invokeArgs($obj, $paramValues); 
     157                $result = $this->invokeWebServiceMethod($paramValues); 
    133158            } 
    134159        } else { 
     
    137162         
    138163        $idPattern = new stubRegexValidator(stubJsonRpcProcessor::idPattern); 
    139         $requestId = ($this->request->getValidatedValue($idPattern, 'id')); 
     164        $requestId = $this->request->getValidatedValue($idPattern, 'id'); 
     165        $this->checkRequestId($requestId); 
    140166         
    141167        $this->fillResponse($requestId, $result); 
    142  
    143168    } 
    144169     
     
    159184            list($className, $methodName) = explode('.', $classAndMethod); 
    160185 
    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); 
    168188            } 
    169189                 
     
    172192        } 
    173193         
     194        $this->checkRequestId($phpJsonObj->id); 
    174195        $this->fillResponse($phpJsonObj->id, $result); 
    175196    }                                
  • trunk/src/test/php/net/stubbles/websites/WebsitesTestSuite.php

    r344 r430  
    3333        $this->addTestFile($dir . '/processors/stubDefaultProcessorResolverTestCase.php'); 
    3434        $this->addTestFile($dir . '/processors/stubSimpleProcessorResolverTestCase.php'); 
     35        $this->addTestFile($dir . '/processors/stubJsonRpcProcessorTestCase.php'); 
    3536         
    3637        // xml tests