From aab7502db726fd8d998b8bf92f982d501e9e4848 Mon Sep 17 00:00:00 2001 From: "Maria Odea B. Ching" Date: Wed, 18 Mar 2009 09:56:41 +0000 Subject: [PATCH] [MRM-901] o merge all versions of artifacts with same groupId in all repos under a group (browse) o added test case git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@755532 13f79535-47bb-0310-9956-ffa450edef68 --- .../webdav/ArchivaDavResourceFactory.java | 2 +- .../webdav/ArchivaDavResourceLocator.java | 13 ++++++- .../archiva/webdav/util/IndexWriter.java | 35 +++++++++++++---- .../webdav/MockServletAuthenticator.java | 34 ++++++++++++++++ .../RepositoryServletRepositoryGroupTest.java | 39 +++++++++++++------ .../archiva/webdav/RepositoryServletTest.xml | 11 ++++++ 6 files changed, 113 insertions(+), 21 deletions(-) create mode 100644 archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/maven/archiva/webdav/MockServletAuthenticator.java diff --git a/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/maven/archiva/webdav/ArchivaDavResourceFactory.java b/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/maven/archiva/webdav/ArchivaDavResourceFactory.java index 201bb828d..54fb20e50 100644 --- a/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/maven/archiva/webdav/ArchivaDavResourceFactory.java +++ b/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/maven/archiva/webdav/ArchivaDavResourceFactory.java @@ -192,7 +192,7 @@ public class ArchivaDavResourceFactory repositories.addAll( repoGroupConfig.getRepositories() ); // handle browse requests for virtual repos - if ( RepositoryPathUtil.getLogicalResource( locator.getResourcePath() ).endsWith( "/" ) ) + if ( RepositoryPathUtil.getLogicalResource( archivaLocator.getOrigResourcePath() ).endsWith( "/" ) ) { return getResource( request, repositories, archivaLocator ); } diff --git a/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/maven/archiva/webdav/ArchivaDavResourceLocator.java b/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/maven/archiva/webdav/ArchivaDavResourceLocator.java index 828667b7a..30c5335f7 100644 --- a/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/maven/archiva/webdav/ArchivaDavResourceLocator.java +++ b/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/maven/archiva/webdav/ArchivaDavResourceLocator.java @@ -37,6 +37,10 @@ public class ArchivaDavResourceLocator private final String repositoryId; private final DavLocatorFactory davLocatorFactory; + + // retains the trailing '/' at the end of the path, which is used to determine if it is a + // virtual repo browse request + private final String origResourcePath; public ArchivaDavResourceLocator( String prefix, String resourcePath, String repositoryId, DavLocatorFactory davLocatorFactory ) @@ -63,8 +67,10 @@ public class ArchivaDavResourceLocator href = hrefPrefix + escapedPath; + this.origResourcePath = path; + //Remove trailing slashes otherwise Text.getRelativeParent fails - if (resourcePath.endsWith("/") && resourcePath.length() > 1) + if ( resourcePath.endsWith( "/" ) && resourcePath.length() > 1 ) { path = resourcePath.substring( 0, resourcePath.length() - 1 ); } @@ -155,4 +161,9 @@ public class ArchivaDavResourceLocator } return false; } + + public String getOrigResourcePath() + { + return origResourcePath; + } } diff --git a/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/maven/archiva/webdav/util/IndexWriter.java b/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/maven/archiva/webdav/util/IndexWriter.java index 25e995532..4df207416 100644 --- a/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/maven/archiva/webdav/util/IndexWriter.java +++ b/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/maven/archiva/webdav/util/IndexWriter.java @@ -125,26 +125,45 @@ public class IndexWriter else { // virtual repository - filter unique directories - Map uniqueChildFiles = new HashMap(); + Map> uniqueChildFiles = new HashMap>(); List sortedList = new ArrayList(); for( File resource : localResources ) - { + { List files = new ArrayList( Arrays.asList( resource.listFiles() ) ); - for ( File file : files ) { + List mergedChildFiles = new ArrayList(); if( uniqueChildFiles.get( file.getName() ) == null ) { - uniqueChildFiles.put( file.getName(), file ); - sortedList.add( file.getName() ); - } + mergedChildFiles.add( file.getAbsolutePath() ); + } + else + { + mergedChildFiles = uniqueChildFiles.get( file.getName() ); + if( !mergedChildFiles.contains( file.getAbsolutePath() ) ) + { + mergedChildFiles.add( file.getAbsolutePath() ); + } + } + uniqueChildFiles.put( file.getName(), mergedChildFiles ); + sortedList.add( file.getName() ); } } Collections.sort( sortedList ); + List written = new ArrayList(); for ( String fileName : sortedList ) - { - writeHyperlink( writer, fileName, ( (File) uniqueChildFiles.get( fileName ) ).isDirectory()); + { + List childFilesFromMap = uniqueChildFiles.get( fileName ); + for( String childFilePath : childFilesFromMap ) + { + File childFile = new File( childFilePath ); + if( !written.contains( childFile.getName() ) ) + { + written.add( childFile.getName() ); + writeHyperlink( writer, fileName, childFile.isDirectory() ); + } + } } } } diff --git a/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/maven/archiva/webdav/MockServletAuthenticator.java b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/maven/archiva/webdav/MockServletAuthenticator.java new file mode 100644 index 000000000..4ea71a929 --- /dev/null +++ b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/maven/archiva/webdav/MockServletAuthenticator.java @@ -0,0 +1,34 @@ +package org.apache.maven.archiva.webdav; + +/* + * 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.maven.archiva.security.ArchivaServletAuthenticator; +import org.codehaus.plexus.redback.authorization.UnauthorizedException; + +public class MockServletAuthenticator + extends ArchivaServletAuthenticator +{ + @Override + public boolean isAuthorized( String principal, String repoId, boolean isWriteRequest ) + throws UnauthorizedException + { + return true; + } +} diff --git a/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/maven/archiva/webdav/RepositoryServletRepositoryGroupTest.java b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/maven/archiva/webdav/RepositoryServletRepositoryGroupTest.java index 853af6d17..47a94a9e3 100644 --- a/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/maven/archiva/webdav/RepositoryServletRepositoryGroupTest.java +++ b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/maven/archiva/webdav/RepositoryServletRepositoryGroupTest.java @@ -27,6 +27,7 @@ import java.util.List; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.FileUtils; +import org.apache.commons.lang.StringUtils; import org.apache.maven.archiva.configuration.Configuration; import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration; @@ -210,17 +211,7 @@ public class RepositoryServletRepositoryGroupTest assertResponseMethodNotAllowed( response ); } - - public void testBrowseRepositoryGroup() - throws Exception - { - WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS ); - WebResponse response = sc.getResponse( request ); - - assertNotNull( "Should have received a response", response ); - assertEquals( "Should have been an 401 response code.", HttpServletResponse.SC_UNAUTHORIZED, response.getResponseCode() ); - } - + // MRM-872 public void testGetMergedMetadata() throws Exception @@ -287,6 +278,32 @@ public class RepositoryServletRepositoryGroupTest assertResponseOK( response ); assertEquals( "5b85ea4aa5f52bb76760041a52f98de8 maven-metadata-group-with-valid-repos.xml", response.getText().trim() ); } + + // MRM-901 + public void testBrowseWithTwoArtifactsWithSameGroupIdInRepos() + throws Exception + { + String resourceName = "dummy/dummy-artifact/1.0/dummy-artifact-1.0.txt"; + + File dummyInternalResourceFile = new File( repoRootFirst, resourceName ); + dummyInternalResourceFile.getParentFile().mkdirs(); + FileUtils.writeStringToFile( dummyInternalResourceFile, "first", null ); + + resourceName = "dummy/dummy-artifact/2.0/dummy-artifact-2.0.txt"; + + File dummyReleasesResourceFile = new File( repoRootLast, resourceName ); + dummyReleasesResourceFile.getParentFile().mkdirs(); + FileUtils.writeStringToFile( dummyReleasesResourceFile, "last", null ); + + WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/dummy-artifact/" ); + WebResponse response = sc.getResource( request ); + + assertResponseOK( response ); + assertTrue( StringUtils.contains( response.getText(), "Collection" ) ); + assertTrue( StringUtils.contains( response.getText(), "dummy/dummy-artifact" ) ); + assertTrue( StringUtils.contains( response.getText(), "1.0" ) ); + assertTrue( StringUtils.contains( response.getText(), "2.0" ) ); + } protected void assertResponseMethodNotAllowed( WebResponse response ) { diff --git a/archiva-modules/archiva-web/archiva-webdav/src/test/resources/org/apache/maven/archiva/webdav/RepositoryServletTest.xml b/archiva-modules/archiva-web/archiva-webdav/src/test/resources/org/apache/maven/archiva/webdav/RepositoryServletTest.xml index e73c0f41f..017d761b8 100644 --- a/archiva-modules/archiva-web/archiva-webdav/src/test/resources/org/apache/maven/archiva/webdav/RepositoryServletTest.xml +++ b/archiva-modules/archiva-web/archiva-webdav/src/test/resources/org/apache/maven/archiva/webdav/RepositoryServletTest.xml @@ -93,6 +93,17 @@ default org.apache.maven.archiva.webdav.BypassSecuritySystem + + + org.apache.maven.archiva.security.ServletAuthenticator + org.apache.maven.archiva.webdav.MockServletAuthenticator + + + org.codehaus.plexus.redback.system.SecuritySystem + securitySystem + + + org.apache.maven.archiva.webdav.ArchivaDavResourceFactory -- 2.39.5