-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from CollaboraOnline/issue-56-refactor-CoolReq…
…uest Issue #56: Refactor CoolRequest
- Loading branch information
Showing
12 changed files
with
470 additions
and
235 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
services: | ||
_defaults: | ||
autowire: true | ||
logger.channel.collabora_online: | ||
parent: logger.channel_base | ||
arguments: ['cool'] | ||
Drupal\collabora_online\Cool\CollaboraDiscoveryFetcherInterface: | ||
class: Drupal\collabora_online\Cool\CollaboraDiscoveryFetcher | ||
Drupal\collabora_online\Cool\CollaboraDiscoveryInterface: | ||
class: Drupal\collabora_online\Cool\CollaboraDiscovery |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright the Collabora Online contributors. | ||
* | ||
* SPDX-License-Identifier: MPL-2.0 | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\collabora_online\Cool; | ||
|
||
use Drupal\collabora_online\Exception\CollaboraNotAvailableException; | ||
|
||
/** | ||
* Service to get a WOPI client url for a given MIME type. | ||
*/ | ||
class CollaboraDiscovery implements CollaboraDiscoveryInterface { | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param \Drupal\collabora_online\Cool\CollaboraDiscoveryFetcherInterface $discoveryFetcher | ||
* Service to load the discovery.xml from the Collabora server. | ||
*/ | ||
public function __construct( | ||
protected readonly CollaboraDiscoveryFetcherInterface $discoveryFetcher, | ||
) {} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getWopiClientURL(string $mimetype = 'text/plain'): string { | ||
$xml = $this->discoveryFetcher->getDiscoveryXml(); | ||
|
||
$discovery_parsed = simplexml_load_string($xml); | ||
if (!$discovery_parsed) { | ||
throw new CollaboraNotAvailableException('The retrieved discovery.xml file is not a valid XML file.'); | ||
} | ||
|
||
$result = $discovery_parsed->xpath(sprintf('/wopi-discovery/net-zone/app[@name=\'%s\']/action', $mimetype)); | ||
if (empty($result[0]['urlsrc'][0])) { | ||
throw new CollaboraNotAvailableException('The requested mime type is not handled.'); | ||
} | ||
|
||
return (string) $result[0]['urlsrc'][0]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright the Collabora Online contributors. | ||
* | ||
* SPDX-License-Identifier: MPL-2.0 | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\collabora_online\Cool; | ||
|
||
use Drupal\collabora_online\Exception\CollaboraNotAvailableException; | ||
use Drupal\Core\Config\ConfigFactoryInterface; | ||
use Drupal\Core\Logger\LoggerChannelInterface; | ||
use GuzzleHttp\ClientInterface; | ||
use GuzzleHttp\RequestOptions; | ||
use Psr\Http\Client\ClientExceptionInterface; | ||
use Symfony\Component\DependencyInjection\Attribute\Autowire; | ||
|
||
/** | ||
* Service to load the discovery.xml from the Collabora server. | ||
*/ | ||
class CollaboraDiscoveryFetcher implements CollaboraDiscoveryFetcherInterface { | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param \Drupal\Core\Logger\LoggerChannelInterface $logger | ||
* Logger channel. | ||
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory | ||
* Config factory. | ||
* @param \GuzzleHttp\ClientInterface $httpClient | ||
* Http client. | ||
*/ | ||
public function __construct( | ||
#[Autowire(service: 'logger.channel.collabora_online')] | ||
protected readonly LoggerChannelInterface $logger, | ||
protected readonly ConfigFactoryInterface $configFactory, | ||
protected readonly ClientInterface $httpClient, | ||
) {} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getDiscoveryXml(): string { | ||
$discovery_url = $this->getWopiClientServerBaseUrl() . '/hosting/discovery'; | ||
|
||
$cool_settings = $this->loadSettings(); | ||
$disable_checks = !empty($cool_settings['disable_cert_check']); | ||
|
||
try { | ||
$response = $this->httpClient->get($discovery_url, [ | ||
RequestOptions::VERIFY => !$disable_checks, | ||
]); | ||
$xml = $response->getBody()->getContents(); | ||
} | ||
catch (ClientExceptionInterface $e) { | ||
// The backtrace of a client exception is typically not very | ||
// interesting. Just log the message. | ||
$this->logger->error("Failed to fetch from '@url': @message.", [ | ||
'@url' => $discovery_url, | ||
'@message' => $e->getMessage(), | ||
]); | ||
throw new CollaboraNotAvailableException( | ||
'Not able to retrieve the discovery.xml file from the Collabora Online server.', | ||
previous: $e, | ||
); | ||
} | ||
return $xml; | ||
} | ||
|
||
/** | ||
* Loads the WOPI server url from configuration. | ||
* | ||
* @return string | ||
* Base URL to access the WOPI server from Drupal. | ||
* | ||
* @throws \Drupal\collabora_online\Exception\CollaboraNotAvailableException | ||
* The WOPI server url is misconfigured, or the protocol does not match | ||
* that of the current Drupal request. | ||
*/ | ||
protected function getWopiClientServerBaseUrl(): string { | ||
$cool_settings = $this->loadSettings(); | ||
$wopi_client_server = $cool_settings['server'] ?? NULL; | ||
if (!$wopi_client_server) { | ||
throw new CollaboraNotAvailableException('The configured Collabora Online server address is empty.'); | ||
} | ||
$wopi_client_server = trim($wopi_client_server); | ||
|
||
if (!str_starts_with($wopi_client_server, 'http://') && !str_starts_with($wopi_client_server, 'https://')) { | ||
throw new CollaboraNotAvailableException(sprintf( | ||
"The configured Collabora Online server address must begin with 'http://' or 'https://'. Found '%s'.", | ||
$wopi_client_server, | ||
)); | ||
} | ||
|
||
return $wopi_client_server; | ||
} | ||
|
||
/** | ||
* Loads the relevant configuration. | ||
* | ||
* @return array | ||
* Configuration. | ||
* | ||
* @throws \Drupal\collabora_online\Exception\CollaboraNotAvailableException | ||
* The module is not configured. | ||
*/ | ||
protected function loadSettings(): array { | ||
$cool_settings = $this->configFactory->get('collabora_online.settings')->get('cool'); | ||
if (!$cool_settings) { | ||
throw new CollaboraNotAvailableException('The Collabora Online connection is not configured.'); | ||
} | ||
return $cool_settings; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright the Collabora Online contributors. | ||
* | ||
* SPDX-License-Identifier: MPL-2.0 | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\collabora_online\Cool; | ||
|
||
/** | ||
* Service to load the discovery.xml from the Collabora server. | ||
*/ | ||
interface CollaboraDiscoveryFetcherInterface { | ||
|
||
/** | ||
* Gets the contents of discovery.xml from the Collabora server. | ||
* | ||
* @return string | ||
* The full contents of discovery.xml. | ||
* | ||
* @throws \Drupal\collabora_online\Exception\CollaboraNotAvailableException | ||
* The client url cannot be retrieved. | ||
*/ | ||
public function getDiscoveryXml(): string; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright the Collabora Online contributors. | ||
* | ||
* SPDX-License-Identifier: MPL-2.0 | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\collabora_online\Cool; | ||
|
||
/** | ||
* Service to get a WOPI client url for a given MIME type. | ||
*/ | ||
interface CollaboraDiscoveryInterface { | ||
|
||
/** | ||
* Gets the URL for the WOPI client. | ||
* | ||
* @param string $mimetype | ||
* Mime type for which to get the WOPI client url. | ||
* This refers to config entries in the discovery.xml file. | ||
* | ||
* @return string | ||
* The WOPI client url. | ||
* | ||
* @throws \Drupal\collabora_online\Exception\CollaboraNotAvailableException | ||
* The client url cannot be retrieved. | ||
*/ | ||
public function getWopiClientURL(string $mimetype = 'text/plain'): string; | ||
|
||
} |
Oops, something went wrong.