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