View Javadoc
1   package org.apache.archiva.rest.services;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  
21  import org.apache.archiva.admin.model.RepositoryAdminException;
22  import org.apache.archiva.admin.model.admin.ArchivaAdministration;
23  import org.apache.archiva.admin.model.beans.FileType;
24  import org.apache.archiva.admin.model.beans.LegacyArtifactPath;
25  import org.apache.archiva.admin.model.beans.NetworkConfiguration;
26  import org.apache.archiva.admin.model.beans.OrganisationInformation;
27  import org.apache.archiva.admin.model.beans.UiConfiguration;
28  import org.apache.archiva.repository.scanner.RepositoryContentConsumers;
29  import org.apache.archiva.rest.api.model.AdminRepositoryConsumer;
30  import org.apache.archiva.rest.api.services.ArchivaAdministrationService;
31  import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
32  import org.apache.archiva.rest.services.utils.AddAdminRepoConsumerClosure;
33  import org.apache.archiva.rest.services.utils.AdminRepositoryConsumerComparator;
34  import org.apache.commons.collections.CollectionUtils;
35  import org.apache.commons.lang.StringUtils;
36  import org.springframework.stereotype.Service;
37  
38  import javax.inject.Inject;
39  import java.util.ArrayList;
40  import java.util.Collections;
41  import java.util.List;
42  
43  /**
44   * @author Olivier Lamy
45   * @since 1.4-M1
46   */
47  @Service ( "archivaAdministrationService#default" )
48  public class DefaultArchivaAdministrationService
49      extends AbstractRestService
50      implements ArchivaAdministrationService
51  {
52      @Inject
53      private ArchivaAdministration archivaAdministration;
54  
55  
56      @Inject
57      private RepositoryContentConsumers repoConsumerUtil;
58  
59      @Override
60      public List<LegacyArtifactPath> getLegacyArtifactPaths()
61          throws ArchivaRestServiceException
62      {
63          try
64          {
65              return archivaAdministration.getLegacyArtifactPaths();
66          }
67          catch ( RepositoryAdminException e )
68          {
69              throw new ArchivaRestServiceException( e.getMessage(), e );
70          }
71      }
72  
73  
74      @Override
75      public Boolean deleteLegacyArtifactPath( String path )
76          throws ArchivaRestServiceException
77      {
78          try
79          {
80              archivaAdministration.deleteLegacyArtifactPath( path, getAuditInformation() );
81              return Boolean.TRUE;
82          }
83          catch ( RepositoryAdminException e )
84          {
85              throw new ArchivaRestServiceException( e.getMessage(), e );
86          }
87      }
88  
89  
90      @Override
91      public Boolean addFileTypePattern( String fileTypeId, String pattern )
92          throws ArchivaRestServiceException
93      {
94          try
95          {
96              archivaAdministration.addFileTypePattern( fileTypeId, pattern, getAuditInformation() );
97              return Boolean.TRUE;
98          }
99          catch ( RepositoryAdminException e )
100         {
101             throw new ArchivaRestServiceException( e.getMessage(), e );
102         }
103     }
104 
105     @Override
106     public Boolean removeFileTypePattern( String fileTypeId, String pattern )
107         throws ArchivaRestServiceException
108     {
109         try
110         {
111             archivaAdministration.removeFileTypePattern( fileTypeId, pattern, getAuditInformation() );
112             return Boolean.TRUE;
113         }
114         catch ( RepositoryAdminException e )
115         {
116             throw new ArchivaRestServiceException( e.getMessage(), e );
117         }
118     }
119 
120     @Override
121     public FileType getFileType( String fileTypeId )
122         throws ArchivaRestServiceException
123     {
124         try
125         {
126             return archivaAdministration.getFileType( fileTypeId );
127         }
128         catch ( RepositoryAdminException e )
129         {
130             throw new ArchivaRestServiceException( e.getMessage(), e );
131         }
132     }
133 
134     @Override
135     public void addFileType( FileType fileType )
136         throws ArchivaRestServiceException
137     {
138         try
139         {
140             archivaAdministration.addFileType( fileType, getAuditInformation() );
141         }
142         catch ( RepositoryAdminException e )
143         {
144             throw new ArchivaRestServiceException( e.getMessage(), e );
145         }
146     }
147 
148     @Override
149     public Boolean removeFileType( String fileTypeId )
150         throws ArchivaRestServiceException
151     {
152         try
153         {
154             archivaAdministration.removeFileType( fileTypeId, getAuditInformation() );
155             return Boolean.TRUE;
156         }
157         catch ( RepositoryAdminException e )
158         {
159             throw new ArchivaRestServiceException( e.getMessage(), e );
160         }
161     }
162 
163     @Override
164     public Boolean enabledKnownContentConsumer( String knownContentConsumer )
165         throws ArchivaRestServiceException
166     {
167         try
168         {
169             archivaAdministration.addKnownContentConsumer( knownContentConsumer, getAuditInformation() );
170             return Boolean.TRUE;
171         }
172         catch ( RepositoryAdminException e )
173         {
174             throw new ArchivaRestServiceException( e.getMessage(), e );
175         }
176     }
177 
178     @Override
179     public void enabledKnownContentConsumers( List<String> knownContentConsumers )
180         throws ArchivaRestServiceException
181     {
182         try
183         {
184             archivaAdministration.setKnownContentConsumers( knownContentConsumers, getAuditInformation() );
185         }
186         catch ( RepositoryAdminException e )
187         {
188             throw new ArchivaRestServiceException( e.getMessage(), e );
189         }
190     }
191 
192     @Override
193     public Boolean disabledKnownContentConsumer( String knownContentConsumer )
194         throws ArchivaRestServiceException
195     {
196         try
197         {
198             archivaAdministration.removeKnownContentConsumer( knownContentConsumer, getAuditInformation() );
199             return Boolean.TRUE;
200         }
201         catch ( RepositoryAdminException e )
202         {
203             throw new ArchivaRestServiceException( e.getMessage(), e );
204         }
205     }
206 
207     @Override
208     public Boolean enabledInvalidContentConsumer( String invalidContentConsumer )
209         throws ArchivaRestServiceException
210     {
211         try
212         {
213             archivaAdministration.addInvalidContentConsumer( invalidContentConsumer, getAuditInformation() );
214             return Boolean.TRUE;
215         }
216         catch ( RepositoryAdminException e )
217         {
218             throw new ArchivaRestServiceException( e.getMessage(), e );
219         }
220     }
221 
222     @Override
223     public void enabledInvalidContentConsumers( List<String> invalidContentConsumers )
224         throws ArchivaRestServiceException
225     {
226         try
227         {
228             archivaAdministration.setInvalidContentConsumers( invalidContentConsumers, getAuditInformation() );
229         }
230         catch ( RepositoryAdminException e )
231         {
232             throw new ArchivaRestServiceException( e.getMessage(), e );
233         }
234     }
235 
236     @Override
237     public Boolean disabledInvalidContentConsumer( String invalidContentConsumer )
238         throws ArchivaRestServiceException
239     {
240         try
241         {
242             archivaAdministration.removeInvalidContentConsumer( invalidContentConsumer, getAuditInformation() );
243             return Boolean.TRUE;
244         }
245         catch ( RepositoryAdminException e )
246         {
247             throw new ArchivaRestServiceException( e.getMessage(), e );
248         }
249     }
250 
251     @Override
252     public List<FileType> getFileTypes()
253         throws ArchivaRestServiceException
254     {
255         try
256         {
257             List<FileType> modelfileTypes = archivaAdministration.getFileTypes();
258             if ( modelfileTypes == null || modelfileTypes.isEmpty() )
259             {
260                 return Collections.emptyList();
261             }
262             return modelfileTypes;
263         }
264         catch ( RepositoryAdminException e )
265         {
266             throw new ArchivaRestServiceException( e.getMessage(), e );
267         }
268     }
269 
270     @Override
271     public List<String> getKnownContentConsumers()
272         throws ArchivaRestServiceException
273     {
274         try
275         {
276             return new ArrayList<String>( archivaAdministration.getKnownContentConsumers() );
277         }
278         catch ( RepositoryAdminException e )
279         {
280             throw new ArchivaRestServiceException( e.getMessage(), e );
281         }
282     }
283 
284     @Override
285     public List<String> getInvalidContentConsumers()
286         throws ArchivaRestServiceException
287     {
288         try
289         {
290             return new ArrayList<String>( archivaAdministration.getInvalidContentConsumers() );
291         }
292         catch ( RepositoryAdminException e )
293         {
294             throw new ArchivaRestServiceException( e.getMessage(), e );
295         }
296     }
297 
298     @Override
299     public OrganisationInformation getOrganisationInformation()
300         throws ArchivaRestServiceException
301     {
302         try
303         {
304             return archivaAdministration.getOrganisationInformation();
305         }
306         catch ( RepositoryAdminException e )
307         {
308             throw new ArchivaRestServiceException( e.getMessage(), e );
309         }
310     }
311 
312     @Override
313     public void setOrganisationInformation( OrganisationInformation organisationInformation )
314         throws ArchivaRestServiceException
315     {
316         try
317         {
318             archivaAdministration.setOrganisationInformation( organisationInformation );
319         }
320         catch ( RepositoryAdminException e )
321         {
322             throw new ArchivaRestServiceException( e.getMessage(), e );
323         }
324     }
325 
326     @Override
327     public Boolean registrationDisabled()
328         throws ArchivaRestServiceException
329     {
330         return getUiConfiguration().isDisableRegistration();
331     }
332 
333     @Override
334     public UiConfiguration getUiConfiguration()
335         throws ArchivaRestServiceException
336     {
337         try
338         {
339             return archivaAdministration.getUiConfiguration();
340         }
341         catch ( RepositoryAdminException e )
342         {
343             throw new ArchivaRestServiceException( e.getMessage(), e );
344         }
345     }
346 
347     @Override
348     public void setUiConfiguration( UiConfiguration uiConfiguration )
349         throws ArchivaRestServiceException
350     {
351         try
352         {
353             // fix for MRM-1757
354             // strip any trailing '/' at the end of the url so it won't affect url/link calculations in UI
355             uiConfiguration.setApplicationUrl(StringUtils.stripEnd(uiConfiguration.getApplicationUrl(), "/"));
356 
357             archivaAdministration.updateUiConfiguration( uiConfiguration );
358         }
359         catch ( RepositoryAdminException e )
360         {
361             throw new ArchivaRestServiceException( e.getMessage(), e );
362         }
363     }
364 
365     @Override
366     public String getApplicationUrl()
367         throws ArchivaRestServiceException
368     {
369         try
370         {
371             return archivaAdministration.getUiConfiguration().getApplicationUrl();
372         }
373         catch ( RepositoryAdminException e )
374         {
375             throw new ArchivaRestServiceException( e.getMessage(), e );
376         }
377     }
378 
379     @Override
380     public NetworkConfiguration getNetworkConfiguration()
381         throws ArchivaRestServiceException
382     {
383         try
384         {
385             return archivaAdministration.getNetworkConfiguration();
386         }
387         catch ( RepositoryAdminException e )
388         {
389             throw new ArchivaRestServiceException( e.getMessage(), e );
390         }
391     }
392 
393     @Override
394     public void setNetworkConfiguration( NetworkConfiguration networkConfiguration )
395         throws ArchivaRestServiceException
396     {
397         try
398         {
399             archivaAdministration.setNetworkConfiguration( networkConfiguration );
400         }
401         catch ( RepositoryAdminException e )
402         {
403             throw new ArchivaRestServiceException( e.getMessage(), e );
404         }
405     }
406 
407     @Override
408     public List<AdminRepositoryConsumer> getKnownContentAdminRepositoryConsumers()
409         throws ArchivaRestServiceException
410     {
411         try
412         {
413             AddAdminRepoConsumerClosure addAdminRepoConsumer =
414                 new AddAdminRepoConsumerClosure( archivaAdministration.getKnownContentConsumers() );
415             CollectionUtils.forAllDo( repoConsumerUtil.getAvailableKnownConsumers(), addAdminRepoConsumer );
416             List<AdminRepositoryConsumer> knownContentConsumers = addAdminRepoConsumer.getList();
417             Collections.sort( knownContentConsumers, AdminRepositoryConsumerComparator.getInstance() );
418             return knownContentConsumers;
419         }
420         catch ( RepositoryAdminException e )
421         {
422             throw new ArchivaRestServiceException( e.getMessage(), e );
423         }
424     }
425 
426     @Override
427     public List<AdminRepositoryConsumer> getInvalidContentAdminRepositoryConsumers()
428         throws ArchivaRestServiceException
429     {
430         try
431         {
432             AddAdminRepoConsumerClosure addAdminRepoConsumer =
433                 new AddAdminRepoConsumerClosure( archivaAdministration.getInvalidContentConsumers() );
434             CollectionUtils.forAllDo( repoConsumerUtil.getAvailableInvalidConsumers(), addAdminRepoConsumer );
435             List<AdminRepositoryConsumer> invalidContentConsumers = addAdminRepoConsumer.getList();
436             Collections.sort( invalidContentConsumers, AdminRepositoryConsumerComparator.getInstance() );
437             return invalidContentConsumers;
438         }
439         catch ( RepositoryAdminException e )
440         {
441             throw new ArchivaRestServiceException( e.getMessage(), e );
442         }
443     }
444 }