]> source.dussan.org Git - archiva.git/blob
4535336d67857b9007e5894f9add09a489af7912
[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.registry.RegistryException;
38 import org.apache.archiva.components.rest.model.PagedResult;
39 import org.apache.archiva.components.rest.util.QueryHelper;
40 import org.apache.archiva.configuration.Configuration;
41 import org.apache.archiva.configuration.IndeterminateConfigurationException;
42 import org.apache.archiva.configuration.RepositoryGroupConfiguration;
43 import org.apache.archiva.repository.EditableRepositoryGroup;
44 import org.apache.archiva.repository.RepositoryException;
45 import org.apache.archiva.repository.RepositoryRegistry;
46 import org.apache.archiva.repository.base.ConfigurationHandler;
47 import org.apache.archiva.repository.validation.ValidationResponse;
48 import org.apache.archiva.rest.api.model.v2.RepositoryGroup;
49 import org.apache.archiva.rest.api.services.v2.ArchivaRestServiceException;
50 import org.apache.archiva.rest.api.services.v2.ErrorKeys;
51 import org.apache.archiva.rest.api.services.v2.ErrorMessage;
52 import org.apache.archiva.rest.api.services.v2.RepositoryGroupService;
53 import org.apache.archiva.rest.api.services.v2.ValidationException;
54 import org.apache.commons.lang3.StringUtils;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57 import org.springframework.stereotype.Service;
58
59 import javax.servlet.http.HttpServletResponse;
60 import javax.ws.rs.core.Context;
61 import javax.ws.rs.core.Response;
62 import javax.ws.rs.core.UriInfo;
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     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         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         final String groupId = repositoryGroup.getId( );
152         if ( StringUtils.isEmpty( groupId ) ) {
153             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_INVALID_ID, groupId ), 422 );
154         }
155         if (repositoryRegistry.hasRepositoryGroup( groupId )) {
156             httpServletResponse.setHeader( "Location", uriInfo.getAbsolutePathBuilder( ).path( groupId ).build( ).toString( ) );
157             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_ID_EXISTS, groupId ), 303 );
158         }
159         try
160         {
161             RepositoryGroupConfiguration configuration = toConfig( repositoryGroup );
162             Configuration config = configurationHandler.getBaseConfiguration( );
163             org.apache.archiva.repository.RepositoryGroup repo = repositoryRegistry.putRepositoryGroup( configuration, config);
164             if ( repo!=null )
165             {
166                 ValidationResponse<org.apache.archiva.repository.RepositoryGroup> validationResult = repositoryRegistry.validateRepository( repo );
167                 if ( validationResult.isValid( ) )
168                 {
169                     httpServletResponse.setStatus( 201 );
170                     configurationHandler.save( config );
171                     return RepositoryGroup.of( repo );
172                 } else {
173                     throw ValidationException.of( validationResult );
174                 }
175             }
176             else
177             {
178                 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_ADD_FAILED ) );
179             }
180         }
181         catch ( RepositoryException | IndeterminateConfigurationException | RegistryException e )
182         {
183             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_ADD_FAILED ) );
184         }
185     }
186
187     @Override
188     public RepositoryGroup updateRepositoryGroup( final String repositoryGroupId, final RepositoryGroup repositoryGroup ) throws ArchivaRestServiceException
189     {
190         if ( StringUtils.isEmpty( repositoryGroupId ) )
191         {
192             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
193         }
194         if ( !repositoryRegistry.hasRepositoryGroup( repositoryGroupId ) )
195         {
196             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND ), 404 );
197         }
198         repositoryGroup.setId( repositoryGroupId );
199
200         try
201         {
202             RepositoryGroupConfiguration configuration = toConfig( repositoryGroup );
203             Configuration config = configurationHandler.getBaseConfiguration( );
204             org.apache.archiva.repository.RepositoryGroup repo = repositoryRegistry.putRepositoryGroup( configuration, config);
205             if ( repo!=null )
206             {
207                 ValidationResponse<org.apache.archiva.repository.RepositoryGroup> validationResult = repositoryRegistry.validateRepository( repo );
208                 if ( validationResult.isValid( ) )
209                 {
210                     httpServletResponse.setStatus( 201 );
211                     configurationHandler.save( config );
212                     return RepositoryGroup.of( repo );
213                 } else {
214                     throw ValidationException.of( validationResult );
215                 }
216             }
217             else
218             {
219                 log.error( "Returned repository group was null {}", repositoryGroupId );
220                 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED ) );
221             }
222
223         }
224         catch ( RepositoryException | IndeterminateConfigurationException | RegistryException e )
225         {
226             log.error( "Exception during repository group update: {}", e.getMessage( ), e );
227             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, e.getMessage() ) );
228
229         }
230     }
231
232     @Override
233     public Response deleteRepositoryGroup( String repositoryGroupId ) throws ArchivaRestServiceException
234     {
235         if ( StringUtils.isEmpty( repositoryGroupId ) )
236         {
237             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
238         }
239         try
240         {
241             org.apache.archiva.repository.RepositoryGroup group = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
242             if (group==null) {
243                 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
244             }
245             repositoryRegistry.removeRepositoryGroup( group );
246             return Response.ok( ).build( );
247         }
248         catch ( RepositoryException e )
249         {
250             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_DELETE_FAILED ) );
251         }
252     }
253
254     @Override
255     public RepositoryGroup addRepositoryToGroup( String repositoryGroupId, String repositoryId ) throws ArchivaRestServiceException
256     {
257         if ( StringUtils.isEmpty( repositoryGroupId ) )
258         {
259             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
260         }
261         if ( StringUtils.isEmpty( repositoryId ) )
262         {
263             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_FOUND, "" ), 404 );
264         }
265         try
266         {
267             org.apache.archiva.repository.RepositoryGroup repositoryGroup = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
268             if (repositoryGroup==null) {
269                 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
270             }
271             if (!(repositoryGroup instanceof EditableRepositoryGroup )) {
272                 log.error( "This group instance is not editable: {}", repositoryGroupId );
273                 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, "" ), 500 );
274             }
275             EditableRepositoryGroup editableRepositoryGroup = (EditableRepositoryGroup) repositoryGroup;
276             if ( editableRepositoryGroup.getRepositories().stream().anyMatch( repo -> repositoryId.equals(repo.getId())) )
277             {
278                 log.info( "Repository {} is already member of group {}", repositoryId, repositoryGroupId );
279                 return RepositoryGroup.of( editableRepositoryGroup );
280             }
281             org.apache.archiva.repository.ManagedRepository managedRepo = repositoryRegistry.getManagedRepository(repositoryId);
282             if (managedRepo==null) {
283                 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_FOUND, "" ), 404 );
284             }
285             editableRepositoryGroup.addRepository( managedRepo );
286             org.apache.archiva.repository.RepositoryGroup newGroup = repositoryRegistry.putRepositoryGroup( editableRepositoryGroup );
287             return RepositoryGroup.of( newGroup );
288         }
289         catch ( RepositoryException e )
290         {
291             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, e.getMessage() ), 500 );
292         }
293     }
294
295     @Override
296     public RepositoryGroup deleteRepositoryFromGroup( String repositoryGroupId, String repositoryId ) throws org.apache.archiva.rest.api.services.v2.ArchivaRestServiceException
297     {
298         if ( StringUtils.isEmpty( repositoryGroupId ) )
299         {
300             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
301         }
302         if ( StringUtils.isEmpty( repositoryId ) )
303         {
304             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_FOUND, "" ), 404 );
305         }
306         try
307         {
308             org.apache.archiva.repository.RepositoryGroup repositoryGroup = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
309             if (repositoryGroup==null) {
310                 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
311             }
312             if (!(repositoryGroup instanceof EditableRepositoryGroup)) {
313                 log.error( "This group instance is not editable: {}", repositoryGroupId );
314                 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, "" ), 500 );
315             }
316             EditableRepositoryGroup editableRepositoryGroup = (EditableRepositoryGroup) repositoryGroup;
317
318             editableRepositoryGroup.removeRepository( repositoryId );
319             org.apache.archiva.repository.RepositoryGroup newGroup = repositoryRegistry.putRepositoryGroup( editableRepositoryGroup );
320             return RepositoryGroup.of( newGroup );
321         }
322         catch ( RepositoryException e )
323         {
324             throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, e.getMessage() ), 500 );
325         }
326     }
327
328
329 }