Large refactoring of SoapKernel

This commit is contained in:
Petr Bechyně
2016-11-01 18:13:23 +01:00
parent 155aa029ce
commit 969709cae5
7 changed files with 72 additions and 169 deletions

View File

@ -53,25 +53,17 @@ class MimeFilter implements SoapRequestFilter, SoapResponseFilter
$this->attachmentType = Helper::ATTACHMENTS_TYPE_SWA;
}
/**
* Modify the given request XML.
*
* @param \BeSimple\SoapCommon\SoapRequest $request SOAP request
*
* @return void
*/
public function filterRequest(SoapRequest $request)
{
// array to store attachments
$attachmentsRecieved = array();
$attachmentsReceived = [];
// check content type if it is a multipart mime message
$requestContentType = $request->getContentType();
if (false !== stripos($requestContentType, 'multipart/related')) {
if (stripos($requestContentType, 'multipart/related') !== false) {
// parse mime message
$headers = array(
$headers = [
'Content-Type' => trim($requestContentType),
);
];
$multipart = MimeParser::parseMimeMessage($request->getContent(), $headers);
// get soap payload and update SoapResponse object
$soapPart = $multipart->getPart();
@ -83,29 +75,20 @@ class MimeFilter implements SoapRequestFilter, SoapResponseFilter
// store attachments
$attachments = $multipart->getParts(false);
foreach ($attachments as $cid => $attachment) {
$attachmentsRecieved[$cid] = $attachment;
$attachmentsReceived[$cid] = $attachment;
}
}
// add attachments to response object
if (count($attachmentsRecieved) > 0) {
$request->setAttachments($attachmentsRecieved);
if (count($attachmentsReceived) > 0) {
$request->setAttachments($attachmentsReceived);
}
return $request;
}
/**
* Modify the given response XML.
*
* @param \BeSimple\SoapCommon\SoapResponse $response SOAP response
*
* @return void
*/
public function filterResponse(SoapResponse $response)
{
// get attachments from request object
$attachmentsToSend = $response->getAttachments();
// build mime message if we have attachments
if (count($attachmentsToSend) > 0) {
$multipart = new MimeMultiPart();
$soapPart = new MimePart($response->getContent(), 'text/xml', 'utf-8', MimePart::ENCODING_EIGHT_BIT);
@ -134,5 +117,7 @@ class MimeFilter implements SoapRequestFilter, SoapResponseFilter
$response->setContentType($contentType);
}
return $response;
}
}

View File

@ -1,43 +1,32 @@
<?php
/*
* This file is part of the BeSimpleSoapCommon.
*
* (c) Christian Kerl <christian-kerl@web.de>
* (c) Francis Besset <francis.besset@gmail.com>
* (c) Andreas Schamberger <mail@andreass.net>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapServer;
use BeSimple\SoapCommon\SoapKernel as CommonSoapKernel;
use BeSimple\SoapCommon\SoapRequest as CommonSoapRequest;
use BeSimple\SoapCommon\SoapResponse as CommonSoapResponse;
use BeSimple\SoapCommon\SoapKernel;
use BeSimple\SoapCommon\SoapRequest;
/**
* SoapKernel for Server.
*
* @todo-critical: kill this shit
* @author Andreas Schamberger <mail@andreass.net>
*/
class SoapKernel extends CommonSoapKernel
class SoapServerKernel extends SoapKernel
{
/**
* {@inheritDoc}
*/
public function filterRequest(CommonSoapRequest $request)
public function filterRequest(SoapRequest $request, array $filters)
{
parent::filterRequest($request);
parent::filterRequest($request, $filters);
$this->attachments = $request->getAttachments();
// attachments are now gone from here
}
/**
* {@inheritDoc}
*/
public function filterResponse(CommonSoapResponse $response)
public function filterResponse(SoapResponse $response)
{
$response->setAttachments($this->attachments);
$this->attachments = array();

View File

@ -12,7 +12,7 @@
namespace BeSimple\SoapServer;
use BeSimple\SoapCommon\SoapMessage;
use BeSimple\SoapCommon\SoapKernel;
use BeSimple\SoapCommon\SoapOptions\SoapOptions;
use BeSimple\SoapCommon\SoapRequest;
use BeSimple\SoapCommon\SoapRequestFactory;
@ -34,6 +34,8 @@ class SoapServer extends \SoapServer
protected $soapVersion;
protected $soapKernel;
protected $soapServerOptions;
protected $soapOptions;
/**
* Constructor.
@ -44,11 +46,13 @@ class SoapServer extends \SoapServer
public function __construct(SoapServerOptions $soapServerOptions, SoapOptions $soapOptions)
{
if ($soapOptions->hasAttachments()) {
$soapOptions = $this->configureMime($soapOptions);
$soapOptions = $this->configureTypeConverters($soapOptions);
}
$this->soapKernel = new SoapKernel();
$this->soapVersion = $soapOptions->getSoapVersion();
$this->soapServerOptions = $soapServerOptions;
$this->soapOptions = $soapOptions;
parent::__construct(
$soapOptions->getWsdlFile(),
@ -95,8 +99,9 @@ class SoapServer extends \SoapServer
*/
private function handleSoapRequest(SoapRequest $soapRequest)
{
// run SoapKernel on SoapRequest
$this->soapKernel->filterRequest($soapRequest);
if ($this->soapOptions->hasAttachments()) {
$soapRequest = $this->soapKernel->filterRequest($soapRequest, $this->getFilters());
}
ob_start();
parent::handle($soapRequest->getContent());
@ -114,36 +119,20 @@ class SoapServer extends \SoapServer
$soapRequest->getVersion()
);
// run SoapKernel on SoapResponse
$this->soapKernel->filterResponse($soapResponse);
if ($this->soapOptions->hasAttachments()) {
$this->soapKernel->filterResponse($soapResponse, $this->getFilters());
}
return $soapResponse;
}
/**
* Get SoapKernel instance.
*
* @return \BeSimple\SoapServer\SoapKernel
*/
public function getSoapKernel()
{
return $this->soapKernel;
}
private function configureMime(SoapOptions $soapOptions)
private function configureTypeConverters(SoapOptions $soapOptions)
{
if ($soapOptions->getAttachmentType() !== SoapOptions::SOAP_ATTACHMENTS_TYPE_BASE64) {
$mimeFilter = new MimeFilter($soapOptions->getAttachmentType());
$this->soapKernel->registerFilter($mimeFilter);
if ($soapOptions->getAttachmentType() === SoapOptions::SOAP_ATTACHMENTS_TYPE_SWA) {
$converter = new SwaTypeConverter();
$converter->setKernel($this->soapKernel);
$soapOptions->getTypeConverterCollection()->add($converter);
$soapOptions->getTypeConverterCollection()->add(new SwaTypeConverter());
} elseif ($soapOptions->getAttachmentType() === SoapOptions::SOAP_ATTACHMENTS_TYPE_MTOM) {
$this->soapKernel->registerFilter(new XmlMimeFilter($soapOptions->getAttachmentType()));
$converter = new MtomTypeConverter();
$converter->setKernel($this->soapKernel);
$soapOptions->getTypeConverterCollection()->add($converter);
$soapOptions->getTypeConverterCollection()->add(new MtomTypeConverter());
} else {
throw new Exception('Unresolved SOAP_ATTACHMENTS_TYPE: ' . $soapOptions->getAttachmentType());
}
@ -151,4 +140,17 @@ class SoapServer extends \SoapServer
return $soapOptions;
}
private function getFilters()
{
$filters = [];
if ($this->soapOptions->getAttachmentType() !== SoapOptions::SOAP_ATTACHMENTS_TYPE_BASE64) {
$filters[] = new MimeFilter($this->soapOptions->getAttachmentType());
}
if ($this->soapOptions->getAttachmentType() === SoapOptions::SOAP_ATTACHMENTS_TYPE_MTOM) {
$filters[] = new XmlMimeFilter($this->soapOptions->getAttachmentType());
}
return $filters;
}
}

View File

@ -31,16 +31,8 @@ class XmlMimeFilter implements SoapResponseFilter
{
}
/**
* Modify the given response XML.
*
* @param \BeSimple\SoapCommon\SoapResponse $response SOAP request
*
* @return void
*/
public function filterResponse(SoapResponse $response)
{
// get \DOMDocument from SOAP request
$dom = $response->getContentDocument();
// create FilterHelper
@ -66,5 +58,6 @@ class XmlMimeFilter implements SoapResponseFilter
}
}
return $response;
}
}