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.EditableRepositoryGroup;
41 import org.apache.archiva.repository.RepositoryException;
42 import org.apache.archiva.repository.RepositoryRegistry;
43 import org.apache.archiva.repository.base.ConfigurationHandler;
44 import org.apache.archiva.repository.validation.CheckedResult;
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;
89 private static final Logger log = LoggerFactory.getLogger( DefaultRepositoryGroupService.class );
90 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 )
101 this.repositoryRegistry = repositoryRegistry;
102 this.configurationHandler = configurationHandler;
106 public PagedResult<RepositoryGroup> getRepositoriesGroups( String searchTerm, Integer offset, Integer limit, List<String> orderBy, String order ) throws ArchivaRestServiceException
110 Predicate<org.apache.archiva.repository.RepositoryGroup> filter = QUERY_HELPER.getQueryFilter( searchTerm );
111 Comparator<org.apache.archiva.repository.RepositoryGroup> ordering = QUERY_HELPER.getComparator( orderBy, QUERY_HELPER.isAscending( order ) );
112 int totalCount = Math.toIntExact( repositoryRegistry.getRepositoryGroups( ).stream( ).filter( filter ).count( ) );
113 List<RepositoryGroup> result = repositoryRegistry.getRepositoryGroups( ).stream( ).filter( filter ).sorted( ordering ).skip( offset ).limit( limit ).map(
115 ).collect( Collectors.toList( ) );
116 return new PagedResult<>( totalCount, offset, limit, result );
118 catch ( ArithmeticException e )
120 log.error( "Could not convert total count: {}", e.getMessage( ) );
121 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.INVALID_RESULT_SET_ERROR ) );
127 public RepositoryGroup getRepositoryGroup( String repositoryGroupId ) throws ArchivaRestServiceException
129 if ( StringUtils.isEmpty( repositoryGroupId ) )
131 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
133 org.apache.archiva.repository.RepositoryGroup group = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
134 return RepositoryGroup.of( group );
137 private RepositoryGroupConfiguration toConfig( RepositoryGroup group )
139 RepositoryGroupConfiguration result = new RepositoryGroupConfiguration( );
140 result.setId( group.getId( ) );
141 result.setName( group.getName() );
142 result.setLocation( group.getLocation( ) );
143 result.setRepositories( group.getRepositories( ) );
144 MergeConfiguration mergeConfig = group.getMergeConfiguration( );
145 if ( mergeConfig != null )
147 result.setMergedIndexPath( mergeConfig.getMergedIndexPath( ) );
148 result.setMergedIndexTtl( mergeConfig.getMergedIndexTtlMinutes( ) );
149 result.setCronExpression( mergeConfig.getIndexMergeSchedule( ) );
155 public RepositoryGroup addRepositoryGroup( RepositoryGroup repositoryGroup ) throws ArchivaRestServiceException
157 final String groupId = repositoryGroup.getId( );
158 if ( StringUtils.isEmpty( groupId ) )
160 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_INVALID_ID, groupId ), 422 );
162 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( ) );
179 throw ValidationException.of( validationResult.getResult( ) );
182 catch ( RepositoryException e )
184 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_ADD_FAILED ) );
189 public RepositoryGroup updateRepositoryGroup( final String repositoryGroupId, final RepositoryGroup repositoryGroup ) throws ArchivaRestServiceException
191 if ( StringUtils.isEmpty( repositoryGroupId ) )
193 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
195 if ( !repositoryRegistry.hasRepositoryGroup( repositoryGroupId ) )
197 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND ), 404 );
199 repositoryGroup.setId( repositoryGroupId );
202 RepositoryGroupConfiguration configuration = toConfig( repositoryGroup );
203 CheckedResult<org.apache.archiva.repository.RepositoryGroup, Map<String, List<ValidationError>>> validationResult = repositoryRegistry.putRepositoryGroupAndValidate( configuration );
204 if ( validationResult.isValid( ) )
206 httpServletResponse.setStatus( 201 );
207 return RepositoryGroup.of( validationResult.getRepository( ) );
211 throw ValidationException.of( validationResult.getResult( ) );
214 catch ( RepositoryException e )
216 log.error( "Exception during repository group update: {}", e.getMessage( ), e );
217 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, e.getMessage( ) ) );
223 public Response deleteRepositoryGroup( String repositoryGroupId ) throws ArchivaRestServiceException
225 if ( StringUtils.isEmpty( repositoryGroupId ) )
227 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
231 org.apache.archiva.repository.RepositoryGroup group = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
234 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
236 repositoryRegistry.removeRepositoryGroup( group );
237 return Response.ok( ).build( );
239 catch ( RepositoryException e )
241 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_DELETE_FAILED ) );
246 public RepositoryGroup addRepositoryToGroup( String repositoryGroupId, String repositoryId ) throws ArchivaRestServiceException
248 if ( StringUtils.isEmpty( repositoryGroupId ) )
250 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
252 if ( StringUtils.isEmpty( repositoryId ) )
254 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_FOUND, "" ), 404 );
258 org.apache.archiva.repository.RepositoryGroup repositoryGroup = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
259 if ( repositoryGroup == null )
261 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
263 if ( !( repositoryGroup instanceof EditableRepositoryGroup ) )
265 log.error( "This group instance is not editable: {}", repositoryGroupId );
266 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, "" ), 500 );
268 EditableRepositoryGroup editableRepositoryGroup = (EditableRepositoryGroup) repositoryGroup;
269 if ( editableRepositoryGroup.getRepositories( ).stream( ).anyMatch( repo -> repositoryId.equals( repo.getId( ) ) ) )
271 log.info( "Repository {} is already member of group {}", repositoryId, repositoryGroupId );
272 return RepositoryGroup.of( editableRepositoryGroup );
274 org.apache.archiva.repository.ManagedRepository managedRepo = repositoryRegistry.getManagedRepository( repositoryId );
275 if ( managedRepo == null )
277 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_FOUND, "" ), 404 );
279 editableRepositoryGroup.addRepository( managedRepo );
280 org.apache.archiva.repository.RepositoryGroup newGroup = repositoryRegistry.putRepositoryGroup( editableRepositoryGroup );
281 return RepositoryGroup.of( newGroup );
283 catch ( RepositoryException e )
285 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, e.getMessage( ) ), 500 );
290 public RepositoryGroup deleteRepositoryFromGroup( final String repositoryGroupId, final String repositoryId ) throws org.apache.archiva.rest.api.services.v2.ArchivaRestServiceException
292 if ( StringUtils.isEmpty( repositoryGroupId ) )
294 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, repositoryGroupId ), 404 );
296 if ( StringUtils.isEmpty( repositoryId ) )
298 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_FOUND, repositoryId ), 404 );
302 org.apache.archiva.repository.RepositoryGroup repositoryGroup = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
303 if ( repositoryGroup == null )
305 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
307 if ( repositoryGroup.getRepositories( ).stream( ).noneMatch( r -> repositoryId.equals( r.getId( ) ) ) )
309 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_FOUND, repositoryId ), 404 );
311 if ( !( repositoryGroup instanceof EditableRepositoryGroup ) )
313 log.error( "This group instance is not editable: {}", repositoryGroupId );
314 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, "" ), 500 );
316 EditableRepositoryGroup editableRepositoryGroup = (EditableRepositoryGroup) repositoryGroup;
317 editableRepositoryGroup.removeRepository( repositoryId );
318 org.apache.archiva.repository.RepositoryGroup newGroup = repositoryRegistry.putRepositoryGroup( editableRepositoryGroup );
319 return RepositoryGroup.of( newGroup );
321 catch ( RepositoryException e )
323 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, e.getMessage( ) ), 500 );