Skip to content

Commit

Permalink
refactor(dsp): use supplier notation for logging (eclipse-edc#2828)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronjaquensel authored May 5, 2023
1 parent 186378e commit 2982893
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public String name() {
public void initialize(ServiceExtensionContext context) {
var mapper = typeManager.getMapper(JSON_LD);
var dspCallbackAddress = apiConfiguration.getDspCallbackAddress();
var catalogController = new CatalogController(mapper, identityService, transformerRegistry, dspCallbackAddress, service, jsonLdService);
var catalogController = new CatalogController(context.getMonitor(), mapper, identityService, transformerRegistry, dspCallbackAddress, service, jsonLdService);
webService.registerResource(apiConfiguration.getContextAlias(), catalogController);

dataServiceRegistry.register(DataService.Builder.newInstance()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.eclipse.edc.spi.EdcException;
import org.eclipse.edc.spi.iam.IdentityService;
import org.eclipse.edc.spi.iam.TokenRepresentation;
import org.eclipse.edc.spi.monitor.Monitor;
import org.eclipse.edc.transform.spi.TypeTransformerRegistry;
import org.eclipse.edc.web.spi.exception.AuthenticationFailedException;
import org.eclipse.edc.web.spi.exception.InvalidRequestException;
Expand All @@ -50,16 +51,19 @@
@Produces({ MediaType.APPLICATION_JSON })
@Path(BASE_PATH)
public class CatalogController {

private final Monitor monitor;
private final ObjectMapper mapper;
private final IdentityService identityService;
private final TypeTransformerRegistry transformerRegistry;
private final String dspCallbackAddress;
private final CatalogProtocolService service;
private final JsonLd jsonLdService;

public CatalogController(ObjectMapper mapper, IdentityService identityService,
public CatalogController(Monitor monitor, ObjectMapper mapper, IdentityService identityService,
TypeTransformerRegistry transformerRegistry, String dspCallbackAddress,
CatalogProtocolService service, JsonLd jsonLdService) {
this.monitor = monitor;
this.mapper = mapper;
this.identityService = identityService;
this.transformerRegistry = transformerRegistry;
Expand All @@ -71,6 +75,8 @@ public CatalogController(ObjectMapper mapper, IdentityService identityService,
@POST
@Path(CATALOG_REQUEST)
public Map<String, Object> getCatalog(JsonObject jsonObject, @HeaderParam(AUTHORIZATION) String token) {
monitor.debug(() -> "DSP: Incoming catalog request.");

var tokenRepresentation = TokenRepresentation.Builder.newInstance()
.token(token)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@

class CatalogControllerTest {

private final Monitor monitor = mock(Monitor.class);
private final ObjectMapper mapper = mock(ObjectMapper.class);
private final IdentityService identityService = mock(IdentityService.class);
private final TypeTransformerRegistry transformerRegistry = mock(TypeTransformerRegistry.class);
Expand All @@ -80,7 +81,7 @@ void setUp() {
jsonLdService.registerNamespace(DCT_PREFIX, DCT_SCHEMA);
jsonLdService.registerNamespace(ODRL_PREFIX, ODRL_SCHEMA);
jsonLdService.registerNamespace(DSPACE_PREFIX, DSPACE_SCHEMA);
controller = new CatalogController(mapper, identityService, transformerRegistry, callbackAddress, service, jsonLdService);
controller = new CatalogController(monitor, mapper, identityService, transformerRegistry, callbackAddress, service, jsonLdService);

request = Json.createObjectBuilder()
.add(TYPE, DSPACE_CATALOG_REQUEST_TYPE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public DspNegotiationController(Monitor monitor, ObjectMapper mapper, String cal
@GET
@Path("{id}")
public Map<String, Object> getNegotiation(@PathParam("id") String id, @HeaderParam(AUTHORIZATION) String token) {
monitor.debug(format("DSP: Incoming request for contract negotiation with id %s", id));
monitor.debug(() -> format("DSP: Incoming request for contract negotiation with id %s", id));

checkAndReturnAuthToken(token);

Expand All @@ -121,7 +121,7 @@ public Map<String, Object> getNegotiation(@PathParam("id") String id, @HeaderPar
@POST
@Path(INITIAL_CONTRACT_REQUEST)
public Map<String, Object> initiateNegotiation(@RequestBody(description = DSPACE_NEGOTIATION_CONTRACT_REQUEST_MESSAGE, required = true) JsonObject body, @HeaderParam(AUTHORIZATION) String token) {
monitor.debug("DSP: Incoming ContractRequestMessage for initiating a contract negotiation.");
monitor.debug(() -> "DSP: Incoming ContractRequestMessage for initiating a contract negotiation.");

var negotiation = processMessage(token, Optional.empty(), body, DSPACE_NEGOTIATION_CONTRACT_REQUEST_MESSAGE, ContractRequestMessage.class, protocolService::notifyRequested);

Expand All @@ -142,7 +142,7 @@ public Map<String, Object> initiateNegotiation(@RequestBody(description = DSPACE
@POST
@Path("{id}" + CONTRACT_REQUEST)
public void consumerOffer(@PathParam("id") String id, @RequestBody(description = DSPACE_NEGOTIATION_CONTRACT_REQUEST_MESSAGE, required = true) JsonObject body, @HeaderParam(AUTHORIZATION) String token) {
monitor.debug(format("DSP: Incoming ContractRequestMessage for process %s", id));
monitor.debug(() -> format("DSP: Incoming ContractRequestMessage for process %s", id));

processMessage(token, Optional.of(id), body, DSPACE_NEGOTIATION_CONTRACT_REQUEST_MESSAGE, ContractRequestMessage.class, protocolService::notifyRequested);
}
Expand All @@ -157,7 +157,7 @@ public void consumerOffer(@PathParam("id") String id, @RequestBody(description =
@POST
@Path("{id}" + EVENT)
public void createEvent(@PathParam("id") String id, @RequestBody(description = DSPACE_NEGOTIATION_EVENT_MESSAGE, required = true) JsonObject body, @HeaderParam(AUTHORIZATION) String token) {
monitor.debug(format("DSP: Incoming ContractNegotiationEventMessage for process %s", id));
monitor.debug(() -> format("DSP: Incoming ContractNegotiationEventMessage for process %s", id));

var claimToken = checkAndReturnAuthToken(token);
var expandedResult = jsonLdService.expand(body);
Expand Down Expand Up @@ -190,7 +190,7 @@ public void createEvent(@PathParam("id") String id, @RequestBody(description = D
@POST
@Path("{id}" + AGREEMENT + VERIFICATION)
public void verifyAgreement(@PathParam("id") String id, @RequestBody(description = DSPACE_NEGOTIATION_AGREEMENT_VERIFICATION_MESSAGE, required = true) JsonObject body, @HeaderParam(AUTHORIZATION) String token) {
monitor.debug(format("DSP: Incoming ContractAgreementVerificationMessage for process %s", id));
monitor.debug(() -> format("DSP: Incoming ContractAgreementVerificationMessage for process %s", id));

processMessage(token, Optional.of(id), body, DSPACE_NEGOTIATION_AGREEMENT_VERIFICATION_MESSAGE, ContractAgreementVerificationMessage.class, protocolService::notifyVerified);
}
Expand All @@ -205,7 +205,7 @@ public void verifyAgreement(@PathParam("id") String id, @RequestBody(description
@POST
@Path("{id}" + TERMINATION)
public void terminateNegotiation(@PathParam("id") String id, @RequestBody(description = DSPACE_NEGOTIATION_TERMINATION_MESSAGE, required = true) JsonObject body, @HeaderParam(AUTHORIZATION) String token) {
monitor.debug(format("DSP: Incoming ContractNegotiationTerminationMessage for process %s", id));
monitor.debug(() -> format("DSP: Incoming ContractNegotiationTerminationMessage for process %s", id));

processMessage(token, Optional.of(id), body, DSPACE_NEGOTIATION_TERMINATION_MESSAGE, ContractNegotiationTerminationMessage.class, protocolService::notifyTerminated);
}
Expand All @@ -221,7 +221,7 @@ public void terminateNegotiation(@PathParam("id") String id, @RequestBody(descri
@Path("{id}" + CONTRACT_OFFER)

public void providerOffer(@PathParam("id") String id, @RequestBody(description = DSPACE_NEGOTIATION_CONTRACT_OFFER_MESSAGE, required = true) JsonObject body, @HeaderParam(AUTHORIZATION) String token) {
monitor.debug(format("DSP: Incoming ContractOfferMessage for process %s", id));
monitor.debug(() -> format("DSP: Incoming ContractOfferMessage for process %s", id));

checkAndReturnAuthToken(token);

Expand All @@ -238,7 +238,7 @@ public void providerOffer(@PathParam("id") String id, @RequestBody(description =
@POST
@Path("{id}" + AGREEMENT)
public void createAgreement(@PathParam("id") String id, @RequestBody(description = DSPACE_NEGOTIATION_AGREEMENT_MESSAGE, required = true) JsonObject body, @HeaderParam(AUTHORIZATION) String token) {
monitor.debug(format("DSP: Incoming ContractAgreementMessage for process %s", id));
monitor.debug(() -> format("DSP: Incoming ContractAgreementMessage for process %s", id));

processMessage(token, Optional.of(id), body, DSPACE_NEGOTIATION_AGREEMENT_MESSAGE, ContractAgreementMessage.class, protocolService::notifyAgreed);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public DspTransferProcessApiController(Monitor monitor, ObjectMapper mapper, Typ
@GET
@Path("/{id}")
public JsonObject getTransferProcess(@PathParam("id") String id) {
monitor.debug(format("DSP: Incoming request for transfer process with id %s", id));
monitor.debug(() -> format("DSP: Incoming request for transfer process with id %s", id));

throw new UnsupportedOperationException("Getting a transfer process not yet supported.");
}
Expand Down Expand Up @@ -172,7 +172,7 @@ public void transferProcessTermination(@PathParam("id") String id, JsonObject js
@POST
@Path("{id}" + TRANSFER_SUSPENSION)
public void transferProcessSuspension(@PathParam("id") String id) {
monitor.debug(format("DSP: Incoming TransferSuspensionMessage for transfer process %s", id));
monitor.debug(() -> format("DSP: Incoming TransferSuspensionMessage for transfer process %s", id));

throw new UnsupportedOperationException("Suspension not yet supported.");
}
Expand All @@ -193,9 +193,9 @@ public void transferProcessSuspension(@PathParam("id") String id) {
* @return the transfer process returned by the service call
*/
private <M extends TransferRemoteMessage> TransferProcess handleMessage(JsonObject request, Optional<String> processId, String token, String expectedType,
Class<M> messageClass, BiFunction<M, ClaimToken, ServiceResult<TransferProcess>> serviceCall) {
processId.ifPresentOrElse(id -> monitor.debug(format("DSP: Incoming %s for transfer process %s", messageClass.getSimpleName(), id)),
() -> monitor.debug(format("DSP: Incoming %s for initiating a transfer process", messageClass.getSimpleName())));
Class<M> messageClass, BiFunction<M, ClaimToken, ServiceResult<TransferProcess>> serviceCall) {
processId.ifPresentOrElse(id -> monitor.debug(() -> format("DSP: Incoming %s for transfer process %s", messageClass.getSimpleName(), id)),
() -> monitor.debug(() -> format("DSP: Incoming %s for initiating a transfer process", messageClass.getSimpleName())));

var claimToken = checkAuthToken(token);

Expand Down

0 comments on commit 2982893

Please sign in to comment.