Changeset 441
- Timestamp:
- 03/29/07 17:09:23 (2 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/websites/processors/stubJsonRpcProcessor.php
r435 r441 1 1 <?php 2 3 stubClassLoader::load('net.stubbles.websites.processors.stubAbstractProcessor',4 'net.stubbles.util.validators.stubRegexValidator',5 'net.stubbles.ipo.request.filters.stubStringFilter',6 'net.stubbles.ipo.request.filters.stubPassThruFilter',7 'net.stubbles.ipo.request.stubXmlRequestValueErrorFactory');8 9 2 /** 10 * The JsonRpc Service3 * JSON-RPC Processor (generic Proxy for Web Services) 11 4 * 12 5 * @author Richard Sternagel … … 14 7 * @subpackage websites_processors 15 8 */ 9 stubClassLoader::load('net.stubbles.websites.processors.stubAbstractProcessor', 10 'net.stubbles.util.validators.stubRegexValidator'); 11 12 /** 13 * JSON-RPC Processor (generic Proxy for Web Services) 14 * 15 * @package stubbles 16 * @subpackage websites_processors 17 */ 16 18 class stubJsonRpcProcessor extends stubAbstractProcessor { 17 19 18 const classAndMethodPattern = '^([a-zA-Z0-9_]*\.[a-zA-Z0-9_]*)$'; 19 // an paramAnnotation könnte Range gebunden werden, der den RegEx-Ausdruck (DefBereich) individuell bestimmt 20 const paramPattern = '^[a-zA-Z0-9_]*$'; 21 const idPattern = '^\d{6,7}$'; 22 const jsonPattern = '^{\"method\":\"(.*)\",\"params\"(.*),\"id\":\"\d{6,7}\"}$'; 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}\"}$'; 23 28 24 29 protected $jsonRpcResponse = array ( … … 26 31 'result' => null, 27 32 'error' => null, 28 );33 ); 29 34 30 35 protected $classMap = array( … … 35 40 protected $classObj = null; 36 41 protected $methodObj = null; 37 38 42 39 43 protected function getReflectionObject($class) { … … 71 75 72 76 public function getJsonResponseObject() { 73 return $this->jsonRpcResponse;77 return json_encode($this->jsonRpcResponse); 74 78 } 75 79 … … 100 104 protected function retrieveGETParams(){ 101 105 $paramValues = array(); 102 $paramPattern = new stubRegexValidator(s tubJsonRpcProcessor::paramPattern);106 $paramPattern = new stubRegexValidator(self::PARAM_PATTERN); 103 107 foreach ($this->methodObj->getParameters() as $param) { 104 108 $paramName = $param->getName(); … … 145 149 */ 146 150 public function doGET() { 147 $classAndMethodPattern = new stubRegexValidator(s tubJsonRpcProcessor::classAndMethodPattern);151 $classAndMethodPattern = new stubRegexValidator(self::CLASS_AND_METHOD_PATTERN); 148 152 $classAndMethod = $this->request->getValidatedValue($classAndMethodPattern, 'method'); 149 153 … … 161 165 } 162 166 163 $idPattern = new stubRegexValidator(s tubJsonRpcProcessor::idPattern);167 $idPattern = new stubRegexValidator(self::ID_PATTERN); 164 168 $requestId = $this->request->getValidatedValue($idPattern, 'id'); 165 169 $this->checkRequestId($requestId); … … 176 180 */ 177 181 public function doPOST() { 178 $jsonPattern = new stubRegexValidator(s tubJsonRpcProcessor::jsonPattern);182 $jsonPattern = new stubRegexValidator(self::JSON_PATTERN); 179 183 $requestJsonObj = $this->request->getValidatedRawData($jsonPattern); 180 184 $phpJsonObj = json_decode($requestJsonObj); 181 185 $classAndMethod = $phpJsonObj->method; 182 186 183 if(isset($classAndMethod) && preg_match('/'.s tubJsonRpcProcessor::classAndMethodPattern.'/', $classAndMethod)) {187 if(isset($classAndMethod) && preg_match('/'.self::CLASS_AND_METHOD_PATTERN.'/', $classAndMethod)) { 184 188 list($className, $methodName) = explode('.', $classAndMethod); 185 189 … … 206 210 } 207 211 208 $this->response->write( json_encode($this->getJsonResponseObject()));212 $this->response->write($this->getJsonResponseObject()); 209 213 } 210 214 } trunk/src/test/php/net/stubbles/websites/processors/stubJsonRpcProcessorTestCase.php
r435 r441 1 1 <?php 2 /** 3 * Test for net.stubbles.websites.processors.stubJsonRpcProcessor 4 * 5 * @author Richard Sternagel 6 * @package stubbles 7 * @subpackage websites_processors_test 8 */ 2 9 stubClassLoader::load('net.stubbles.websites.processors.stubJsonRpcProcessor', 3 10 '_test.BuddyQuoteService'); … … 5 12 Mock::generate('stubRequest'); 6 13 Mock::generate('stubSession'); 7 14 8 15 /** 9 * Test for stubDomXMLStreamWriter16 * Tests for net.stubbles.websites.processors.stubJsonRpcProcessor 10 17 * 11 * @author Richard Sternagel 12 * @package stubbles 13 * @subpackage 18 * @package stubbles 19 * @subpackage websites_processors_test 14 20 */ 15 21 class stubJsonRpcProcessorTestCase extends UnitTestCase { 16 22 23 /** 24 * mocked page factory 25 * 26 * @var SimpleMock 27 */ 17 28 protected $mockPageFactory; 18 /** 29 30 /** 19 31 * mocked request to use 20 32 * … … 22 34 */ 23 35 protected $mockRequest; 36 24 37 /** 25 38 * mocked response instance … … 28 41 */ 29 42 protected $mockResponse; 43 30 44 /** 31 45 * mocked session to use … … 33 47 * @var SimpleMock 34 48 */ 49 protected $mockSession; 35 50 36 protected $mockSession = null; 37 protected $jsonDummyReq = null; 38 protected $jsonDummyResp = null; 39 protected $bqService = null; 40 protected $proc = null; 51 /** 52 * dummy POST Request (JSON-RPC) 53 * 54 * @var String 55 */ 56 protected $jsonDummyPOSTReq; 41 57 42 58 /** 43 * create enviroment 59 * dummy GET Request (Method) 60 * 61 * @var String 44 62 */ 63 protected $jsonDummyGETReqMethod; 64 65 /** 66 * dummy GET Request (Params) 67 * 68 * @var String 69 */ 70 protected $jsonDummyGETReqParams; 71 72 /** 73 * dummy GET Request (Id) 74 * 75 * @var String 76 */ 77 protected $jsonDummyGETReqId; 78 79 /** 80 * dummy JSON-RPC Response 81 * 82 * @var String 83 */ 84 protected $jsonDummyResp; 85 86 /** 87 * mocked processor 88 * 89 * @var stubJsonRpcProcessor 90 */ 91 protected $proc; 92 93 /** 94 * set up test environment 95 */ 45 96 public function setUp() { 46 $this->jsonDummyReq = '{"method":"BuddyQuoteService.getQuote","params":[5],"id":"186252"}'; 47 $this->jsonDummyResp = '{"id":"186252","result":"An meinem Arm wird nicht gepuffert!","error":null}'; 48 $this->bqService = new BuddyQuoteService(); 97 $this->jsonDummyPOSTReq = '{"method":"BuddyQuoteService.getQuote","params":[5],"id":"186252"}'; 49 98 50 $this->mockPageFactory = new MockstubPageFactory(); 99 $this->jsonDummyGETReqMethod = 'BuddyQuoteService.getQuote'; 100 $this->jsonDummyGETReqParams = '5'; 101 $this->jsonDummyGETReqId = '186252'; 102 103 $this->jsonDummyResp = '{"id":"186252","result":"An meinem Arm wird nicht gepuffert!","error":null}'; 51 104 52 $this->mockRequest = new MockstubRequest(); 53 54 $this->mockSession = new MockstubSession(); 55 $this->mockResponse = new MockstubResponse(); 56 57 $this->proc = new stubJsonRpcProcessor($this->mockRequest, 58 $this->mockSession, 59 $this->mockResponse, 60 $this->mockPageFactory); 105 $this->mockRequest = new MockstubRequest(); 106 $this->mockSession = new MockstubSession(); 107 $this->mockResponse = new MockstubResponse(); 108 $this->mockPageFactory = new MockstubPageFactory(); 109 $this->proc = new stubJsonRpcProcessor($this->mockRequest, 110 $this->mockSession, 111 $this->mockResponse, 112 $this->mockPageFactory); 61 113 } 62 114 63 115 /** 64 * JSON-RPC GET request116 * test HTTP-POST Request 65 117 */ 66 118 public function testDoPost() { 67 $ buddyQuote = $this->bqService->getQuote(3);68 $this->mockRequest->setReturnValue('getValidatedRawData', $this->jsonDummyReq);119 $this->mockRequest->setReturnValue('getValidatedRawData', $this->jsonDummyPOSTReq); 120 69 121 $this->proc->doPOST(); 70 $this->assertEqual($this->jsonDummyResp, json_encode($this->proc->getJsonResponseObject())); 122 $this->assertEqual($this->jsonDummyResp, $this->proc->getJsonResponseObject()); 123 } 124 125 /* 126 * test HTTP-GET Request 127 */ 128 public function testDoGet() { 129 $this->mockRequest->setReturnValue('getValidatedValue', $this->jsonDummyGETReqMethod, array('*', 'method')); 130 $this->mockRequest->setReturnValue('getValidatedValue', $this->jsonDummyGETReqParams, array('*', 'arrKey')); 131 $this->mockRequest->setReturnValue('getValidatedValue', $this->jsonDummyGETReqId, array('*', 'id')); 132 133 $this->proc->doGET(); 134 $this->assertEqual($this->jsonDummyResp, $this->proc->getJsonResponseObject()); 71 135 } 72 136 }
