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
19 import org.apache.archiva.admin.model.AuditInformation;
20 import org.apache.archiva.admin.model.EntityExistsException;
21 import org.apache.archiva.admin.model.EntityNotFoundException;
22 import org.apache.archiva.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.group.RepositoryGroupAdmin;
24 import org.apache.archiva.components.rest.model.PagedResult;
25 import org.apache.archiva.components.rest.util.QueryHelper;
26 import org.apache.archiva.redback.rest.services.RedbackAuthenticationThreadLocal;
27 import org.apache.archiva.redback.rest.services.RedbackRequestInformation;
28 import org.apache.archiva.redback.users.User;
29 import org.apache.archiva.rest.api.model.v2.RepositoryGroup;
30 import org.apache.archiva.rest.api.services.v2.ArchivaRestServiceException;
31 import org.apache.archiva.rest.api.services.v2.ErrorMessage;
32 import org.apache.archiva.rest.api.services.v2.RepositoryGroupService;
33 import org.apache.commons.lang3.StringUtils;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.stereotype.Service;
38 import javax.inject.Inject;
39 import javax.servlet.http.HttpServletResponse;
40 import javax.ws.rs.core.Context;
41 import javax.ws.rs.core.Response;
42 import javax.ws.rs.core.UriInfo;
43 import java.util.ArrayList;
44 import java.util.Comparator;
45 import java.util.List;
46 import java.util.function.Predicate;
47 import java.util.stream.Collectors;
50 * REST V2 Implementation for repository groups.
52 * @author Martin Stockhammer <martin_s@apache.org>
53 * @see RepositoryGroupService
56 @Service("v2.repositoryGroupService#rest")
57 public class DefaultRepositoryGroupService implements RepositoryGroupService
60 HttpServletResponse httpServletResponse;
65 private static final Logger log = LoggerFactory.getLogger( DefaultRepositoryGroupService.class );
67 private static final QueryHelper<org.apache.archiva.admin.model.beans.RepositoryGroup> QUERY_HELPER = new QueryHelper<>( new String[]{"id"} );
70 private RepositoryGroupAdmin repositoryGroupAdmin;
75 QUERY_HELPER.addStringFilter( "id", org.apache.archiva.admin.model.beans.RepositoryGroup::getId );
76 QUERY_HELPER.addNullsafeFieldComparator( "id", org.apache.archiva.admin.model.beans.RepositoryGroup::getId );
80 protected AuditInformation getAuditInformation( )
82 RedbackRequestInformation redbackRequestInformation = RedbackAuthenticationThreadLocal.get( );
83 User user = redbackRequestInformation == null ? null : redbackRequestInformation.getUser( );
84 String remoteAddr = redbackRequestInformation == null ? null : redbackRequestInformation.getRemoteAddr( );
85 return new AuditInformation( user, remoteAddr );
89 public PagedResult<RepositoryGroup> getRepositoriesGroups( String searchTerm, Integer offset, Integer limit, List<String> orderBy, String order ) throws ArchivaRestServiceException
93 Predicate<org.apache.archiva.admin.model.beans.RepositoryGroup> filter = QUERY_HELPER.getQueryFilter( searchTerm );
94 Comparator<org.apache.archiva.admin.model.beans.RepositoryGroup> ordering = QUERY_HELPER.getComparator( orderBy, QUERY_HELPER.isAscending( order ) );
95 int totalCount = Math.toIntExact( repositoryGroupAdmin.getRepositoriesGroups( ).stream( ).filter( filter ).count( ) );
96 List<RepositoryGroup> result = repositoryGroupAdmin.getRepositoriesGroups( ).stream( ).filter( filter ).sorted( ordering ).skip( offset ).limit( limit ).map(
98 ).collect( Collectors.toList( ) );
99 return new PagedResult<>( totalCount, offset, limit, result );
101 catch ( RepositoryAdminException e )
103 log.error( "Repository admin error: {}", e.getMessage( ), e );
104 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_ADMIN_ERROR, e.getMessage( ) ) );
106 catch ( ArithmeticException e )
108 log.error( "Could not convert total count: {}", e.getMessage( ) );
109 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.INVALID_RESULT_SET_ERROR ) );
115 public RepositoryGroup getRepositoryGroup( String repositoryGroupId ) throws ArchivaRestServiceException
117 if ( StringUtils.isEmpty( repositoryGroupId ) )
119 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_EXIST, "" ), 404 );
123 org.apache.archiva.admin.model.beans.RepositoryGroup group = repositoryGroupAdmin.getRepositoryGroup( repositoryGroupId );
124 return RepositoryGroup.of( group );
126 catch ( EntityNotFoundException e )
128 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_EXIST, repositoryGroupId ), 404 );
130 catch ( RepositoryAdminException e )
132 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_ADMIN_ERROR, e.getMessage( ) ) );
136 private org.apache.archiva.admin.model.beans.RepositoryGroup toModel( RepositoryGroup group )
138 org.apache.archiva.admin.model.beans.RepositoryGroup result = new org.apache.archiva.admin.model.beans.RepositoryGroup( );
139 result.setId( group.getId( ) );
140 result.setLocation( group.getLocation( ) );
141 result.setRepositories( new ArrayList<>( group.getRepositories( ) ) );
142 result.setMergedIndexPath( group.getMergeConfiguration( ).getMergedIndexPath( ) );
143 result.setMergedIndexTtl( group.getMergeConfiguration( ).getMergedIndexTtlMinutes( ) );
144 result.setCronExpression( group.getMergeConfiguration( ).getIndexMergeSchedule( ) );
149 public RepositoryGroup addRepositoryGroup( RepositoryGroup repositoryGroup ) throws ArchivaRestServiceException
153 Boolean result = repositoryGroupAdmin.addRepositoryGroup( toModel( repositoryGroup ), getAuditInformation( ) );
156 org.apache.archiva.admin.model.beans.RepositoryGroup newGroup = repositoryGroupAdmin.getRepositoryGroup( repositoryGroup.getId( ) );
157 if ( newGroup != null )
159 return RepositoryGroup.of( newGroup );
163 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_ADD_FAILED ) );
168 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_ADD_FAILED ) );
171 catch ( EntityExistsException e )
173 httpServletResponse.setHeader( "Location", uriInfo.getAbsolutePathBuilder( ).path( repositoryGroup.getId( ) ).build( ).toString( ) );
174 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_EXIST, repositoryGroup.getId( ) ), 303 );
176 catch ( RepositoryAdminException e )
178 return handleAdminException( e );
182 private RepositoryGroup handleAdminException( RepositoryAdminException e ) throws ArchivaRestServiceException
184 log.error( "Repository admin error: {}", e.getMessage( ), e );
185 if ( e.keyExists( ) )
187 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.PREFIX + e.getKey( ), e.getParameters( ) ) );
191 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_ADMIN_ERROR, e.getMessage( ) ) );
196 public RepositoryGroup updateRepositoryGroup( String repositoryGroupId, RepositoryGroup repositoryGroup ) throws ArchivaRestServiceException
198 if ( StringUtils.isEmpty( repositoryGroupId ) )
200 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_EXIST, "" ), 404 );
202 org.apache.archiva.admin.model.beans.RepositoryGroup updateGroup = toModel( repositoryGroup );
205 org.apache.archiva.admin.model.beans.RepositoryGroup originGroup = repositoryGroupAdmin.getRepositoryGroup( repositoryGroupId );
206 if ( StringUtils.isEmpty( updateGroup.getId( ) ) )
208 updateGroup.setId( repositoryGroupId );
210 if ( StringUtils.isEmpty( updateGroup.getLocation( ) ) )
212 updateGroup.setLocation( originGroup.getLocation( ) );
214 if ( StringUtils.isEmpty( updateGroup.getMergedIndexPath( ) ) )
216 updateGroup.setMergedIndexPath( originGroup.getMergedIndexPath( ) );
218 if ( updateGroup.getCronExpression( ) == null )
220 updateGroup.setCronExpression( originGroup.getCronExpression( ) );
222 if ( updateGroup.getRepositories( ) == null || updateGroup.getRepositories( ).size( ) == 0 )
224 updateGroup.setRepositories( originGroup.getRepositories( ) );
226 if ( updateGroup.getMergedIndexTtl( ) <= 0 )
228 updateGroup.setMergedIndexTtl( originGroup.getMergedIndexTtl( ) );
230 repositoryGroupAdmin.updateRepositoryGroup( updateGroup, getAuditInformation( ) );
231 return RepositoryGroup.of( repositoryGroupAdmin.getRepositoryGroup( repositoryGroupId ) );
233 catch ( EntityNotFoundException e )
235 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_EXIST, repositoryGroupId ), 404 );
237 catch ( RepositoryAdminException e )
239 return handleAdminException( e );
244 public Response deleteRepositoryGroup( String repositoryGroupId ) throws ArchivaRestServiceException
246 if ( StringUtils.isEmpty( repositoryGroupId ) )
248 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_EXIST, "" ), 404 );
252 Boolean deleted = repositoryGroupAdmin.deleteRepositoryGroup( repositoryGroupId, getAuditInformation( ) );
255 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_DELETE_FAILED ) );
257 return Response.ok( ).build( );
259 catch ( EntityNotFoundException e )
261 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_EXIST, repositoryGroupId ), 404 );
263 catch ( RepositoryAdminException e )
265 handleAdminException( e );
272 public RepositoryGroup addRepositoryToGroup( String repositoryGroupId, String repositoryId ) throws ArchivaRestServiceException
274 if ( StringUtils.isEmpty( repositoryGroupId ) )
276 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_EXIST, "" ), 404 );
278 if ( StringUtils.isEmpty( repositoryId ) )
280 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_EXIST, "" ), 404 );
284 repositoryGroupAdmin.addRepositoryToGroup( repositoryGroupId, repositoryId, getAuditInformation( ) );
285 return RepositoryGroup.of( repositoryGroupAdmin.getRepositoryGroup( repositoryGroupId ) );
287 catch ( EntityNotFoundException e )
289 return handleNotFoundException( repositoryGroupId, repositoryId, e );
291 catch ( EntityExistsException e )
293 // This is thrown, if the repositoryId is already assigned to the group. We ignore this for the PUT action (nothing to do).
296 return RepositoryGroup.of( repositoryGroupAdmin.getRepositoryGroup( repositoryGroupId ) );
298 catch ( RepositoryAdminException repositoryAdminException )
300 return handleAdminException( e );
303 catch ( RepositoryAdminException e )
305 return handleAdminException( e );
310 public RepositoryGroup deleteRepositoryFromGroup( String repositoryGroupId, String repositoryId ) throws org.apache.archiva.rest.api.services.v2.ArchivaRestServiceException
312 if ( StringUtils.isEmpty( repositoryGroupId ) )
314 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_EXIST, "" ), 404 );
316 if ( StringUtils.isEmpty( repositoryId ) )
318 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_EXIST, "" ), 404 );
322 repositoryGroupAdmin.deleteRepositoryFromGroup( repositoryGroupId, repositoryId, getAuditInformation( ) );
323 return RepositoryGroup.of( repositoryGroupAdmin.getRepositoryGroup( repositoryGroupId ) );
325 catch ( EntityNotFoundException e )
327 return handleNotFoundException( repositoryGroupId, repositoryId, e );
329 catch ( RepositoryAdminException e )
331 return handleAdminException( e );
335 protected RepositoryGroup handleNotFoundException( String repositoryGroupId, String repositoryId, EntityNotFoundException e ) throws ArchivaRestServiceException
337 if ( e.getParameters( ).length > 0 )
339 if ( repositoryGroupId.equals( e.getParameters( )[0] ) )
341 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_EXIST, repositoryGroupId ), 404 );
343 else if ( repositoryId.equals( e.getParameters( )[0] ) )
345 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_EXIST, repositoryGroupId ), 404 );
348 log.warn( "Entity not found but neither group nor repo set in exception" );
349 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_EXIST, repositoryGroupId ), 404 );