john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

oxygenget

// 2012-08-06 johnpfeiffer requires O2JavaSDK-0.0.241 , OxygenSpace, OxygenFile, httpcore-4.2 , httpclient-4.2

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import com.oxygen.sdk.O2Agent;
import com.oxygen.sdk.O2File;
import com.oxygen.sdk.O2Space;

public class oxygenget
{
    private static final String CLASSVERSION = "0.12";
    private static final String CORRECTUSAGE = " CORRECT Usage: java -jar oxygenget-" + CLASSVERSION
            + ".jar URL APIKEY oxygenUserId oxygenPassword spaceName fileId";

    private String accessUrl;
    private String apiKey;
    private String oxygenUserId;
    private String oxygenPassword;
    private String spaceIdentifier;
    private String targetFileId;

    public static void main( String[] args ) throws Exception
    {
        long start = System.currentTimeMillis();
        oxygenget main = new oxygenget();
        try
        {
            main.validArguments( args );
        }catch( IllegalArgumentException e )
        {
            System.out.println( e.getMessage() + CORRECTUSAGE );
            System.exit( 1 );
        }
        OxygenLogin login = new OxygenLogin( main.accessUrl , main.apiKey , main.oxygenUserId , main.oxygenPassword );
        O2Agent agent = login.getAgent();
        try
        {
            O2Space tempSpace = OxygenSpace.getInstanceById( agent , main.spaceIdentifier );
            if( tempSpace == null )
            {
                tempSpace = OxygenSpace.getInstanceByName( agent , main.spaceIdentifier ); // try again by Name
            }
            if( tempSpace == null )
            {
                throw new FileNotFoundException( "ERROR: [Space] " + main.spaceIdentifier + " not found." );
            }
            OxygenSpace space = new OxygenSpace( tempSpace );

            // throws an IllegalArgumentException if the id isn't found
            O2File searchResult = OxygenFile.getInstanceByIdFromSpace( agent , main.targetFileId , space.get() );
            OxygenFile targetFile = new OxygenFile( searchResult );
            System.out.println( "filename,id,lastModifiedBy,created,sizeInBytes" );
            System.out.println( targetFile.getName() + "," + targetFile.getId() + "," + targetFile.getLastModifiedBy() + "," + targetFile.getCreatedTimestamp() + ","
                    + targetFile.getSizeInBytes() );
            String targetUrlString = agent.getDownloadURL( targetFile.get() );
            System.out.println( "DEBUG: Downloading from " + targetUrlString );
            URL targetUrl = new URL( targetUrlString );

            main.download( targetUrl, targetFile );


        }catch( MalformedURLException e )
        {
            System.err.println( "ERROR searching Space " + main.spaceIdentifier + " for " + main.targetFileId + " resulted in a malformed URL " + e.getMessage() );
        }catch( IllegalArgumentException e )
        {
            System.err.println( "ERROR searching Space " + main.spaceIdentifier + " for " + main.targetFileId + " : " + e.getMessage() );
        }catch( FileNotFoundException e )
        {
            System.out.print( e.getMessage() );
        }finally
        {
            agent.logout();
            System.out.print( " [in " + main.calculateRuntime( start ) + " ms]" );
        }
    }

    //TODO: make the save location more flexible
    private void download( URL url , OxygenFile file ) throws IOException
    {
        url.openConnection();
        InputStream reader = url.openStream();
        int chunkSize = 64 * 1024;
        int bytesRead = 0;
        byte[] buffer = new byte[chunkSize];
        File outputFile = new File( System.getProperty( "user.dir" ) + System.getProperty( "file.separator" ) + file.getName() );
        FileOutputStream writer = new FileOutputStream( outputFile );
        while( ( bytesRead = reader.read( buffer ) ) > 0 )
        {
            writer.write( buffer , 0 , bytesRead );
            buffer = new byte[chunkSize];
        }
        writer.close();
        reader.close();
    }

    private long calculateRuntime( long start )
    {
        return ( System.currentTimeMillis() - start );
    }

    private void validArguments( String args[] ) throws IllegalArgumentException
    {
        if( args.length != 6 )
        {
            throw new IllegalArgumentException( "ERROR: " + args.length + " is the incorrect number of arguments" );
        }
        for( int i = 0 ; i < args.length ; i++ )
        {
            if( args[i] == null || args[i].isEmpty() )
            {
                throw new IllegalArgumentException( "ERROR: argument " + i + 1 + " is null or empty" );
            }
        }
        try
        {
            new URL( args[0] );
        }catch( MalformedURLException e )
        {
            throw new IllegalArgumentException( "ERROR: malformed URL: " + args[0] );
        }
        accessUrl = args[0];
        apiKey = args[1];
        oxygenUserId = args[2];
        oxygenPassword = args[3];
        spaceIdentifier = args[4];
        targetFileId = args[5];
        System.out.println( "DEBUG: searching " + spaceIdentifier + " for " + targetFileId );
    }

} // end class

  • « getoxygenfileinfo
  • Highlight text copy to clipboard parcellite »

Published

Aug 8, 2012

Category

java-oxygencloud

~372 words

Tags

  • java 252
  • oxygencloud 19
  • oxygenget 1