|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* A controller for RESTful calls |
|
4
|
|
|
* |
|
5
|
|
|
* @package application/controllers |
|
6
|
|
|
* @author marius orcsik <[email protected]> |
|
7
|
|
|
* @date 2013.10.04 |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace vsc\rest\application\controllers; |
|
10
|
|
|
|
|
11
|
|
|
use vsc\application\controllers\ExceptionController; |
|
12
|
|
|
use vsc\application\controllers\JsonController; |
|
13
|
|
|
use vsc\application\processors\AuthenticatedProcessorInterface; |
|
14
|
|
|
use vsc\application\processors\ProcessorA; |
|
15
|
|
|
use vsc\presentation\requests\ContentType; |
|
16
|
|
|
use vsc\presentation\responses\HttpResponseType; |
|
17
|
|
|
use vsc\rest\application\processors\RESTProcessorA; |
|
18
|
|
|
use vsc\presentation\requests\HttpRequestA; |
|
19
|
|
|
use vsc\presentation\responses\ExceptionAuthenticationNeeded; |
|
20
|
|
|
use vsc\presentation\responses\ExceptionResponseError; |
|
21
|
|
|
use vsc\presentation\responses\HttpResponseA; |
|
22
|
|
|
|
|
23
|
|
|
class RESTController extends JsonController { |
|
24
|
|
|
/** |
|
25
|
|
|
* @param HttpRequestA $oRequest |
|
26
|
|
|
* @param RESTProcessorA $oProcessor |
|
|
|
|
|
|
27
|
|
|
* @returns HttpResponseA |
|
28
|
|
|
* @throws \vsc\presentation\responses\ExceptionResponse |
|
29
|
|
|
* @throws \vsc\presentation\responses\ExceptionResponseError |
|
30
|
|
|
* @throws \vsc\presentation\views\ExceptionView |
|
31
|
|
|
* @throws ExceptionResponseError |
|
32
|
|
|
*/ |
|
33
|
9 |
|
public function getResponse(HttpRequestA $oRequest, $oProcessor = null) { |
|
34
|
9 |
|
$oModel = null; |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
try { |
|
37
|
9 |
|
if (!$oRequest->isGet()) { |
|
38
|
|
|
if ($oRequest->hasContentType() && !ContentType::isValidContentType($oRequest->getContentType())) { |
|
39
|
|
|
throw new ExceptionResponseError('Invalid request content type', HttpResponseType::UNSUPPORTED_MEDIA_TYPE); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
9 |
|
if (!ProcessorA::isValid($oProcessor)) { |
|
43
|
2 |
|
throw new ExceptionController('Invalid request processor'); |
|
44
|
|
|
} |
|
45
|
|
|
/* @var RESTProcessorA $oProcessor */ |
|
46
|
7 |
|
if (RESTProcessorA::isValid($oProcessor) && !$oProcessor->validRequestMethod($oRequest->getHttpMethod())) { |
|
47
|
2 |
|
throw new ExceptionResponseError('Invalid request method', HttpResponseType::METHOD_NOT_ALLOWED); |
|
48
|
|
|
} |
|
49
|
5 |
|
$oMap = $oProcessor->getMap(); |
|
50
|
5 |
|
if ($oMap->requiresAuthentication()) { |
|
51
|
|
|
try { |
|
52
|
4 |
|
if ($oProcessor instanceof AuthenticatedProcessorInterface) { |
|
53
|
|
|
/* @var AuthenticatedProcessorInterface $oProcessor */ |
|
54
|
3 |
|
if (!$oRequest->hasAuthenticationData()) { |
|
55
|
|
|
throw new ExceptionAuthenticationNeeded('This resource needs authentication'); |
|
56
|
|
|
} |
|
57
|
|
|
// here we check that the request contains the same authentication type as the map |
|
58
|
3 |
|
if (($oRequest->getAuthentication()->getType() & $oMap->getAuthenticationType()) !== $oMap->getAuthenticationType()) { |
|
59
|
1 |
|
throw new ExceptionAuthenticationNeeded('Invalid authorization scheme. Supported schemes: ' . implode(', ', $oMap->getValidAuthenticationSchemas())); |
|
60
|
|
|
} |
|
61
|
2 |
|
if (!$oProcessor->handleAuthentication($oRequest->getAuthentication())) { |
|
62
|
2 |
|
throw new ExceptionAuthenticationNeeded('Invalid authentication data', 'testrealm'); |
|
63
|
|
|
} |
|
64
|
|
|
} else { |
|
65
|
1 |
|
throw new ExceptionAuthenticationNeeded('This resource requires authentication but doesn\'t support any authorization scheme'); |
|
66
|
|
|
} |
|
67
|
4 |
|
} catch (ExceptionAuthenticationNeeded $e) { |
|
68
|
5 |
|
return $this->getErrorResponse($e, $oRequest); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
4 |
|
} catch (\Exception $e) { |
|
72
|
4 |
|
return $this->getErrorResponse($e, $oRequest); |
|
73
|
|
|
} |
|
74
|
1 |
|
return parent::getResponse($oRequest, $oProcessor); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Returns a view based on the |
|
79
|
|
|
* @fixme |
|
80
|
|
|
* @return \vsc\presentation\views\JsonView |
|
81
|
|
|
*/ |
|
82
|
1 |
|
public function getDefaultView() { |
|
83
|
1 |
|
return parent::getDefaultView(); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.