1 package org.apache.archiva.rest.services;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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
21 import org.apache.archiva.metadata.repository.MetadataResolutionException;
22 import org.apache.archiva.metadata.repository.MetadataResolver;
23 import org.apache.archiva.metadata.repository.RepositorySession;
24 import org.apache.archiva.rest.api.model.GroupIdList;
25 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
26 import org.apache.archiva.rest.api.services.BrowseService;
27 import org.apache.commons.collections.CollectionUtils;
28 import org.springframework.stereotype.Service;
30 import javax.ws.rs.core.Response;
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.Collections;
34 import java.util.LinkedHashSet;
35 import java.util.List;
39 * @author Olivier Lamy
42 @Service( "browseService#rest" )
43 public class DefaultBrowseService
44 extends AbstractRestService
45 implements BrowseService
48 public GroupIdList getRootGroups()
49 throws ArchivaRestServiceException
51 List<String> selectedRepos = getObservableRepos();
52 if ( CollectionUtils.isEmpty( selectedRepos ) )
55 return new GroupIdList( Collections.<String>emptyList() );
58 Set<String> namespaces = new LinkedHashSet<String>();
60 // TODO: this logic should be optional, particularly remembering we want to keep this code simple
61 // it is located here to avoid the content repository implementation needing to do too much for what
62 // is essentially presentation code
63 Set<String> namespacesToCollapse;
64 RepositorySession repositorySession = repositorySessionFactory.createSession();
67 MetadataResolver metadataResolver = repositorySession.getResolver();
68 namespacesToCollapse = new LinkedHashSet<String>();
69 for ( String repoId : selectedRepos )
71 namespacesToCollapse.addAll( metadataResolver.resolveRootNamespaces( repositorySession, repoId ) );
74 for ( String n : namespacesToCollapse )
76 // TODO: check performance of this
77 namespaces.add( collapseNamespaces( repositorySession, metadataResolver, selectedRepos, n ) );
80 catch ( MetadataResolutionException e )
82 throw new ArchivaRestServiceException( e.getMessage(),
83 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode() );
87 repositorySession.close();
90 return new GroupIdList( getSortedList( namespaces ) );
93 //---------------------------
95 //---------------------------
97 private List<String> getSortedList( Set<String> set )
99 List<String> list = new ArrayList<String>( set );
100 Collections.sort( list );
104 private String collapseNamespaces( RepositorySession repositorySession, MetadataResolver metadataResolver,
105 Collection<String> repoIds, String n )
106 throws MetadataResolutionException
108 Set<String> subNamespaces = new LinkedHashSet<String>();
109 for ( String repoId : repoIds )
111 subNamespaces.addAll( metadataResolver.resolveNamespaces( repositorySession, repoId, n ) );
113 if ( subNamespaces.size() != 1 )
115 log.debug( "{} is not collapsible as it has sub-namespaces: {}", n, subNamespaces );
120 for ( String repoId : repoIds )
122 Collection<String> projects = metadataResolver.resolveProjects( repositorySession, repoId, n );
123 if ( projects != null && !projects.isEmpty() )
125 log.debug( "{} is not collapsible as it has projects", n );
129 return collapseNamespaces( repositorySession, metadataResolver, repoIds,
130 n + "." + subNamespaces.iterator().next() );