1 package org.apache.archiva.rest.services.v2;/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
20 * Licensed to the Apache Software Foundation (ASF) under one
21 * or more contributor license agreements. See the NOTICE file
22 * distributed with this work for additional information
23 * regarding copyright ownership. The ASF licenses this file
24 * to you under the Apache License, Version 2.0 (the
25 * "License"); you may not use this file except in compliance
26 * with the License. You may obtain a copy of the License at
28 * http://www.apache.org/licenses/LICENSE-2.0
29 * Unless required by applicable law or agreed to in writing,
30 * software distributed under the License is distributed on an
31 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
32 * KIND, either express or implied. See the License for the
33 * specific language governing permissions and limitations
37 import org.apache.archiva.components.registry.RegistryException;
38 import org.apache.archiva.components.rest.model.PagedResult;
39 import org.apache.archiva.components.rest.util.QueryHelper;
40 import org.apache.archiva.configuration.Configuration;
41 import org.apache.archiva.configuration.IndeterminateConfigurationException;
42 import org.apache.archiva.configuration.RepositoryGroupConfiguration;
43 import org.apache.archiva.repository.CheckedResult;
44 import org.apache.archiva.repository.EditableRepositoryGroup;
45 import org.apache.archiva.repository.RepositoryException;
46 import org.apache.archiva.repository.RepositoryRegistry;
47 import org.apache.archiva.repository.base.ConfigurationHandler;
48 import org.apache.archiva.repository.validation.ValidationError;
49 import org.apache.archiva.repository.validation.ValidationResponse;
50 import org.apache.archiva.rest.api.model.v2.MergeConfiguration;
51 import org.apache.archiva.rest.api.model.v2.RepositoryGroup;
52 import org.apache.archiva.rest.api.services.v2.ArchivaRestServiceException;
53 import org.apache.archiva.rest.api.services.v2.ErrorKeys;
54 import org.apache.archiva.rest.api.services.v2.ErrorMessage;
55 import org.apache.archiva.rest.api.services.v2.RepositoryGroupService;
56 import org.apache.archiva.rest.api.services.v2.ValidationException;
57 import org.apache.commons.lang3.StringUtils;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
60 import org.springframework.stereotype.Service;
62 import javax.servlet.http.HttpServletResponse;
63 import javax.ws.rs.core.Context;
64 import javax.ws.rs.core.Response;
65 import javax.ws.rs.core.UriInfo;
66 import java.util.Comparator;
67 import java.util.List;
69 import java.util.function.Predicate;
70 import java.util.stream.Collectors;
73 * REST V2 Implementation for repository groups.
75 * @author Martin Stockhammer <martin_s@apache.org>
76 * @see RepositoryGroupService
79 @Service("v2.repositoryGroupService#rest")
80 public class DefaultRepositoryGroupService implements RepositoryGroupService
82 private final ConfigurationHandler configurationHandler;
85 HttpServletResponse httpServletResponse;
90 final private RepositoryRegistry repositoryRegistry;
94 private static final Logger log = LoggerFactory.getLogger( DefaultRepositoryGroupService.class );
95 private static final QueryHelper<org.apache.archiva.repository.RepositoryGroup> QUERY_HELPER = new QueryHelper<>( new String[]{"id"} );
98 QUERY_HELPER.addStringFilter( "id", org.apache.archiva.repository.RepositoryGroup::getId );
99 QUERY_HELPER.addNullsafeFieldComparator( "id", org.apache.archiva.repository.RepositoryGroup::getId );
103 public DefaultRepositoryGroupService( RepositoryRegistry repositoryRegistry, ConfigurationHandler configurationHandler ) {
104 this.repositoryRegistry = repositoryRegistry;
105 this.configurationHandler = configurationHandler;
109 public PagedResult<RepositoryGroup> getRepositoriesGroups( String searchTerm, Integer offset, Integer limit, List<String> orderBy, String order ) throws ArchivaRestServiceException
113 Predicate<org.apache.archiva.repository.RepositoryGroup> filter = QUERY_HELPER.getQueryFilter( searchTerm );
114 Comparator<org.apache.archiva.repository.RepositoryGroup> ordering = QUERY_HELPER.getComparator( orderBy, QUERY_HELPER.isAscending( order ) );
115 int totalCount = Math.toIntExact( repositoryRegistry.getRepositoryGroups( ).stream( ).filter( filter ).count( ) );
116 List<RepositoryGroup> result = repositoryRegistry.getRepositoryGroups( ).stream( ).filter( filter ).sorted( ordering ).skip( offset ).limit( limit ).map(
118 ).collect( Collectors.toList( ) );
119 return new PagedResult<>( totalCount, offset, limit, result );
121 catch ( ArithmeticException e )
123 log.error( "Could not convert total count: {}", e.getMessage( ) );
124 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.INVALID_RESULT_SET_ERROR ) );
130 public RepositoryGroup getRepositoryGroup( String repositoryGroupId ) throws ArchivaRestServiceException
132 if ( StringUtils.isEmpty( repositoryGroupId ) )
134 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
136 org.apache.archiva.repository.RepositoryGroup group = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
137 return RepositoryGroup.of( group );
140 private RepositoryGroupConfiguration toConfig( RepositoryGroup group )
142 RepositoryGroupConfiguration result = new RepositoryGroupConfiguration( );
143 result.setId( group.getId( ) );
144 result.setLocation( group.getLocation( ) );
145 result.setRepositories( group.getRepositories( ) );
146 MergeConfiguration mergeConfig = group.getMergeConfiguration( );
147 if (mergeConfig!=null)
149 result.setMergedIndexPath( mergeConfig.getMergedIndexPath( ) );
150 result.setMergedIndexTtl( mergeConfig.getMergedIndexTtlMinutes( ) );
151 result.setCronExpression( mergeConfig.getIndexMergeSchedule( ) );
157 public RepositoryGroup addRepositoryGroup( RepositoryGroup repositoryGroup ) throws ArchivaRestServiceException
159 final String groupId = repositoryGroup.getId( );
160 if ( StringUtils.isEmpty( groupId ) ) {
161 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_INVALID_ID, groupId ), 422 );
163 if (repositoryRegistry.hasRepositoryGroup( groupId )) {
164 httpServletResponse.setHeader( "Location", uriInfo.getAbsolutePathBuilder( ).path( groupId ).build( ).toString( ) );
165 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_ID_EXISTS, groupId ), 303 );
170 RepositoryGroupConfiguration configuration = toConfig( repositoryGroup );
171 CheckedResult<org.apache.archiva.repository.RepositoryGroup, Map<String, List<ValidationError>>> validationResult = repositoryRegistry.putRepositoryGroupAndValidate( configuration );
172 if ( validationResult.isValid( ) )
174 httpServletResponse.setStatus( 201 );
175 return RepositoryGroup.of( validationResult.getRepository() );
177 throw ValidationException.of( validationResult.getResult() );
180 catch ( RepositoryException e )
182 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_ADD_FAILED ) );
187 public RepositoryGroup updateRepositoryGroup( final String repositoryGroupId, final RepositoryGroup repositoryGroup ) throws ArchivaRestServiceException
189 if ( StringUtils.isEmpty( repositoryGroupId ) )
191 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
193 if ( !repositoryRegistry.hasRepositoryGroup( repositoryGroupId ) )
195 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND ), 404 );
197 repositoryGroup.setId( repositoryGroupId );
200 RepositoryGroupConfiguration configuration = toConfig( repositoryGroup );
201 CheckedResult<org.apache.archiva.repository.RepositoryGroup, Map<String, List<ValidationError>>> validationResult = repositoryRegistry.putRepositoryGroupAndValidate( configuration );
202 if ( validationResult.isValid( ) )
204 httpServletResponse.setStatus( 201 );
205 return RepositoryGroup.of( validationResult.getRepository() );
207 throw ValidationException.of( validationResult.getResult() );
210 catch ( RepositoryException e )
212 log.error( "Exception during repository group update: {}", e.getMessage( ), e );
213 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, e.getMessage() ) );
219 public Response deleteRepositoryGroup( String repositoryGroupId ) throws ArchivaRestServiceException
221 if ( StringUtils.isEmpty( repositoryGroupId ) )
223 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
227 org.apache.archiva.repository.RepositoryGroup group = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
229 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
231 repositoryRegistry.removeRepositoryGroup( group );
232 return Response.ok( ).build( );
234 catch ( RepositoryException e )
236 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_DELETE_FAILED ) );
241 public RepositoryGroup addRepositoryToGroup( String repositoryGroupId, String repositoryId ) throws ArchivaRestServiceException
243 if ( StringUtils.isEmpty( repositoryGroupId ) )
245 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
247 if ( StringUtils.isEmpty( repositoryId ) )
249 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_FOUND, "" ), 404 );
253 org.apache.archiva.repository.RepositoryGroup repositoryGroup = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
254 if (repositoryGroup==null) {
255 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
257 if (!(repositoryGroup instanceof EditableRepositoryGroup )) {
258 log.error( "This group instance is not editable: {}", repositoryGroupId );
259 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, "" ), 500 );
261 EditableRepositoryGroup editableRepositoryGroup = (EditableRepositoryGroup) repositoryGroup;
262 if ( editableRepositoryGroup.getRepositories().stream().anyMatch( repo -> repositoryId.equals(repo.getId())) )
264 log.info( "Repository {} is already member of group {}", repositoryId, repositoryGroupId );
265 return RepositoryGroup.of( editableRepositoryGroup );
267 org.apache.archiva.repository.ManagedRepository managedRepo = repositoryRegistry.getManagedRepository(repositoryId);
268 if (managedRepo==null) {
269 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_FOUND, "" ), 404 );
271 editableRepositoryGroup.addRepository( managedRepo );
272 org.apache.archiva.repository.RepositoryGroup newGroup = repositoryRegistry.putRepositoryGroup( editableRepositoryGroup );
273 return RepositoryGroup.of( newGroup );
275 catch ( RepositoryException e )
277 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, e.getMessage() ), 500 );
282 public RepositoryGroup deleteRepositoryFromGroup( String repositoryGroupId, String repositoryId ) throws org.apache.archiva.rest.api.services.v2.ArchivaRestServiceException
284 if ( StringUtils.isEmpty( repositoryGroupId ) )
286 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
288 if ( StringUtils.isEmpty( repositoryId ) )
290 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_FOUND, "" ), 404 );
294 org.apache.archiva.repository.RepositoryGroup repositoryGroup = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
295 if (repositoryGroup==null) {
296 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
298 if (!(repositoryGroup instanceof EditableRepositoryGroup)) {
299 log.error( "This group instance is not editable: {}", repositoryGroupId );
300 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, "" ), 500 );
302 EditableRepositoryGroup editableRepositoryGroup = (EditableRepositoryGroup) repositoryGroup;
304 editableRepositoryGroup.removeRepository( repositoryId );
305 org.apache.archiva.repository.RepositoryGroup newGroup = repositoryRegistry.putRepositoryGroup( editableRepositoryGroup );
306 return RepositoryGroup.of( newGroup );
308 catch ( RepositoryException e )
310 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, e.getMessage() ), 500 );