From: Olivier Lamy Date: Wed, 15 Feb 2012 17:42:55 +0000 (+0000) Subject: [MRM-1573] start work on browse screen X-Git-Tag: archiva-1.4-M3~1279 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f6ec5a7e86b3d5b9d0b40020328d3060aa252b84;p=archiva.git [MRM-1573] start work on browse screen git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1244606 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/BrowseService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/BrowseService.java new file mode 100644 index 000000000..9e6b85423 --- /dev/null +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/BrowseService.java @@ -0,0 +1,43 @@ +package org.apache.archiva.rest.api.services; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.archiva.rest.api.model.GroupIdList; +import org.codehaus.plexus.redback.authorization.RedbackAuthorization; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import java.util.List; + +/** + * @author Olivier Lamy + * @since 1.4-M3 + */ +@Path( "/browseService/" ) +public interface BrowseService +{ + @Path( "rootGroups" ) + @GET + @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) + @RedbackAuthorization( noRestriction = true, noPermission = false ) + GroupIdList getRootGroups() + throws ArchivaRestServiceException; +} diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/AbstractRestService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/AbstractRestService.java index 5a252633c..3cf1545b7 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/AbstractRestService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/AbstractRestService.java @@ -20,8 +20,14 @@ package org.apache.archiva.rest.services; import org.apache.archiva.admin.model.AuditInformation; import org.apache.archiva.audit.AuditListener; +import org.apache.archiva.metadata.repository.RepositorySessionFactory; +import org.apache.archiva.security.AccessDeniedException; +import org.apache.archiva.security.ArchivaSecurityException; +import org.apache.archiva.security.PrincipalNotFoundException; +import org.apache.archiva.security.UserRepositories; import org.apache.commons.lang.StringUtils; import org.codehaus.plexus.redback.users.User; +import org.codehaus.plexus.redback.users.UserManager; import org.codehaus.redback.rest.services.RedbackAuthenticationThreadLocal; import org.codehaus.redback.rest.services.RedbackRequestInformation; import org.slf4j.Logger; @@ -29,9 +35,11 @@ import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationContext; import javax.inject.Inject; +import javax.inject.Named; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.core.Context; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -50,6 +58,14 @@ public abstract class AbstractRestService @Inject private List auditListeners = new ArrayList(); + @Inject + private UserRepositories userRepositories; + + + @Inject + @Named( value = "repositorySessionFactory" ) + protected RepositorySessionFactory repositorySessionFactory; + @Context protected HttpServletRequest httpServletRequest; @@ -71,6 +87,39 @@ public abstract class AbstractRestService this.auditListeners = auditListeners; } + protected List getObservableRepos() + { + try + { + List ids = userRepositories.getObservableRepositoryIds( getPrincipal() ); + return ids == null ? Collections.emptyList() : ids; + } + catch ( PrincipalNotFoundException e ) + { + log.warn( e.getMessage(), e ); + } + catch ( AccessDeniedException e ) + { + log.warn( e.getMessage(), e ); + } + catch ( ArchivaSecurityException e ) + { + log.warn( e.getMessage(), e ); + } + return Collections.emptyList(); + } + + protected String getPrincipal() + { + RedbackRequestInformation redbackRequestInformation = RedbackAuthenticationThreadLocal.get(); + + return redbackRequestInformation == null + ? UserManager.GUEST_USERNAME + : ( redbackRequestInformation.getUser() == null + ? UserManager.GUEST_USERNAME + : redbackRequestInformation.getUser().getUsername() ); + } + protected String getBaseUrl( HttpServletRequest req ) { return req.getScheme() + "://" + req.getServerName() + ( req.getServerPort() == 80 diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java new file mode 100644 index 000000000..c83b9f19c --- /dev/null +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java @@ -0,0 +1,133 @@ +package org.apache.archiva.rest.services; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.archiva.metadata.repository.MetadataResolutionException; +import org.apache.archiva.metadata.repository.MetadataResolver; +import org.apache.archiva.metadata.repository.RepositorySession; +import org.apache.archiva.rest.api.model.GroupIdList; +import org.apache.archiva.rest.api.services.ArchivaRestServiceException; +import org.apache.archiva.rest.api.services.BrowseService; +import org.apache.commons.collections.CollectionUtils; +import org.springframework.stereotype.Service; + +import javax.ws.rs.core.Response; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; + +/** + * @author Olivier Lamy + * @since 1.4-M3 + */ +@Service( "browseService#rest" ) +public class DefaultBrowseService + extends AbstractRestService + implements BrowseService +{ + + public GroupIdList getRootGroups() + throws ArchivaRestServiceException + { + List selectedRepos = getObservableRepos(); + if ( CollectionUtils.isEmpty( selectedRepos ) ) + { + // FIXME 403 ??? + return new GroupIdList( Collections.emptyList() ); + } + + Set namespaces = new LinkedHashSet(); + + // TODO: this logic should be optional, particularly remembering we want to keep this code simple + // it is located here to avoid the content repository implementation needing to do too much for what + // is essentially presentation code + Set namespacesToCollapse; + RepositorySession repositorySession = repositorySessionFactory.createSession(); + try + { + MetadataResolver metadataResolver = repositorySession.getResolver(); + namespacesToCollapse = new LinkedHashSet(); + for ( String repoId : selectedRepos ) + { + namespacesToCollapse.addAll( metadataResolver.resolveRootNamespaces( repositorySession, repoId ) ); + } + + for ( String n : namespacesToCollapse ) + { + // TODO: check performance of this + namespaces.add( collapseNamespaces( repositorySession, metadataResolver, selectedRepos, n ) ); + } + } + catch ( MetadataResolutionException e ) + { + throw new ArchivaRestServiceException( e.getMessage(), + Response.Status.INTERNAL_SERVER_ERROR.getStatusCode() ); + } + finally + { + repositorySession.close(); + } + + return new GroupIdList( getSortedList( namespaces ) ); + } + + //--------------------------- + // internals + //--------------------------- + + private List getSortedList( Set set ) + { + List list = new ArrayList( set ); + Collections.sort( list ); + return list; + } + + private String collapseNamespaces( RepositorySession repositorySession, MetadataResolver metadataResolver, + Collection repoIds, String n ) + throws MetadataResolutionException + { + Set subNamespaces = new LinkedHashSet(); + for ( String repoId : repoIds ) + { + subNamespaces.addAll( metadataResolver.resolveNamespaces( repositorySession, repoId, n ) ); + } + if ( subNamespaces.size() != 1 ) + { + log.debug( "{} is not collapsible as it has sub-namespaces: {}", n, subNamespaces ); + return n; + } + else + { + for ( String repoId : repoIds ) + { + Collection projects = metadataResolver.resolveProjects( repositorySession, repoId, n ); + if ( projects != null && !projects.isEmpty() ) + { + log.debug( "{} is not collapsible as it has projects", n ); + return n; + } + } + return collapseNamespaces( repositorySession, metadataResolver, repoIds, + n + "." + subNamespaces.iterator().next() ); + } + } +} diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultManagedRepositoriesService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultManagedRepositoriesService.java index 490a6f570..51dd403a5 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultManagedRepositoriesService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultManagedRepositoriesService.java @@ -56,20 +56,12 @@ public class DefaultManagedRepositoriesService @Inject private ManagedRepositoryAdmin managedRepositoryAdmin; - @Inject - private PlexusSisuBridge plexusSisuBridge; - @Inject private RepositoryCommonValidator repositoryCommonValidator; @Inject private RepositoryStatisticsManager repositoryStatisticsManager; - @Inject - @Named( value = "repositorySessionFactory" ) - protected RepositorySessionFactory repositorySessionFactory; - - public List getManagedRepositories() throws ArchivaRestServiceException { diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultSearchService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultSearchService.java index 5d306f439..aedd2641d 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultSearchService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultSearchService.java @@ -61,14 +61,10 @@ public class DefaultSearchService implements SearchService { - private Logger log = LoggerFactory.getLogger( getClass() ); @Inject private RepositorySearch repositorySearch; - @Inject - private UserRepositories userRepositories; - public List quickSearch( String queryString ) throws ArchivaRestServiceException { @@ -180,40 +176,6 @@ public class DefaultSearchService return null; //To change body of implemented methods use File | Settings | File Templates. } - - protected List getObservableRepos() - { - try - { - List ids = userRepositories.getObservableRepositoryIds( getPrincipal() ); - return ids == null ? Collections.emptyList() : ids; - } - catch ( PrincipalNotFoundException e ) - { - log.warn( e.getMessage(), e ); - } - catch ( AccessDeniedException e ) - { - log.warn( e.getMessage(), e ); - } - catch ( ArchivaSecurityException e ) - { - log.warn( e.getMessage(), e ); - } - return Collections.emptyList(); - } - - protected String getPrincipal() - { - RedbackRequestInformation redbackRequestInformation = RedbackAuthenticationThreadLocal.get(); - - return redbackRequestInformation == null - ? UserManager.GUEST_USERNAME - : ( redbackRequestInformation.getUser() == null - ? UserManager.GUEST_USERNAME - : redbackRequestInformation.getUser().getUsername() ); - } - protected List getArtifacts( SearchResults searchResults ) { diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/resources/META-INF/spring-context.xml b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/resources/META-INF/spring-context.xml index 6f0b6b9bc..5ebd88b0e 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/resources/META-INF/spring-context.xml +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/resources/META-INF/spring-context.xml @@ -58,6 +58,7 @@ + diff --git a/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/archiva/search.js b/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/archiva/search.js index b8dcdaad0..4d55519c4 100644 --- a/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/archiva/search.js +++ b/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/archiva/search.js @@ -17,8 +17,29 @@ * under the License. */ $(function() { + + BrowseTopViewModel=function(groupIds){ + this.groupIds=groupIds; + + + } + displayBrowse=function(){ - $("#main-content" ).html("coming soon :-)"); + var mainContent = $("#main-content"); + mainContent.html(mediumSpinnerImg()); + $.ajax("restServices/archivaServices/browseService/rootGroups", { + type: "GET", + dataType: 'json', + success: function(data) { + var groupdIds = $.map(data.groupIdList.groupIds,function(item){ + return item; + }); + $.log("size:"+groupdIds.length); + var browseTopViewModel = new BrowseTopViewModel(groupdIds); + mainContent.html($("#browse-tmpl" ).tmpl()); + ko.applyBindings(browseTopViewModel,mainContent.find("#browse_result" ).get(0)); + } + }); } displaySearch=function(){ diff --git a/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/archiva/templates/search.html b/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/archiva/templates/search.html index 9ba7181cb..03cd2d97d 100644 --- a/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/archiva/templates/search.html +++ b/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/archiva/templates/search.html @@ -15,4 +15,23 @@ ~ KIND, either express or implied. See the License for the ~ specific language governing permissions and limitations ~ under the License. ---> \ No newline at end of file +--> + + + + \ No newline at end of file diff --git a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/archiva/web/action/BrowseAction.java b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/archiva/web/action/BrowseAction.java index 09d180a06..a0d18cd31 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/archiva/web/action/BrowseAction.java +++ b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/archiva/web/action/BrowseAction.java @@ -183,9 +183,9 @@ public class BrowseAction return SUCCESS; } - private ArrayList getSortedList( Set set ) + private List getSortedList( Set set ) { - ArrayList list = new ArrayList( set ); + List list = new ArrayList( set ); Collections.sort( list ); return list; }