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