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.rest.model.PagedResult;
38 import org.apache.archiva.components.rest.util.QueryHelper;
39 import org.apache.archiva.configuration.RepositoryGroupConfiguration;
40 import org.apache.archiva.repository.validation.CheckedResult;
41 import org.apache.archiva.repository.EditableRepositoryGroup;
42 import org.apache.archiva.repository.RepositoryException;
43 import org.apache.archiva.repository.RepositoryRegistry;
44 import org.apache.archiva.repository.base.ConfigurationHandler;
45 import org.apache.archiva.repository.validation.ValidationError;
46 import org.apache.archiva.rest.api.model.v2.MergeConfiguration;
47 import org.apache.archiva.rest.api.model.v2.RepositoryGroup;
48 import org.apache.archiva.rest.api.services.v2.ArchivaRestServiceException;
49 import org.apache.archiva.rest.api.services.v2.ErrorKeys;
50 import org.apache.archiva.rest.api.services.v2.ErrorMessage;
51 import org.apache.archiva.rest.api.services.v2.RepositoryGroupService;
52 import org.apache.archiva.rest.api.services.v2.ValidationException;
53 import org.apache.commons.lang3.StringUtils;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56 import org.springframework.stereotype.Service;
58 import javax.servlet.http.HttpServletResponse;
59 import javax.ws.rs.core.Context;
60 import javax.ws.rs.core.Response;
61 import javax.ws.rs.core.UriInfo;
62 import java.util.Comparator;
63 import java.util.List;
65 import java.util.function.Predicate;
66 import java.util.stream.Collectors;
69 * REST V2 Implementation for repository groups.
71 * @author Martin Stockhammer <martin_s@apache.org>
72 * @see RepositoryGroupService
75 @Service("v2.repositoryGroupService#rest")
76 public class DefaultRepositoryGroupService implements RepositoryGroupService
78 private final ConfigurationHandler configurationHandler;
81 HttpServletResponse httpServletResponse;
86 final private RepositoryRegistry repositoryRegistry;
90 private static final Logger log = LoggerFactory.getLogger( DefaultRepositoryGroupService.class );
91 private static final QueryHelper<org.apache.archiva.repository.RepositoryGroup> QUERY_HELPER = new QueryHelper<>( new String[]{"id"} );
94 QUERY_HELPER.addStringFilter( "id", org.apache.archiva.repository.RepositoryGroup::getId );
95 QUERY_HELPER.addNullsafeFieldComparator( "id", org.apache.archiva.repository.RepositoryGroup::getId );
99 public DefaultRepositoryGroupService( RepositoryRegistry repositoryRegistry, ConfigurationHandler configurationHandler ) {
100 this.repositoryRegistry = repositoryRegistry;
101 this.configurationHandler = configurationHandler;
105 public PagedResult<RepositoryGroup> getRepositoriesGroups( String searchTerm, Integer offset, Integer limit, List<String> orderBy, String order ) throws ArchivaRestServiceException
109 Predicate<org.apache.archiva.repository.RepositoryGroup> filter = QUERY_HELPER.getQueryFilter( searchTerm );
110 Comparator<org.apache.archiva.repository.RepositoryGroup> ordering = QUERY_HELPER.getComparator( orderBy, QUERY_HELPER.isAscending( order ) );
111 int totalCount = Math.toIntExact( repositoryRegistry.getRepositoryGroups( ).stream( ).filter( filter ).count( ) );
112 List<RepositoryGroup> result = repositoryRegistry.getRepositoryGroups( ).stream( ).filter( filter ).sorted( ordering ).skip( offset ).limit( limit ).map(
114 ).collect( Collectors.toList( ) );
115 return new PagedResult<>( totalCount, offset, limit, result );
117 catch ( ArithmeticException e )
119 log.error( "Could not convert total count: {}", e.getMessage( ) );
120 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.INVALID_RESULT_SET_ERROR ) );
126 public RepositoryGroup getRepositoryGroup( String repositoryGroupId ) throws ArchivaRestServiceException
128 if ( StringUtils.isEmpty( repositoryGroupId ) )
130 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
132 org.apache.archiva.repository.RepositoryGroup group = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
133 return RepositoryGroup.of( group );
136 private RepositoryGroupConfiguration toConfig( RepositoryGroup group )
138 RepositoryGroupConfiguration result = new RepositoryGroupConfiguration( );
139 result.setId( group.getId( ) );
140 result.setLocation( group.getLocation( ) );
141 result.setRepositories( group.getRepositories( ) );
142 MergeConfiguration mergeConfig = group.getMergeConfiguration( );
143 if (mergeConfig!=null)
145 result.setMergedIndexPath( mergeConfig.getMergedIndexPath( ) );
146 result.setMergedIndexTtl( mergeConfig.getMergedIndexTtlMinutes( ) );
147 result.setCronExpression( mergeConfig.getIndexMergeSchedule( ) );
153 public RepositoryGroup addRepositoryGroup( RepositoryGroup repositoryGroup ) throws ArchivaRestServiceException
155 final String groupId = repositoryGroup.getId( );
156 if ( StringUtils.isEmpty( groupId ) ) {
157 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_INVALID_ID, groupId ), 422 );
159 if (repositoryRegistry.hasRepositoryGroup( groupId )) {
160 httpServletResponse.setHeader( "Location", uriInfo.getAbsolutePathBuilder( ).path( groupId ).build( ).toString( ) );
161 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_ID_EXISTS, groupId ), 303 );
166 RepositoryGroupConfiguration configuration = toConfig( repositoryGroup );
167 CheckedResult<org.apache.archiva.repository.RepositoryGroup, Map<String, List<ValidationError>>> validationResult = repositoryRegistry.putRepositoryGroupAndValidate( configuration );
168 if ( validationResult.isValid( ) )
170 httpServletResponse.setStatus( 201 );
171 return RepositoryGroup.of( validationResult.getRepository() );
173 throw ValidationException.of( validationResult.getResult() );
176 catch ( RepositoryException e )
178 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_ADD_FAILED ) );
183 public RepositoryGroup updateRepositoryGroup( final String repositoryGroupId, final RepositoryGroup repositoryGroup ) throws ArchivaRestServiceException
185 if ( StringUtils.isEmpty( repositoryGroupId ) )
187 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
189 if ( !repositoryRegistry.hasRepositoryGroup( repositoryGroupId ) )
191 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND ), 404 );
193 repositoryGroup.setId( repositoryGroupId );
196 RepositoryGroupConfiguration configuration = toConfig( repositoryGroup );
197 CheckedResult<org.apache.archiva.repository.RepositoryGroup, Map<String, List<ValidationError>>> validationResult = repositoryRegistry.putRepositoryGroupAndValidate( configuration );
198 if ( validationResult.isValid( ) )
200 httpServletResponse.setStatus( 201 );
201 return RepositoryGroup.of( validationResult.getRepository() );
203 throw ValidationException.of( validationResult.getResult() );
206 catch ( RepositoryException e )
208 log.error( "Exception during repository group update: {}", e.getMessage( ), e );
209 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, e.getMessage() ) );
215 public Response deleteRepositoryGroup( String repositoryGroupId ) throws ArchivaRestServiceException
217 if ( StringUtils.isEmpty( repositoryGroupId ) )
219 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
223 org.apache.archiva.repository.RepositoryGroup group = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
225 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
227 repositoryRegistry.removeRepositoryGroup( group );
228 return Response.ok( ).build( );
230 catch ( RepositoryException e )
232 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_DELETE_FAILED ) );
237 public RepositoryGroup addRepositoryToGroup( String repositoryGroupId, String repositoryId ) throws ArchivaRestServiceException
239 if ( StringUtils.isEmpty( repositoryGroupId ) )
241 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
243 if ( StringUtils.isEmpty( repositoryId ) )
245 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_FOUND, "" ), 404 );
249 org.apache.archiva.repository.RepositoryGroup repositoryGroup = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
250 if (repositoryGroup==null) {
251 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
253 if (!(repositoryGroup instanceof EditableRepositoryGroup )) {
254 log.error( "This group instance is not editable: {}", repositoryGroupId );
255 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, "" ), 500 );
257 EditableRepositoryGroup editableRepositoryGroup = (EditableRepositoryGroup) repositoryGroup;
258 if ( editableRepositoryGroup.getRepositories().stream().anyMatch( repo -> repositoryId.equals(repo.getId())) )
260 log.info( "Repository {} is already member of group {}", repositoryId, repositoryGroupId );
261 return RepositoryGroup.of( editableRepositoryGroup );
263 org.apache.archiva.repository.ManagedRepository managedRepo = repositoryRegistry.getManagedRepository(repositoryId);
264 if (managedRepo==null) {
265 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_FOUND, "" ), 404 );
267 editableRepositoryGroup.addRepository( managedRepo );
268 org.apache.archiva.repository.RepositoryGroup newGroup = repositoryRegistry.putRepositoryGroup( editableRepositoryGroup );
269 return RepositoryGroup.of( newGroup );
271 catch ( RepositoryException e )
273 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, e.getMessage() ), 500 );
278 public RepositoryGroup deleteRepositoryFromGroup( final String repositoryGroupId, final String repositoryId ) throws org.apache.archiva.rest.api.services.v2.ArchivaRestServiceException
280 if ( StringUtils.isEmpty( repositoryGroupId ) )
282 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, repositoryGroupId ), 404 );
284 if ( StringUtils.isEmpty( repositoryId ) )
286 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_FOUND, repositoryId ), 404 );
290 org.apache.archiva.repository.RepositoryGroup repositoryGroup = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
291 if (repositoryGroup==null) {
292 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
294 if (repositoryGroup.getRepositories().stream().noneMatch( r -> repositoryId.equals( r.getId() ) )) {
295 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_FOUND, repositoryId ), 404 );
297 if (!(repositoryGroup instanceof EditableRepositoryGroup)) {
298 log.error( "This group instance is not editable: {}", repositoryGroupId );
299 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, "" ), 500 );
301 EditableRepositoryGroup editableRepositoryGroup = (EditableRepositoryGroup) repositoryGroup;
302 editableRepositoryGroup.removeRepository( repositoryId );
303 org.apache.archiva.repository.RepositoryGroup newGroup = repositoryRegistry.putRepositoryGroup( editableRepositoryGroup );
304 return RepositoryGroup.of( newGroup );
306 catch ( RepositoryException e )
308 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, e.getMessage() ), 500 );