Changeset 458

Show
Ignore:
Timestamp:
04/05/07 10:47:37 (2 years ago)
Author:
richi
Message:

minor regexp changes in stubJsonRpcProcessor

Files:

Legend:

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

    r441 r458  
    88 */ 
    99stubClassLoader::load('net.stubbles.websites.processors.stubAbstractProcessor', 
    10                      'net.stubbles.util.validators.stubRegexValidator'); 
     10                      'net.stubbles.util.validators.stubRegexValidator'); 
    1111 
    1212/** 
     
    1818class stubJsonRpcProcessor extends stubAbstractProcessor { 
    1919 
    20     // TODO: catch exception from web service method and set error in JSON-Response 
    21     // TODO: paramAnnotation could define an individual Range for the RegExValidator 
    22      
    23     const CLASS_AND_METHOD_PATTERN = '^([a-zA-Z0-9_]*\.[a-zA-Z0-9_]*)$'; 
    24      
    25     const PARAM_PATTERN            = '^[a-zA-Z0-9_]*$'; 
    26     const ID_PATTERN               = '^\d{6,7}$'; 
    27     const JSON_PATTERN             = '^{\"method\":\"(.*)\",\"params\"(.*),\"id\":\"\d{6,7}\"}$'; 
    28      
    29     protected $jsonRpcResponse = array ( 
    30                                         'id'        => null, 
    31                                         'result'    => null, 
    32                                         'error'     => null, 
    33                                        ); 
    34  
    35     protected $classMap = array( 
    36         'BuddyQuoteService' => '_test.BuddyQuoteService' 
    37     ); 
    38  
    39     // storing information for reflection calls 
    40     protected $classObj = null; 
    41     protected $methodObj = null; 
    42      
    43     protected function getReflectionObject($class) { 
    44         $fqClass = $this->classMap[$class];      
    45         $classObj = new stubReflectionClass($fqClass); 
    46         return $classObj; 
    47     } 
    48  
    49     protected function fillResponse($requestId, $result) { 
    50         $error = $this->getError(); 
    51         if (isset($result) && !isset($error)) { 
    52             $this->setJsonResponseObject($requestId, $result, null); 
    53         } else { 
    54             $this->setJsonResponseObject($requestId, null, $error);  
    55         } 
    56     } 
    57      
    58     protected function setError($msg) { 
    59         $this->jsonRpcResponse['error'] = $msg; 
    60     } 
    61      
    62     protected function getError() { 
    63         return $this->jsonRpcResponse['error']; 
    64     } 
    65      
    66     private function setJsonResponseObject($id, $result, $error) { 
    67         $this->jsonRpcResponse['id']          = $id;   
    68         if (isset($result)) { 
    69             $this->jsonRpcResponse['result']  = utf8_encode($result); 
    70         } else { 
    71             $this->jsonRpcResponse['result']  = $result; 
    72         } 
    73         $this->jsonRpcResponse['error']       = $error; 
    74     } 
    75      
    76     public function getJsonResponseObject() { 
    77         return json_encode($this->jsonRpcResponse); 
    78     } 
    79      
    80     protected function isRegisteredClass($className) { 
    81         $registeredServices = key($this->classMap); 
    82         for ($i = 0; $i < count($registeredServices); $i++) { 
    83             if ($registeredServices === $className) {                
    84                 $this->classObj = $this->getReflectionObject($className); 
    85                 return true; 
    86             } 
    87         } 
    88         $this->setError('class is not registered as web service'); 
    89         return false; 
    90     } 
    91      
    92     protected function hasWebServiceMethod($methodName) { 
    93         $methods = $this->classObj->getMethods(); 
    94         foreach ($methods as $method) { 
    95             if ($method->getName() === $methodName) { 
    96                 $this->methodObj = $method; 
    97                 return true; 
    98             } 
    99         }        
    100         $this->setError('no such method availible as web service'); 
    101         return false; 
    102     } 
    103      
    104     protected function retrieveGETParams(){ 
    105         $paramValues = array(); 
    106         $paramPattern = new stubRegexValidator(self::PARAM_PATTERN); 
    107         foreach ($this->methodObj->getParameters() as $param) { 
    108             $paramName = $param->getName(); 
    109             $currentRequest = $this->request->getValidatedValue($paramPattern, $paramName); 
    110             if (!isset($currentRequest)) { 
    111                 $this->setError('GET-Parameter: '.$paramName.' fehlt.'); 
    112             } 
    113             array_push($paramValues, $currentRequest); 
    114         } 
    115         return $paramValues; 
    116     } 
    117      
    118     protected function invokeWebServiceMethod($params) { 
    119         $result = null; 
    120         $obj = $this->classObj->newInstance(); 
    121         if ($this->hasRequiredNumberOfParams($params)) { 
    122             $result = $this->methodObj->invokeArgs($obj, $params);       
    123         } else { 
    124             $this->setError('number of required parameters overshooted or undershooted'); 
    125             $result = null; 
    126         } 
    127         return $result; 
    128     } 
    129      
    130     private function hasRequiredNumberOfParams($params){ 
    131         return $this->methodObj->getNumberOfRequiredParameters() == count($params) ? true : false; 
    132     } 
    133      
    134     protected function checkRequestId($id) { 
    135         if (!isset($id)) { 
    136             $this->setError('id has to have 6-7 digits'); 
    137         } 
    138     } 
    139      
    140     /** 
    141      * doGET() only for testing the service 
    142      *  
    143      * http://localhost/stubbles/docroot/json.php? 
    144      * <paramName>=2 
    145      * [&<paramName>=3]* 
    146      * &method=<classname>.<methodname> 
    147      * &id=186252 
    148      * 
    149      */ 
    150     public function doGET() { 
    151         $classAndMethodPattern = new stubRegexValidator(self::CLASS_AND_METHOD_PATTERN); 
    152         $classAndMethod = $this->request->getValidatedValue($classAndMethodPattern, 'method'); 
    153          
    154         if (isset($classAndMethod)) { 
    155             list($className, $methodName) = explode('.', $classAndMethod);   
    156              
    157             if ($this->isRegisteredClass($className) && $this->hasWebServiceMethod($methodName)) { 
    158                 $paramValues = array(); 
    159                 $paramValues = $this->retrieveGETParams(); 
    160                  
    161                 $result = $this->invokeWebServiceMethod($paramValues); 
    162             } 
    163         } else { 
    164             $this->setError('method value has to be [className].[methodName]'); 
    165         } 
    166          
    167         $idPattern = new stubRegexValidator(self::ID_PATTERN); 
    168         $requestId = $this->request->getValidatedValue($idPattern, 'id'); 
    169         $this->checkRequestId($requestId); 
    170          
    171         $this->fillResponse($requestId, $result); 
    172     } 
    173      
    174     /** 
    175      * doPOST() for live usage 
    176      *  
    177      * http://localhost/stubbles/docroot/json.php 
    178      * ReqObj = {"method":"<className>.<methodName>","params":[3],"id":"186252"} 
    179      *  
    180      */ 
    181     public function doPOST() { 
    182         $jsonPattern     = new stubRegexValidator(self::JSON_PATTERN); 
    183         $requestJsonObj  = $this->request->getValidatedRawData($jsonPattern); 
    184         $phpJsonObj      = json_decode($requestJsonObj); 
    185         $classAndMethod  = $phpJsonObj->method; 
    186          
    187         if(isset($classAndMethod) && preg_match('/'.self::CLASS_AND_METHOD_PATTERN.'/', $classAndMethod)) { 
    188             list($className, $methodName) = explode('.', $classAndMethod); 
    189  
    190             if ($this->isRegisteredClass($className) && $this->hasWebServiceMethod($methodName)) { 
    191                 $result = $this->invokeWebServiceMethod($phpJsonObj->params); 
    192             } 
    193                  
    194         } else { 
    195             $this->setError('method-Pattern has to be <className>.<methodName>'); 
    196         } 
    197          
    198         $this->checkRequestId($phpJsonObj->id); 
    199         $this->fillResponse($phpJsonObj->id, $result); 
    200     }                                
    201                                      
    202     public function doProcess() { 
    203         switch ($this->request->getMethod()) { 
    204         case 'get': 
    205             $this->doGET(); 
    206             break; 
    207         case 'post': 
    208             $this->doPOST(); 
    209             break; 
    210         } 
    211  
    212         $this->response->write($this->getJsonResponseObject()); 
    213     } 
     20  // TODO: catch exception from web service method and set error in JSON-Response 
     21  // TODO: paramAnnotation could define an individual Range for the RegExValidator 
     22 
     23  const CLASS_AND_METHOD_PATTERN = '^([a-zA-Z0-9_]+\.[a-zA-Z0-9_]+)$'; 
     24  const PARAM_PATTERN            = '^[a-zA-Z0-9_]+$'; 
     25  const ID_PATTERN               = '^\d{6,7}$'; 
     26  const JSON_PATTERN             = '^{\"method\":\"(.*)\",\"params\"(.*),\"id\":\"\d{6,7}\"}$'; 
     27 
     28  protected $jsonRpcResponse = array ( 
     29                    'id'      => null, 
     30                    'result'  => null, 
     31                    'error'   => null, 
     32                     ); 
     33 
     34  protected $classMap = array( 
     35    'BuddyQuoteService' => '_test.BuddyQuoteService' 
     36  ); 
     37 
     38  // storing information for reflection calls 
     39  protected $classObj = null; 
     40  protected $methodObj = null; 
     41 
     42  protected function getReflectionObject($class) { 
     43    $fqClass = $this->classMap[$class]; 
     44    $classObj = new stubReflectionClass($fqClass); 
     45    return $classObj; 
     46  } 
     47 
     48  protected function fillResponse($requestId, $result) { 
     49    $error = $this->getError(); 
     50    if (isset($result) && !isset($error)) { 
     51      $this->setJsonResponseObject($requestId, $result, null); 
     52    } else { 
     53      $this->setJsonResponseObject($requestId, null, $error); 
     54    } 
     55  } 
     56 
     57  protected function setError($msg) { 
     58    $this->jsonRpcResponse['error'] = $msg; 
     59  } 
     60 
     61  protected function getError() { 
     62    return $this->jsonRpcResponse['error']; 
     63  } 
     64 
     65  private function setJsonResponseObject($id, $result, $error) { 
     66    $this->jsonRpcResponse['id']        = $id; 
     67    if (isset($result)) { 
     68      $this->jsonRpcResponse['result']  = utf8_encode($result); 
     69    } else { 
     70      $this->jsonRpcResponse['result']  = $result; 
     71    } 
     72    $this->jsonRpcResponse['error']     = $error; 
     73  } 
     74 
     75  public function getJsonResponseObject() { 
     76    return json_encode($this->jsonRpcResponse); 
     77  } 
     78 
     79  protected function isRegisteredClass($className) { 
     80    $registeredServices = key($this->classMap); 
     81    for ($i = 0; $i < count($registeredServices); $i++) { 
     82      if ($registeredServices === $className) { 
     83        $this->classObj = $this->getReflectionObject($className); 
     84        return true; 
     85      } 
     86    } 
     87    $this->setError('class is not registered as web service'); 
     88    return false; 
     89  } 
     90 
     91  protected function hasWebServiceMethod($methodName) { 
     92    $methods = $this->classObj->getMethods(); 
     93    foreach ($methods as $method) { 
     94      if ($method->getName() === $methodName) { 
     95        $this->methodObj = $method; 
     96        return true; 
     97      } 
     98    } 
     99    $this->setError('no such method availible as web service'); 
     100    return false; 
     101  } 
     102 
     103  protected function retrieveGETParams(){ 
     104    $paramValues = array(); 
     105    $paramPattern = new stubRegexValidator(self::PARAM_PATTERN); 
     106    foreach ($this->methodObj->getParameters() as $param) { 
     107      $paramName = $param->getName(); 
     108      $currentRequest = $this->request->getValidatedValue($paramPattern, $paramName); 
     109      if (!isset($currentRequest)) { 
     110        $this->setError('GET-Parameter: '.$paramName.' fehlt.'); 
     111      } 
     112      array_push($paramValues, $currentRequest); 
     113    } 
     114    return $paramValues; 
     115  } 
     116 
     117  protected function invokeWebServiceMethod($params) { 
     118    $result = null; 
     119    $obj = $this->classObj->newInstance(); 
     120    if ($this->hasRequiredNumberOfParams($params)) { 
     121      $result = $this->methodObj->invokeArgs($obj, $params); 
     122    } else { 
     123      $this->setError('number of required parameters overshooted or undershooted'); 
     124      $result = null; 
     125    } 
     126    return $result; 
     127  } 
     128 
     129  private function hasRequiredNumberOfParams($params){ 
     130    return $this->methodObj->getNumberOfRequiredParameters() == count($params) ? true : false; 
     131  } 
     132 
     133  protected function checkRequestId($id) { 
     134    if (!isset($id)) { 
     135      $this->setError('id has to have 6-7 digits'); 
     136    } 
     137  } 
     138 
     139  /** 
     140   * doGET() only for testing the service 
     141   * 
     142   * http://localhost/stubbles/docroot/json.php? 
     143   * <paramName>=2 
     144   * [&<paramName>=3]* 
     145   * &method=<classname>.<methodname> 
     146   * &id=186252 
     147   * 
     148   */ 
     149  public function doGET() { 
     150    $classAndMethodPattern = new stubRegexValidator(self::CLASS_AND_METHOD_PATTERN); 
     151    $classAndMethod = $this->request->getValidatedValue($classAndMethodPattern, 'method'); 
     152 
     153    if (isset($classAndMethod)) { 
     154      list($className, $methodName) = explode('.', $classAndMethod); 
     155 
     156      if ($this->isRegisteredClass($className) && $this->hasWebServiceMethod($methodName)) { 
     157        $paramValues = array(); 
     158        $paramValues = $this->retrieveGETParams(); 
     159 
     160        $result = $this->invokeWebServiceMethod($paramValues); 
     161      } 
     162    } else { 
     163      $this->setError('method value has to be [className].[methodName]'); 
     164    } 
     165 
     166    $idPattern = new stubRegexValidator(self::ID_PATTERN); 
     167    $requestId = $this->request->getValidatedValue($idPattern, 'id'); 
     168    $this->checkRequestId($requestId); 
     169 
     170    $this->fillResponse($requestId, $result); 
     171  } 
     172 
     173  /** 
     174   * doPOST() for live usage 
     175   * 
     176   * http://localhost/stubbles/docroot/json.php 
     177   * ReqObj = {"method":"<className>.<methodName>","params":[3],"id":"186252"} 
     178   * 
     179   */ 
     180  public function doPOST() { 
     181    $jsonPattern     = new stubRegexValidator(self::JSON_PATTERN); 
     182    $requestJsonObj  = $this->request->getValidatedRawData($jsonPattern); 
     183    $phpJsonObj      = json_decode($requestJsonObj); 
     184    $classAndMethod  = $phpJsonObj->method; 
     185 
     186    if(isset($classAndMethod) && preg_match('/'.self::CLASS_AND_METHOD_PATTERN.'/', $classAndMethod)) { 
     187      list($className, $methodName) = explode('.', $classAndMethod); 
     188 
     189      if ($this->isRegisteredClass($className) && $this->hasWebServiceMethod($methodName)) { 
     190        $result = $this->invokeWebServiceMethod($phpJsonObj->params); 
     191      } 
     192 
     193    } else { 
     194      $this->setError('method-Pattern has to be <className>.<methodName>'); 
     195    } 
     196 
     197    $this->checkRequestId($phpJsonObj->id); 
     198    $this->fillResponse($phpJsonObj->id, $result); 
     199  } 
     200 
     201  public function doProcess() { 
     202    switch ($this->request->getMethod()) { 
     203    case 'get': 
     204      $this->doGET(); 
     205      break; 
     206    case 'post': 
     207      $this->doPOST(); 
     208      break; 
     209    } 
     210 
     211    $this->response->write($this->getJsonResponseObject()); 
     212  } 
    214213} 
    215214?>