Package org.apache.maven.wagon.resource

Examples of org.apache.maven.wagon.resource.Resource


    }

    public void putFromStream( final InputStream stream, String destination, long contentLength, long lastModified )
        throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
    {
        Resource resource = new Resource( destination );

        firePutInitiated( resource, null );

        resource.setContentLength( contentLength );

        resource.setLastModified( lastModified );

        put( stream, resource, null );
    }
View Full Code Here


    }

    public void fillInputData( InputData inputData )
        throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
    {
        Resource resource = inputData.getResource();

        String repositoryUrl = getRepository().getUrl();
        String url = repositoryUrl + ( repositoryUrl.endsWith( "/" ) ? "" : "/" ) + resource.getName();
        HttpGet getMethod = new HttpGet( url );
        long timestamp = resource.getLastModified();
        if ( timestamp > 0 )
        {
            SimpleDateFormat fmt = new SimpleDateFormat( "EEE, dd-MMM-yy HH:mm:ss zzz", Locale.US );
            fmt.setTimeZone( GMT_TIME_ZONE );
            Header hdr = new BasicHeader( "If-Modified-Since", fmt.format( new Date( timestamp ) ) );
            fireTransferDebug( "sending ==> " + hdr + "(" + timestamp + ")" );
            getMethod.addHeader( hdr );
        }

        try
        {
            CloseableHttpResponse response = execute( getMethod );
            closeable = response;
            int statusCode = response.getStatusLine().getStatusCode();

            String reasonPhrase = ", ReasonPhrase:" + response.getStatusLine().getReasonPhrase() + ".";

            fireTransferDebug( url + " - Status code: " + statusCode + reasonPhrase );

            switch ( statusCode )
            {
                case HttpStatus.SC_OK:
                    break;

                case HttpStatus.SC_NOT_MODIFIED:
                    // return, leaving last modified set to original value so getIfNewer should return unmodified
                    return;
                case HttpStatus.SC_FORBIDDEN:
                    fireSessionConnectionRefused();
                    throw new AuthorizationException( "Access denied to: " + url + " " + reasonPhrase );

                case HttpStatus.SC_UNAUTHORIZED:
                    fireSessionConnectionRefused();
                    throw new AuthorizationException( "Not authorized " + reasonPhrase );

                case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED:
                    fireSessionConnectionRefused();
                    throw new AuthorizationException( "Not authorized by proxy " + reasonPhrase );

                case HttpStatus.SC_NOT_FOUND:
                    throw new ResourceDoesNotExistException( "File: " + url + " " + reasonPhrase );

                    // add more entries here
                default:
                {
                    cleanupGetTransfer( resource );
                    TransferFailedException e = new TransferFailedException(
                        "Failed to transfer file: " + url + ". Return code is: " + statusCode + " " + reasonPhrase );
                    fireTransferError( resource, e, TransferEvent.REQUEST_GET );
                    throw e;
                }
            }

            Header contentLengthHeader = response.getFirstHeader( "Content-Length" );

            if ( contentLengthHeader != null )
            {
                try
                {
                    long contentLength = Long.parseLong( contentLengthHeader.getValue() );

                    resource.setContentLength( contentLength );
                }
                catch ( NumberFormatException e )
                {
                    fireTransferDebug(
                        "error parsing content length header '" + contentLengthHeader.getValue() + "' " + e );
                }
            }

            Header lastModifiedHeader = response.getFirstHeader( "Last-Modified" );
            if ( lastModifiedHeader != null )
            {
                Date lastModified = DateUtils.parseDate( lastModifiedHeader.getValue() );
                if ( lastModified != null )
                {
                    resource.setLastModified( lastModified.getTime() );
                    fireTransferDebug( "last-modified = " + lastModifiedHeader.getValue() +
                                           " (" + lastModified.getTime() + ")" );
                }
            }
View Full Code Here

           
            channel.cd( ".." );
        }
        else
        {
            Resource resource = ScpHelper.getResource( getFileName( prefix, fileName ) );

            firePutInitiated( resource, sourceFile );

            putFile( sourceFile, resource, permissions );
        }
View Full Code Here

    }

    public void fillInputData( InputData inputData )
        throws TransferFailedException, ResourceDoesNotExistException
    {
        Resource resource = inputData.getResource();
       
        String filename = ScpHelper.getResourceFilename( resource.getName() );

        String dir = ScpHelper.getResourceDirectory( resource.getName() );

        // we already setuped the root directory. Ignore beginning /
        if ( dir.length() > 0 && dir.charAt( 0 ) == ScpHelper.PATH_SEPARATOR )
        {
            dir = dir.substring( 1 );
        }

        try
        {
            SftpATTRS attrs = changeToRepositoryDirectory( dir, filename );

            long lastModified = attrs.getMTime() * MILLIS_PER_SEC;
            resource.setContentLength( attrs.getSize() );

            resource.setLastModified( lastModified );
           
            inputData.setInputStream( channel.get( filename ) );
        }
        catch ( SftpException e )
        {
View Full Code Here

    public void fillOutputData( OutputData outputData )
        throws TransferFailedException
    {
        int directoryMode = getDirectoryMode( getRepository().getPermissions() );

        Resource resource = outputData.getResource();
       
        try
        {
            channel.cd( "/" );

            String basedir = getRepository().getBasedir();
            mkdirs( basedir + "/", directoryMode );

            mkdirs( resource.getName(), directoryMode );

            String filename = ScpHelper.getResourceFilename( resource.getName() );
            outputData.setOutputStream( channel.put( filename ) );
        }
        catch ( TransferFailedException e )
        {
            fireTransferError( resource, e, TransferEvent.REQUEST_PUT );

            throw e;
        }
        catch ( SftpException e )
        {
            fireTransferError( resource, e, TransferEvent.REQUEST_PUT );

            String msg =
                "Error occurred while deploying '" + resource.getName() + "' " + "to remote repository: "
                    + getRepository().getUrl() + ": " + e.getMessage();

            throw new TransferFailedException( msg, e );
        }
    }
View Full Code Here

    }

    public void fillInputData( InputData inputData )
        throws TransferFailedException, ResourceDoesNotExistException
    {
        Resource resource = inputData.getResource();

        String path = getPath( getRepository().getBasedir(), resource.getName() );
        //String cmd = "scp -p -f " + path;
        String cmd = "scp -p -f \"" + path + "\"";

        fireTransferDebug( "Executing command: " + cmd );

        try
        {
            channel = (ChannelExec) session.openChannel( EXEC_CHANNEL );

            channel.setCommand( cmd );

            // get I/O streams for remote scp
            channelOutputStream = channel.getOutputStream();

            InputStream in = channel.getInputStream();
            inputData.setInputStream( in );

            channel.connect();

            sendEom( channelOutputStream );

            int exitCode = in.read();

            if ( exitCode == 'T' )
            {
                String line = readLine( in );

                String[] times = line.split( " " );

                resource.setLastModified( Long.valueOf( times[0] ).longValue() * 1000 );

                sendEom( channelOutputStream );

                exitCode = in.read();
            }

            String line = readLine( in );

            if ( exitCode != COPY_START_CHAR )
            {
                if ( exitCode == 1 && ( line.indexOf( "No such file or directory" ) != -1
                    || line.indexOf( "no such file or directory" ) != 1 ) )
                {
                    throw new ResourceDoesNotExistException( line );
                }
                else
                {
                    throw new IOException( "Exit code: " + exitCode + " - " + line );
                }
            }

            if ( line == null )
            {
                throw new EOFException( "Unexpected end of data" );
            }

            String perms = line.substring( 0, 4 );
            fireTransferDebug( "Remote file permissions: " + perms );

            if ( line.charAt( 4 ) != ACK_SEPARATOR && line.charAt( 5 ) != ACK_SEPARATOR )
            {
                throw new IOException( "Invalid transfer header: " + line );
            }

            int index = line.indexOf( ACK_SEPARATOR, 5 );
            if ( index < 0 )
            {
                throw new IOException( "Invalid transfer header: " + line );
            }

            int filesize = Integer.valueOf( line.substring( 5, index ) ).intValue();
            fireTransferDebug( "Remote file size: " + filesize );

            resource.setContentLength( filesize );

            String filename = line.substring( index + 1 );
            fireTransferDebug( "Remote filename: " + filename );

            sendEom( channelOutputStream );
View Full Code Here

    }

    public void fillOutputData( OutputData outputData )
        throws TransferFailedException
    {
        Resource resource = outputData.getResource();

        String basedir = getRepository().getBasedir();

        String path = getPath( basedir, resource.getName() );

        String dir = ScpHelper.getResourceDirectory( resource.getName() );

        try
        {
            sshTool.createRemoteDirectories( getPath( basedir, dir ), getRepository().getPermissions() );
        }
        catch ( CommandExecutionException e )
        {
            fireTransferError( resource, e, TransferEvent.REQUEST_PUT );

            throw new TransferFailedException( e.getMessage(), e );
        }

        String octalMode = getOctalMode( getRepository().getPermissions() );

        // exec 'scp -p -t rfile' remotely
        String command = "scp";
        if ( octalMode != null )
        {
            command += " -p";
        }
        command += " -t \"" + path + "\"";

        fireTransferDebug( "Executing command: " + command );

        String resourceName = resource.getName();

        OutputStream out = null;
        try
        {
            channel = (ChannelExec) session.openChannel( EXEC_CHANNEL );

            channel.setCommand( command );

            // get I/O streams for remote scp
            out = channel.getOutputStream();
            outputData.setOutputStream( out );

            channelInputStream = channel.getInputStream();

            channel.connect();

            checkAck( channelInputStream );

            // send "C0644 filesize filename", where filename should not include '/'
            long filesize = resource.getContentLength();

            String mode = octalMode == null ? "0644" : octalMode;
            command = "C" + mode + " " + filesize + " ";

            if ( resourceName.lastIndexOf( ScpHelper.PATH_SEPARATOR ) > 0 )
View Full Code Here

    }

    public void fillInputData( InputData inputData )
        throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
    {
        Resource resource = inputData.getResource();

        StringBuilder url = new StringBuilder( getRepository().getUrl() );
        if ( !url.toString().endsWith( "/" ) )
        {
            url.append( '/' );
        }
        url.append( resource.getName() );

        getMethod = new GetMethod( url.toString() );
        long timestamp = resource.getLastModified();
        if ( timestamp > 0 )
        {
            SimpleDateFormat fmt = new SimpleDateFormat( "EEE, dd-MMM-yy HH:mm:ss zzz", Locale.US );
            fmt.setTimeZone( GMT_TIME_ZONE );
            Header hdr = new Header( "If-Modified-Since", fmt.format( new Date( timestamp ) ) );
            fireTransferDebug( "sending ==> " + hdr + "(" + timestamp + ")" );
            getMethod.addRequestHeader( hdr );
        }

        int statusCode;
        try
        {
            statusCode = execute( getMethod );
        }
        catch ( IOException e )
        {
            fireTransferError( resource, e, TransferEvent.REQUEST_GET );

            throw new TransferFailedException( e.getMessage(), e );
        }

        fireTransferDebug( url + " - Status code: " + statusCode );

        // TODO [BP]: according to httpclient docs, really should swallow the output on error. verify if that is
        // required
        switch ( statusCode )
        {
            case HttpStatus.SC_OK:
                break;

            case HttpStatus.SC_NOT_MODIFIED:
                // return, leaving last modified set to original value so getIfNewer should return unmodified
                return;

            case SC_NULL:
            {
                TransferFailedException e = new TransferFailedException( "Failed to transfer file: " + url );
                fireTransferError( resource, e, TransferEvent.REQUEST_GET );
                throw e;
            }

            case HttpStatus.SC_FORBIDDEN:
                fireSessionConnectionRefused();
                throw new AuthorizationException( "Access denied to: " + url );

            case HttpStatus.SC_UNAUTHORIZED:
                fireSessionConnectionRefused();
                throw new AuthorizationException( "Not authorized." );

            case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED:
                fireSessionConnectionRefused();
                throw new AuthorizationException( "Not authorized by proxy." );

            case HttpStatus.SC_NOT_FOUND:
                throw new ResourceDoesNotExistException( "File: " + url + " does not exist" );

                // add more entries here
            default:
            {
                cleanupGetTransfer( resource );
                TransferFailedException e = new TransferFailedException(
                    "Failed to transfer file: " + url + ". Return code is: " + statusCode );
                fireTransferError( resource, e, TransferEvent.REQUEST_GET );
                throw e;
            }
        }

        InputStream is = null;

        Header contentLengthHeader = getMethod.getResponseHeader( "Content-Length" );

        if ( contentLengthHeader != null )
        {
            try
            {
                long contentLength = Integer.valueOf( contentLengthHeader.getValue() ).intValue();

                resource.setContentLength( contentLength );
            }
            catch ( NumberFormatException e )
            {
                fireTransferDebug(
                    "error parsing content length header '" + contentLengthHeader.getValue() + "' " + e );
            }
        }

        Header lastModifiedHeader = getMethod.getResponseHeader( "Last-Modified" );

        long lastModified = 0;

        if ( lastModifiedHeader != null )
        {
            try
            {
                lastModified = DateUtil.parseDate( lastModifiedHeader.getValue() ).getTime();

                resource.setLastModified( lastModified );
            }
            catch ( DateParseException e )
            {
                fireTransferDebug( "Unable to parse last modified header" );
            }
View Full Code Here

        if ( supportsGetIfNewer() )
        {
            setupRepositories();
            setupWagonTestingFixtures();
            int expectedSize = putFile();
            getIfNewer( getExpectedLastModifiedOnGet( testRepository, new Resource( resource ) ) + 30000, false,
                        expectedSize );
        }
    }
View Full Code Here

        if ( supportsGetIfNewer() )
        {
            setupRepositories();
            setupWagonTestingFixtures();
            int expectedSize = putFile();
            getIfNewer( getExpectedLastModifiedOnGet( testRepository, new Resource( resource ) ), false, expectedSize );
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.maven.wagon.resource.Resource

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.