123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- package org.apache.archiva.rest.services.v2;/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you 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.
- */
-
- /*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you 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.
- */
-
- import org.apache.archiva.components.registry.RegistryException;
- import org.apache.archiva.components.rest.model.PagedResult;
- import org.apache.archiva.components.rest.util.QueryHelper;
- import org.apache.archiva.configuration.Configuration;
- import org.apache.archiva.configuration.IndeterminateConfigurationException;
- import org.apache.archiva.configuration.RepositoryGroupConfiguration;
- import org.apache.archiva.repository.CheckedResult;
- import org.apache.archiva.repository.EditableRepositoryGroup;
- import org.apache.archiva.repository.RepositoryException;
- import org.apache.archiva.repository.RepositoryRegistry;
- import org.apache.archiva.repository.base.ConfigurationHandler;
- import org.apache.archiva.repository.validation.ValidationError;
- import org.apache.archiva.repository.validation.ValidationResponse;
- import org.apache.archiva.rest.api.model.v2.MergeConfiguration;
- import org.apache.archiva.rest.api.model.v2.RepositoryGroup;
- import org.apache.archiva.rest.api.services.v2.ArchivaRestServiceException;
- import org.apache.archiva.rest.api.services.v2.ErrorKeys;
- import org.apache.archiva.rest.api.services.v2.ErrorMessage;
- import org.apache.archiva.rest.api.services.v2.RepositoryGroupService;
- import org.apache.archiva.rest.api.services.v2.ValidationException;
- import org.apache.commons.lang3.StringUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.stereotype.Service;
-
- import javax.servlet.http.HttpServletResponse;
- import javax.ws.rs.core.Context;
- import javax.ws.rs.core.Response;
- import javax.ws.rs.core.UriInfo;
- import java.util.Comparator;
- import java.util.List;
- import java.util.Map;
- import java.util.function.Predicate;
- import java.util.stream.Collectors;
-
- /**
- * REST V2 Implementation for repository groups.
- *
- * @author Martin Stockhammer <martin_s@apache.org>
- * @see RepositoryGroupService
- * @since 3.0
- */
- @Service("v2.repositoryGroupService#rest")
- public class DefaultRepositoryGroupService implements RepositoryGroupService
- {
- private final ConfigurationHandler configurationHandler;
-
- @Context
- HttpServletResponse httpServletResponse;
-
- @Context
- UriInfo uriInfo;
-
- final private RepositoryRegistry repositoryRegistry;
-
-
-
- private static final Logger log = LoggerFactory.getLogger( DefaultRepositoryGroupService.class );
- private static final QueryHelper<org.apache.archiva.repository.RepositoryGroup> QUERY_HELPER = new QueryHelper<>( new String[]{"id"} );
- static
- {
- QUERY_HELPER.addStringFilter( "id", org.apache.archiva.repository.RepositoryGroup::getId );
- QUERY_HELPER.addNullsafeFieldComparator( "id", org.apache.archiva.repository.RepositoryGroup::getId );
- }
-
-
- public DefaultRepositoryGroupService( RepositoryRegistry repositoryRegistry, ConfigurationHandler configurationHandler ) {
- this.repositoryRegistry = repositoryRegistry;
- this.configurationHandler = configurationHandler;
- }
-
- @Override
- public PagedResult<RepositoryGroup> getRepositoriesGroups( String searchTerm, Integer offset, Integer limit, List<String> orderBy, String order ) throws ArchivaRestServiceException
- {
- try
- {
- Predicate<org.apache.archiva.repository.RepositoryGroup> filter = QUERY_HELPER.getQueryFilter( searchTerm );
- Comparator<org.apache.archiva.repository.RepositoryGroup> ordering = QUERY_HELPER.getComparator( orderBy, QUERY_HELPER.isAscending( order ) );
- int totalCount = Math.toIntExact( repositoryRegistry.getRepositoryGroups( ).stream( ).filter( filter ).count( ) );
- List<RepositoryGroup> result = repositoryRegistry.getRepositoryGroups( ).stream( ).filter( filter ).sorted( ordering ).skip( offset ).limit( limit ).map(
- RepositoryGroup::of
- ).collect( Collectors.toList( ) );
- return new PagedResult<>( totalCount, offset, limit, result );
- }
- catch ( ArithmeticException e )
- {
- log.error( "Could not convert total count: {}", e.getMessage( ) );
- throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.INVALID_RESULT_SET_ERROR ) );
- }
-
- }
-
- @Override
- public RepositoryGroup getRepositoryGroup( String repositoryGroupId ) throws ArchivaRestServiceException
- {
- if ( StringUtils.isEmpty( repositoryGroupId ) )
- {
- throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
- }
- org.apache.archiva.repository.RepositoryGroup group = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
- return RepositoryGroup.of( group );
- }
-
- private RepositoryGroupConfiguration toConfig( RepositoryGroup group )
- {
- RepositoryGroupConfiguration result = new RepositoryGroupConfiguration( );
- result.setId( group.getId( ) );
- result.setLocation( group.getLocation( ) );
- result.setRepositories( group.getRepositories( ) );
- MergeConfiguration mergeConfig = group.getMergeConfiguration( );
- if (mergeConfig!=null)
- {
- result.setMergedIndexPath( mergeConfig.getMergedIndexPath( ) );
- result.setMergedIndexTtl( mergeConfig.getMergedIndexTtlMinutes( ) );
- result.setCronExpression( mergeConfig.getIndexMergeSchedule( ) );
- }
- return result;
- }
-
- @Override
- public RepositoryGroup addRepositoryGroup( RepositoryGroup repositoryGroup ) throws ArchivaRestServiceException
- {
- final String groupId = repositoryGroup.getId( );
- if ( StringUtils.isEmpty( groupId ) ) {
- throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_INVALID_ID, groupId ), 422 );
- }
- if (repositoryRegistry.hasRepositoryGroup( groupId )) {
- httpServletResponse.setHeader( "Location", uriInfo.getAbsolutePathBuilder( ).path( groupId ).build( ).toString( ) );
- throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_ID_EXISTS, groupId ), 303 );
- }
- try
- {
-
- RepositoryGroupConfiguration configuration = toConfig( repositoryGroup );
- CheckedResult<org.apache.archiva.repository.RepositoryGroup, Map<String, List<ValidationError>>> validationResult = repositoryRegistry.putRepositoryGroupAndValidate( configuration );
- if ( validationResult.isValid( ) )
- {
- httpServletResponse.setStatus( 201 );
- return RepositoryGroup.of( validationResult.getRepository() );
- } else {
- throw ValidationException.of( validationResult.getResult() );
- }
- }
- catch ( RepositoryException e )
- {
- throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_ADD_FAILED ) );
- }
- }
-
- @Override
- public RepositoryGroup updateRepositoryGroup( final String repositoryGroupId, final RepositoryGroup repositoryGroup ) throws ArchivaRestServiceException
- {
- if ( StringUtils.isEmpty( repositoryGroupId ) )
- {
- throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
- }
- if ( !repositoryRegistry.hasRepositoryGroup( repositoryGroupId ) )
- {
- throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND ), 404 );
- }
- repositoryGroup.setId( repositoryGroupId );
- try
- {
- RepositoryGroupConfiguration configuration = toConfig( repositoryGroup );
- CheckedResult<org.apache.archiva.repository.RepositoryGroup, Map<String, List<ValidationError>>> validationResult = repositoryRegistry.putRepositoryGroupAndValidate( configuration );
- if ( validationResult.isValid( ) )
- {
- httpServletResponse.setStatus( 201 );
- return RepositoryGroup.of( validationResult.getRepository() );
- } else {
- throw ValidationException.of( validationResult.getResult() );
- }
- }
- catch ( RepositoryException e )
- {
- log.error( "Exception during repository group update: {}", e.getMessage( ), e );
- throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, e.getMessage() ) );
-
- }
- }
-
- @Override
- public Response deleteRepositoryGroup( String repositoryGroupId ) throws ArchivaRestServiceException
- {
- if ( StringUtils.isEmpty( repositoryGroupId ) )
- {
- throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
- }
- try
- {
- org.apache.archiva.repository.RepositoryGroup group = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
- if (group==null) {
- throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
- }
- repositoryRegistry.removeRepositoryGroup( group );
- return Response.ok( ).build( );
- }
- catch ( RepositoryException e )
- {
- throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_DELETE_FAILED ) );
- }
- }
-
- @Override
- public RepositoryGroup addRepositoryToGroup( String repositoryGroupId, String repositoryId ) throws ArchivaRestServiceException
- {
- if ( StringUtils.isEmpty( repositoryGroupId ) )
- {
- throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
- }
- if ( StringUtils.isEmpty( repositoryId ) )
- {
- throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_FOUND, "" ), 404 );
- }
- try
- {
- org.apache.archiva.repository.RepositoryGroup repositoryGroup = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
- if (repositoryGroup==null) {
- throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
- }
- if (!(repositoryGroup instanceof EditableRepositoryGroup )) {
- log.error( "This group instance is not editable: {}", repositoryGroupId );
- throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, "" ), 500 );
- }
- EditableRepositoryGroup editableRepositoryGroup = (EditableRepositoryGroup) repositoryGroup;
- if ( editableRepositoryGroup.getRepositories().stream().anyMatch( repo -> repositoryId.equals(repo.getId())) )
- {
- log.info( "Repository {} is already member of group {}", repositoryId, repositoryGroupId );
- return RepositoryGroup.of( editableRepositoryGroup );
- }
- org.apache.archiva.repository.ManagedRepository managedRepo = repositoryRegistry.getManagedRepository(repositoryId);
- if (managedRepo==null) {
- throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_FOUND, "" ), 404 );
- }
- editableRepositoryGroup.addRepository( managedRepo );
- org.apache.archiva.repository.RepositoryGroup newGroup = repositoryRegistry.putRepositoryGroup( editableRepositoryGroup );
- return RepositoryGroup.of( newGroup );
- }
- catch ( RepositoryException e )
- {
- throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, e.getMessage() ), 500 );
- }
- }
-
- @Override
- public RepositoryGroup deleteRepositoryFromGroup( final String repositoryGroupId, final String repositoryId ) throws org.apache.archiva.rest.api.services.v2.ArchivaRestServiceException
- {
- if ( StringUtils.isEmpty( repositoryGroupId ) )
- {
- throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, repositoryGroupId ), 404 );
- }
- if ( StringUtils.isEmpty( repositoryId ) )
- {
- throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_FOUND, repositoryId ), 404 );
- }
- try
- {
- org.apache.archiva.repository.RepositoryGroup repositoryGroup = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
- if (repositoryGroup==null) {
- throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
- }
- if (repositoryGroup.getRepositories().stream().noneMatch( r -> repositoryId.equals( r.getId() ) )) {
- throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_FOUND, repositoryId ), 404 );
- }
- if (!(repositoryGroup instanceof EditableRepositoryGroup)) {
- log.error( "This group instance is not editable: {}", repositoryGroupId );
- throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, "" ), 500 );
- }
- EditableRepositoryGroup editableRepositoryGroup = (EditableRepositoryGroup) repositoryGroup;
- editableRepositoryGroup.removeRepository( repositoryId );
- org.apache.archiva.repository.RepositoryGroup newGroup = repositoryRegistry.putRepositoryGroup( editableRepositoryGroup );
- return RepositoryGroup.of( newGroup );
- }
- catch ( RepositoryException e )
- {
- throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, e.getMessage() ), 500 );
- }
- }
-
-
- }
|