From 17e68b9a7f13e265fab51be8bec599dee4e626cf Mon Sep 17 00:00:00 2001 From: dmeidlin <14339308+dmeidlin@users.noreply.github.com> Date: Fri, 1 Nov 2024 10:33:47 -0400 Subject: [PATCH 1/2] add delete catch-all endpoint to brapi proxy --- .../brapi/v2/BrAPIGermplasmController.java | 2 +- .../brapi/v2/BrAPIV2Controller.java | 49 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/breedinginsight/brapi/v2/BrAPIGermplasmController.java b/src/main/java/org/breedinginsight/brapi/v2/BrAPIGermplasmController.java index 948e68c20..a0fdad1ac 100644 --- a/src/main/java/org/breedinginsight/brapi/v2/BrAPIGermplasmController.java +++ b/src/main/java/org/breedinginsight/brapi/v2/BrAPIGermplasmController.java @@ -178,7 +178,7 @@ private void batchProcessGermplasm(List germplasmList, String pr Pattern programKeyPattern = Utilities.getRegexPatternMatchAllProgramKeysAnyAccession(programKey); germplasmList.parallelStream().forEach(germplasm -> { // Set dbId - germplasm.germplasmDbId(Utilities.getExternalReference(germplasm.getExternalReferences(), "breedinginsight.org") + germplasm.germplasmDbId(Utilities.getExternalReference(germplasm.getExternalReferences(), "breeding-insight.net") .orElseThrow(() -> new IllegalStateException("No BI external reference found")) .getReferenceId()); // Process synonyms diff --git a/src/main/java/org/breedinginsight/brapi/v2/BrAPIV2Controller.java b/src/main/java/org/breedinginsight/brapi/v2/BrAPIV2Controller.java index be5046077..7f28d215b 100644 --- a/src/main/java/org/breedinginsight/brapi/v2/BrAPIV2Controller.java +++ b/src/main/java/org/breedinginsight/brapi/v2/BrAPIV2Controller.java @@ -17,6 +17,7 @@ package org.breedinginsight.brapi.v2; +import io.micronaut.context.annotation.Value; import io.micronaut.http.HttpRequest; import io.micronaut.http.HttpResponse; import io.micronaut.http.HttpStatus; @@ -35,6 +36,8 @@ import org.breedinginsight.model.ProgramBrAPIEndpoints; import org.breedinginsight.services.ProgramService; import org.breedinginsight.services.exceptions.DoesNotExistException; +import io.micronaut.http.annotation.Body; + import javax.inject.Inject; import java.io.IOException; @@ -51,6 +54,8 @@ public class BrAPIV2Controller { private final SecurityService securityService; private final ProgramService programService; + @Value("${micronaut.bi.api.version}") + private String apiVersion; @Inject public BrAPIV2Controller(SecurityService securityService, ProgramService programService) { @@ -186,6 +191,17 @@ public HttpResponse getCatchall(@PathVariable("path") String path, @PathVaria return executeRequest(path, programId, request, "GET"); } + @Delete("/${micronaut.bi.api.version}/programs/{programId}" + BrapiVersion.BRAPI_V2 + "/{+path}") + @Produces(MediaType.APPLICATION_JSON) + @ProgramSecured(roleGroups = {ProgramSecuredRoleGroup.PROGRAM_SCOPED_ROLES}) + public HttpResponse deleteCatchall(@PathVariable("path") String path, + @PathVariable("programId") UUID programId, + HttpRequest request) { + + //HttpRequest request = HttpRequest.DELETE("/" + apiVersion + "/programs/" + programId + BrapiVersion.BRAPI_V2 + "/" + path); + return executeDeleteRequest(path, programId, request); + } + @Post("/${micronaut.bi.api.version}/programs/{programId}" + BrapiVersion.BRAPI_V2 + "/{+path}") @Consumes(MediaType.ALL) @Produces(MediaType.APPLICATION_JSON) @@ -241,6 +257,22 @@ private HttpResponse executeRequest(String path, UUID programId, HttpReq throw new HttpStatusException(HttpStatus.UNAUTHORIZED, "Unauthorized BrAPI Request"); } + private HttpResponse executeDeleteRequest(String path, UUID programId, HttpRequest request) { + AuthenticatedUser actingUser = securityService.getUser(); + + if (programId != null) { + HttpUrl requestUrl = getUrl(programId, path, request); + + var brapiRequest = new Request.Builder().url(requestUrl) + .method("DELETE", null) + .build(); + + return makeCall(brapiRequest); + } + + throw new HttpStatusException(HttpStatus.UNAUTHORIZED, "Unauthorized BrAPI Request"); + } + private HttpResponse makeCall(Request brapiRequest) { // TODO: use config parameter for timeout OkHttpClient client = new OkHttpClient.Builder() @@ -263,6 +295,23 @@ private HttpResponse makeCall(Request brapiRequest) { } } +// private HttpUrl getDeleteUrl(UUID programId, String path) { +// var programBrAPIBaseUrl = getProgramBrAPIBaseUrl(programId); +// +// var requestUrl = HttpUrl.parse(programBrAPIBaseUrl + "/" + path).newBuilder(); +// +// request.getParameters() +// .asMap() +// .entrySet() +// .stream() +// .filter(param -> !param.getKey() +// .equals("programId")) +// .forEach(param -> param.getValue() +// .forEach(val -> requestUrl.addQueryParameter(param.getKey(), val))); +// +// return requestUrl.build(); +// } + private HttpUrl getUrl(UUID programId, String path, HttpRequest request) { var programBrAPIBaseUrl = getProgramBrAPIBaseUrl(programId); From d233d59fda1b91b96fcb30be8f97be5b5baff526 Mon Sep 17 00:00:00 2001 From: dmeidlin <14339308+dmeidlin@users.noreply.github.com> Date: Wed, 6 Nov 2024 10:43:22 -0500 Subject: [PATCH 2/2] optimize imports and clean up code --- .../brapi/v2/BrAPIV2Controller.java | 23 ------------------- 1 file changed, 23 deletions(-) diff --git a/src/main/java/org/breedinginsight/brapi/v2/BrAPIV2Controller.java b/src/main/java/org/breedinginsight/brapi/v2/BrAPIV2Controller.java index 7f28d215b..42af9b8d7 100644 --- a/src/main/java/org/breedinginsight/brapi/v2/BrAPIV2Controller.java +++ b/src/main/java/org/breedinginsight/brapi/v2/BrAPIV2Controller.java @@ -36,8 +36,6 @@ import org.breedinginsight.model.ProgramBrAPIEndpoints; import org.breedinginsight.services.ProgramService; import org.breedinginsight.services.exceptions.DoesNotExistException; -import io.micronaut.http.annotation.Body; - import javax.inject.Inject; import java.io.IOException; @@ -54,8 +52,6 @@ public class BrAPIV2Controller { private final SecurityService securityService; private final ProgramService programService; - @Value("${micronaut.bi.api.version}") - private String apiVersion; @Inject public BrAPIV2Controller(SecurityService securityService, ProgramService programService) { @@ -197,8 +193,6 @@ public HttpResponse getCatchall(@PathVariable("path") String path, @PathVaria public HttpResponse deleteCatchall(@PathVariable("path") String path, @PathVariable("programId") UUID programId, HttpRequest request) { - - //HttpRequest request = HttpRequest.DELETE("/" + apiVersion + "/programs/" + programId + BrapiVersion.BRAPI_V2 + "/" + path); return executeDeleteRequest(path, programId, request); } @@ -295,23 +289,6 @@ private HttpResponse makeCall(Request brapiRequest) { } } -// private HttpUrl getDeleteUrl(UUID programId, String path) { -// var programBrAPIBaseUrl = getProgramBrAPIBaseUrl(programId); -// -// var requestUrl = HttpUrl.parse(programBrAPIBaseUrl + "/" + path).newBuilder(); -// -// request.getParameters() -// .asMap() -// .entrySet() -// .stream() -// .filter(param -> !param.getKey() -// .equals("programId")) -// .forEach(param -> param.getValue() -// .forEach(val -> requestUrl.addQueryParameter(param.getKey(), val))); -// -// return requestUrl.build(); -// } - private HttpUrl getUrl(UUID programId, String path, HttpRequest request) { var programBrAPIBaseUrl = getProgramBrAPIBaseUrl(programId);