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.admin.model.AuditInformation;
38 import org.apache.archiva.admin.model.EntityExistsException;
39 import org.apache.archiva.admin.model.EntityNotFoundException;
40 import org.apache.archiva.admin.model.RepositoryAdminException;
41 import org.apache.archiva.admin.model.group.RepositoryGroupAdmin;
42 import org.apache.archiva.components.rest.model.PagedResult;
43 import org.apache.archiva.components.rest.util.QueryHelper;
44 import org.apache.archiva.redback.rest.services.RedbackAuthenticationThreadLocal;
45 import org.apache.archiva.redback.rest.services.RedbackRequestInformation;
46 import org.apache.archiva.redback.users.User;
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.commons.lang3.StringUtils;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55 import org.springframework.stereotype.Service;
57 import javax.inject.Inject;
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.ArrayList;
63 import java.util.Comparator;
64 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
79 HttpServletResponse httpServletResponse;
85 private RepositoryGroupAdmin repositoryGroupAdmin;
87 private static final Logger log = LoggerFactory.getLogger( DefaultRepositoryGroupService.class );
88 private static final QueryHelper<org.apache.archiva.admin.model.beans.RepositoryGroup> QUERY_HELPER = new QueryHelper<>( new String[]{"id"} );
91 QUERY_HELPER.addStringFilter( "id", org.apache.archiva.admin.model.beans.RepositoryGroup::getId );
92 QUERY_HELPER.addNullsafeFieldComparator( "id", org.apache.archiva.admin.model.beans.RepositoryGroup::getId );
96 protected AuditInformation getAuditInformation( )
98 RedbackRequestInformation redbackRequestInformation = RedbackAuthenticationThreadLocal.get( );
99 User user = redbackRequestInformation == null ? null : redbackRequestInformation.getUser( );
100 String remoteAddr = redbackRequestInformation == null ? null : redbackRequestInformation.getRemoteAddr( );
101 return new AuditInformation( user, remoteAddr );
105 public PagedResult<RepositoryGroup> getRepositoriesGroups( String searchTerm, Integer offset, Integer limit, List<String> orderBy, String order ) throws ArchivaRestServiceException
109 Predicate<org.apache.archiva.admin.model.beans.RepositoryGroup> filter = QUERY_HELPER.getQueryFilter( searchTerm );
110 Comparator<org.apache.archiva.admin.model.beans.RepositoryGroup> ordering = QUERY_HELPER.getComparator( orderBy, QUERY_HELPER.isAscending( order ) );
111 int totalCount = Math.toIntExact( repositoryGroupAdmin.getRepositoriesGroups( ).stream( ).filter( filter ).count( ) );
112 List<RepositoryGroup> result = repositoryGroupAdmin.getRepositoriesGroups( ).stream( ).filter( filter ).sorted( ordering ).skip( offset ).limit( limit ).map(
114 ).collect( Collectors.toList( ) );
115 return new PagedResult<>( totalCount, offset, limit, result );
117 catch ( RepositoryAdminException e )
119 log.error( "Repository admin error: {}", e.getMessage( ), e );
120 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_ADMIN_ERROR, e.getMessage( ) ) );
122 catch ( ArithmeticException e )
124 log.error( "Could not convert total count: {}", e.getMessage( ) );
125 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.INVALID_RESULT_SET_ERROR ) );
131 public RepositoryGroup getRepositoryGroup( String repositoryGroupId ) throws ArchivaRestServiceException
133 if ( StringUtils.isEmpty( repositoryGroupId ) )
135 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
139 org.apache.archiva.admin.model.beans.RepositoryGroup group = repositoryGroupAdmin.getRepositoryGroup( repositoryGroupId );
140 return RepositoryGroup.of( group );
142 catch ( EntityNotFoundException e )
144 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, repositoryGroupId ), 404 );
146 catch ( RepositoryAdminException e )
148 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_ADMIN_ERROR, e.getMessage( ) ) );
152 private org.apache.archiva.admin.model.beans.RepositoryGroup toModel( RepositoryGroup group )
154 org.apache.archiva.admin.model.beans.RepositoryGroup result = new org.apache.archiva.admin.model.beans.RepositoryGroup( );
155 result.setId( group.getId( ) );
156 result.setLocation( group.getLocation( ) );
157 result.setRepositories( new ArrayList<>( group.getRepositories( ) ) );
158 result.setMergedIndexPath( group.getMergeConfiguration( ).getMergedIndexPath( ) );
159 result.setMergedIndexTtl( group.getMergeConfiguration( ).getMergedIndexTtlMinutes( ) );
160 result.setCronExpression( group.getMergeConfiguration( ).getIndexMergeSchedule( ) );
165 public RepositoryGroup addRepositoryGroup( RepositoryGroup repositoryGroup ) throws ArchivaRestServiceException
169 Boolean result = repositoryGroupAdmin.addRepositoryGroup( toModel( repositoryGroup ), getAuditInformation( ) );
172 org.apache.archiva.admin.model.beans.RepositoryGroup newGroup = repositoryGroupAdmin.getRepositoryGroup( repositoryGroup.getId( ) );
173 if ( newGroup != null )
175 return RepositoryGroup.of( newGroup );
179 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_ADD_FAILED ) );
184 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_ADD_FAILED ) );
187 catch ( EntityExistsException e )
189 httpServletResponse.setHeader( "Location", uriInfo.getAbsolutePathBuilder( ).path( repositoryGroup.getId( ) ).build( ).toString( ) );
190 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_EXIST, repositoryGroup.getId( ) ), 303 );
192 catch ( RepositoryAdminException e )
194 return handleAdminException( e );
198 private RepositoryGroup handleAdminException( RepositoryAdminException e ) throws ArchivaRestServiceException
200 log.error( "Repository admin error: {}", e.getMessage( ), e );
201 if ( e.keyExists( ) )
203 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.PREFIX + e.getKey( ), e.getParameters( ) ) );
207 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_ADMIN_ERROR, e.getMessage( ) ) );
212 public RepositoryGroup updateRepositoryGroup( String repositoryGroupId, RepositoryGroup repositoryGroup ) throws ArchivaRestServiceException
214 if ( StringUtils.isEmpty( repositoryGroupId ) )
216 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
218 org.apache.archiva.admin.model.beans.RepositoryGroup updateGroup = toModel( repositoryGroup );
221 org.apache.archiva.admin.model.beans.RepositoryGroup originGroup = repositoryGroupAdmin.getRepositoryGroup( repositoryGroupId );
222 if ( StringUtils.isEmpty( updateGroup.getId( ) ) )
224 updateGroup.setId( repositoryGroupId );
226 if ( StringUtils.isEmpty( updateGroup.getLocation( ) ) )
228 updateGroup.setLocation( originGroup.getLocation( ) );
230 if ( StringUtils.isEmpty( updateGroup.getMergedIndexPath( ) ) )
232 updateGroup.setMergedIndexPath( originGroup.getMergedIndexPath( ) );
234 if ( updateGroup.getCronExpression( ) == null )
236 updateGroup.setCronExpression( originGroup.getCronExpression( ) );
238 if ( updateGroup.getRepositories( ) == null || updateGroup.getRepositories( ).size( ) == 0 )
240 updateGroup.setRepositories( originGroup.getRepositories( ) );
242 if ( updateGroup.getMergedIndexTtl( ) <= 0 )
244 updateGroup.setMergedIndexTtl( originGroup.getMergedIndexTtl( ) );
246 repositoryGroupAdmin.updateRepositoryGroup( updateGroup, getAuditInformation( ) );
247 return RepositoryGroup.of( repositoryGroupAdmin.getRepositoryGroup( repositoryGroupId ) );
249 catch ( EntityNotFoundException e )
251 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, repositoryGroupId ), 404 );
253 catch ( RepositoryAdminException e )
255 return handleAdminException( e );
260 public Response deleteRepositoryGroup( String repositoryGroupId ) throws ArchivaRestServiceException
262 if ( StringUtils.isEmpty( repositoryGroupId ) )
264 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
268 Boolean deleted = repositoryGroupAdmin.deleteRepositoryGroup( repositoryGroupId, getAuditInformation( ) );
271 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_DELETE_FAILED ) );
273 return Response.ok( ).build( );
275 catch ( EntityNotFoundException e )
277 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, repositoryGroupId ), 404 );
279 catch ( RepositoryAdminException e )
281 handleAdminException( e );
288 public RepositoryGroup addRepositoryToGroup( String repositoryGroupId, String repositoryId ) throws ArchivaRestServiceException
290 if ( StringUtils.isEmpty( repositoryGroupId ) )
292 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
294 if ( StringUtils.isEmpty( repositoryId ) )
296 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_FOUND, "" ), 404 );
300 repositoryGroupAdmin.addRepositoryToGroup( repositoryGroupId, repositoryId, getAuditInformation( ) );
301 return RepositoryGroup.of( repositoryGroupAdmin.getRepositoryGroup( repositoryGroupId ) );
303 catch ( EntityNotFoundException e )
305 return handleNotFoundException( repositoryGroupId, repositoryId, e );
307 catch ( EntityExistsException e )
309 // This is thrown, if the repositoryId is already assigned to the group. We ignore this for the PUT action (nothing to do).
312 return RepositoryGroup.of( repositoryGroupAdmin.getRepositoryGroup( repositoryGroupId ) );
314 catch ( RepositoryAdminException repositoryAdminException )
316 return handleAdminException( e );
319 catch ( RepositoryAdminException e )
321 return handleAdminException( e );
326 public RepositoryGroup deleteRepositoryFromGroup( String repositoryGroupId, String repositoryId ) throws org.apache.archiva.rest.api.services.v2.ArchivaRestServiceException
328 if ( StringUtils.isEmpty( repositoryGroupId ) )
330 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
332 if ( StringUtils.isEmpty( repositoryId ) )
334 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_FOUND, "" ), 404 );
338 repositoryGroupAdmin.deleteRepositoryFromGroup( repositoryGroupId, repositoryId, getAuditInformation( ) );
339 return RepositoryGroup.of( repositoryGroupAdmin.getRepositoryGroup( repositoryGroupId ) );
341 catch ( EntityNotFoundException e )
343 return handleNotFoundException( repositoryGroupId, repositoryId, e );
345 catch ( RepositoryAdminException e )
347 return handleAdminException( e );
351 protected RepositoryGroup handleNotFoundException( String repositoryGroupId, String repositoryId, EntityNotFoundException e ) throws ArchivaRestServiceException
353 if ( e.getParameters( ).length > 0 )
355 if ( repositoryGroupId.equals( e.getParameters( )[0] ) )
357 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, repositoryGroupId ), 404 );
359 else if ( repositoryId.equals( e.getParameters( )[0] ) )
361 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_FOUND, repositoryGroupId ), 404 );
364 log.warn( "Entity not found but neither group nor repo set in exception" );
365 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, repositoryGroupId ), 404 );