001    package org.apache.archiva.rest.services;
002    /*
003     * Licensed to the Apache Software Foundation (ASF) under one
004     * or more contributor license agreements.  See the NOTICE file
005     * distributed with this work for additional information
006     * regarding copyright ownership.  The ASF licenses this file
007     * to you under the Apache License, Version 2.0 (the
008     * "License"); you may not use this file except in compliance
009     * with the License.  You may obtain a copy of the License at
010     *
011     *   http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing,
014     * software distributed under the License is distributed on an
015     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016     * KIND, either express or implied.  See the License for the
017     * specific language governing permissions and limitations
018     * under the License.
019     */
020    
021    import org.apache.archiva.admin.model.RepositoryAdminException;
022    import org.apache.archiva.admin.model.admin.ArchivaAdministration;
023    import org.apache.archiva.admin.model.beans.FileType;
024    import org.apache.archiva.admin.model.beans.LegacyArtifactPath;
025    import org.apache.archiva.admin.model.beans.NetworkConfiguration;
026    import org.apache.archiva.admin.model.beans.OrganisationInformation;
027    import org.apache.archiva.admin.model.beans.UiConfiguration;
028    import org.apache.archiva.model.ArtifactReference;
029    import org.apache.archiva.repository.ManagedRepositoryContent;
030    import org.apache.archiva.repository.scanner.RepositoryContentConsumers;
031    import org.apache.archiva.rest.api.model.AdminRepositoryConsumer;
032    import org.apache.archiva.rest.api.services.ArchivaAdministrationService;
033    import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
034    import org.apache.archiva.rest.services.utils.AddAdminRepoConsumerClosure;
035    import org.apache.archiva.rest.services.utils.AdminRepositoryConsumerComparator;
036    import org.apache.commons.collections.CollectionUtils;
037    import org.apache.commons.lang.StringUtils;
038    import org.springframework.stereotype.Service;
039    
040    import javax.inject.Inject;
041    import javax.inject.Named;
042    import javax.ws.rs.core.Response;
043    import java.util.ArrayList;
044    import java.util.Collections;
045    import java.util.List;
046    
047    /**
048     * @author Olivier Lamy
049     * @since 1.4-M1
050     */
051    @Service ( "archivaAdministrationService#default" )
052    public class DefaultArchivaAdministrationService
053        extends AbstractRestService
054        implements ArchivaAdministrationService
055    {
056        @Inject
057        private ArchivaAdministration archivaAdministration;
058    
059        @Inject
060        @Named ( value = "managedRepositoryContent#legacy" )
061        private ManagedRepositoryContent repositoryContent;
062    
063        @Inject
064        private RepositoryContentConsumers repoConsumerUtil;
065    
066        public List<LegacyArtifactPath> getLegacyArtifactPaths()
067            throws ArchivaRestServiceException
068        {
069            try
070            {
071                return archivaAdministration.getLegacyArtifactPaths();
072            }
073            catch ( RepositoryAdminException e )
074            {
075                throw new ArchivaRestServiceException( e.getMessage(), e );
076            }
077        }
078    
079        public void addLegacyArtifactPath( LegacyArtifactPath legacyArtifactPath )
080            throws ArchivaRestServiceException
081        {
082    
083            // Check the proposed Artifact matches the path
084            ArtifactReference artifact = new ArtifactReference();
085    
086            artifact.setGroupId( legacyArtifactPath.getGroupId() );
087            artifact.setArtifactId( legacyArtifactPath.getArtifactId() );
088            artifact.setClassifier( legacyArtifactPath.getClassifier() );
089            artifact.setVersion( legacyArtifactPath.getVersion() );
090            artifact.setType( legacyArtifactPath.getType() );
091            String path = repositoryContent.toPath( artifact );
092            if ( !StringUtils.equals( path, legacyArtifactPath.getPath() ) )
093            {
094                throw new ArchivaRestServiceException(
095                    "artifact path reference '" + legacyArtifactPath.getPath() + "' does not match the initial path: '"
096                        + path + "'", Response.Status.BAD_REQUEST.getStatusCode(), null );
097            }
098    
099            try
100            {
101    
102                archivaAdministration.addLegacyArtifactPath( legacyArtifactPath, getAuditInformation() );
103            }
104            catch ( RepositoryAdminException e )
105            {
106                throw new ArchivaRestServiceException( e.getMessage(), e );
107            }
108        }
109    
110        public Boolean deleteLegacyArtifactPath( String path )
111            throws ArchivaRestServiceException
112        {
113            try
114            {
115                archivaAdministration.deleteLegacyArtifactPath( path, getAuditInformation() );
116                return Boolean.TRUE;
117            }
118            catch ( RepositoryAdminException e )
119            {
120                throw new ArchivaRestServiceException( e.getMessage(), e );
121            }
122        }
123    
124    
125        public Boolean addFileTypePattern( String fileTypeId, String pattern )
126            throws ArchivaRestServiceException
127        {
128            try
129            {
130                archivaAdministration.addFileTypePattern( fileTypeId, pattern, getAuditInformation() );
131                return Boolean.TRUE;
132            }
133            catch ( RepositoryAdminException e )
134            {
135                throw new ArchivaRestServiceException( e.getMessage(), e );
136            }
137        }
138    
139        public Boolean removeFileTypePattern( String fileTypeId, String pattern )
140            throws ArchivaRestServiceException
141        {
142            try
143            {
144                archivaAdministration.removeFileTypePattern( fileTypeId, pattern, getAuditInformation() );
145                return Boolean.TRUE;
146            }
147            catch ( RepositoryAdminException e )
148            {
149                throw new ArchivaRestServiceException( e.getMessage(), e );
150            }
151        }
152    
153        public FileType getFileType( String fileTypeId )
154            throws ArchivaRestServiceException
155        {
156            try
157            {
158                return archivaAdministration.getFileType( fileTypeId );
159            }
160            catch ( RepositoryAdminException e )
161            {
162                throw new ArchivaRestServiceException( e.getMessage(), e );
163            }
164        }
165    
166        public void addFileType( FileType fileType )
167            throws ArchivaRestServiceException
168        {
169            try
170            {
171                archivaAdministration.addFileType( fileType, getAuditInformation() );
172            }
173            catch ( RepositoryAdminException e )
174            {
175                throw new ArchivaRestServiceException( e.getMessage(), e );
176            }
177        }
178    
179        public Boolean removeFileType( String fileTypeId )
180            throws ArchivaRestServiceException
181        {
182            try
183            {
184                archivaAdministration.removeFileType( fileTypeId, getAuditInformation() );
185                return Boolean.TRUE;
186            }
187            catch ( RepositoryAdminException e )
188            {
189                throw new ArchivaRestServiceException( e.getMessage(), e );
190            }
191        }
192    
193        public Boolean enabledKnownContentConsumer( String knownContentConsumer )
194            throws ArchivaRestServiceException
195        {
196            try
197            {
198                archivaAdministration.addKnownContentConsumer( knownContentConsumer, getAuditInformation() );
199                return Boolean.TRUE;
200            }
201            catch ( RepositoryAdminException e )
202            {
203                throw new ArchivaRestServiceException( e.getMessage(), e );
204            }
205        }
206    
207        public void enabledKnownContentConsumers( List<String> knownContentConsumers )
208            throws ArchivaRestServiceException
209        {
210            try
211            {
212                archivaAdministration.setKnownContentConsumers( knownContentConsumers, getAuditInformation() );
213            }
214            catch ( RepositoryAdminException e )
215            {
216                throw new ArchivaRestServiceException( e.getMessage(), e );
217            }
218        }
219    
220        public Boolean disabledKnownContentConsumer( String knownContentConsumer )
221            throws ArchivaRestServiceException
222        {
223            try
224            {
225                archivaAdministration.removeKnownContentConsumer( knownContentConsumer, getAuditInformation() );
226                return Boolean.TRUE;
227            }
228            catch ( RepositoryAdminException e )
229            {
230                throw new ArchivaRestServiceException( e.getMessage(), e );
231            }
232        }
233    
234        public Boolean enabledInvalidContentConsumer( String invalidContentConsumer )
235            throws ArchivaRestServiceException
236        {
237            try
238            {
239                archivaAdministration.addInvalidContentConsumer( invalidContentConsumer, getAuditInformation() );
240                return Boolean.TRUE;
241            }
242            catch ( RepositoryAdminException e )
243            {
244                throw new ArchivaRestServiceException( e.getMessage(), e );
245            }
246        }
247    
248        public void enabledInvalidContentConsumers( List<String> invalidContentConsumers )
249            throws ArchivaRestServiceException
250        {
251            try
252            {
253                archivaAdministration.setInvalidContentConsumers( invalidContentConsumers, getAuditInformation() );
254            }
255            catch ( RepositoryAdminException e )
256            {
257                throw new ArchivaRestServiceException( e.getMessage(), e );
258            }
259        }
260    
261        public Boolean disabledInvalidContentConsumer( String invalidContentConsumer )
262            throws ArchivaRestServiceException
263        {
264            try
265            {
266                archivaAdministration.removeInvalidContentConsumer( invalidContentConsumer, getAuditInformation() );
267                return Boolean.TRUE;
268            }
269            catch ( RepositoryAdminException e )
270            {
271                throw new ArchivaRestServiceException( e.getMessage(), e );
272            }
273        }
274    
275        public List<FileType> getFileTypes()
276            throws ArchivaRestServiceException
277        {
278            try
279            {
280                List<FileType> modelfileTypes = archivaAdministration.getFileTypes();
281                if ( modelfileTypes == null || modelfileTypes.isEmpty() )
282                {
283                    return Collections.emptyList();
284                }
285                return modelfileTypes;
286            }
287            catch ( RepositoryAdminException e )
288            {
289                throw new ArchivaRestServiceException( e.getMessage(), e );
290            }
291        }
292    
293        public List<String> getKnownContentConsumers()
294            throws ArchivaRestServiceException
295        {
296            try
297            {
298                return new ArrayList<String>( archivaAdministration.getKnownContentConsumers() );
299            }
300            catch ( RepositoryAdminException e )
301            {
302                throw new ArchivaRestServiceException( e.getMessage(), e );
303            }
304        }
305    
306        public List<String> getInvalidContentConsumers()
307            throws ArchivaRestServiceException
308        {
309            try
310            {
311                return new ArrayList<String>( archivaAdministration.getInvalidContentConsumers() );
312            }
313            catch ( RepositoryAdminException e )
314            {
315                throw new ArchivaRestServiceException( e.getMessage(), e );
316            }
317        }
318    
319        public OrganisationInformation getOrganisationInformation()
320            throws ArchivaRestServiceException
321        {
322            try
323            {
324                return archivaAdministration.getOrganisationInformation();
325            }
326            catch ( RepositoryAdminException e )
327            {
328                throw new ArchivaRestServiceException( e.getMessage(), e );
329            }
330        }
331    
332        public void setOrganisationInformation( OrganisationInformation organisationInformation )
333            throws ArchivaRestServiceException
334        {
335            try
336            {
337                archivaAdministration.setOrganisationInformation( organisationInformation );
338            }
339            catch ( RepositoryAdminException e )
340            {
341                throw new ArchivaRestServiceException( e.getMessage(), e );
342            }
343        }
344    
345        public Boolean registrationDisabled()
346            throws ArchivaRestServiceException
347        {
348            return getUiConfiguration().isDisableRegistration();
349        }
350    
351        public UiConfiguration getUiConfiguration()
352            throws ArchivaRestServiceException
353        {
354            try
355            {
356                return archivaAdministration.getUiConfiguration();
357            }
358            catch ( RepositoryAdminException e )
359            {
360                throw new ArchivaRestServiceException( e.getMessage(), e );
361            }
362        }
363    
364        public void setUiConfiguration( UiConfiguration uiConfiguration )
365            throws ArchivaRestServiceException
366        {
367            try
368            {
369                // fix for MRM-1757
370                // strip any trailing '/' at the end of the url so it won't affect url/link calculations in UI
371                uiConfiguration.setApplicationUrl(StringUtils.stripEnd(uiConfiguration.getApplicationUrl(), "/"));
372    
373                archivaAdministration.updateUiConfiguration( uiConfiguration );
374            }
375            catch ( RepositoryAdminException e )
376            {
377                throw new ArchivaRestServiceException( e.getMessage(), e );
378            }
379        }
380    
381        public String getApplicationUrl()
382            throws ArchivaRestServiceException
383        {
384            try
385            {
386                return archivaAdministration.getUiConfiguration().getApplicationUrl();
387            }
388            catch ( RepositoryAdminException e )
389            {
390                throw new ArchivaRestServiceException( e.getMessage(), e );
391            }
392        }
393    
394        public NetworkConfiguration getNetworkConfiguration()
395            throws ArchivaRestServiceException
396        {
397            try
398            {
399                return archivaAdministration.getNetworkConfiguration();
400            }
401            catch ( RepositoryAdminException e )
402            {
403                throw new ArchivaRestServiceException( e.getMessage(), e );
404            }
405        }
406    
407        public void setNetworkConfiguration( NetworkConfiguration networkConfiguration )
408            throws ArchivaRestServiceException
409        {
410            try
411            {
412                archivaAdministration.setNetworkConfiguration( networkConfiguration );
413            }
414            catch ( RepositoryAdminException e )
415            {
416                throw new ArchivaRestServiceException( e.getMessage(), e );
417            }
418        }
419    
420        public List<AdminRepositoryConsumer> getKnownContentAdminRepositoryConsumers()
421            throws ArchivaRestServiceException
422        {
423            try
424            {
425                AddAdminRepoConsumerClosure addAdminRepoConsumer =
426                    new AddAdminRepoConsumerClosure( archivaAdministration.getKnownContentConsumers() );
427                CollectionUtils.forAllDo( repoConsumerUtil.getAvailableKnownConsumers(), addAdminRepoConsumer );
428                List<AdminRepositoryConsumer> knownContentConsumers = addAdminRepoConsumer.getList();
429                Collections.sort( knownContentConsumers, AdminRepositoryConsumerComparator.getInstance() );
430                return knownContentConsumers;
431            }
432            catch ( RepositoryAdminException e )
433            {
434                throw new ArchivaRestServiceException( e.getMessage(), e );
435            }
436        }
437    
438        public List<AdminRepositoryConsumer> getInvalidContentAdminRepositoryConsumers()
439            throws ArchivaRestServiceException
440        {
441            try
442            {
443                AddAdminRepoConsumerClosure addAdminRepoConsumer =
444                    new AddAdminRepoConsumerClosure( archivaAdministration.getInvalidContentConsumers() );
445                CollectionUtils.forAllDo( repoConsumerUtil.getAvailableInvalidConsumers(), addAdminRepoConsumer );
446                List<AdminRepositoryConsumer> invalidContentConsumers = addAdminRepoConsumer.getList();
447                Collections.sort( invalidContentConsumers, AdminRepositoryConsumerComparator.getInstance() );
448                return invalidContentConsumers;
449            }
450            catch ( RepositoryAdminException e )
451            {
452                throw new ArchivaRestServiceException( e.getMessage(), e );
453            }
454        }
455    }