From ac7babe5a1d3e5cf1798dd3c1948811759e0e924 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Tue, 23 Oct 2007 19:21:19 +0000 Subject: [PATCH] [MRM-565] Archiva 1.0-beta-3 fails in 404 on all legacy request. Preparing RepositoryRequest for support needed to detect ... * Support Files * Maven Metadata * Artifacts And translate to a native path for the managed repository. git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@587623 13f79535-47bb-0310-9956-ffa450edef68 --- .../repository/content/RepositoryRequest.java | 151 +++++++++++- .../content/RepositoryRequestTest.java | 227 +++++++++++++++++- 2 files changed, 366 insertions(+), 12 deletions(-) diff --git a/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/content/RepositoryRequest.java b/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/content/RepositoryRequest.java index d6f5dfce7..12158586b 100644 --- a/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/content/RepositoryRequest.java +++ b/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/content/RepositoryRequest.java @@ -23,7 +23,9 @@ import org.apache.commons.lang.StringUtils; import org.apache.maven.archiva.configuration.ArchivaConfiguration; import org.apache.maven.archiva.configuration.FileTypes; import org.apache.maven.archiva.model.ArtifactReference; +import org.apache.maven.archiva.repository.ManagedRepositoryContent; import org.apache.maven.archiva.repository.layout.LayoutException; +import org.apache.maven.archiva.repository.metadata.MetadataTools; import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; import org.codehaus.plexus.registry.Registry; @@ -97,13 +99,11 @@ public class RepositoryRequest public ArtifactReference toArtifactReference( String requestedPath ) throws LayoutException { - String pathParts[] = StringUtils.splitPreserveAllTokens( requestedPath, '/' ); - - if ( pathParts.length > 3 ) + if ( isDefault( requestedPath ) ) { return DefaultPathParser.toArtifactReference( requestedPath ); } - else if ( pathParts.length == 3 ) + else if ( isLegacy( requestedPath ) ) { return LegacyPathParser.toArtifactReference( requestedPath ); } @@ -112,6 +112,149 @@ public class RepositoryRequest throw new LayoutException( "Not a valid request path layout, too short." ); } } + + /** + *

+ * Tests the path to see if it conforms to the expectations of a metadata request. + *

+ *

+ * NOTE: This does a cursory check on the path's last element. A result of true + * from this method is not a guarantee that the metadata is in a valid format, or + * that it even contains data. + *

+ * + * @param requestedPath the path to test. + * @return true if the requestedPath is likely a metadata request. + */ + public boolean isMetadata( String requestedPath ) + { + return requestedPath.endsWith( "/" + MetadataTools.MAVEN_METADATA ); + } + + /** + *

+ * Tests the path to see if it conforms to the expectations of a support file request. + *

+ *

+ * Tests for .sha1, .md5, .asc, and .php. + *

+ *

+ * NOTE: This does a cursory check on the path's extension only. A result of true + * from this method is not a guarantee that the support resource is in a valid format, or + * that it even contains data. + *

+ * + * @param requestedPath the path to test. + * @return true if the requestedPath is likely that of a support file request. + */ + public boolean isSupportFile( String requestedPath ) + { + int idx = requestedPath.lastIndexOf( '.' ); + if ( idx <= 0 ) + { + return false; + } + + String ext = requestedPath.substring( idx ); + return ( ".sha1".equals( ext ) || ".md5".equals( ext ) || ".asc".equals( ext ) || ".pgp".equals( ext ) ); + } + + /** + *

+ * Tests the path to see if it conforms to the expectations of a default layout request. + *

+ *

+ * NOTE: This does a cursory check on the count of path elements only. A result of + * true from this method is not a guarantee that the path sections are valid and + * can be resolved to an artifact reference. use {@link #toArtifactReference(String)} + * if you want a more complete analysis of the validity of the path. + *

+ * + * @param requestedPath the path to test. + * @return true if the requestedPath is likely that of a default layout request. + */ + public boolean isDefault( String requestedPath ) + { + if ( StringUtils.isBlank( requestedPath ) ) + { + return false; + } + + String pathParts[] = StringUtils.splitPreserveAllTokens( requestedPath, '/' ); + return pathParts.length > 3; + } + + /** + *

+ * Tests the path to see if it conforms to the expectations of a legacy layout request. + *

+ *

+ * NOTE: This does a cursory check on the count of path elements only. A result of + * true from this method is not a guarantee that the path sections are valid and + * can be resolved to an artifact reference. use {@link #toArtifactReference(String)} + * if you want a more complete analysis of the validity of the path. + *

+ * + * @param requestedPath the path to test. + * @return true if the requestedPath is likely that of a legacy layout request. + */ + public boolean isLegacy( String requestedPath ) + { + if ( StringUtils.isBlank( requestedPath ) ) + { + return false; + } + + String pathParts[] = StringUtils.splitPreserveAllTokens( requestedPath, '/' ); + return pathParts.length == 3; + } + + /** + * Adjust the requestedPath to conform to the native layout of the provided {@link ManagedRepositoryContent}. + * + * @param requestedPath the incoming requested path. + * @param repository the repository to adjust to. + * @return the adjusted (to native) path. + * @throws LayoutException if the path cannot be parsed. + */ + public String toNativePath( String requestedPath, ManagedRepositoryContent repository ) throws LayoutException + { + if ( StringUtils.isBlank( requestedPath ) ) + { + throw new LayoutException( "Request Path is blank." ); + } + + String referencedResource = requestedPath; + // No checksum by default. + String supportfile = ""; + + // Figure out support file, and actual referencedResource. + if( isSupportFile( requestedPath ) ) + { + int idx = requestedPath.lastIndexOf( '.' ); + referencedResource = requestedPath.substring( 0, idx ); + supportfile = requestedPath.substring( idx ); + } + + if ( isMetadata( referencedResource ) ) + { + if ( repository instanceof ManagedLegacyRepositoryContent ) + { + throw new LayoutException( "Cannot translate metadata request to legacy layout." ); + } + + /* Nothing to translate. + * Default layout is the only layout that can contain maven-metadata.xml files, and + * if the managedRepository is layout legacy, this request would never occur. + */ + return requestedPath; + } + + // Treat as an artifact reference. + ArtifactReference ref = toArtifactReference( referencedResource ); + String adjustedPath = repository.toPath( ref ); + return adjustedPath + supportfile; + } public void initialize() throws InitializationException diff --git a/archiva-base/archiva-repository-layer/src/test/java/org/apache/maven/archiva/repository/content/RepositoryRequestTest.java b/archiva-base/archiva-repository-layer/src/test/java/org/apache/maven/archiva/repository/content/RepositoryRequestTest.java index 56ba649bb..0d4cfa791 100644 --- a/archiva-base/archiva-repository-layer/src/test/java/org/apache/maven/archiva/repository/content/RepositoryRequestTest.java +++ b/archiva-base/archiva-repository-layer/src/test/java/org/apache/maven/archiva/repository/content/RepositoryRequestTest.java @@ -22,8 +22,11 @@ package org.apache.maven.archiva.repository.content; import org.apache.commons.lang.StringUtils; import org.apache.maven.archiva.model.ArtifactReference; import org.apache.maven.archiva.repository.AbstractRepositoryLayerTestCase; +import org.apache.maven.archiva.repository.ManagedRepositoryContent; import org.apache.maven.archiva.repository.layout.LayoutException; +import java.io.File; + /** * RepositoryRequestTest * @@ -185,8 +188,7 @@ public class RepositoryRequestTest public void testValidDefaultOddDottedArtifactId() throws Exception { - assertValid( - "com/company/department/com.company.department.project/0.2/com.company.department.project-0.2.pom", + assertValid( "com/company/department/com.company.department.project/0.2/com.company.department.project-0.2.pom", "com.company.department", "com.company.department.project", "0.2", null, "pom" ); } @@ -211,12 +213,221 @@ public class RepositoryRequestTest assertTrue( repoRequest.isArtifact( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) ); assertTrue( repoRequest.isArtifact( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) ); - assertFalse( repoRequest.isArtifact( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.sha1" )); - assertFalse( repoRequest.isArtifact( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.md5" )); - assertFalse( repoRequest.isArtifact( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.asc" )); - assertFalse( repoRequest.isArtifact( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" )); - assertFalse( repoRequest.isArtifact( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml" )); - assertFalse( repoRequest.isArtifact( "org/apache/derby/derby/maven-metadata.xml" )); + assertFalse( repoRequest.isArtifact( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.sha1" ) ); + assertFalse( repoRequest.isArtifact( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.md5" ) ); + assertFalse( repoRequest.isArtifact( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.asc" ) ); + assertFalse( repoRequest.isArtifact( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) ); + assertFalse( repoRequest.isArtifact( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml" ) ); + assertFalse( repoRequest.isArtifact( "org/apache/derby/derby/maven-metadata.xml" ) ); + } + + public void testIsSupportFile() + { + assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.sha1" ) ); + assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.md5" ) ); + assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.asc" ) ); + assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) ); + assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) ); + assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.md5" ) ); + + assertFalse( repoRequest.isSupportFile( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) ); + assertFalse( repoRequest.isSupportFile( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) ); + assertFalse( repoRequest.isSupportFile( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) ); + assertFalse( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) ); + assertFalse( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml" ) ); + assertFalse( repoRequest.isSupportFile( "org/apache/derby/derby/maven-metadata.xml" ) ); + } + + public void testIsMetadata() + { + assertTrue( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml" )); + assertTrue( repoRequest.isMetadata( "org/apache/derby/derby/maven-metadata.xml" )); + + assertFalse( repoRequest.isMetadata( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) ); + assertFalse( repoRequest.isMetadata( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) ); + assertFalse( repoRequest.isMetadata( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) ); + assertFalse( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) ); + assertFalse( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) ); + assertFalse( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) ); + } + + public void testIsDefault() + { + assertFalse( repoRequest.isDefault( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) ); + assertFalse( repoRequest.isDefault( "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom" ) ); + assertFalse( repoRequest.isDefault( "commons-lang/jars/commons-lang-2.1-javadoc.jar" ) ); + + assertTrue( repoRequest.isDefault( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) ); + assertTrue( repoRequest.isDefault( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) ); + assertTrue( repoRequest.isDefault( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) ); + assertTrue( repoRequest.isDefault( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) ); + assertTrue( repoRequest.isDefault( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) ); + + assertFalse( repoRequest.isDefault( null ) ); + assertFalse( repoRequest.isDefault( "" ) ); + assertFalse( repoRequest.isDefault( "foo" ) ); + assertFalse( repoRequest.isDefault( "some.short/path" ) ); + } + + public void testIsLegacy() + { + assertTrue( repoRequest.isLegacy( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) ); + assertTrue( repoRequest.isLegacy( "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom" ) ); + assertTrue( repoRequest.isLegacy( "commons-lang/jars/commons-lang-2.1-javadoc.jar" ) ); + + assertFalse( repoRequest.isLegacy( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) ); + assertFalse( repoRequest.isLegacy( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) ); + assertFalse( repoRequest.isLegacy( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) ); + assertFalse( repoRequest.isLegacy( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) ); + assertFalse( repoRequest.isLegacy( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) ); + + assertFalse( repoRequest.isLegacy( null ) ); + assertFalse( repoRequest.isLegacy( "" ) ); + assertFalse( repoRequest.isLegacy( "some.short/path" ) ); + } + + private ManagedRepositoryContent createManagedRepo( String layout ) + throws Exception + { + File repoRoot = getTestFile( "target/test-repo" ); + return createManagedRepositoryContent( "test-internal", "Internal Test Repo", repoRoot, layout ); + } + + public void testToNativePathMetadataDefaultToDefault() + throws Exception + { + ManagedRepositoryContent repository = createManagedRepo( "default" ); + + // Test (metadata) default to default + assertEquals( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1", repoRequest + .toNativePath( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1", repository ) ); + } + + public void testNativePathPomLegacyToDefault() + throws Exception + { + ManagedRepositoryContent repository = createManagedRepo( "default" ); + + // Test (pom) legacy to default + assertEquals( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0.pom", repoRequest + .toNativePath( "org.apache.derby/poms/derby-10.2.2.0.pom", repository ) ); + } + + public void testNativePathSupportFileLegacyToDefault() + throws Exception + { + ManagedRepositoryContent repository = createManagedRepo( "default" ); + + // Test (supportfile) legacy to default + assertEquals( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0.jar.sha1", repoRequest + .toNativePath( "org.apache.derby/jars/derby-10.2.2.0.jar.sha1", repository ) ); + } + + public void testNativePathBadRequestTooShort() + throws Exception + { + ManagedRepositoryContent repository = createManagedRepo( "default" ); + + // Test bad request path (too short) + try + { + repoRequest.toNativePath( "org.apache.derby/license.txt", repository ); + fail( "Should have thrown an exception about a too short path." ); + } + catch ( LayoutException e ) + { + // expected path. + } + } + + public void testNativePathBadRequestBlank() + throws Exception + { + ManagedRepositoryContent repository = createManagedRepo( "default" ); + + // Test bad request path (too short) + try + { + repoRequest.toNativePath( "", repository ); + fail( "Should have thrown an exception about an blank request." ); + } + catch ( LayoutException e ) + { + // expected path. + } + } + + public void testNativePathBadRequestNull() + throws Exception + { + ManagedRepositoryContent repository = createManagedRepo( "default" ); + + // Test bad request path (too short) + try + { + repoRequest.toNativePath( null, repository ); + fail( "Should have thrown an exception about an null request." ); + } + catch ( LayoutException e ) + { + // expected path. + } + } + + public void testNativePathBadRequestUnknownType() + throws Exception + { + ManagedRepositoryContent repository = createManagedRepo( "default" ); + + // Test bad request path (too short) + try + { + repoRequest.toNativePath( "org/apache/derby/derby/10.2.2.0/license.txt", repository ); + fail( "Should have thrown an exception about an invalid type." ); + } + catch ( LayoutException e ) + { + // expected path. + } + } + + public void testToNativePathLegacyMetadataDefaultToLegacy() + throws Exception + { + ManagedRepositoryContent repository = createManagedRepo( "legacy" ); + + // Test (metadata) default to legacy + + // Special Case: This direction is not supported, should throw a LayoutException. + try + { + repoRequest.toNativePath( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml", repository ); + fail("Should have thrown a LayoutException, can't translate a maven-metadata.xml to a legacy layout."); + } + catch(LayoutException e) + { + // expected path. + } + } + + public void testNativePathPomDefaultToLegacy() + throws Exception + { + ManagedRepositoryContent repository = createManagedRepo( "legacy" ); + + // Test (pom) default to legacy + assertEquals( "org.apache.derby/poms/derby-10.2.2.0.pom", repoRequest + .toNativePath( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0.pom", repository ) ); + } + + public void testNativePathSupportFileDefaultToLegacy() + throws Exception + { + ManagedRepositoryContent repository = createManagedRepo( "legacy" ); + + // Test (supportfile) default to legacy + assertEquals( "org.apache.derby/jars/derby-10.2.2.0.jar.sha1", repoRequest + .toNativePath( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0.jar.sha1", repository ) ); } private void assertValid( String path, String groupId, String artifactId, String version, String classifier, -- 2.39.5