Skip to content

Commit

Permalink
Merge pull request #31 from itk-dev/feature/huge-xml-documents
Browse files Browse the repository at this point in the history
Added XML load helper function
  • Loading branch information
rimi-itk authored Aug 6, 2024
2 parents 0e35481 + cba50e7 commit 536bb16
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 33 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- [PR-31](https://github.com/itk-dev/serviceplatformen/pull/31)
Added XML load helper function

## [1.5.1] - 2024-04-24

- [PR-29](https://github.com/itk-dev/serviceplatformen/pull/29)
Expand Down
3 changes: 1 addition & 2 deletions src/Command/SF1601/KombiPostAfsendCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$meMoMessage = $service->getLastKombiMeMoMessage();
$io->writeln($meMoMessage->ownerDocument->saveXML($meMoMessage));

$dom = new DOMDocument();
$dom = Serializer::loadXML($response->getContent());
$dom->formatOutput = true;
$dom->loadXML($response->getContent());

$headers = [];
$length = max(...array_map(static fn (string $name) => strlen($name), array_keys($response->getHeaders())));
Expand Down
17 changes: 6 additions & 11 deletions src/Service/SF1500/SF1500.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use ItkDev\Serviceplatformen\Service\Exception\SAMLTokenException;
use ItkDev\Serviceplatformen\Service\Exception\SF1500Exception;
use ItkDev\Serviceplatformen\Service\SF1514\SF1514;
use ItkDev\Serviceplatformen\Service\SF1601\Serializer;
use ItkDev\Serviceplatformen\SF1500\Adresse\ClassMap as AdresseClassMap;
use ItkDev\Serviceplatformen\SF1500\Adresse\ServiceType\_List as AdresseList;
use ItkDev\Serviceplatformen\SF1500\Adresse\ServiceType\Laes as AdresseLaes;
Expand Down Expand Up @@ -682,8 +683,7 @@ public function formatSoapRequest(
int $version,
bool $oneWay = false
): string {
$doc = new \DOMDocument();
$doc->loadXML($request);
$doc = Serializer::loadXML($request);

// Set an id.
/** @var \DOMElement $body */
Expand Down Expand Up @@ -764,8 +764,7 @@ public function cacheSoapRequest(array $cacheKeys, callable $callable)
private function preventCaching(string $response): bool
{
try {
$document = new \DOMDocument();
$document->loadXML($response);
$document = Serializer::loadXML($response);
// Prevent caching if we have a SOAP fault.
if ($document->getElementsByTagNameNS(self::NS_SOAP_ENVELOPE, 'Fault')->count() > 0) {
return true;
Expand Down Expand Up @@ -802,13 +801,9 @@ public function getSoapRequestCacheExpirationDateTime(): ?\DateTimeImmutable
$now = new \DateTimeImmutable('now');
$times = [];
foreach ($this->options['soap_request_cache_expiration_time'] as $spec) {
try {
$time = $now->modify($spec);
if ($time > $now) {
$times[] = $time;
}
} catch (\Exception $exception) {
// Ignore any exceptions.
$time = $now->modify($spec);
if ($time > $now) {
$times[] = $time;
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/Service/SF1500/SF1500XMLBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace ItkDev\Serviceplatformen\Service\SF1500;

use ItkDev\Serviceplatformen\Service\SF1601\Serializer;

/**
* Helper class for SF1500 Organisation.
*/
Expand Down Expand Up @@ -86,8 +88,7 @@ public function buildSoapHeader(\DOMElement $header, string $to, string $action,
$timestampElement->appendChild($expiresElement);
$securityElement->appendChild($timestampElement);

$tokenDocument = new \DOMDocument();
$tokenDocument->loadXML($tokenXml);
$tokenDocument = Serializer::loadXML($tokenXml);
$tokenElement = $document->importNode($tokenDocument->documentElement, true);
$tokenUuid = $this->getElementId($tokenElement);

Expand Down Expand Up @@ -145,7 +146,7 @@ public function buildSignedRequest(string $requestSimple, string $privKey)
$documentRequest = new \DOMDocument('1.0', 'utf-8');
$documentRequest->preserveWhiteSpace = false;
$documentRequest->formatOutput = false;
$documentRequest->loadXML($requestSimple);
Serializer::loadXML($requestSimple, $documentRequest);

$signatureElement = $documentRequest->getElementsByTagName('Signature')[1];
$signedInfoElement = $signatureElement->getElementsByTagName('SignedInfo')[0];
Expand Down
4 changes: 2 additions & 2 deletions src/Service/SF1514/SF1514.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use ItkDev\Serviceplatformen\Certificate\CertificateLocatorInterface;
use ItkDev\Serviceplatformen\Service\Exception\SAMLTokenException;
use ItkDev\Serviceplatformen\Service\Exception\ServiceException;
use ItkDev\Serviceplatformen\Service\SF1601\Serializer;
use ItkDev\Serviceplatformen\Service\SoapClient;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\OptionsResolver\Options;
Expand Down Expand Up @@ -338,8 +339,7 @@ public function generateUuid()
*/
public function parseRequestSecurityTokenResponse($result)
{
$dom = new \DOMDocument();
$dom->loadXML($result);
$dom = Serializer::loadXML($result);
$doc = $dom->documentElement;
$xpath = new \DOMXpath($dom);
$xpath->registerNamespace('s', 'http://www.w3.org/2003/05/soap-envelope');
Expand Down
9 changes: 3 additions & 6 deletions src/Service/SF1601/SF1601.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ private function buildKombiRequestDocument(string $type, ?Message $message, ?For
}

// Build kombi document.
$document = new DOMDocument();
$document->loadXML('<kombi_request><KombiValgKode/></kombi_request>');
$document = Serializer::loadXML('<kombi_request><KombiValgKode/></kombi_request>');
// Set KombiValgKode.
$document->documentElement->firstChild->appendChild(new DOMText($type));

Expand Down Expand Up @@ -193,17 +192,15 @@ private function buildKombiRequestDocument(string $type, ?Message $message, ?For
}

// Serialize message and import and append it to kombi_request element.
$messageDocument = new DOMDocument();
$messageDocument->loadXML((new Serializer())->serialize($message));
$messageDocument = Serializer::loadXML((new Serializer())->serialize($message));

$document->documentElement->appendChild($document->importNode($messageDocument->documentElement, true));
}

if (null !== $forsendelse) {
$forsendelseSamling = $document->createElementNS('urn:oio:fjernprint:1.0.0', 'ForsendelseISamling');

$forsendelseDocument = new DOMDocument();
$forsendelseDocument->loadXML((new Serializer())->serialize($forsendelse));
$forsendelseDocument = Serializer::loadXML((new Serializer())->serialize($forsendelse));
$forsendelseSamling->appendChild($document->importNode($forsendelseDocument->documentElement, true));

$document->documentElement->appendChild($forsendelseSamling);
Expand Down
17 changes: 15 additions & 2 deletions src/Service/SF1601/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,23 @@ public static function createUuid(): string
return Uuid::v4()->toRfc4122();
}

/**
* Helper function to load XML into a DOM document.
*
* We need to be able to handle very long node values (base64 encoded PDF files).
*/
public static function loadXML(string $xml, ?\DOMDocument $document = null): \DOMDocument
{
$document ??= new DOMDocument();

$document->loadXML($xml, LIBXML_PARSEHUGE);

return $document;
}

private function normalizeXml(string $xml): string
{
$document = new DOMDocument();
$document->loadXML($xml);
$document = static::loadXML($xml);

return $document->saveXML();
}
Expand Down
10 changes: 3 additions & 7 deletions src/Service/SoapClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,9 @@ public function determineExpirationDateTime(): ?\DateTimeImmutable
$now = new \DateTimeImmutable('now');
$times = [];
foreach ($this->options['cache_expiration_time'] as $spec) {
try {
$time = $now->modify($spec);
if ($time > $now) {
$times[] = $time;
}
} catch (\Exception $exception) {
// Ignore any exceptions.
$time = $now->modify($spec);
if ($time > $now) {
$times[] = $time;
}
}

Expand Down

0 comments on commit 536bb16

Please sign in to comment.