|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* ParamConverter class for entry point to ProxyApi Bundle |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Graviton\ProxyApiBundle\Manager; |
|
7
|
|
|
|
|
8
|
|
|
use Graviton\ExceptionBundle\Exception\NotFoundException; |
|
9
|
|
|
use Graviton\ProxyApiBundle\Helper\HttpHelper; |
|
10
|
|
|
use Graviton\ProxyApiBundle\Listener\ProxyExceptionListener; |
|
11
|
|
|
use Graviton\ProxyApiBundle\Model\ProxyModel; |
|
12
|
|
|
use Graviton\ProxyApiBundle\Processor\PostProcessorInterface; |
|
13
|
|
|
use Graviton\ProxyApiBundle\Processor\PreProcessor; |
|
14
|
|
|
use Graviton\ProxyApiBundle\Processor\PreProcessorInterface; |
|
15
|
|
|
use Graviton\ProxyApiBundle\Processor\ProxyProcessorInterface; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
17
|
|
|
use Doctrine\Common\Cache\CacheProvider; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Service Request Converter and startup for ProxyApi |
|
23
|
|
|
* |
|
24
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
25
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
26
|
|
|
* @link http://swisscom.ch |
|
27
|
|
|
*/ |
|
28
|
|
|
class ServiceManager |
|
29
|
|
|
{ |
|
30
|
|
|
/** TODO Cache name for services */ |
|
31
|
|
|
const CACHE_KEY_SERVICES = 'proxy_services'; |
|
32
|
|
|
const CACHE_KEY_SERVICES_TIME = 10; |
|
33
|
|
|
const CACHE_KEY_SERVICES_URLS = 'proxy_services_urls'; |
|
34
|
|
|
const CACHE_KEY_SERVICES_URLS_TIME = 10; |
|
35
|
|
|
const CACHE_KEY_SERVICES_PREFIX = 'proxy_'; |
|
36
|
|
|
|
|
37
|
|
|
/** @var Request */ |
|
38
|
|
|
protected $request; |
|
39
|
|
|
|
|
40
|
|
|
/** @var ProxyManager */ |
|
41
|
|
|
protected $proxyManager; |
|
42
|
|
|
|
|
43
|
|
|
/** @var CacheProvider */ |
|
44
|
|
|
protected $cacheProvider; |
|
45
|
|
|
|
|
46
|
|
|
/** @var HttpHelper */ |
|
47
|
|
|
protected $httpHelper; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Client request "source" |
|
51
|
|
|
* |
|
52
|
|
|
* @var string |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $reqClient; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Client request service |
|
58
|
|
|
* |
|
59
|
|
|
* @var string |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $reqService; |
|
62
|
|
|
|
|
63
|
|
|
/** @var PreProcessor */ |
|
64
|
|
|
protected $preProcessor; |
|
65
|
|
|
protected $proxyProcessor; |
|
66
|
|
|
protected $postProcessor; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* ServiceConverter constructor. |
|
70
|
|
|
* @param RequestStack $requestStack Sf Request information service |
|
71
|
|
|
* @param HttpHelper $httpHelper Request http builder |
|
72
|
|
|
* @param ProxyManager $proxyManager Db Manager and query control |
|
73
|
|
|
* @param CacheProvider $cacheProvider Cache service |
|
74
|
|
|
*/ |
|
75
|
|
|
public function __construct( |
|
76
|
|
|
RequestStack $requestStack, |
|
77
|
|
|
HttpHelper $httpHelper, |
|
78
|
|
|
ProxyManager $proxyManager, |
|
79
|
|
|
CacheProvider $cacheProvider |
|
80
|
|
|
) { |
|
81
|
|
|
$this->request = $requestStack->getCurrentRequest(); |
|
82
|
|
|
$this->httpHelper = $httpHelper; |
|
83
|
|
|
$this->proxyManager = $proxyManager; |
|
84
|
|
|
$this->cacheProvider = $cacheProvider; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Get Request Proxy model |
|
89
|
|
|
* |
|
90
|
|
|
* @return ProxyModel |
|
91
|
|
|
*/ |
|
92
|
|
|
private function getProxyModel() |
|
93
|
|
|
{ |
|
94
|
|
|
$this->reqClient = $this->request->get('client'); |
|
95
|
|
|
$this->reqService = $this->request->get('service'); |
|
96
|
|
|
|
|
97
|
|
|
if (!$this->reqClient || !$this->reqService) { |
|
98
|
|
|
throw new ProxyExceptionListener(404, 'Proxy service not found'); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** @var ProxyModel $proxyModel */ |
|
102
|
|
|
$proxyModel = $this->proxyManager->getService($this->reqClient); |
|
103
|
|
|
|
|
104
|
|
|
return $proxyModel; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* List all services |
|
109
|
|
|
* |
|
110
|
|
|
* @return array |
|
|
|
|
|
|
111
|
|
|
*/ |
|
112
|
|
|
public function getServices() |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->proxyManager->getServices(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Execute proxy request |
|
119
|
|
|
* |
|
120
|
|
|
* @return Response |
|
121
|
|
|
*/ |
|
122
|
|
|
public function processRequest() |
|
123
|
|
|
{ |
|
124
|
|
|
// Check if there is a service with that name |
|
125
|
|
|
$proxyModel = $this->getProxyModel(); |
|
126
|
|
|
|
|
127
|
|
|
$this->httpHelper->setBaseUri($proxyModel->getUri()); |
|
128
|
|
|
|
|
129
|
|
|
// Do processing steps |
|
130
|
|
|
$preProcessing = $proxyModel->getPreProcessorService(); |
|
131
|
|
|
if (!$preProcessing instanceof PreProcessorInterface) { |
|
132
|
|
|
throw new ProxyExceptionListener(412, 'Configured PreProcessing configuration is incorrect'); |
|
133
|
|
|
} |
|
134
|
|
|
$this->httpHelper = $preProcessing->process($this->request, $this->httpHelper); |
|
135
|
|
|
|
|
136
|
|
|
$proxyProcesor = $proxyModel->getProxyProcessorService(); |
|
137
|
|
|
if (!$proxyProcesor instanceof ProxyProcessorInterface) { |
|
138
|
|
|
throw new ProxyExceptionListener(412, 'Configured ProxyProcessor configuration is incorrect'); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** @var Response $response */ |
|
142
|
|
|
$response = $proxyProcesor->process($this->request, $this->httpHelper, $proxyModel); |
|
143
|
|
|
|
|
144
|
|
|
$postProcesor = $proxyModel->getPostProcessorService(); |
|
145
|
|
|
if (!$postProcesor instanceof PostProcessorInterface) { |
|
146
|
|
|
throw new ProxyExceptionListener(412, 'Configured ProxyProcessor configuration is incorrect'); |
|
147
|
|
|
} |
|
148
|
|
|
$response = $postProcesor->process($response); |
|
149
|
|
|
|
|
150
|
|
|
return $response; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.