]> source.dussan.org Git - archiva.git/blob
4927b2161c12cc68465342dfd5ad479d36327b4c
[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.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.validation.CheckedResult;
41 import org.apache.archiva.repository.EditableRepositoryGroup;
42 import org.apache.archiva.repository.RepositoryException;
43 import org.apache.archiva.repository.RepositoryRegistry;
44 import org.apache.archiva.repository.base.ConfigurationHandler;
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;
57
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;
64 import java.util.Map;
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     private final ConfigurationHandler configurationHandler;
79
80     @Context
81     HttpServletResponse httpServletResponse;
82
83     @Context
84     UriInfo uriInfo;
85
86     final private RepositoryRegistry repositoryRegistry;
87
88
89
90     private static final Logger log = LoggerFactory.getLogger( DefaultRepositoryGroupService.class );
91     private static final QueryHelper<org.apache.archiva.repository.RepositoryGroup> QUERY_HELPER = new QueryHelper<>( new String[]{"id"} );
92     static
93     {
94         QUERY_HELPER.addStringFilter( "id", org.apache.archiva.repository.RepositoryGroup::getId );
95         QUERY_HELPER.addNullsafeFieldComparator( "id", org.apache.archiva.repository.RepositoryGroup::getId );
96     }
97
98
99     public DefaultRepositoryGroupService( RepositoryRegistry repositoryRegistry, ConfigurationHandler configurationHandler ) {
100         this.repositoryRegistry = repositoryRegistry;
101         this.configurationHandler = configurationHandler;
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.repository.RepositoryGroup> filter = QUERY_HELPER.getQueryFilter( searchTerm );
110             Comparator<org.apache.archiva.repository.RepositoryGroup> ordering = QUERY_HELPER.getComparator( orderBy, QUERY_HELPER.isAscending( order ) );
111             int totalCount = Math.toIntExact( repositoryRegistry.getRepositoryGroups( ).stream( ).filter( filter ).count( ) );
112             List<RepositoryGroup> result = repositoryRegistry.getRepositoryGroups( ).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 ( ArithmeticException e )
118         {
119             log.error( "Could not convert total count: {}", e.getMessage( ) );
120             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.INVALID_RESULT_SET_ERROR ) );
121         }
122
123     }
124
125     @Override
126     public RepositoryGroup getRepositoryGroup( String repositoryGroupId ) throws ArchivaRestServiceException
127     {
128         if ( StringUtils.isEmpty( repositoryGroupId ) )
129         {
130             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
131         }
132         org.apache.archiva.repository.RepositoryGroup group = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
133         return RepositoryGroup.of( group );
134     }
135
136     private RepositoryGroupConfiguration toConfig( RepositoryGroup group )
137     {
138         RepositoryGroupConfiguration result = new RepositoryGroupConfiguration( );
139         result.setId( group.getId( ) );
140         result.setLocation( group.getLocation( ) );
141         result.setRepositories( group.getRepositories( ) );
142         MergeConfiguration mergeConfig = group.getMergeConfiguration( );
143         if (mergeConfig!=null)
144         {
145             result.setMergedIndexPath( mergeConfig.getMergedIndexPath( ) );
146             result.setMergedIndexTtl( mergeConfig.getMergedIndexTtlMinutes( ) );
147             result.setCronExpression( mergeConfig.getIndexMergeSchedule( ) );
148         }
149         return result;
150     }
151
152     @Override
153     public RepositoryGroup addRepositoryGroup( RepositoryGroup repositoryGroup ) throws ArchivaRestServiceException
154     {
155         final String groupId = repositoryGroup.getId( );
156         if ( StringUtils.isEmpty( groupId ) ) {
157             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_INVALID_ID, groupId ), 422 );
158         }
159         if (repositoryRegistry.hasRepositoryGroup( groupId )) {
160             httpServletResponse.setHeader( "Location", uriInfo.getAbsolutePathBuilder( ).path( groupId ).build( ).toString( ) );
161             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_ID_EXISTS, groupId ), 303 );
162         }
163         try
164         {
165
166             RepositoryGroupConfiguration configuration = toConfig( repositoryGroup );
167             CheckedResult<org.apache.archiva.repository.RepositoryGroup, Map<String, List<ValidationError>>> validationResult = repositoryRegistry.putRepositoryGroupAndValidate( configuration );
168                 if ( validationResult.isValid( ) )
169                 {
170                     httpServletResponse.setStatus( 201 );
171                     return RepositoryGroup.of( validationResult.getRepository() );
172                 } else {
173                     throw ValidationException.of( validationResult.getResult() );
174                 }
175         }
176         catch ( RepositoryException e )
177         {
178             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_ADD_FAILED ) );
179         }
180     }
181
182     @Override
183     public RepositoryGroup updateRepositoryGroup( final String repositoryGroupId, final RepositoryGroup repositoryGroup ) throws ArchivaRestServiceException
184     {
185         if ( StringUtils.isEmpty( repositoryGroupId ) )
186         {
187             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
188         }
189         if ( !repositoryRegistry.hasRepositoryGroup( repositoryGroupId ) )
190         {
191             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND ), 404 );
192         }
193         repositoryGroup.setId( repositoryGroupId );
194         try
195         {
196             RepositoryGroupConfiguration configuration = toConfig( repositoryGroup );
197                 CheckedResult<org.apache.archiva.repository.RepositoryGroup, Map<String, List<ValidationError>>> validationResult = repositoryRegistry.putRepositoryGroupAndValidate( configuration );
198                 if ( validationResult.isValid( ) )
199                 {
200                     httpServletResponse.setStatus( 201 );
201                     return RepositoryGroup.of( validationResult.getRepository() );
202                 } else {
203                     throw ValidationException.of( validationResult.getResult() );
204                 }
205         }
206         catch ( RepositoryException e )
207         {
208             log.error( "Exception during repository group update: {}", e.getMessage( ), e );
209             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, e.getMessage() ) );
210
211         }
212     }
213
214     @Override
215     public Response deleteRepositoryGroup( String repositoryGroupId ) throws ArchivaRestServiceException
216     {
217         if ( StringUtils.isEmpty( repositoryGroupId ) )
218         {
219             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
220         }
221         try
222         {
223             org.apache.archiva.repository.RepositoryGroup group = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
224             if (group==null) {
225                 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
226             }
227             repositoryRegistry.removeRepositoryGroup( group );
228             return Response.ok( ).build( );
229         }
230         catch ( RepositoryException e )
231         {
232             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_DELETE_FAILED ) );
233         }
234     }
235
236     @Override
237     public RepositoryGroup addRepositoryToGroup( String repositoryGroupId, String repositoryId ) throws ArchivaRestServiceException
238     {
239         if ( StringUtils.isEmpty( repositoryGroupId ) )
240         {
241             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
242         }
243         if ( StringUtils.isEmpty( repositoryId ) )
244         {
245             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_FOUND, "" ), 404 );
246         }
247         try
248         {
249             org.apache.archiva.repository.RepositoryGroup repositoryGroup = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
250             if (repositoryGroup==null) {
251                 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
252             }
253             if (!(repositoryGroup instanceof EditableRepositoryGroup )) {
254                 log.error( "This group instance is not editable: {}", repositoryGroupId );
255                 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, "" ), 500 );
256             }
257             EditableRepositoryGroup editableRepositoryGroup = (EditableRepositoryGroup) repositoryGroup;
258             if ( editableRepositoryGroup.getRepositories().stream().anyMatch( repo -> repositoryId.equals(repo.getId())) )
259             {
260                 log.info( "Repository {} is already member of group {}", repositoryId, repositoryGroupId );
261                 return RepositoryGroup.of( editableRepositoryGroup );
262             }
263             org.apache.archiva.repository.ManagedRepository managedRepo = repositoryRegistry.getManagedRepository(repositoryId);
264             if (managedRepo==null) {
265                 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_FOUND, "" ), 404 );
266             }
267             editableRepositoryGroup.addRepository( managedRepo );
268             org.apache.archiva.repository.RepositoryGroup newGroup = repositoryRegistry.putRepositoryGroup( editableRepositoryGroup );
269             return RepositoryGroup.of( newGroup );
270         }
271         catch ( RepositoryException e )
272         {
273             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, e.getMessage() ), 500 );
274         }
275     }
276
277     @Override
278     public RepositoryGroup deleteRepositoryFromGroup( final String repositoryGroupId, final String repositoryId ) throws org.apache.archiva.rest.api.services.v2.ArchivaRestServiceException
279     {
280         if ( StringUtils.isEmpty( repositoryGroupId ) )
281         {
282             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, repositoryGroupId ), 404 );
283         }
284         if ( StringUtils.isEmpty( repositoryId ) )
285         {
286             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_FOUND, repositoryId ), 404 );
287         }
288         try
289         {
290             org.apache.archiva.repository.RepositoryGroup repositoryGroup = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
291             if (repositoryGroup==null) {
292                 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
293             }
294             if (repositoryGroup.getRepositories().stream().noneMatch( r -> repositoryId.equals( r.getId() ) )) {
295                 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_FOUND, repositoryId ), 404 );
296             }
297             if (!(repositoryGroup instanceof EditableRepositoryGroup)) {
298                 log.error( "This group instance is not editable: {}", repositoryGroupId );
299                 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, "" ), 500 );
300             }
301             EditableRepositoryGroup editableRepositoryGroup = (EditableRepositoryGroup) repositoryGroup;
302             editableRepositoryGroup.removeRepository( repositoryId );
303             org.apache.archiva.repository.RepositoryGroup newGroup = repositoryRegistry.putRepositoryGroup( editableRepositoryGroup );
304             return RepositoryGroup.of( newGroup );
305         }
306         catch ( RepositoryException e )
307         {
308             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, e.getMessage() ), 500 );
309         }
310     }
311
312
313 }