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.BrowseResultEntry;
25 import org.apache.archiva.rest.api.model.BrowseResult;
26 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
27 import org.apache.archiva.rest.api.services.BrowseService;
28 import org.apache.commons.collections.CollectionUtils;
29 import org.springframework.stereotype.Service;
31 import javax.ws.rs.core.Response;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.Collections;
35 import java.util.LinkedHashSet;
36 import java.util.List;
40 * @author Olivier Lamy
43 @Service( "browseService#rest" )
44 public class DefaultBrowseService
45 extends AbstractRestService
46 implements BrowseService
49 public BrowseResult getRootGroups()
50 throws ArchivaRestServiceException
52 List<String> selectedRepos = getObservableRepos();
53 if ( CollectionUtils.isEmpty( selectedRepos ) )
56 return new BrowseResult();
59 Set<String> namespaces = new LinkedHashSet<String>();
61 // TODO: this logic should be optional, particularly remembering we want to keep this code simple
62 // it is located here to avoid the content repository implementation needing to do too much for what
63 // is essentially presentation code
64 Set<String> namespacesToCollapse;
65 RepositorySession repositorySession = repositorySessionFactory.createSession();
68 MetadataResolver metadataResolver = repositorySession.getResolver();
69 namespacesToCollapse = new LinkedHashSet<String>();
70 for ( String repoId : selectedRepos )
72 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 List<BrowseResultEntry> browseGroupResultEntries = new ArrayList<BrowseResultEntry>( namespaces.size() );
91 for ( String namespace : namespaces )
93 browseGroupResultEntries.add( new BrowseResultEntry( namespace, false ) );
96 Collections.sort( browseGroupResultEntries );
97 return new BrowseResult( browseGroupResultEntries );
100 public BrowseResult browseGroupId( String groupId )
101 throws ArchivaRestServiceException
104 List<String> selectedRepos = getObservableRepos();
105 if ( CollectionUtils.isEmpty( selectedRepos ) )
108 return new BrowseResult();
111 Set<String> projects = new LinkedHashSet<String>();
113 RepositorySession repositorySession = repositorySessionFactory.createSession();
114 Set<String> namespaces;
117 MetadataResolver metadataResolver = repositorySession.getResolver();
119 Set<String> namespacesToCollapse = new LinkedHashSet<String>();
120 for ( String repoId : selectedRepos )
122 namespacesToCollapse.addAll( metadataResolver.resolveNamespaces( repositorySession, repoId, groupId ) );
124 projects.addAll( metadataResolver.resolveProjects( repositorySession, repoId, groupId ) );
127 // TODO: this logic should be optional, particularly remembering we want to keep this code simple
128 // it is located here to avoid the content repository implementation needing to do too much for what
129 // is essentially presentation code
130 namespaces = new LinkedHashSet<String>();
131 for ( String n : namespacesToCollapse )
133 // TODO: check performance of this
135 collapseNamespaces( repositorySession, metadataResolver, selectedRepos, groupId + "." + n ) );
138 catch ( MetadataResolutionException e )
140 throw new ArchivaRestServiceException( e.getMessage(),
141 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode() );
145 repositorySession.close();
147 List<BrowseResultEntry> browseGroupResultEntries = new ArrayList<BrowseResultEntry>( namespaces.size() + projects.size() );
148 for ( String namespace : namespaces )
150 browseGroupResultEntries.add( new BrowseResultEntry( namespace, false ) );
152 for ( String project : projects )
154 browseGroupResultEntries.add( new BrowseResultEntry( project, true ) );
156 Collections.sort( browseGroupResultEntries );
157 return new BrowseResult( browseGroupResultEntries );
161 //---------------------------
163 //---------------------------
165 private List<String> getSortedList( Set<String> set )
167 List<String> list = new ArrayList<String>( set );
168 Collections.sort( list );
172 private String collapseNamespaces( RepositorySession repositorySession, MetadataResolver metadataResolver,
173 Collection<String> repoIds, String n )
174 throws MetadataResolutionException
176 Set<String> subNamespaces = new LinkedHashSet<String>();
177 for ( String repoId : repoIds )
179 subNamespaces.addAll( metadataResolver.resolveNamespaces( repositorySession, repoId, n ) );
181 if ( subNamespaces.size() != 1 )
183 log.debug( "{} is not collapsible as it has sub-namespaces: {}", n, subNamespaces );
188 for ( String repoId : repoIds )
190 Collection<String> projects = metadataResolver.resolveProjects( repositorySession, repoId, n );
191 if ( projects != null && !projects.isEmpty() )
193 log.debug( "{} is not collapsible as it has projects", n );
197 return collapseNamespaces( repositorySession, metadataResolver, repoIds,
198 n + "." + subNamespaces.iterator().next() );