Skip to content

Commit

Permalink
feat: revoir la récup du fichier métadonnée de geonetwork #381
Browse files Browse the repository at this point in the history
  • Loading branch information
ocruze committed Dec 9, 2024
1 parent 327e94c commit 2464e4c
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 39 deletions.
2 changes: 1 addition & 1 deletion assets/entrepot/api/geonetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import SymfonyRouting from "../../modules/Routing";
import { jsonFetch } from "../../modules/jsonFetch";

const getMetadataInfos = (fileIdentifier: string, otherOptions: RequestInit = {}) => {
const url = SymfonyRouting.generate("cartesgouvfr_geonetwork_metadata_get_infos", { fileIdentifier });
const url = SymfonyRouting.generate("cartesgouvfr_geonetwork_metadata_get", { fileIdentifier });
return jsonFetch<GeonetworkMetadataResponse>(url, {
...otherOptions,
});
Expand Down
5 changes: 3 additions & 2 deletions assets/entrepot/pages/AccessesRequest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { useQuery } from "@tanstack/react-query";
import { ComponentProps, FC, ReactNode, useMemo, useState } from "react";
import { useForm } from "react-hook-form";
import * as yup from "yup";
import { GeonetworkMetadataResponse } from "../../@types/app";

import type { GeonetworkMetadataResponse } from "../../@types/app";
import AppLayout from "../../components/Layout/AppLayout";
import LoadingText from "../../components/Utils/LoadingText";
import Wait from "../../components/Utils/Wait";
Expand All @@ -29,7 +30,7 @@ const AccessesRequest: FC<AskForAccesses> = ({ fileIdentifier }) => {
const { t } = useTranslation({ AccessesRequest });
const { t: tCommon } = useTranslation("Common");

const { user } = useAuthStore();
const user = useAuthStore((state) => state.user);

const [isSending, setIsSending] = useState(false);
const [sendError, setSendError] = useState<string | undefined>(undefined);
Expand Down
6 changes: 4 additions & 2 deletions assets/router/RouterRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ const PyramidVectorTmsServiceForm = lazy(() => import("../entrepot/pages/service

const ServiceView = lazy(() => import("../entrepot/pages/service/view/ServiceView"));

const AccessesRequest = lazy(() => import("../entrepot/pages/AccessesRequest"));

const EspaceCoCommunityList = lazy(() => import("../espaceco/pages/communities/Communities"));

const RouterRenderer: FC = () => {
Expand Down Expand Up @@ -119,8 +121,8 @@ const RouterRenderer: FC = () => {
return <UserKeyForm />;
case "user_key_edit":
return <UserKeyForm keyId={route.params.keyId} />;
// case "accesses_request":
// return <AccessesRequest fileIdentifier={route.params.fileIdentifier} />;
case "accesses_request":
return <AccessesRequest fileIdentifier={route.params.fileIdentifier} />;
case "datastore_manage_storage":
return <DatastoreManageStorage datastoreId={route.params.datastoreId} />;
case "datastore_manage_permissions":
Expand Down
13 changes: 6 additions & 7 deletions assets/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,12 @@ const routeDefs = {
(p) => `${appRoot}/communaute/${p.communityId}/membres`
),

// NOTE : désactivé car la variable d'environnement `geonetwork_url` a été supprimée, parce qu'en production la route `geonetwork/srv/api/records/$fileIdentifier/formatters/xml` n'est pas disponible
// accesses_request: defineRoute(
// {
// fileIdentifier: param.path.string,
// },
// (p) => `${appRoot}/demande-acces/${p.fileIdentifier}`
// ),
accesses_request: defineRoute(
{
fileIdentifier: param.path.string,
},
(p) => `${appRoot}/demande-acces/${p.fileIdentifier}`
),

datastore_create_request_confirm: defineRoute(`${appRoot}/demande-acces/demande-envoyee`),

Expand Down
1 change: 0 additions & 1 deletion config/parameters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,3 @@ parameters:

api_espaceco_url: "%env(resolve:API_ESPACE_COLLABORATIF_URL)%"
catalogue_url: "%env(resolve:CATALOGUE_URL)%"
# geonetwork_url: "%env(resolve:GEONETWORK_URL)%"
10 changes: 5 additions & 5 deletions src/Controller/ContactController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

#[Route(
name: 'cartesgouvfr_contact_',
options: ['expose' => true],
condition: 'request.isXmlHttpRequest()',
)]
class ContactController extends AbstractController
{
public function __construct(
private UserApiService $userApiService,
private MailerService $mailerService,
private LoggerInterface $mailerLogger
private LoggerInterface $mailerLogger,
) {
}

Expand Down Expand Up @@ -215,9 +217,7 @@ public function joinCommunity(Request $request): JsonResponse
#[Route(
'/accesses_request',
name: 'accesses_request',
options: ['expose' => true],
methods: ['POST'],
condition: 'request.isXmlHttpRequest()'
)]
public function accessesRequest(Request $request): JsonResponse
{
Expand All @@ -237,12 +237,12 @@ public function accessesRequest(Request $request): JsonResponse
'layers' => $data['layers'],
];
if (isset($data['myself'])) {
$mailParams['myself'] = true;
$mailParams['myself'] = true;
}
if (isset($data['beneficiaries'])) {
$mailParams['beneficiaries'] = $data['beneficiaries'];
}

$context = array_merge(['userEmail' => $userEmail], $mailParams);
$this->mailerLogger->info('User ({userEmail}) : Demande d\'accès à des services de diffusion de données dont l\'accès est restreint', $context);

Expand Down
27 changes: 15 additions & 12 deletions src/Controller/Entrepot/GeonetworkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;

/**
* // NOTE : ne pas utiliser car la variable d'environnement `geonetwork_url` a été supprimée, parce qu'en production la route `geonetwork/srv/api/records/$fileIdentifier/formatters/xml` n'est pas disponible.
*/
#[Route(
'/geonetwork/metadata',
'/api/geonetwork/metadata',
name: 'cartesgouvfr_geonetwork_metadata_',
options: ['expose' => true],
condition: 'request.isXmlHttpRequest()'
Expand All @@ -25,19 +22,25 @@ public function __construct(
) {
}

#[Route('/get_infos/{fileIdentifier}', name: 'get_infos', methods: ['GET'])]
public function getInfos(string $fileIdentifier): JsonResponse
#[Route('/{fileIdentifier}', name: 'get', methods: ['GET'])]
public function get(string $fileIdentifier): JsonResponse
{
// https://data.geopf.fr/csw
$xml = $this->geonetworkApiService->getMetadataXml($fileIdentifier);

// supprime la balise csw:GetRecordByIdResponse
$xml = explode("\n", $xml);
$xml = array_filter($xml, fn ($l) => !str_contains($l, 'csw:GetRecordByIdResponse'));
$xml = implode("\n", $xml);

$cswMetadata = $this->cswMetadataHelper->fromXml($xml);

$privateLayers = [];
foreach ($cswMetadata->layers as $layer) {
$privateLayers = array_filter($cswMetadata->layers, function ($layer) {
$parts = parse_url($layer->endpointUrl);
if (preg_match('/private/', $parts['path'])) {
$privateLayers[] = $layer;
}
}

return preg_match('/private/', $parts['path']);
});
$privateLayers = array_values($privateLayers);

return new JsonResponse([
'contact_email' => $cswMetadata->contactEmail,
Expand Down
38 changes: 29 additions & 9 deletions src/Services/GeonetworkApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Exception\CartesApiException;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpClient\HttpOptions;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

Expand All @@ -18,26 +19,45 @@ public function __construct(
ParameterBagInterface $parameterBag,
HttpClientInterface $httpClient,
) {
$this->geonetworkClient = $httpClient->withOptions([
// 'base_uri' => $parameterBag->get('geonetwork_url').'/',
'proxy' => $parameterBag->get('http_proxy'),
'verify_peer' => false,
'verify_host' => false,
]);
$parsedUrl = parse_url($parameterBag->get('api_entrepot_url'));
$baseUrl = sprintf('%s://%s', $parsedUrl['scheme'], $parsedUrl['host']);

$this->geonetworkClient = $httpClient->withOptions(
(new HttpOptions())
->setBaseUri($baseUrl)
->setProxy($parameterBag->get('http_proxy'))
->verifyHost(false)
->verifyPeer(false)
->toArray()
);
}

public function getMetadataXml(string $fileIdentifier): string
{
$url = "geonetwork/srv/api/records/$fileIdentifier/formatters/xml";
$response = $this->geonetworkClient->request('GET', $url);
$query = [
'REQUEST' => 'GetRecordById',
'SERVICE' => 'CSW',
'VERSION' => '2.0.2',
'OUTPUTSCHEMA' => 'http://www.isotc211.org/2005/gmd',
'elementSetName' => 'full',
'ID' => $fileIdentifier,
];

// $url = "geonetwork/srv/api/records/$fileIdentifier/formatters/xml";
$url = '/csw';
if (str_starts_with($fileIdentifier, 'sandbox')) {
$url = '/sandbox/csw';
}

$response = $this->geonetworkClient->request('GET', $url, ['query' => $query]);

return $this->handleResponse($response);
}

protected function handleResponse(ResponseInterface $response): mixed
{
$statusCode = $response->getStatusCode();
if (200 == $statusCode) { // requête réussie
if (200 === $statusCode) { // requête réussie
return $response->getContent();
}

Expand Down

0 comments on commit 2464e4c

Please sign in to comment.