Skip to content

Commit

Permalink
Add new query APIs
Browse files Browse the repository at this point in the history
  * Move endpoints from stats to query resource
  * Add package type parameter to endpoints API
  * Add storekeys API with package type parameter
  • Loading branch information
ligangty committed Jan 12, 2024
1 parent 5949600 commit e34e077
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import org.commonjava.indy.service.repository.model.RemoteRepository;
import org.commonjava.indy.service.repository.model.StoreKey;
import org.commonjava.indy.service.repository.model.StoreType;
import org.commonjava.indy.service.repository.model.dto.EndpointView;
import org.commonjava.indy.service.repository.model.dto.EndpointViewListing;
import org.commonjava.indy.service.repository.util.JaxRsUriFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -34,8 +37,10 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -82,7 +87,7 @@ public List<ArtifactStore> getAllArtifactStores( final String packageType, final
// when packageType and type are all unique value, use storeManager.getArtifactStoresByPkgAndType to improve performance
stores = storeManager.getArtifactStoresByPkgAndType( packageType, typeList.get( 0 ) );
}
else if ( !isValidPackageType( packageType ) && typeList.size() == 0 )
else if ( !isValidPackageType( packageType ) && typeList.isEmpty() )
{
stores = storeManager.getAllArtifactStores();
}
Expand Down Expand Up @@ -251,6 +256,75 @@ public Boolean isStoreDataEmpty()
return storeManager.isEmpty();
}

public EndpointViewListing getEndpointsListing( final String pkgType, final String baseUri, final JaxRsUriFormatter uriFormatter )
throws IndyWorkflowException
{
List<ArtifactStore> stores;
try
{
stores = new ArrayList<>( storeManager.getAllArtifactStores() );
if ( StringUtils.isNotBlank( pkgType ) && !"all".equals( pkgType ) )
{
stores = stores.stream()
.filter( s -> pkgType.equals( s.getPackageType() ) )
.collect( Collectors.toList() );
}
}
catch ( final IndyDataException e )
{
throw new IndyWorkflowException( INTERNAL_SERVER_ERROR.getStatusCode(),
"Failed to retrieve all endpoints: {}", e, e.getMessage() );
}

final List<EndpointView> points = new ArrayList<>();
for ( final ArtifactStore store : stores )
{
final StoreKey key = store.getKey();
final String resourceUri = uriFormatter.formatAbsolutePathTo( baseUri, "content", key.getPackageType(),
key.getType().singularEndpointName(),
key.getName() );

final EndpointView point = new EndpointView( store, resourceUri );
if ( !points.contains( point ) )
{
points.add( point );
}
}

return new EndpointViewListing( points );
}

public Map<String, List<String>> getStoreKeysByPackageType( final String pkgType )
throws IndyWorkflowException
{
final List<ArtifactStore> stores;

try
{
final Map<String, List<String>> result = new HashMap<>();
stores = new ArrayList<>( storeManager.getAllArtifactStores() );
List<String> items;
if ( pkgType.equals( "all" ) )
{
items = stores.stream().map( s -> s.getKey().toString() ).collect( Collectors.toList() );
}
else
{
items = stores.stream()
.filter( s -> pkgType.equals( s.getPackageType() ) )
.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() );
}
}

private StoreKey validateStoreKey( final String storeKey )
throws IndyWorkflowException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,10 @@
package org.commonjava.indy.service.repository.controller;

import org.commonjava.indy.service.repository.data.StoreDataManager;
import org.commonjava.indy.service.repository.exception.IndyDataException;
import org.commonjava.indy.service.repository.exception.IndyWorkflowException;
import org.commonjava.indy.service.repository.model.ArtifactStore;
import org.commonjava.indy.service.repository.model.StoreKey;
import org.commonjava.indy.service.repository.model.dto.EndpointView;
import org.commonjava.indy.service.repository.model.dto.EndpointViewListing;
import org.commonjava.indy.service.repository.model.version.Versioning;
import org.commonjava.indy.service.repository.util.JaxRsUriFormatter;

import javax.enterprise.context.ApplicationScoped;
import javax.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 javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;

@ApplicationScoped
public class StatsController
Expand All @@ -56,56 +42,4 @@ public Versioning getVersionInfo()
return versioning;
}

public EndpointViewListing getEndpointsListing( final String baseUri, final JaxRsUriFormatter uriFormatter )
throws IndyWorkflowException
{
final List<ArtifactStore> stores;
try
{
stores = new ArrayList<>( dataManager.getAllArtifactStores() );
}
catch ( final IndyDataException e )
{
throw new IndyWorkflowException( INTERNAL_SERVER_ERROR.getStatusCode(),
"Failed to retrieve all endpoints: {}", e, e.getMessage() );
}

final List<EndpointView> points = new ArrayList<>();
for ( final ArtifactStore store : stores )
{
final StoreKey key = store.getKey();
final String resourceUri = uriFormatter.formatAbsolutePathTo( baseUri, "content", key.getPackageType(),
key.getType().singularEndpointName(),
key.getName() );

final EndpointView point = new EndpointView( store, resourceUri );
if ( !points.contains( point ) )
{
points.add( point );
}
}

return new EndpointViewListing( points );
}

public Map<String, List<String>> getAllStoreKeys()
throws IndyWorkflowException
{
final List<ArtifactStore> stores;
try
{
final Map<String, List<String>> result = new HashMap<>();
stores = new ArrayList<>( dataManager.getAllArtifactStores() );
List<String> 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() );
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import org.commonjava.indy.service.repository.controller.QueryController;
import org.commonjava.indy.service.repository.exception.IndyWorkflowException;
import org.commonjava.indy.service.repository.model.ArtifactStore;
import org.commonjava.indy.service.repository.model.dto.EndpointViewListing;
import org.commonjava.indy.service.repository.model.dto.SimpleBooleanResultDTO;
import org.commonjava.indy.service.repository.model.dto.StoreListingDTO;
import org.commonjava.indy.service.repository.util.JaxRsUriFormatter;
import org.commonjava.indy.service.repository.util.UrlUtils;
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.media.Content;
Expand All @@ -40,13 +42,18 @@
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
import static javax.ws.rs.core.Response.Status.NOT_FOUND;
import static javax.ws.rs.core.Response.ok;
import static org.commonjava.indy.service.repository.util.Constants.API_PREFIX;
import static org.eclipse.microprofile.openapi.annotations.enums.ParameterIn.QUERY;

@Tag( name = "Store Querying APIs", description = "Resource for querying artifact store definitions" )
Expand All @@ -63,6 +70,9 @@ public class RepositoryQueryResources
@Inject
QueryController queryController;

@Inject
JaxRsUriFormatter uriFormatter;

public RepositoryQueryResources()
{
logger.info( "\n\n\n\nStarted Store Querying resources\n\n\n\n" );
Expand Down Expand Up @@ -295,6 +305,62 @@ public Response getStoreEmpty()
return responseHelper.formatOkResponseWithJsonEntity( dto );
}

@Operation(
summary = "Retrieve a listing of the artifact stores available on the system. This is especially useful for setting up a network of Indy instances that reference one another" )
@APIResponse( responseCode = "200",
content = @Content( schema = @Schema( implementation = EndpointViewListing.class ) ),
description = "The artifact store listing" )
@Path( "/endpoints/{packageType}" )
@GET
@Produces( APPLICATION_JSON )
public Response getEndpoints( @PathParam( "packageType" ) final String pkgType, @Context final UriInfo uriInfo )
{
Response response;
try
{
final String baseUri = uriInfo.getBaseUriBuilder().path( API_PREFIX ).build().toString();

final EndpointViewListing listing = queryController.getEndpointsListing( pkgType, baseUri, uriFormatter );
response = responseHelper.formatOkResponseWithJsonEntity( listing );

logger.info( "\n\n\n\n\n\n{} Sent all-endpoints:\n\n{}\n\n\n\n\n\n\n", new Date(), listing );
}
catch ( final IndyWorkflowException e )
{
logger.error( String.format( "Failed to retrieve endpoint listing: %s", responseHelper.formatEntity( e ) ),
e );
response = responseHelper.formatResponse( e );
}
return response;
}

@Operation( summary = "Retrieve a listing of the artifact stores keys available on the system." )
@APIResponse( responseCode = "200", content = @Content( schema = @Schema( implementation = Map.class ) ),
description = "The artifact store keys listing" )
@Path( "/storekeys/{packageType}" )
@GET
@Produces( APPLICATION_JSON )
public Response getStoreKeys( @PathParam( "packageType" ) final String pkgType, @Context final UriInfo uriInfo )
{
Response response;
try
{

Map<String, List<String>> result = queryController.getStoreKeysByPackageType( pkgType );

response = responseHelper.formatOkResponseWithJsonEntity( result );

logger.debug( "\n\n\n\n\n\n{} Sent store 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 by type %s: %s", pkgType,
responseHelper.formatEntity( e ) ), e );
response = responseHelper.formatResponse( e );
}
return response;
}

@SuppressWarnings( { "unchecked", "rawtypes" } )
private Response generateStoreListingResponse( ArtifactStoreListSupplier supplier )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@
package org.commonjava.indy.service.repository.jaxrs.version;

import org.commonjava.indy.service.repository.controller.StatsController;
import org.commonjava.indy.service.repository.exception.IndyWorkflowException;
import org.commonjava.indy.service.repository.jaxrs.ResponseHelper;
import org.commonjava.indy.service.repository.model.dto.EndpointViewListing;
import org.commonjava.indy.service.repository.model.version.Versioning;
import org.commonjava.indy.service.repository.util.JaxRsUriFormatter;
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.media.Content;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
Expand All @@ -33,14 +30,7 @@
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.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;
Expand All @@ -49,7 +39,6 @@
import static javax.ws.rs.core.Response.ok;
import static org.commonjava.indy.service.repository.model.PackageTypes.getPackageTypeDescriptorMap;
import static org.commonjava.indy.service.repository.model.PackageTypes.getPackageTypes;
import static org.commonjava.indy.service.repository.util.Constants.API_PREFIX;

@Tag( name = "Generic Infrastructure Queries (UI Support)",
description = "Various read-only operations for retrieving information about the system." )
Expand All @@ -64,9 +53,6 @@ public class StatsHandler
@Inject
ResponseHelper responseHelper;

@Inject
JaxRsUriFormatter uriFormatter;

@Operation( summary = "Retrieve versioning information about this APP instance" )
@APIResponse( responseCode = "200", content = @Content( schema = @Schema( implementation = Versioning.class ) ),
description = "The version info of the APIs" )
Expand Down Expand Up @@ -106,62 +92,4 @@ public Response getPackageTypeNames()
return ok( new TreeSet<>( getPackageTypes() ) ).build();
}

@Operation(
summary = "Retrieve a listing of the artifact stores available on the system. This is especially useful for setting up a network of Indy instances that reference one another" )
@APIResponse( responseCode = "200",
content = @Content( schema = @Schema( implementation = EndpointViewListing.class ) ),
description = "The artifact store listing" )
@Path( "/all-endpoints" )
@GET
@Produces( APPLICATION_JSON )
public Response getAllEndpoints( @Context final UriInfo uriInfo )
{
Response response;
try
{
final String baseUri = uriInfo.getBaseUriBuilder().path( API_PREFIX ).build().toString();

final EndpointViewListing listing = statsController.getEndpointsListing( baseUri, uriFormatter );
response = responseHelper.formatOkResponseWithJsonEntity( listing );

logger.info( "\n\n\n\n\n\n{} Sent all-endpoints:\n\n{}\n\n\n\n\n\n\n", new Date(), listing );
}
catch ( final IndyWorkflowException e )
{
logger.error( String.format( "Failed to retrieve endpoint listing: %s", responseHelper.formatEntity( e ) ),
e );
response = responseHelper.formatResponse( e );
}
return response;
}

@Operation(
summary = "Retrieve a listing of the artifact stores keys available on the system." )
@APIResponse( responseCode = "200",
content = @Content( schema = @Schema( implementation = Map.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<String, List<String>> 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;
}

}
Loading

0 comments on commit e34e077

Please sign in to comment.