forked from christiankerl/WebServiceBundle
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathSoapWebServiceController.php
253 lines (216 loc) · 8.36 KB
/
SoapWebServiceController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
/*
* This file is part of the BeSimpleSoapBundle.
*
* (c) Christian Kerl <[email protected]>
* (c) Francis Besset <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapBundle\Controller;
use BeSimple\SoapBundle\Handler\ExceptionHandler;
use BeSimple\SoapBundle\Soap\SoapRequest;
use BeSimple\SoapBundle\Soap\SoapResponse;
use BeSimple\SoapServer\SoapServerBuilder;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\FlattenException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* @author Christian Kerl <[email protected]>
* @author Francis Besset <[email protected]>
*/
class SoapWebServiceController extends Controller
{
use ContainerAwareTrait;
/**
* @var \SoapServer
*/
protected $soapServer;
/**
* @var \BeSimple\SoapBundle\Soap\SoapRequest
*/
protected $soapRequest;
/**
* @var \BeSimple\SoapBundle\Soap\SoapResponse
*/
protected $soapResponse;
/**
* @var \BeSimple\SoapBundle\ServiceBinding\ServiceBinder
*/
protected $serviceBinder;
/**
* @var array
*/
private $headers = array();
/**
* @return \BeSimple\SoapBundle\Soap\SoapResponse
*/
public function callAction($webservice)
{
$webServiceContext = $this->getWebServiceContext($webservice);
$this->serviceBinder = $webServiceContext->getServiceBinder();
$this->soapRequest = SoapRequest::createFromHttpRequest($this->container->get('request_stack')->pop());
$this->soapServer = $webServiceContext
->getServerBuilder()
->withSoapVersion11()
->withHandler($this)
->build()
;
ob_start();
$this->soapServer->handle($this->soapRequest->getSoapMessage());
$response = $this->getResponse();
$response->setContent(ob_get_clean());
// The Symfony 2.0 Response::setContent() does not return the Response instance
return $response;
}
/**
* @return Symfony\Component\HttpFoundation\Response
*/
public function definitionAction($webservice)
{
$response = new Response($this->getWebServiceContext($webservice)->getWsdlFileContent(
$this->container->get('router')->generate(
'_webservice_call',
array('webservice' => $webservice),
UrlGeneratorInterface::ABSOLUTE_URL
)
));
$request = $this->container->get('request_stack')->pop();
$query = $request->query;
if ($query->has('wsdl') || $query->has('WSDL')) {
$request->setRequestFormat('wsdl');
}
return $response;
}
/**
* Converts an Exception to a SoapFault Response.
*
* @param Request $request The request
* @param FlattenException $exception A FlattenException instance
* @param DebugLoggerInterface $logger A DebugLoggerInterface instance
*
* @return Response
*
* @throws \LogicException When the request query parameter "_besimple_soap_webservice" does not exist
*/
public function exceptionAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null)
{
if (!$webservice = $request->query->get('_besimple_soap_webservice')) {
throw new \LogicException(sprintf('The parameter "%s" is required in Request::$query parameter bag to generate the SoapFault.', '_besimple_soap_webservice'), null, $e);
}
$view = 'TwigBundle:Exception:'.($this->container->get('kernel')->isDebug() ? 'exception' : 'error').'.txt.twig';
$code = $exception->getStatusCode();
$details = $this->container->get('templating')->render($view, array(
'status_code' => $code,
'status_text' => isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '',
'exception' => $exception,
'logger' => $logger,
));
$handler = new ExceptionHandler($exception, $details);
if ($soapFault = $request->query->get('_besimple_soap_fault')) {
$handler->setSoapFault($soapFault);
// Remove parameter from query because cannot be Serialized in Logger
$request->query->remove('_besimple_soap_fault');
}
$server = SoapServerBuilder::createWithDefaults()
->withWsdl(__DIR__.'/../Handler/wsdl/exception.wsdl')
->withWsdlCacheNone()
->withHandler($handler)
->build()
;
ob_start();
$server->handle(
'<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://besim.pl/soap/exception/1.0/">'.
'<soapenv:Header/>'.
'<soapenv:Body>'.
'<ns:exception />'.
'</soapenv:Body>'.
'</soapenv:Envelope>'
);
return new Response(ob_get_clean());
}
/**
* This method gets called once for every SOAP header the \SoapServer received
* and afterwards once for the called SOAP operation.
*
* @param string $method The SOAP header or SOAP operation name
* @param array $arguments
*
* @return mixed
*/
public function __call($method, $arguments)
{
if ($this->serviceBinder->isServiceMethod($method)) {
// @TODO Add all SoapHeaders in SoapRequest
foreach ($this->headers as $name => $value) {
if ($this->serviceBinder->isServiceHeader($method, $name)) {
$this->soapRequest->getSoapHeaders()->add($this->serviceBinder->processServiceHeader($method, $name, $value));
}
}
$this->headers = null;
$this->soapRequest->attributes->add(
$this->serviceBinder->processServiceMethodArguments($method, $arguments)
);
// forward to controller
$response = $this->container->get('http_kernel')->handle($this->soapRequest, HttpKernelInterface::SUB_REQUEST, false);
$this->setResponse($response);
// add response soap headers to soap server
foreach ($response->getSoapHeaders() as $header) {
$this->soapServer->addSoapHeader($header->toNativeSoapHeader());
}
// return operation return value to soap server
return $this->serviceBinder->processServiceMethodReturnValue(
$method,
$response->getReturnValue()
);
} else {
// collect request soap headers
$this->headers[$method] = $arguments[0];
}
}
/**
* @return \BeSimple\SoapBundle\Soap\SoapRequest
*/
protected function getRequest()
{
return $this->soapRequest;
}
/**
* @return \BeSimple\SoapBundle\Soap\SoapResponse
*/
protected function getResponse()
{
return $this->soapResponse ?: $this->soapResponse = $this->container->get('besimple.soap.response');
}
/**
* Set the SoapResponse
*
* @param Response $response A response to check and set
*
* @return \BeSimple\SoapBundle\Soap\SoapResponse A valid SoapResponse
*
* @throws InvalidArgumentException If the given Response is not an instance of SoapResponse
*/
protected function setResponse(Response $response)
{
if (!$response instanceof SoapResponse) {
throw new \InvalidArgumentException('You must return an instance of BeSimple\SoapBundle\Soap\SoapResponse');
}
return $this->soapResponse = $response;
}
private function getWebServiceContext($webservice)
{
$context = sprintf('besimple.soap.context.%s', $webservice);
if (!$this->container->has($context)) {
throw new NotFoundHttpException(sprintf('No WebService with name "%s" found.', $webservice));
}
return $this->container->get($context);
}
}