From 7191f694293bc92d2dc3eec40cefa4992737ff99 Mon Sep 17 00:00:00 2001 From: Gang Li Date: Thu, 11 Jan 2024 18:17:25 +0800 Subject: [PATCH 1/2] Add new API to get all store keys --- .../controller/StatsController.java | 23 +++++++++++++ .../jaxrs/version/StatsHandler.java | 32 +++++++++++++++++++ .../repository/jaxrs/StatsHandlerTest.java | 10 ++++++ 3 files changed, 65 insertions(+) diff --git a/src/main/java/org/commonjava/indy/service/repository/controller/StatsController.java b/src/main/java/org/commonjava/indy/service/repository/controller/StatsController.java index 2e8ff44..6900188 100644 --- a/src/main/java/org/commonjava/indy/service/repository/controller/StatsController.java +++ b/src/main/java/org/commonjava/indy/service/repository/controller/StatsController.java @@ -28,7 +28,10 @@ import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; import static jakarta.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; @@ -85,4 +88,24 @@ public EndpointViewListing getEndpointsListing( final String baseUri, final JaxR return new EndpointViewListing( points ); } + public Map> getAllStoreKeys() + throws IndyWorkflowException + { + final List stores; + try + { + final Map> result = new HashMap<>(); + stores = new ArrayList<>( dataManager.getAllArtifactStores() ); + List items = stores.stream().map( s-> s.getKey().toString() ).collect( Collectors.toList() ); + result.put("items", items); + return result; + } + catch ( final IndyDataException e ) + { + throw new IndyWorkflowException( INTERNAL_SERVER_ERROR.getStatusCode(), + "Failed to retrieve all store keys: {}", e, e.getMessage() ); + } + + } + } diff --git a/src/main/java/org/commonjava/indy/service/repository/jaxrs/version/StatsHandler.java b/src/main/java/org/commonjava/indy/service/repository/jaxrs/version/StatsHandler.java index 9c3da70..8065f56 100644 --- a/src/main/java/org/commonjava/indy/service/repository/jaxrs/version/StatsHandler.java +++ b/src/main/java/org/commonjava/indy/service/repository/jaxrs/version/StatsHandler.java @@ -37,7 +37,10 @@ import jakarta.ws.rs.core.Response; import jakarta.ws.rs.core.UriInfo; +import java.util.ArrayList; import java.util.Date; +import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeSet; @@ -132,4 +135,33 @@ public Response getAllEndpoints( @Context final UriInfo uriInfo ) return response; } + @Operation( + summary = "Retrieve a listing of the artifact stores keys available on the system." ) + @APIResponse( responseCode = "200", + content = @Content( schema = @Schema( implementation = EndpointViewListing.class ) ), + description = "The artifact store keys listing" ) + @Path( "/all-storekeys" ) + @GET + @Produces( APPLICATION_JSON ) + public Response getAllStoreKeys( @Context final UriInfo uriInfo ) + { + Response response; + try + { + + Map> result = statsController.getAllStoreKeys(); + + response = responseHelper.formatOkResponseWithJsonEntity( result ); + + logger.debug( "\n\n\n\n\n\n{} Sent all-keys:\n\n{}\n\n\n\n\n\n\n", new Date(), result ); + } + catch ( final IndyWorkflowException e ) + { + logger.error( String.format( "Failed to retrieve store keys listing: %s", responseHelper.formatEntity( e ) ), + e ); + response = responseHelper.formatResponse( e ); + } + return response; + } + } diff --git a/src/test/java/org/commonjava/indy/service/repository/jaxrs/StatsHandlerTest.java b/src/test/java/org/commonjava/indy/service/repository/jaxrs/StatsHandlerTest.java index 3ee5ddd..12f6ff1 100644 --- a/src/test/java/org/commonjava/indy/service/repository/jaxrs/StatsHandlerTest.java +++ b/src/test/java/org/commonjava/indy/service/repository/jaxrs/StatsHandlerTest.java @@ -63,4 +63,14 @@ public void testGetAllEndpoints() .body( "size()", is( 1 ) ) .body( "items.size()", greaterThan( 1 ) ); } + + @Test + public void testGetAllStoreKeys() + { + given().get( "/api/stats/all-storekeys" ) + .then() + .statusCode( OK.getStatusCode() ) + .body( "size()", is( 1 ) ) + .body( "items.size()", greaterThan( 1 ) ); + } } From 8453d76a86c03d27a9a0bea67ca968481f7a1e8e Mon Sep 17 00:00:00 2001 From: Gang Li Date: Thu, 11 Jan 2024 18:30:17 +0800 Subject: [PATCH 2/2] Fix a API doc Schema issue --- .../indy/service/repository/jaxrs/version/StatsHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/commonjava/indy/service/repository/jaxrs/version/StatsHandler.java b/src/main/java/org/commonjava/indy/service/repository/jaxrs/version/StatsHandler.java index 8065f56..f19388b 100644 --- a/src/main/java/org/commonjava/indy/service/repository/jaxrs/version/StatsHandler.java +++ b/src/main/java/org/commonjava/indy/service/repository/jaxrs/version/StatsHandler.java @@ -138,7 +138,7 @@ public Response getAllEndpoints( @Context final UriInfo uriInfo ) @Operation( summary = "Retrieve a listing of the artifact stores keys available on the system." ) @APIResponse( responseCode = "200", - content = @Content( schema = @Schema( implementation = EndpointViewListing.class ) ), + content = @Content( schema = @Schema( implementation = Map.class ) ), description = "The artifact store keys listing" ) @Path( "/all-storekeys" ) @GET