From c53e3e0d302d952a2e862a9de69133036d3b411d Mon Sep 17 00:00:00 2001 From: edunn Date: Tue, 9 Jul 2024 14:47:53 -0700 Subject: [PATCH 1/2] generalise MissSortWithSearchAfterException -> SortSearchAfterMismatchException --- .../pds/api/registry/controllers/ProductsController.java | 9 +++------ .../RegistryApiResponseEntityExceptionHandler.java | 8 ++++---- .../exceptions/MissSortWithSearchAfterException.java | 8 -------- .../exceptions/SortSearchAfterMismatchException.java | 8 ++++++++ .../registry/search/RegistrySearchRequestBuilder.java | 8 ++++---- 5 files changed, 19 insertions(+), 22 deletions(-) delete mode 100644 service/src/main/java/gov/nasa/pds/api/registry/model/exceptions/MissSortWithSearchAfterException.java create mode 100644 service/src/main/java/gov/nasa/pds/api/registry/model/exceptions/SortSearchAfterMismatchException.java diff --git a/service/src/main/java/gov/nasa/pds/api/registry/controllers/ProductsController.java b/service/src/main/java/gov/nasa/pds/api/registry/controllers/ProductsController.java index 7910a961..63eb4d18 100644 --- a/service/src/main/java/gov/nasa/pds/api/registry/controllers/ProductsController.java +++ b/service/src/main/java/gov/nasa/pds/api/registry/controllers/ProductsController.java @@ -6,7 +6,6 @@ import java.util.Map; import java.util.HashMap; import jakarta.servlet.http.HttpServletRequest; -import jakarta.validation.Valid; import com.fasterxml.jackson.databind.ObjectMapper; import org.opensearch.client.opensearch.OpenSearchClient; import org.opensearch.client.opensearch._types.OpenSearchException; @@ -18,15 +17,13 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import gov.nasa.pds.api.base.ProductsApi; import gov.nasa.pds.api.registry.ConnectionContext; import gov.nasa.pds.api.registry.model.ErrorMessageFactory; import gov.nasa.pds.api.registry.model.exceptions.AcceptFormatNotSupportedException; -import gov.nasa.pds.api.registry.model.exceptions.MissSortWithSearchAfterException; +import gov.nasa.pds.api.registry.model.exceptions.SortSearchAfterMismatchException; import gov.nasa.pds.api.registry.model.exceptions.NotFoundException; import gov.nasa.pds.api.registry.model.exceptions.UnhandledException; import gov.nasa.pds.api.registry.model.api_responses.PdsProductBusinessObject; @@ -216,7 +213,7 @@ public ResponseEntity selectByLidvidLatest(String identifier, List selectByLidvidAll(String identifier, List fields, Integer limit, List sort, List searchAfter) throws UnhandledException, - NotFoundException, AcceptFormatNotSupportedException, MissSortWithSearchAfterException { + NotFoundException, AcceptFormatNotSupportedException, SortSearchAfterMismatchException { RawMultipleProductResponse response; @@ -321,7 +318,7 @@ private HashMap getLatestLidVid(PdsProductIdentifier identifier, private RawMultipleProductResponse getAllLidVid(PdsProductIdentifier identifier, List fields, Integer limit, List sort, List searchAfter) - throws OpenSearchException, IOException, NotFoundException, MissSortWithSearchAfterException { + throws OpenSearchException, IOException, NotFoundException, SortSearchAfterMismatchException { RegistrySearchRequestBuilder registrySearchRequestBuilder = new RegistrySearchRequestBuilder(this.registrySearchRequestBuilder); diff --git a/service/src/main/java/gov/nasa/pds/api/registry/controllers/RegistryApiResponseEntityExceptionHandler.java b/service/src/main/java/gov/nasa/pds/api/registry/controllers/RegistryApiResponseEntityExceptionHandler.java index 4e2bf989..4caf77f4 100644 --- a/service/src/main/java/gov/nasa/pds/api/registry/controllers/RegistryApiResponseEntityExceptionHandler.java +++ b/service/src/main/java/gov/nasa/pds/api/registry/controllers/RegistryApiResponseEntityExceptionHandler.java @@ -10,7 +10,7 @@ import org.springframework.web.context.request.WebRequest; import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; import gov.nasa.pds.api.registry.model.exceptions.AcceptFormatNotSupportedException; -import gov.nasa.pds.api.registry.model.exceptions.MissSortWithSearchAfterException; +import gov.nasa.pds.api.registry.model.exceptions.SortSearchAfterMismatchException; import gov.nasa.pds.api.registry.model.exceptions.NotFoundException; import gov.nasa.pds.api.registry.model.exceptions.RegistryApiException; import gov.nasa.pds.api.registry.model.exceptions.UnhandledException; @@ -66,9 +66,9 @@ protected ResponseEntity notAcceptable(AcceptFormatNotSupportedException } - @ExceptionHandler(value = {MissSortWithSearchAfterException.class}) - protected ResponseEntity missSort(MissSortWithSearchAfterException ex, - WebRequest request) { + @ExceptionHandler(value = {SortSearchAfterMismatchException.class}) + protected ResponseEntity missSort(SortSearchAfterMismatchException ex, + WebRequest request) { return genericExceptionHandler(ex, request, "", HttpStatus.BAD_REQUEST); } diff --git a/service/src/main/java/gov/nasa/pds/api/registry/model/exceptions/MissSortWithSearchAfterException.java b/service/src/main/java/gov/nasa/pds/api/registry/model/exceptions/MissSortWithSearchAfterException.java deleted file mode 100644 index 3dfef3ac..00000000 --- a/service/src/main/java/gov/nasa/pds/api/registry/model/exceptions/MissSortWithSearchAfterException.java +++ /dev/null @@ -1,8 +0,0 @@ -package gov.nasa.pds.api.registry.model.exceptions; - -public class MissSortWithSearchAfterException extends RegistryApiException { - - public MissSortWithSearchAfterException() { - super("sort parameter missing, sort is mandatory with search-after"); - } -} diff --git a/service/src/main/java/gov/nasa/pds/api/registry/model/exceptions/SortSearchAfterMismatchException.java b/service/src/main/java/gov/nasa/pds/api/registry/model/exceptions/SortSearchAfterMismatchException.java new file mode 100644 index 00000000..3c5d6367 --- /dev/null +++ b/service/src/main/java/gov/nasa/pds/api/registry/model/exceptions/SortSearchAfterMismatchException.java @@ -0,0 +1,8 @@ +package gov.nasa.pds.api.registry.model.exceptions; + +public class SortSearchAfterMismatchException extends RegistryApiException { + + public SortSearchAfterMismatchException(String detail) { + super("Invalid combination of sort and searchAfter values: " + detail); + } +} diff --git a/service/src/main/java/gov/nasa/pds/api/registry/search/RegistrySearchRequestBuilder.java b/service/src/main/java/gov/nasa/pds/api/registry/search/RegistrySearchRequestBuilder.java index f69c2f8c..7c5a25dd 100644 --- a/service/src/main/java/gov/nasa/pds/api/registry/search/RegistrySearchRequestBuilder.java +++ b/service/src/main/java/gov/nasa/pds/api/registry/search/RegistrySearchRequestBuilder.java @@ -3,7 +3,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.stream.Collectors; + import org.antlr.v4.runtime.BailErrorStrategy; import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.CodePointCharStream; @@ -34,7 +34,7 @@ import gov.nasa.pds.api.registry.model.Antlr4SearchListener; import gov.nasa.pds.api.registry.model.EntityProduct; import gov.nasa.pds.api.registry.model.SearchUtil; -import gov.nasa.pds.api.registry.model.exceptions.MissSortWithSearchAfterException; +import gov.nasa.pds.api.registry.model.exceptions.SortSearchAfterMismatchException; import gov.nasa.pds.api.registry.model.exceptions.UnparsableQParamException; import gov.nasa.pds.api.registry.model.identifiers.PdsProductIdentifier; @@ -154,7 +154,7 @@ public RegistrySearchRequestBuilder addLidMatch(PdsProductIdentifier identifier) } public RegistrySearchRequestBuilder paginates(Integer limit, List sort, - List searchAfter) throws MissSortWithSearchAfterException { + List searchAfter) throws SortSearchAfterMismatchException { if ((sort != null) && (!sort.isEmpty())) { this.sort(sort); @@ -164,7 +164,7 @@ public RegistrySearchRequestBuilder paginates(Integer limit, List sort, if ((searchAfter != null) && (!searchAfter.isEmpty())) { if ((sort == null) || (sort.isEmpty())) { - throw new MissSortWithSearchAfterException(); + throw new SortSearchAfterMismatchException("sort argument must be provided if searchAfter argument is provided"); } this.searchAfter(searchAfter); } From df5bc52180946c61df5a088c8b93e6140c7f6770 Mon Sep 17 00:00:00 2001 From: edunn Date: Tue, 9 Jul 2024 14:50:44 -0700 Subject: [PATCH 2/2] expand sort/searchAfter error checks to catch any invalid case --- .../pds/api/registry/search/RegistrySearchRequestBuilder.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/service/src/main/java/gov/nasa/pds/api/registry/search/RegistrySearchRequestBuilder.java b/service/src/main/java/gov/nasa/pds/api/registry/search/RegistrySearchRequestBuilder.java index 7c5a25dd..a4fe5340 100644 --- a/service/src/main/java/gov/nasa/pds/api/registry/search/RegistrySearchRequestBuilder.java +++ b/service/src/main/java/gov/nasa/pds/api/registry/search/RegistrySearchRequestBuilder.java @@ -163,8 +163,10 @@ public RegistrySearchRequestBuilder paginates(Integer limit, List sort, this.size(limit); if ((searchAfter != null) && (!searchAfter.isEmpty())) { - if ((sort == null) || (sort.isEmpty())) { + if (sort == null) { throw new SortSearchAfterMismatchException("sort argument must be provided if searchAfter argument is provided"); + } else if (searchAfter.size() != sort.size()) { + throw new SortSearchAfterMismatchException("sort and searchAfter arguments must be of equal length if provided"); } this.searchAfter(searchAfter); }