]> source.dussan.org Git - archiva.git/blob
e348cdcab090ee3f3b40898e6fecff59b04be0f2
[archiva.git] /
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
9  *
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
16  * under the License.
17  */
18
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;
37
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;
48
49 /**
50  * REST V2 Implementation for repository groups.
51  *
52  * @author Martin Stockhammer <martin_s@apache.org>
53  * @see RepositoryGroupService
54  * @since 3.0
55  */
56 @Service("v2.repositoryGroupService#rest")
57 public class DefaultRepositoryGroupService implements RepositoryGroupService
58 {
59     @Context
60     HttpServletResponse httpServletResponse;
61
62     @Context
63     UriInfo uriInfo;
64
65     private static final Logger log = LoggerFactory.getLogger( DefaultRepositoryGroupService.class );
66
67     private static final QueryHelper<org.apache.archiva.admin.model.beans.RepositoryGroup> QUERY_HELPER = new QueryHelper<>( new String[]{"id"} );
68
69     @Inject
70     private RepositoryGroupAdmin repositoryGroupAdmin;
71
72
73     static
74     {
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 );
77     }
78
79
80     protected AuditInformation getAuditInformation( )
81     {
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 );
86     }
87
88     @Override
89     public PagedResult<RepositoryGroup> getRepositoriesGroups( String searchTerm, Integer offset, Integer limit, List<String> orderBy, String order ) throws ArchivaRestServiceException
90     {
91         try
92         {
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(
97                 RepositoryGroup::of
98             ).collect( Collectors.toList( ) );
99             return new PagedResult<>( totalCount, offset, limit, result );
100         }
101         catch ( RepositoryAdminException e )
102         {
103             log.error( "Repository admin error: {}", e.getMessage( ), e );
104             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_ADMIN_ERROR, e.getMessage( ) ) );
105         }
106         catch ( ArithmeticException e )
107         {
108             log.error( "Could not convert total count: {}", e.getMessage( ) );
109             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.INVALID_RESULT_SET_ERROR ) );
110         }
111
112     }
113
114     @Override
115     public RepositoryGroup getRepositoryGroup( String repositoryGroupId ) throws ArchivaRestServiceException
116     {
117         if ( StringUtils.isEmpty( repositoryGroupId ) )
118         {
119             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_EXIST, "" ), 404 );
120         }
121         try
122         {
123             org.apache.archiva.admin.model.beans.RepositoryGroup group = repositoryGroupAdmin.getRepositoryGroup( repositoryGroupId );
124             return RepositoryGroup.of( group );
125         }
126         catch ( EntityNotFoundException e )
127         {
128             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_EXIST, repositoryGroupId ), 404 );
129         }
130         catch ( RepositoryAdminException e )
131         {
132             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_ADMIN_ERROR, e.getMessage( ) ) );
133         }
134     }
135
136     private org.apache.archiva.admin.model.beans.RepositoryGroup toModel( RepositoryGroup group )
137     {
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( ) );
145         return result;
146     }
147
148     @Override
149     public RepositoryGroup addRepositoryGroup( RepositoryGroup repositoryGroup ) throws ArchivaRestServiceException
150     {
151         try
152         {
153             Boolean result = repositoryGroupAdmin.addRepositoryGroup( toModel( repositoryGroup ), getAuditInformation( ) );
154             if ( result )
155             {
156                 org.apache.archiva.admin.model.beans.RepositoryGroup newGroup = repositoryGroupAdmin.getRepositoryGroup( repositoryGroup.getId( ) );
157                 if ( newGroup != null )
158                 {
159                     return RepositoryGroup.of( newGroup );
160                 }
161                 else
162                 {
163                     throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_ADD_FAILED ) );
164                 }
165             }
166             else
167             {
168                 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_ADD_FAILED ) );
169             }
170         }
171         catch ( EntityExistsException e )
172         {
173             httpServletResponse.setHeader( "Location", uriInfo.getAbsolutePathBuilder( ).path( repositoryGroup.getId( ) ).build( ).toString( ) );
174             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_EXIST, repositoryGroup.getId( ) ), 303 );
175         }
176         catch ( RepositoryAdminException e )
177         {
178             return handleAdminException( e );
179         }
180     }
181
182     private RepositoryGroup handleAdminException( RepositoryAdminException e ) throws ArchivaRestServiceException
183     {
184         log.error( "Repository admin error: {}", e.getMessage( ), e );
185         if ( e.keyExists( ) )
186         {
187             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.PREFIX + e.getKey( ), e.getParameters( ) ) );
188         }
189         else
190         {
191             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_ADMIN_ERROR, e.getMessage( ) ) );
192         }
193     }
194
195     @Override
196     public RepositoryGroup updateRepositoryGroup( String repositoryGroupId, RepositoryGroup repositoryGroup ) throws ArchivaRestServiceException
197     {
198         if ( StringUtils.isEmpty( repositoryGroupId ) )
199         {
200             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_EXIST, "" ), 404 );
201         }
202         org.apache.archiva.admin.model.beans.RepositoryGroup updateGroup = toModel( repositoryGroup );
203         try
204         {
205             org.apache.archiva.admin.model.beans.RepositoryGroup originGroup = repositoryGroupAdmin.getRepositoryGroup( repositoryGroupId );
206             if ( StringUtils.isEmpty( updateGroup.getId( ) ) )
207             {
208                 updateGroup.setId( repositoryGroupId );
209             }
210             if ( StringUtils.isEmpty( updateGroup.getLocation( ) ) )
211             {
212                 updateGroup.setLocation( originGroup.getLocation( ) );
213             }
214             if ( StringUtils.isEmpty( updateGroup.getMergedIndexPath( ) ) )
215             {
216                 updateGroup.setMergedIndexPath( originGroup.getMergedIndexPath( ) );
217             }
218             if ( updateGroup.getCronExpression( ) == null )
219             {
220                 updateGroup.setCronExpression( originGroup.getCronExpression( ) );
221             }
222             if ( updateGroup.getRepositories( ) == null || updateGroup.getRepositories( ).size( ) == 0 )
223             {
224                 updateGroup.setRepositories( originGroup.getRepositories( ) );
225             }
226             if ( updateGroup.getMergedIndexTtl( ) <= 0 )
227             {
228                 updateGroup.setMergedIndexTtl( originGroup.getMergedIndexTtl( ) );
229             }
230             repositoryGroupAdmin.updateRepositoryGroup( updateGroup, getAuditInformation( ) );
231             return RepositoryGroup.of( repositoryGroupAdmin.getRepositoryGroup( repositoryGroupId ) );
232         }
233         catch ( EntityNotFoundException e )
234         {
235             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_EXIST, repositoryGroupId ), 404 );
236         }
237         catch ( RepositoryAdminException e )
238         {
239             return handleAdminException( e );
240         }
241     }
242
243     @Override
244     public Response deleteRepositoryGroup( String repositoryGroupId ) throws ArchivaRestServiceException
245     {
246         if ( StringUtils.isEmpty( repositoryGroupId ) )
247         {
248             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_EXIST, "" ), 404 );
249         }
250         try
251         {
252             Boolean deleted = repositoryGroupAdmin.deleteRepositoryGroup( repositoryGroupId, getAuditInformation( ) );
253             if ( !deleted )
254             {
255                 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_DELETE_FAILED ) );
256             }
257             return Response.ok( ).build( );
258         }
259         catch ( EntityNotFoundException e )
260         {
261             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_EXIST, repositoryGroupId ), 404 );
262         }
263         catch ( RepositoryAdminException e )
264         {
265             handleAdminException( e );
266             // cannot happen:
267             return null;
268         }
269     }
270
271     @Override
272     public RepositoryGroup addRepositoryToGroup( String repositoryGroupId, String repositoryId ) throws ArchivaRestServiceException
273     {
274         if ( StringUtils.isEmpty( repositoryGroupId ) )
275         {
276             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_EXIST, "" ), 404 );
277         }
278         if ( StringUtils.isEmpty( repositoryId ) )
279         {
280             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_EXIST, "" ), 404 );
281         }
282         try
283         {
284             repositoryGroupAdmin.addRepositoryToGroup( repositoryGroupId, repositoryId, getAuditInformation( ) );
285             return RepositoryGroup.of( repositoryGroupAdmin.getRepositoryGroup( repositoryGroupId ) );
286         }
287         catch ( EntityNotFoundException e )
288         {
289             return handleNotFoundException( repositoryGroupId, repositoryId, e );
290         }
291         catch ( EntityExistsException e )
292         {
293             // This is thrown, if the repositoryId is already assigned to the group. We ignore this for the PUT action (nothing to do).
294             try
295             {
296                 return RepositoryGroup.of( repositoryGroupAdmin.getRepositoryGroup( repositoryGroupId ) );
297             }
298             catch ( RepositoryAdminException repositoryAdminException )
299             {
300                 return handleAdminException( e );
301             }
302         }
303         catch ( RepositoryAdminException e )
304         {
305             return handleAdminException( e );
306         }
307     }
308
309     @Override
310     public RepositoryGroup deleteRepositoryFromGroup( String repositoryGroupId, String repositoryId ) throws org.apache.archiva.rest.api.services.v2.ArchivaRestServiceException
311     {
312         if ( StringUtils.isEmpty( repositoryGroupId ) )
313         {
314             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_EXIST, "" ), 404 );
315         }
316         if ( StringUtils.isEmpty( repositoryId ) )
317         {
318             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_EXIST, "" ), 404 );
319         }
320         try
321         {
322             repositoryGroupAdmin.deleteRepositoryFromGroup( repositoryGroupId, repositoryId, getAuditInformation( ) );
323             return RepositoryGroup.of( repositoryGroupAdmin.getRepositoryGroup( repositoryGroupId ) );
324         }
325         catch ( EntityNotFoundException e )
326         {
327             return handleNotFoundException( repositoryGroupId, repositoryId, e );
328         }
329         catch ( RepositoryAdminException e )
330         {
331             return handleAdminException( e );
332         }
333     }
334
335     protected RepositoryGroup handleNotFoundException( String repositoryGroupId, String repositoryId, EntityNotFoundException e ) throws ArchivaRestServiceException
336     {
337         if ( e.getParameters( ).length > 0 )
338         {
339             if ( repositoryGroupId.equals( e.getParameters( )[0] ) )
340             {
341                 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_EXIST, repositoryGroupId ), 404 );
342             }
343             else if ( repositoryId.equals( e.getParameters( )[0] ) )
344             {
345                 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_EXIST, repositoryGroupId ), 404 );
346             }
347         }
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 );
350     }
351
352
353 }