PageRenderTime 58ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Magento/Webapi/Controller/Soap.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 258 lines | 141 code | 21 blank | 96 comment | 7 complexity | c31986d2a5be3e89dc220760a12aec52 MD5 | raw file
  1. <?php
  2. /**
  3. * Front controller for WebAPI SOAP area.
  4. *
  5. * Copyright © 2016 Magento. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Webapi\Controller;
  9. use Magento\Framework\Webapi\ErrorProcessor;
  10. use Magento\Framework\Webapi\Request;
  11. use Magento\Framework\Webapi\Response;
  12. /**
  13. *
  14. * SOAP Web API entry point.
  15. *
  16. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  17. */
  18. class Soap implements \Magento\Framework\App\FrontControllerInterface
  19. {
  20. /**#@+
  21. * Content types used for responses processed by SOAP web API.
  22. */
  23. const CONTENT_TYPE_SOAP_CALL = 'application/soap+xml';
  24. const CONTENT_TYPE_WSDL_REQUEST = 'text/xml';
  25. /**#@-*/
  26. /**
  27. * @var \Magento\Webapi\Model\Soap\Server
  28. */
  29. protected $_soapServer;
  30. /**
  31. * @var \Magento\Webapi\Model\Soap\Wsdl\Generator
  32. */
  33. protected $_wsdlGenerator;
  34. /**
  35. * @var Request
  36. */
  37. protected $_request;
  38. /**
  39. * @var Response
  40. */
  41. protected $_response;
  42. /**
  43. * @var ErrorProcessor
  44. */
  45. protected $_errorProcessor;
  46. /**
  47. * @var \Magento\Framework\Locale\ResolverInterface
  48. */
  49. protected $_localeResolver;
  50. /**
  51. * @var PathProcessor
  52. */
  53. protected $_pathProcessor;
  54. /**
  55. * @var \Magento\Framework\App\AreaList
  56. */
  57. protected $areaList;
  58. /**
  59. * @var \Magento\Framework\Webapi\Rest\Response\RendererFactory
  60. */
  61. protected $rendererFactory;
  62. /**
  63. * @param Request $request
  64. * @param Response $response
  65. * @param \Magento\Webapi\Model\Soap\Wsdl\Generator $wsdlGenerator
  66. * @param \Magento\Webapi\Model\Soap\Server $soapServer
  67. * @param ErrorProcessor $errorProcessor
  68. * @param \Magento\Framework\App\State $appState
  69. * @param \Magento\Framework\Locale\ResolverInterface $localeResolver
  70. * @param PathProcessor $pathProcessor
  71. * @param \Magento\Framework\Webapi\Rest\Response\RendererFactory $rendererFactory
  72. * @param \Magento\Framework\App\AreaList $areaList
  73. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  74. */
  75. public function __construct(
  76. Request $request,
  77. \Magento\Framework\Webapi\Response $response,
  78. \Magento\Webapi\Model\Soap\Wsdl\Generator $wsdlGenerator,
  79. \Magento\Webapi\Model\Soap\Server $soapServer,
  80. ErrorProcessor $errorProcessor,
  81. \Magento\Framework\App\State $appState,
  82. \Magento\Framework\Locale\ResolverInterface $localeResolver,
  83. PathProcessor $pathProcessor,
  84. \Magento\Framework\Webapi\Rest\Response\RendererFactory $rendererFactory,
  85. \Magento\Framework\App\AreaList $areaList
  86. ) {
  87. $this->_request = $request;
  88. $this->_response = $response;
  89. $this->_wsdlGenerator = $wsdlGenerator;
  90. $this->_soapServer = $soapServer;
  91. $this->_errorProcessor = $errorProcessor;
  92. $this->_appState = $appState;
  93. $this->_localeResolver = $localeResolver;
  94. $this->_pathProcessor = $pathProcessor;
  95. $this->areaList = $areaList;
  96. $this->rendererFactory = $rendererFactory;
  97. }
  98. /**
  99. * Dispatch SOAP request.
  100. *
  101. * @param \Magento\Framework\App\RequestInterface $request
  102. * @return \Magento\Framework\App\ResponseInterface
  103. */
  104. public function dispatch(\Magento\Framework\App\RequestInterface $request)
  105. {
  106. $path = $this->_pathProcessor->process($request->getPathInfo());
  107. $this->_request->setPathInfo($path);
  108. $this->areaList->getArea($this->_appState->getAreaCode())->load(\Magento\Framework\App\Area::PART_TRANSLATE);
  109. try {
  110. if ($this->_isWsdlRequest()) {
  111. $this->validateWsdlRequest();
  112. $responseBody = $this->_wsdlGenerator->generate(
  113. $this->_request->getRequestedServices(),
  114. $this->_request->getScheme(),
  115. $this->_request->getHttpHost(),
  116. $this->_soapServer->generateUri()
  117. );
  118. $this->_setResponseContentType(self::CONTENT_TYPE_WSDL_REQUEST);
  119. $this->_setResponseBody($responseBody);
  120. } else if ($this->_isWsdlListRequest()) {
  121. $servicesList = [];
  122. foreach ($this->_wsdlGenerator->getListOfServices() as $serviceName) {
  123. $servicesList[$serviceName]['wsdl_endpoint'] = $this->_soapServer->getEndpointUri()
  124. . '?' . \Magento\Webapi\Model\Soap\Server::REQUEST_PARAM_WSDL . '&services=' . $serviceName;
  125. }
  126. $renderer = $this->rendererFactory->get();
  127. $this->_setResponseContentType($renderer->getMimeType());
  128. $this->_setResponseBody($renderer->render($servicesList));
  129. } else {
  130. $this->_soapServer->handle();
  131. }
  132. } catch (\Exception $e) {
  133. $this->_prepareErrorResponse($e);
  134. }
  135. return $this->_response;
  136. }
  137. /**
  138. * Check if current request is WSDL request. SOAP operation execution request is another type of requests.
  139. *
  140. * @return bool
  141. */
  142. protected function _isWsdlRequest()
  143. {
  144. return $this->_request->getParam(\Magento\Webapi\Model\Soap\Server::REQUEST_PARAM_WSDL) !== null;
  145. }
  146. /**
  147. * Check if current request is WSDL request. SOAP operation execution request is another type of requests.
  148. *
  149. * @return bool
  150. */
  151. protected function _isWsdlListRequest()
  152. {
  153. return $this->_request->getParam(\Magento\Webapi\Model\Soap\Server::REQUEST_PARAM_LIST_WSDL) !== null;
  154. }
  155. /**
  156. * Set body and status code to response using information extracted from provided exception.
  157. *
  158. * @param \Exception $exception
  159. * @return void
  160. */
  161. protected function _prepareErrorResponse($exception)
  162. {
  163. $maskedException = $this->_errorProcessor->maskException($exception);
  164. if ($this->_isWsdlRequest()) {
  165. $httpCode = $maskedException->getHttpCode();
  166. $contentType = self::CONTENT_TYPE_WSDL_REQUEST;
  167. } else {
  168. $httpCode = Response::HTTP_OK;
  169. $contentType = self::CONTENT_TYPE_SOAP_CALL;
  170. }
  171. $this->_setResponseContentType($contentType);
  172. $this->_response->setHttpResponseCode($httpCode);
  173. $soapFault = new \Magento\Webapi\Model\Soap\Fault(
  174. $this->_request,
  175. $this->_soapServer,
  176. $maskedException,
  177. $this->_localeResolver,
  178. $this->_appState
  179. );
  180. $this->_setResponseBody($soapFault->toXml());
  181. }
  182. /**
  183. * Set content type to response object.
  184. *
  185. * @param string $contentType
  186. * @return $this
  187. */
  188. protected function _setResponseContentType($contentType = 'text/xml')
  189. {
  190. $this->_response->clearHeaders()->setHeader(
  191. 'Content-Type',
  192. "{$contentType}; charset={$this->_soapServer->getApiCharset()}"
  193. );
  194. return $this;
  195. }
  196. /**
  197. * Replace WSDL xml encoding from config, if present, else default to UTF-8 and set it to the response object.
  198. *
  199. * @param string $responseBody
  200. * @return $this
  201. */
  202. protected function _setResponseBody($responseBody)
  203. {
  204. $this->_response->setBody(
  205. preg_replace(
  206. '/<\?xml version="([^\"]+)"([^\>]+)>/i',
  207. '<?xml version="$1" encoding="' . $this->_soapServer->getApiCharset() . '"?>',
  208. $responseBody
  209. )
  210. );
  211. return $this;
  212. }
  213. /**
  214. * Validate wsdl request
  215. *
  216. * @return void
  217. * @throws \Magento\Framework\Webapi\Exception
  218. */
  219. protected function validateWsdlRequest()
  220. {
  221. $wsdlParam = \Magento\Webapi\Model\Soap\Server::REQUEST_PARAM_WSDL;
  222. $servicesParam = Request::REQUEST_PARAM_SERVICES;
  223. $requestParams = array_keys($this->_request->getParams());
  224. $allowedParams = [$wsdlParam, $servicesParam];
  225. $notAllowedParameters = array_diff($requestParams, $allowedParams);
  226. if (count($notAllowedParameters)) {
  227. $notAllowed = implode(', ', $notAllowedParameters);
  228. $message = __(
  229. 'Not allowed parameters: %1. Please use only %2 and %3.',
  230. $notAllowed,
  231. $wsdlParam,
  232. $servicesParam
  233. );
  234. throw new \Magento\Framework\Webapi\Exception($message);
  235. }
  236. }
  237. }