private void externalDownload()

in nbm-maven-plugin/src/main/java/org/apache/netbeans/nbm/CreateClusterAppMojo.java [975:1058]


    private void externalDownload( File f, InputStream is )
            throws IOException
    {
        // Cf. org.netbeans.nbbuild.AutoUpdate
        BufferedReader r = new BufferedReader( new InputStreamReader( is, "UTF-8" ) );
        long crc = -1;
        long size = -1;
        boolean found = false;
        String line;
        while ( ( line = r.readLine() ) != null )
        {
            if ( line.startsWith( "CRC:" ) )
            {
                crc = Long.parseLong( line.substring( 4 ).trim() );
            }
            else if ( line.startsWith( "URL:" ) )
            {
                String rest = line.substring( 4 ).trim();
                if ( rest.startsWith( "m2:/" ) )
                {
                    if ( !found )
                    {
                        String[] coords = rest.substring( 4 ).trim().split( ":" );
                        Artifact artifact;
                        if ( coords.length == 4 )
                        {
                            artifact = artifactFactory.
                                    createArtifact( coords[0], coords[1], coords[2], null, coords[3] );
                        }
                        else
                        {
                            artifact = artifactFactory.createArtifactWithClassifier( coords[0], coords[1], coords[2],
                                                                                     coords[3], coords[4] );
                        }
                        try
                        {
                            artifactResolver.
                                    resolve( artifact, project.getRemoteArtifactRepositories(), session.getLocalRepository() );
                            FileUtils.copyFile( artifact.getFile(), f );
                            found = true;
                        }
                        catch ( AbstractArtifactResolutionException x )
                        {
                            getLog().warn( "Cannot find " + line.substring( 8 ), x );
                        }
                    }
                }
                else if ( !found )
                {
                    String url = line.substring( 4 ).trim();
                    try
                    {
                        // XXX use Wagon API instead
                        FileUtils.copyURLToFile( new URL( url ), f );
                        found = true;
                    }
                    catch ( IOException x )
                    {
                        getLog().warn( "Cannot download " + url, x );
                    }
                }
            }
            else if ( line.startsWith( "SIZE:" ) )
            {
                size = Long.parseLong( line.substring( 5 ).trim() );
            }
            else
            {
                getLog().warn( "Unrecognized line: " + line );
            }
        }
        if ( !found )
        {
            throw new IOException( "Could not download " + f );
        }
        if ( crc != -1 && crc != crcForFile( f ).getValue() )
        {
            throw new IOException( "CRC-32 of " + f + " does not match declared " + crc );
        }
        if ( size != -1 && size != f.length() )
        {
            throw new IOException( "Size of " + f + " does not match declared " + size );
        }
    }