|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 Metaform Systems, Inc. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Apache License, Version 2.0 which is available at |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: Apache-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * Metaform Systems, Inc. - initial API and implementation |
| 12 | + * |
| 13 | + */ |
| 14 | + |
| 15 | +package org.eclipse.edc.connector.controlplane.api.management.catalog.v5; |
| 16 | + |
| 17 | +import jakarta.annotation.security.RolesAllowed; |
| 18 | +import jakarta.json.JsonObject; |
| 19 | +import jakarta.ws.rs.Consumes; |
| 20 | +import jakarta.ws.rs.POST; |
| 21 | +import jakarta.ws.rs.Path; |
| 22 | +import jakarta.ws.rs.PathParam; |
| 23 | +import jakarta.ws.rs.Produces; |
| 24 | +import jakarta.ws.rs.container.AsyncResponse; |
| 25 | +import jakarta.ws.rs.container.Suspended; |
| 26 | +import jakarta.ws.rs.core.Context; |
| 27 | +import jakarta.ws.rs.core.SecurityContext; |
| 28 | +import org.eclipse.edc.api.auth.spi.AuthorizationService; |
| 29 | +import org.eclipse.edc.api.auth.spi.ParticipantPrincipal; |
| 30 | +import org.eclipse.edc.api.auth.spi.RequiredScope; |
| 31 | +import org.eclipse.edc.connector.controlplane.catalog.spi.CatalogRequest; |
| 32 | +import org.eclipse.edc.connector.controlplane.catalog.spi.DatasetRequest; |
| 33 | +import org.eclipse.edc.connector.controlplane.services.spi.catalog.CatalogService; |
| 34 | +import org.eclipse.edc.participantcontext.spi.service.ParticipantContextService; |
| 35 | +import org.eclipse.edc.participantcontext.spi.types.ParticipantContext; |
| 36 | +import org.eclipse.edc.spi.EdcException; |
| 37 | +import org.eclipse.edc.spi.response.StatusResult; |
| 38 | +import org.eclipse.edc.transform.spi.TypeTransformerRegistry; |
| 39 | +import org.eclipse.edc.web.spi.exception.BadGatewayException; |
| 40 | +import org.eclipse.edc.web.spi.exception.InvalidRequestException; |
| 41 | +import org.eclipse.edc.web.spi.validation.SchemaType; |
| 42 | + |
| 43 | +import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON; |
| 44 | +import static org.eclipse.edc.connector.controlplane.catalog.spi.CatalogRequest.CATALOG_REQUEST_TYPE_TERM; |
| 45 | +import static org.eclipse.edc.connector.controlplane.catalog.spi.DatasetRequest.DATASET_REQUEST_TYPE_TERM; |
| 46 | +import static org.eclipse.edc.web.spi.exception.ServiceResultHandler.exceptionMapper; |
| 47 | + |
| 48 | +@Consumes(APPLICATION_JSON) |
| 49 | +@Produces(APPLICATION_JSON) |
| 50 | +@Path("/v5alpha/participants/{participantContextId}/catalog") |
| 51 | +public class CatalogApiV5Controller implements CatalogApiV5 { |
| 52 | + private final AuthorizationService authorizationService; |
| 53 | + private final CatalogService service; |
| 54 | + private final TypeTransformerRegistry transformerRegistry; |
| 55 | + private final ParticipantContextService participantContextService; |
| 56 | + |
| 57 | + public CatalogApiV5Controller(CatalogService service, TypeTransformerRegistry transformerRegistry, |
| 58 | + AuthorizationService authorizationService, ParticipantContextService participantContextService) { |
| 59 | + this.service = service; |
| 60 | + this.transformerRegistry = transformerRegistry; |
| 61 | + this.authorizationService = authorizationService; |
| 62 | + this.participantContextService = participantContextService; |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + @POST |
| 67 | + @Path("/request") |
| 68 | + @RolesAllowed({ParticipantPrincipal.ROLE_ADMIN, ParticipantPrincipal.ROLE_PARTICIPANT}) |
| 69 | + @RequiredScope("management-api:read") |
| 70 | + @Override |
| 71 | + public void requestCatalogV5(@PathParam("participantContextId") String participantContextId, |
| 72 | + @SchemaType(CATALOG_REQUEST_TYPE_TERM) JsonObject requestBody, |
| 73 | + @Suspended AsyncResponse response, |
| 74 | + @Context SecurityContext securityContext) { |
| 75 | + |
| 76 | + var participantContext = preAuthorize(participantContextId, securityContext); |
| 77 | + |
| 78 | + var request = transformerRegistry.transform(requestBody, CatalogRequest.class) |
| 79 | + .orElseThrow(InvalidRequestException::new); |
| 80 | + |
| 81 | + var scopes = request.getAdditionalScopes().toArray(new String[0]); |
| 82 | + service.requestCatalog(participantContext, request.getCounterPartyId(), request.getCounterPartyAddress(), request.getProtocol(), request.getQuerySpec(), scopes) |
| 83 | + .whenComplete((result, throwable) -> { |
| 84 | + try { |
| 85 | + response.resume(toResponse(result, throwable)); |
| 86 | + } catch (Throwable mapped) { |
| 87 | + response.resume(mapped); |
| 88 | + } |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + @POST |
| 93 | + @Path("/dataset/request") |
| 94 | + @RolesAllowed({ParticipantPrincipal.ROLE_ADMIN, ParticipantPrincipal.ROLE_PARTICIPANT}) |
| 95 | + @RequiredScope("management-api:read") |
| 96 | + @Override |
| 97 | + public void getDatasetV5(@PathParam("participantContextId") String participantContextId, |
| 98 | + @SchemaType(DATASET_REQUEST_TYPE_TERM) JsonObject requestBody, |
| 99 | + @Suspended AsyncResponse response, |
| 100 | + @Context SecurityContext securityContext) { |
| 101 | + |
| 102 | + var participantContext = preAuthorize(participantContextId, securityContext); |
| 103 | + |
| 104 | + var request = transformerRegistry.transform(requestBody, DatasetRequest.class) |
| 105 | + .orElseThrow(InvalidRequestException::new); |
| 106 | + |
| 107 | + service.requestDataset(participantContext, request.getId(), request.getCounterPartyId(), request.getCounterPartyAddress(), request.getProtocol()) |
| 108 | + .whenComplete((result, throwable) -> { |
| 109 | + try { |
| 110 | + response.resume(toResponse(result, throwable)); |
| 111 | + } catch (Throwable mapped) { |
| 112 | + response.resume(mapped); |
| 113 | + } |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * utility method that performs authorization on the participant context with the given ID and return the {@link ParticipantContext} |
| 119 | + * throws an exception if the authorization fails. the exception does not need to be caught. |
| 120 | + */ |
| 121 | + |
| 122 | + private ParticipantContext preAuthorize(String participantContextId, SecurityContext securityContext) { |
| 123 | + authorizationService.authorize(securityContext, participantContextId, participantContextId, ParticipantContext.class) |
| 124 | + .orElseThrow(exceptionMapper(ParticipantContext.class, participantContextId)); |
| 125 | + |
| 126 | + return participantContextService.getParticipantContext(participantContextId) |
| 127 | + .orElseThrow(exceptionMapper(ParticipantContext.class, participantContextId)); |
| 128 | + } |
| 129 | + |
| 130 | + private byte[] toResponse(StatusResult<byte[]> result, Throwable throwable) throws Throwable { |
| 131 | + if (throwable == null) { |
| 132 | + if (result.succeeded()) { |
| 133 | + return result.getContent(); |
| 134 | + } else { |
| 135 | + throw new BadGatewayException(result.getFailureDetail()); |
| 136 | + } |
| 137 | + } else { |
| 138 | + if (throwable instanceof EdcException || throwable.getCause() instanceof EdcException) { |
| 139 | + throw new BadGatewayException(throwable.getMessage()); |
| 140 | + } else { |
| 141 | + throw throwable; |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | +} |
0 commit comments