Пример #1
0
 /**
  * @param HttpRequestA $oRequest
  * @param RESTProcessorA $oProcessor
  * @returns HttpResponseA
  * @throws \vsc\presentation\responses\ExceptionResponse
  * @throws \vsc\presentation\responses\ExceptionResponseError
  * @throws \vsc\presentation\views\ExceptionView
  * @throws ExceptionResponseError
  */
 public function getResponse(HttpRequestA $oRequest, $oProcessor = null)
 {
     $oModel = null;
     try {
         if (!$oRequest->isGet()) {
             if ($oRequest->hasContentType() && !RESTRequest::validContentType($oRequest->getContentType())) {
                 throw new ExceptionResponseError('Invalid request content type', HttpResponseType::UNSUPPORTED_MEDIA_TYPE);
             }
         }
         if (!ProcessorA::isValid($oProcessor)) {
             throw new ExceptionController('Invalid request processor');
         }
         /* @var RESTProcessorA $oProcessor */
         if (RESTProcessorA::isValid($oProcessor) && !$oProcessor->validRequestMethod($oRequest->getHttpMethod())) {
             throw new ExceptionResponseError('Invalid request method', HttpResponseType::METHOD_NOT_ALLOWED);
         }
         $oMap = $oProcessor->getMap();
         if ($oMap->requiresAuthentication()) {
             try {
                 if ($oProcessor instanceof AuthenticatedProcessorI) {
                     /* @var AuthenticatedProcessorI $oProcessor */
                     if (!$oRequest->hasAuthenticationData()) {
                         throw new ExceptionAuthenticationNeeded('This resource needs authentication');
                     }
                     // here we check that the request contains the same authentication type as the map
                     if (($oRequest->getAuthentication()->getType() & $oMap->getAuthenticationType()) !== $oMap->getAuthenticationType()) {
                         throw new ExceptionAuthenticationNeeded('Invalid authorization scheme. Supported schemes: ' . implode(', ', $oMap->getValidAuthenticationSchemas()));
                     }
                     if (!$oProcessor->handleAuthentication($oRequest->getAuthentication())) {
                         throw new ExceptionAuthenticationNeeded('Invalid authentication data', 'testrealm');
                     }
                 } else {
                     throw new ExceptionAuthenticationNeeded('This resource requires authentication but doesn\'t support any authorization scheme');
                 }
             } catch (ExceptionAuthenticationNeeded $e) {
                 return $this->getErrorResponse($e, $oRequest);
             }
         }
     } catch (\Exception $e) {
         return $this->getErrorResponse($e, $oRequest);
     }
     return parent::getResponse($oRequest, $oProcessor);
 }
Пример #2
0
 public function testValidContentType()
 {
     $o = new RESTRequest();
     $this->assertTrue($o->validContentType('application/json'));
     $this->assertFalse($o->validContentType('text/plain'));
 }
Пример #3
0
 public function constructRawVars($sRawInput = null)
 {
     parent::constructRawVars($sRawInput);
 }