diff --git a/core/pom.xml b/core/pom.xml index 7f66be1f4e..ca40b3383c 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -151,6 +151,15 @@ org.commonjava.indy indy-schedule-common + + org.commonjava.indy + indy-subsys-jaxrs + + + io.swagger + swagger-annotations + compile + diff --git a/core/src/main/java/org/commonjava/indy/core/inject/CoreProvider.java b/core/src/main/java/org/commonjava/indy/core/inject/CoreProvider.java index e0bf186354..f0ab15e250 100644 --- a/core/src/main/java/org/commonjava/indy/core/inject/CoreProvider.java +++ b/core/src/main/java/org/commonjava/indy/core/inject/CoreProvider.java @@ -23,6 +23,7 @@ import org.commonjava.indy.subsys.infinispan.BasicCacheHandle; import org.commonjava.indy.subsys.infinispan.CacheProducer; import org.commonjava.maven.galley.spi.nfc.NotFoundCache; +import org.commonjava.maven.galley.spi.proxy.ProxySitesCache; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -32,6 +33,8 @@ import javax.enterprise.inject.Instance; import javax.enterprise.inject.Produces; import javax.inject.Inject; +import java.util.Collections; +import java.util.Set; import static org.commonjava.indy.conf.DefaultIndyConfiguration.CASSANDRA_NFC_PROVIDER; @@ -61,6 +64,8 @@ public class CoreProvider private IndyObjectMapper objectMapper; + private ProxySitesCache proxySitesCache; + public CoreProvider() { } @@ -69,6 +74,7 @@ public CoreProvider() public void init() { this.objectMapper = new IndyObjectMapper( objectMapperModules, objectMapperModuleSets ); + this.proxySitesCache = new MemoryProxySitesCache(); String nfcProvider = indyConfiguration.getNfcProvider(); logger.info( "Apply nfc provider: {}", nfcProvider ); @@ -98,4 +104,35 @@ public IndyObjectMapper getIndyObjectMapper() @Default public NotFoundCache getNotFoundCache() { return notFoundCache; } + @Produces + @Default + public ProxySitesCache getProxySitesCache() + { + return proxySitesCache; + } + + public Set getProxySites() + { + if ( proxySitesCache != null ) + { + return proxySitesCache.getProxySites(); + } + return Collections.emptySet(); + } + + public void saveProxySite( String site ) + { + proxySitesCache.saveProxySite( site ); + } + + public void deleteProxySite( String site ) + { + proxySitesCache.deleteProxySite( site ); + } + + public void deleteAllProxySites() + { + proxySitesCache.deleteAllProxySites(); + } + } diff --git a/core/src/main/java/org/commonjava/indy/core/inject/MemoryProxySitesCache.java b/core/src/main/java/org/commonjava/indy/core/inject/MemoryProxySitesCache.java new file mode 100644 index 0000000000..024e0dcdb4 --- /dev/null +++ b/core/src/main/java/org/commonjava/indy/core/inject/MemoryProxySitesCache.java @@ -0,0 +1,62 @@ +/** + * Copyright (C) 2011-2023 Red Hat, Inc. (https://github.com/Commonjava/indy) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.commonjava.indy.core.inject; + +import org.commonjava.maven.galley.spi.proxy.ProxySitesCache; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.Alternative; +import java.util.HashSet; +import java.util.Set; + +@ApplicationScoped +@Alternative +public class MemoryProxySitesCache + implements ProxySitesCache +{ + + protected Set proxySitesCache = new HashSet<>(); + + @Override + public Set getProxySites() + { + return proxySitesCache; + } + + @Override + public boolean isProxySite( String site ) + { + return proxySitesCache.contains( site ); + } + + @Override + public void saveProxySite( String site ) + { + proxySitesCache.add( site ); + } + + @Override + public void deleteProxySite( String site ) + { + proxySitesCache.remove( site ); + } + + @Override + public void deleteAllProxySites() + { + proxySitesCache.clear(); + } +} diff --git a/core/src/main/java/org/commonjava/indy/core/jaxrs/ProxySiteAccessResource.java b/core/src/main/java/org/commonjava/indy/core/jaxrs/ProxySiteAccessResource.java new file mode 100644 index 0000000000..e1a9df519e --- /dev/null +++ b/core/src/main/java/org/commonjava/indy/core/jaxrs/ProxySiteAccessResource.java @@ -0,0 +1,101 @@ +/** + * Copyright (C) 2011-2023 Red Hat, Inc. (https://github.com/Commonjava/indy) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.commonjava.indy.core.jaxrs; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; +import org.commonjava.indy.bind.jaxrs.IndyResources; +import org.commonjava.indy.bind.jaxrs.util.REST; +import org.commonjava.indy.core.inject.CoreProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.enterprise.context.ApplicationScoped; +import javax.inject.Inject; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.StreamingOutput; +import javax.ws.rs.core.UriInfo; +import java.util.Set; + +@Api( value = "Proxy Site Cache Access and Storage" ) +@Path( "/api/proxysite" ) +@ApplicationScoped +@REST +public class ProxySiteAccessResource + implements IndyResources +{ + protected final Logger logger = LoggerFactory.getLogger( getClass() ); + + @Inject + CoreProvider provider; + + @ApiOperation( "Retrieve All Proxy Sites." ) + @ApiResponses( { @ApiResponse( code = 404, message = "Site is not available" ), + @ApiResponse( code = 200, response = StreamingOutput.class, message = "Site stream" ), } ) + @Produces( "application/json" ) + @Path( "/all" ) + @GET + public Response doGet( @Context final UriInfo uriInfo, @Context final HttpServletRequest request ) + { + Set cache = provider.getProxySites(); + logger.info( "Proxy Site Cache list: {}", cache ); + return ( cache == null || cache.isEmpty() ) ? + Response.status( Response.Status.NOT_FOUND ).build() : + Response.ok( cache ).build(); + } + + @ApiOperation( "Store Proxy Site." ) + @ApiResponses( { @ApiResponse( code = 201, message = "Site was stored successfully" ) } ) + @PUT + @Path( "/{site}" ) + public Response doCreate( @PathParam( "site" ) final String site, @Context final HttpServletRequest request, + @Context final UriInfo uriInfo ) + { + provider.saveProxySite( site ); + return Response.created( uriInfo.getRequestUri() ).build(); + } + + @ApiOperation( "Delete Proxy Site." ) + @ApiResponse( code = 200, message = "Delete complete." ) + @Path( "/{site}" ) + @DELETE + public Response doDelete( @PathParam( "site" ) final String site ) + { + provider.deleteProxySite( site ); + return Response.ok().build(); + } + + @ApiOperation( "Delete All Proxy Sites." ) + @ApiResponse( code = 200, message = "Delete complete." ) + @Path( "/all" ) + @DELETE + public Response doDeleteAll() + { + provider.deleteAllProxySites(); + return Response.ok().build(); + } + +} diff --git a/pom.xml b/pom.xml index 14072a49e8..5cf4341ea1 100644 --- a/pom.xml +++ b/pom.xml @@ -94,7 +94,7 @@ 1.5 3.4.0 1.1.4 - 1.17 + 1.18-SNAPSHOT 1.24 3.2.1