Package org.apache.maven.archiva.repository.layout

Examples of org.apache.maven.archiva.repository.layout.LayoutException


         */

        if ( pathParts.length != 3 )
        {
            // Illegal Path Parts Length.
            throw new LayoutException( INVALID_ARTIFACT_PATH
                + "legacy paths should only have 3 parts [groupId]/[type]s/[artifactId]-[version].[type], found "
                + pathParts.length + " instead." );
        }

        // The Group ID.
        artifact.setGroupId( pathParts[0] );

        // The Expected Type.
        String expectedType = pathParts[1];

        // Sanity Check: expectedType should end in "s".
        if ( !expectedType.endsWith( "s" ) )
        {
            throw new LayoutException( INVALID_ARTIFACT_PATH
                + "legacy paths should have an expected type ending in [s] in the second part of the path." );
        }

        // The Filename.
        String filename = pathParts[2];

        FilenameParser parser = new FilenameParser( filename );

        artifact.setArtifactId( parser.nextNonVersion() );

        // Sanity Check: does it have an artifact id?
        if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
        {
            // Special Case: The filename might start with a version id (like "test-arch-1.0.jar").
            int idx = filename.indexOf( '-' );
            if ( idx > 0 )
            {
                parser.reset();
                // Take the first section regardless of content.
                String artifactId = parser.next();
               
                // Is there anything more that is considered not a version id?
                String moreArtifactId = parser.nextNonVersion();
                if ( StringUtils.isNotBlank( moreArtifactId ) )
                {
                    artifact.setArtifactId( artifactId + "-" + moreArtifactId );
                }
                else
                {
                    artifact.setArtifactId( artifactId );
                }
            }

            // Sanity Check: still no artifact id?
            if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
            {
                throw new LayoutException( INVALID_ARTIFACT_PATH + "no artifact id present." );
            }
        }

        artifact.setVersion( parser.remaining() );

        // Sanity Check: does it have a version?
        if ( StringUtils.isEmpty( artifact.getVersion() ) )
        {
            // Special Case: use last section of artifactId as version.
            String artifactId = artifact.getArtifactId();
            int idx = artifactId.lastIndexOf( '-' );
            if ( idx > 0 )
            {
                artifact.setVersion( artifactId.substring( idx + 1 ) );
                artifact.setArtifactId( artifactId.substring( 0, idx ) );
            }
            else
            {
                throw new LayoutException( INVALID_ARTIFACT_PATH + "no version found." );
            }
        }

        // Set Type
        artifact.setType( ArtifactExtensionMapping.guessTypeFromFilename( filename ) );
       
        // Sanity Check: does it have an extension?
        if ( StringUtils.isEmpty( artifact.getType() ) )
        {
            throw new LayoutException( INVALID_ARTIFACT_PATH + "no extension found." );
        }

        // Special Case with Maven Plugins
        if ( StringUtils.equals( "jar", artifact.getType() ) && StringUtils.equals( "plugins", expectedType ) )
        {
            artifact.setType( ArtifactExtensionMapping.MAVEN_PLUGIN );
        }
        else
        {
            // Sanity Check: does extension match pathType on path?
            String trimPathType = expectedType.substring( 0, expectedType.length() - 1 );
   
            String expectedExtension = ArtifactExtensionMapping.getExtension( trimPathType );
            String actualExtension = parser.getExtension();
   
            if ( !expectedExtension.equals( actualExtension ) )
            {
                throw new LayoutException( INVALID_ARTIFACT_PATH + "mismatch on extension [" + actualExtension
                    + "] and layout specified type [" + expectedType + "] (which maps to extension: ["
                    + expectedExtension + "]) on path [" + path + "]" );
            }
        }

        String classifier = ArtifactClassifierMapping.getClassifier( artifact.getType() );
        if ( classifier != null )
        {
            String version = artifact.getVersion();
            if ( ! version.endsWith( "-" + classifier ) )
            {
                throw new LayoutException( INVALID_ARTIFACT_PATH + expectedType + " artifacts must use the classifier " + classifier );
            }
            version = version.substring( 0, version.length() - classifier.length() - 1 );
            artifact.setVersion( version );
            artifact.setClassifier( classifier );
        }
View Full Code Here


         */

        if ( pathParts.length != 3 )
        {
            // Illegal Path Parts Length.
            throw new LayoutException( INVALID_ARTIFACT_PATH +
                                           "legacy paths should only have 3 parts [groupId]/[type]s/[artifactId]-[version].[type], found " +
                                           pathParts.length + " instead." );
        }

        // The Group ID.
        artifact.setGroupId( pathParts[0] );

        // The Expected Type.
        String expectedType = pathParts[1];

        // Sanity Check: expectedType should end in "s".
        if ( !expectedType.endsWith( "s" ) )
        {
            throw new LayoutException( INVALID_ARTIFACT_PATH +
                                           "legacy paths should have an expected type ending in [s] in the second part of the path." );
        }

        // The Filename.
        String filename = pathParts[2];

        FilenameParser parser = new FilenameParser( filename );

        artifact.setArtifactId( parser.nextNonVersion() );

        // Sanity Check: does it have an artifact id?
        if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
        {
            // Special Case: The filename might start with a version id (like "test-arch-1.0.jar").
            int idx = filename.indexOf( '-' );
            if ( idx > 0 )
            {
                parser.reset();
                // Take the first section regardless of content.
                String artifactId = parser.next();

                // Is there anything more that is considered not a version id?
                String moreArtifactId = parser.nextNonVersion();
                if ( StringUtils.isNotBlank( moreArtifactId ) )
                {
                    artifact.setArtifactId( artifactId + "-" + moreArtifactId );
                }
                else
                {
                    artifact.setArtifactId( artifactId );
                }
            }

            // Sanity Check: still no artifact id?
            if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
            {
                throw new LayoutException( INVALID_ARTIFACT_PATH + "no artifact id present." );
            }
        }

        artifact.setVersion( parser.remaining() );

        // Sanity Check: does it have a version?
        if ( StringUtils.isEmpty( artifact.getVersion() ) )
        {
            // Special Case: use last section of artifactId as version.
            String artifactId = artifact.getArtifactId();
            int idx = artifactId.lastIndexOf( '-' );
            if ( idx > 0 )
            {
                artifact.setVersion( artifactId.substring( idx + 1 ) );
                artifact.setArtifactId( artifactId.substring( 0, idx ) );
            }
            else
            {
                throw new LayoutException( INVALID_ARTIFACT_PATH + "no version found." );
            }
        }

        String classifier = ArtifactClassifierMapping.getClassifier( expectedType );
        if ( classifier != null )
        {
            String version = artifact.getVersion();
            if ( !version.endsWith( "-" + classifier ) )
            {
                throw new LayoutException(
                    INVALID_ARTIFACT_PATH + expectedType + " artifacts must use the classifier " + classifier );
            }
            version = version.substring( 0, version.length() - classifier.length() - 1 );
            artifact.setVersion( version );
            artifact.setClassifier( classifier );
        }

        String extension = parser.getExtension();

        // Set Type
        String defaultExtension = expectedType.substring( 0, expectedType.length() - 1 );
        artifact.setType( ArtifactExtensionMapping.mapExtensionAndClassifierToType( classifier, extension,
                                                                                    defaultExtension ) );

        // Sanity Check: does it have an extension?
        if ( StringUtils.isEmpty( artifact.getType() ) )
        {
            throw new LayoutException( INVALID_ARTIFACT_PATH + "no extension found." );
        }

        // Special Case with Maven Plugins
        if ( StringUtils.equals( "jar", extension ) && StringUtils.equals( "plugins", expectedType ) )
        {
            artifact.setType( ArtifactExtensionMapping.MAVEN_ONE_PLUGIN );
        }
        else
        {
            // Sanity Check: does extension match pathType on path?
            String expectedExtension = ArtifactExtensionMapping.getExtension( artifact.getType() );

            if ( !expectedExtension.equals( extension ) )
            {
                throw new LayoutException( INVALID_ARTIFACT_PATH + "mismatch on extension [" + extension +
                                               "] and layout specified type [" + artifact.getType() +
                                               "] (which maps to extension: [" +
                                               expectedExtension + "]) on path [" + path + "]" );
            }
        }
View Full Code Here

         */

        if ( pathParts.length != 3 )
        {
            // Illegal Path Parts Length.
            throw new LayoutException( INVALID_ARTIFACT_PATH
                + "legacy paths should only have 3 parts [groupId]/[type]s/[artifactId]-[version].[type], found "
                + pathParts.length + " instead." );
        }

        // The Group ID.
        artifact.setGroupId( pathParts[0] );

        // The Expected Type.
        String expectedType = pathParts[1];

        // Sanity Check: expectedType should end in "s".
        if ( !expectedType.endsWith( "s" ) )
        {
            throw new LayoutException( INVALID_ARTIFACT_PATH
                + "legacy paths should have an expected type ending in [s] in the second part of the path." );
        }

        // The Filename.
        String filename = pathParts[2];

        FilenameParser parser = new FilenameParser( filename );

        artifact.setArtifactId( parser.nextNonVersion() );

        // Sanity Check: does it have an artifact id?
        if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
        {
            // Special Case: The filename might start with a version id (like "test-arch-1.0.jar").
            int idx = filename.indexOf( '-' );
            if ( idx > 0 )
            {
                parser.reset();
                // Take the first section regardless of content.
                String artifactId = parser.next();
               
                // Is there anything more that is considered not a version id?
                String moreArtifactId = parser.nextNonVersion();
                if ( StringUtils.isNotBlank( moreArtifactId ) )
                {
                    artifact.setArtifactId( artifactId + "-" + moreArtifactId );
                }
                else
                {
                    artifact.setArtifactId( artifactId );
                }
            }

            // Sanity Check: still no artifact id?
            if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
            {
                throw new LayoutException( INVALID_ARTIFACT_PATH + "no artifact id present." );
            }
        }

        artifact.setVersion( parser.remaining() );

        // Sanity Check: does it have a version?
        if ( StringUtils.isEmpty( artifact.getVersion() ) )
        {
            // Special Case: use last section of artifactId as version.
            String artifactId = artifact.getArtifactId();
            int idx = artifactId.lastIndexOf( '-' );
            if ( idx > 0 )
            {
                artifact.setVersion( artifactId.substring( idx + 1 ) );
                artifact.setArtifactId( artifactId.substring( 0, idx ) );
            }
            else
            {
                throw new LayoutException( INVALID_ARTIFACT_PATH + "no version found." );
            }
        }

        // Set Type
        artifact.setType( ArtifactExtensionMapping.guessTypeFromFilename( filename ) );
       
        // Sanity Check: does it have an extension?
        if ( StringUtils.isEmpty( artifact.getType() ) )
        {
            throw new LayoutException( INVALID_ARTIFACT_PATH + "no extension found." );
        }

        // Special Case with Maven Plugins
        if ( StringUtils.equals( "jar", artifact.getType() ) && StringUtils.equals( "plugins", expectedType ) )
        {
            artifact.setType( ArtifactExtensionMapping.MAVEN_PLUGIN );
        }
        else
        {
            // Sanity Check: does extension match pathType on path?
            String trimPathType = expectedType.substring( 0, expectedType.length() - 1 );
   
            String expectedExtension = ArtifactExtensionMapping.getExtension( trimPathType );
            String actualExtension = parser.getExtension();
   
            if ( !expectedExtension.equals( actualExtension ) )
            {
                throw new LayoutException( INVALID_ARTIFACT_PATH + "mismatch on extension [" + actualExtension
                    + "] and layout specified type [" + expectedType + "] (which maps to extension: ["
                    + expectedExtension + "]) on path [" + path + "]" );
            }
        }

View Full Code Here

    public ArtifactReference toArtifactReference( String requestedPath )
        throws LayoutException
    {
        if ( StringUtils.isBlank( requestedPath ) )
        {
            throw new LayoutException( "Blank request path is not a valid." );
        }
       
        String path = requestedPath;
        while ( path.startsWith( "/" ) )
        {
            path = path.substring( 1 );

            // Only slash? that's bad, mmm-kay?
            if ( "/".equals( path ) )
            {
                throw new LayoutException( "Invalid request path: Slash only." );
            }
        }

        if ( isDefault( path ) )
        {
            return DefaultPathParser.toArtifactReference( path );
        }
        else if ( isLegacy( path ) )
        {
            return LegacyPathParser.toArtifactReference( path );
        }
        else
        {
            throw new LayoutException( "Not a valid request path layout, too short." );
        }
    }
View Full Code Here

     */
    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.
View Full Code Here

    protected static ArtifactReference toArtifactReference( String path )
        throws LayoutException
    {
        if ( StringUtils.isBlank( path ) )
        {
            throw new LayoutException( "Unable to convert blank path." );
        }

        ArtifactReference artifact = new ArtifactReference();

        String normalizedPath = StringUtils.replace( path, "\\", "/" );
        String pathParts[] = StringUtils.split( normalizedPath, '/' );

        /* Minimum parts.
         *
         *   path = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar"
         *   path[0] = "commons-lang";        // The Group ID
         *   path[1] = "commons-lang";        // The Artifact ID
         *   path[2] = "2.1";                 // The Version
         *   path[3] = "commons-lang-2.1.jar" // The filename.
         */

        if ( pathParts.length < 4 )
        {
            // Illegal Path Parts Length.
            throw new LayoutException( "Not enough parts to the path [" + path
                + "] to construct an ArchivaArtifact from. (Requires at least 4 parts)" );
        }

        // Maven 2.x path.
        int partCount = pathParts.length;
        int filenamePos = partCount - 1;
        int baseVersionPos = partCount - 2;
        int artifactIdPos = partCount - 3;
        int groupIdPos = partCount - 4;

        // Second to last is the baseVersion (the directory version)
        String baseVersion = pathParts[baseVersionPos];

        // Third to last is the artifact Id.
        artifact.setArtifactId( pathParts[artifactIdPos] );

        // Remaining pieces are the groupId.
        for ( int i = 0; i <= groupIdPos; i++ )
        {
            if ( i == 0 )
            {
                artifact.setGroupId( pathParts[i] );
            }
            else
            {
                artifact.setGroupId( artifact.getGroupId() + "." + pathParts[i] );
            }
        }

        try
        {
            // Last part is the filename
            String filename = pathParts[filenamePos];

            // Now we need to parse the filename to get the artifact version Id.
            if ( StringUtils.isBlank( filename ) )
            {
                throw new IllegalArgumentException( INVALID_ARTIFACT_PATH + "Unable to split blank filename." );
            }

            FilenameParser parser = new FilenameParser( filename );

            // Expect the filename to start with the artifactId.
            artifact.setArtifactId( parser.expect( artifact.getArtifactId() ) );

            if ( artifact.getArtifactId() == null )
            {
                throw new LayoutException( INVALID_ARTIFACT_PATH + "filename format is invalid, "
                    + "should start with artifactId as stated in path." );
            }

            // Process the version.
            artifact.setVersion( parser.expect( baseVersion ) );

            if ( artifact.getVersion() == null )
            {
                // We working with a snapshot?
                if ( VersionUtil.isSnapshot( baseVersion ) )
                {
                    artifact.setVersion( parser.nextVersion() );
                    if ( !VersionUtil.isUniqueSnapshot( artifact.getVersion() ) )
                    {
                        throw new LayoutException( INVALID_ARTIFACT_PATH + "filename format is invalid,"
                            + "expected timestamp format in filename." );
                    }
                }
                else
                {
                    throw new LayoutException( INVALID_ARTIFACT_PATH + "filename format is invalid, "
                        + "expected version as stated in path." );
                }
            }

            // Do we have a classifier?
            switch(parser.seperator())
            {
                case '-':
                    // Definately a classifier.
                    artifact.setClassifier( parser.remaining() );
                   
                    // Set the type.
                    artifact.setType( ArtifactExtensionMapping.guessTypeFromFilename( filename ) );
                    break;
                case '.':
                    // We have an dual extension possibility.
                    String extension = parser.remaining() + '.' + parser.getExtension();
                    artifact.setType( extension.replace( '.', '-' ) );
                    break;
                case 0:
                    // End of the filename, only a simple extension left. - Set the type.
                    artifact.setType( ArtifactExtensionMapping.guessTypeFromFilename( filename ) );
                    break;
            }
           
            // Special case for maven plugins
            if ( StringUtils.equals( "jar", artifact.getType() ) &&
                 ArtifactExtensionMapping.isMavenPlugin( artifact.getArtifactId() ) )
            {
                artifact.setType( ArtifactExtensionMapping.MAVEN_PLUGIN );
            }
        }
        catch ( LayoutException e )
        {
            throw e;
        }

        // Sanity Checks.

        // Do we have a snapshot version?
        if ( VersionUtil.isSnapshot( artifact.getVersion() ) )
        {
            // Rules are different for SNAPSHOTS
            if ( !VersionUtil.isGenericSnapshot( baseVersion ) )
            {
                String filenameBaseVersion = VersionUtil.getBaseVersion( artifact.getVersion() );
                throw new LayoutException( "Invalid snapshot artifact location, version directory should be "
                    + filenameBaseVersion );
            }
        }
        else
        {
            // Non SNAPSHOT rules.
            // Do we pass the simple test?
            if ( !StringUtils.equals( baseVersion, artifact.getVersion() ) )
            {
                throw new LayoutException( "Invalid artifact: version declared in directory path does"
                    + " not match what was found in the artifact filename." );
            }
        }

        return artifact;
View Full Code Here

         */

        if ( pathParts.length != 3 )
        {
            // Illegal Path Parts Length.
            throw new LayoutException( INVALID_ARTIFACT_PATH
                    + "legacy paths should only have 3 parts [groupId]/[type]s/[artifactId]-[version].[type], found "
                    + pathParts.length + " instead." );
        }

        // The Group ID.
        artifact.setGroupId( pathParts[0] );

        // The Expected Type.
        String expectedType = pathParts[1];

        // Sanity Check: expectedType should end in "s".
        if ( !expectedType.endsWith( "s" ) )
        {
            throw new LayoutException( INVALID_ARTIFACT_PATH
                    + "legacy paths should have an expected type ending in [s] in the second part of the path." );
        }

        // The Filename.
        String filename = pathParts[2];

        FilenameParser parser = new FilenameParser( filename );

        artifact.setArtifactId( parser.nextNonVersion() );

        // Sanity Check: does it have an artifact id?
        if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
        {
            // Special Case: The filename might start with a version id (like "test-arch-1.0.jar").
            int idx = filename.indexOf( '-' );
            if ( idx > 0 )
            {
                parser.reset();
                // Take the first section regardless of content.
                String artifactId = parser.next();

                // Is there anything more that is considered not a version id?
                String moreArtifactId = parser.nextNonVersion();
                if ( StringUtils.isNotBlank( moreArtifactId ) )
                {
                    artifact.setArtifactId( artifactId + "-" + moreArtifactId );
                }
                else
                {
                    artifact.setArtifactId( artifactId );
                }
            }

            // Sanity Check: still no artifact id?
            if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
            {
                throw new LayoutException( INVALID_ARTIFACT_PATH + "no artifact id present." );
            }
        }

        artifact.setVersion( parser.remaining() );

        // Sanity Check: does it have a version?
        if ( StringUtils.isEmpty( artifact.getVersion() ) )
        {
            // Special Case: use last section of artifactId as version.
            String artifactId = artifact.getArtifactId();
            int idx = artifactId.lastIndexOf( '-' );
            if ( idx > 0 )
            {
                artifact.setVersion( artifactId.substring( idx + 1 ) );
                artifact.setArtifactId( artifactId.substring( 0, idx ) );
            }
            else
            {
                throw new LayoutException( INVALID_ARTIFACT_PATH + "no version found." );
            }
        }

        String classifier = ArtifactClassifierMapping.getClassifier( expectedType );
        if ( classifier != null )
        {
            String version = artifact.getVersion();
            if ( ! version.endsWith( "-" + classifier ) )
            {
                throw new LayoutException( INVALID_ARTIFACT_PATH + expectedType + " artifacts must use the classifier " + classifier );
            }
            version = version.substring( 0, version.length() - classifier.length() - 1 );
            artifact.setVersion( version );
            artifact.setClassifier( classifier );
        }

        String extension = parser.getExtension();

        // Set Type
        String defaultExtension = expectedType.substring( 0, expectedType.length() - 1 );
        artifact.setType(
            ArtifactExtensionMapping.mapExtensionAndClassifierToType( classifier, extension, defaultExtension ) );

        // Sanity Check: does it have an extension?
        if ( StringUtils.isEmpty( artifact.getType() ) )
        {
            throw new LayoutException( INVALID_ARTIFACT_PATH + "no extension found." );
        }

        // Special Case with Maven Plugins
        if ( StringUtils.equals( "jar", extension ) && StringUtils.equals( "plugins", expectedType ) )
        {
            artifact.setType( ArtifactExtensionMapping.MAVEN_ONE_PLUGIN );
        }
        else
        {
            // Sanity Check: does extension match pathType on path?
            String expectedExtension = ArtifactExtensionMapping.getExtension( artifact.getType() );

            if ( !expectedExtension.equals( extension ) )
            {
                throw new LayoutException( INVALID_ARTIFACT_PATH + "mismatch on extension [" + extension
                    + "] and layout specified type [" + artifact.getType() + "] (which maps to extension: ["
                    + expectedExtension + "]) on path [" + path + "]" );
            }
        }

View Full Code Here

    public ArtifactReference toArtifactReference( String requestedPath )
        throws LayoutException
    {
        if ( StringUtils.isBlank( requestedPath ) )
        {
            throw new LayoutException( "Blank request path is not a valid." );
        }

        String path = requestedPath;
        while ( path.startsWith( "/" ) )
        {
            path = path.substring( 1 );

            // Only slash? that's bad, mmm-kay?
            if ( "/".equals( path ) )
            {
                throw new LayoutException( "Invalid request path: Slash only." );
            }
        }

        if ( isDefault( path ) )
        {
            return defaultPathParser.toArtifactReference( path );
        }
        else if ( isLegacy( path ) )
        {
            return legacyPathParser.toArtifactReference( path );
        }
        else
        {
            throw new LayoutException( "Not a valid request path layout, too short." );
        }
    }
View Full Code Here

     */
    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.
View Full Code Here

    public ArtifactReference toArtifactReference( String path )
        throws LayoutException
    {
        if ( StringUtils.isBlank( path ) )
        {
            throw new LayoutException( "Unable to convert blank path." );
        }

        ArtifactReference artifact = new ArtifactReference();

        String normalizedPath = StringUtils.replace( path, "\\", "/" );
        String pathParts[] = StringUtils.split( normalizedPath, '/' );

        /* Minimum parts.
         *
         *   path = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar"
         *   path[0] = "commons-lang";        // The Group ID
         *   path[1] = "commons-lang";        // The Artifact ID
         *   path[2] = "2.1";                 // The Version
         *   path[3] = "commons-lang-2.1.jar" // The filename.
         */

        if ( pathParts.length < 4 )
        {
            // Illegal Path Parts Length.
            throw new LayoutException( "Not enough parts to the path [" + path
                + "] to construct an ArchivaArtifact from. (Requires at least 4 parts)" );
        }

        // Maven 2.x path.
        int partCount = pathParts.length;
        int filenamePos = partCount - 1;
        int baseVersionPos = partCount - 2;
        int artifactIdPos = partCount - 3;
        int groupIdPos = partCount - 4;

        // Second to last is the baseVersion (the directory version)
        String baseVersion = pathParts[baseVersionPos];

        // Third to last is the artifact Id.
        artifact.setArtifactId( pathParts[artifactIdPos] );

        // Remaining pieces are the groupId.
        for ( int i = 0; i <= groupIdPos; i++ )
        {
            if ( i == 0 )
            {
                artifact.setGroupId( pathParts[i] );
            }
            else
            {
                artifact.setGroupId( artifact.getGroupId() + "." + pathParts[i] );
            }
        }

        try
        {
            // Last part is the filename
            String filename = pathParts[filenamePos];

            // Now we need to parse the filename to get the artifact version Id.
            if ( StringUtils.isBlank( filename ) )
            {
                throw new IllegalArgumentException( INVALID_ARTIFACT_PATH + "Unable to split blank filename." );
            }

            FilenameParser parser = new FilenameParser( filename );

            // Expect the filename to start with the artifactId.
            artifact.setArtifactId( parser.expect( artifact.getArtifactId() ) );

            if ( artifact.getArtifactId() == null )
            {
                throw new LayoutException( INVALID_ARTIFACT_PATH + "filename format is invalid, "
                    + "should start with artifactId as stated in path." );
            }

            // Process the version.
            artifact.setVersion( parser.expect( baseVersion ) );

            if ( artifact.getVersion() == null )
            {
                // We working with a snapshot?
                if ( VersionUtil.isSnapshot( baseVersion ) )
                {
                    artifact.setVersion( parser.nextVersion() );
                    if ( !VersionUtil.isUniqueSnapshot( artifact.getVersion() ) )
                    {
                        throw new LayoutException( INVALID_ARTIFACT_PATH + "filename format is invalid,"
                            + "expected timestamp format in filename." );
                    }
                }
                else
                {
                    throw new LayoutException( INVALID_ARTIFACT_PATH + "filename format is invalid, "
                        + "expected version as stated in path." );
                }
            }

            // Do we have a classifier?
            switch(parser.seperator())
            {
                case '-':
                    // Definately a classifier.
                    artifact.setClassifier( parser.remaining() );

                    // Set the type.
                    artifact.setType( ArtifactExtensionMapping.mapExtensionAndClassifierToType( artifact.getClassifier(), parser.getExtension() ) );
                    break;
                case '.':
                    // We have an dual extension possibility.
                    String extension = parser.remaining() + '.' + parser.getExtension();
                    artifact.setType( extension );
                    break;
                case 0:
                    // End of the filename, only a simple extension left. - Set the type.
                    String type = ArtifactExtensionMapping.mapExtensionToType( parser.getExtension() );
                    if ( type == null )
                    {
                        throw new LayoutException( "Invalid artifact: no type was specified" );
                    }
                    artifact.setType( type );
                    break;
            }

            // Special case for maven plugins
            if ( StringUtils.equals( "jar", artifact.getType() ) &&
                 ArtifactExtensionMapping.isMavenPlugin( artifact.getArtifactId() ) )
            {
                artifact.setType( ArtifactExtensionMapping.MAVEN_PLUGIN );
            }
        }
        catch ( LayoutException e )
        {
            throw e;
        }

        // Sanity Checks.

        // Do we have a snapshot version?
        if ( VersionUtil.isSnapshot( artifact.getVersion() ) )
        {
            // Rules are different for SNAPSHOTS
            if ( !VersionUtil.isGenericSnapshot( baseVersion ) )
            {
                String filenameBaseVersion = VersionUtil.getBaseVersion( artifact.getVersion() );
                throw new LayoutException( "Invalid snapshot artifact location, version directory should be "
                    + filenameBaseVersion );
            }
        }
        else
        {
            // Non SNAPSHOT rules.
            // Do we pass the simple test?
            if ( !StringUtils.equals( baseVersion, artifact.getVersion() ) )
            {
                throw new LayoutException( "Invalid artifact: version declared in directory path does"
                    + " not match what was found in the artifact filename." );
            }
        }

        return artifact;
View Full Code Here

TOP

Related Classes of org.apache.maven.archiva.repository.layout.LayoutException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.