Exemplo n.º 1
0
 /**
  * @param HttpRequestA $oRequest
  * @return \vsc\domain\models\ModelA
  * @throws \vsc\presentation\responses\ExceptionResponseError
  */
 public function handleRequest(HttpRequestA $oRequest)
 {
     if (!$oRequest->isGet() && !RawHttpRequest::isValid($oRequest)) {
         $oRequest = new RawHttpRequest();
         vsc::getEnv()->setHttpRequest($oRequest);
     }
     switch ($oRequest->getHttpMethod()) {
         case HttpRequestTypes::GET:
             $oModel = $this->handleGet($oRequest);
             break;
         case HttpRequestTypes::HEAD:
             $oModel = $this->handleHead($oRequest);
             break;
         case HttpRequestTypes::POST:
             $oModel = $this->handlePost($oRequest);
             break;
         case HttpRequestTypes::PUT:
             $oModel = $this->handlePut($oRequest);
             break;
         case HttpRequestTypes::DELETE:
             $oModel = $this->handleDelete($oRequest);
             break;
         default:
             throw new ExceptionResponseError('Method [' . $oRequest->getHttpMethod() . '] is unavailable.', HttpResponseType::METHOD_NOT_ALLOWED);
     }
     return $oModel;
 }
Exemplo n.º 2
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);
 }