john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

OxygenUploadCLI

// 2012-10-30 johnpfeiffer requires O2JavaSDK-0.0.260, OxygenLogin, OxygenSpace
//, OxygenSpace, httpcore-4.2 , httpclient-4.2
// TODO: more elegant solution than brute force of search for SpaceName vs SpaceId
// https://api.oxygencloud.com/gateway  key  username  password   spaceNameOrId
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.net.URL;
import com.oxygen.sdk.O2Agent;
import com.oxygen.sdk.O2Space;

public class OxygenUploadCLI
{
    public static final String CLASSVERSION = "0.18";
    private static final String CORRECTUSAGE = " CORRECT Usage: java -jar getoxygenspace-" + CLASSVERSION
            + ".jar URL APIKEY oxygenUserId oxygenPassword [spaceName OR spaceId]";

    private String accessUrl;
    private String ApiKey;
    private String oxygenUserId;
    private String oxygenPassword;
    private String spaceIdentifier;

    public static void main( String[] args ) throws Exception
    {
        long start = System.currentTimeMillis();
        OxygenUploadCLI main = new OxygenUploadCLI();
        String message = main.validArguments( args );
        if( !message.isEmpty() )
        {
            System.err.println( message );
            System.exit( 1 );
        }
        OxygenLogin login = new OxygenLogin( main.accessUrl , main.ApiKey , main.oxygenUserId , main.oxygenPassword );
        O2Agent agent = login.getAgent();
        if( agent == null )
        {
            System.out.println( "ERROR: Unable to login.  Check accessUrl, apiKey, username, or password" );
            System.exit( 1 );
        }
        System.out.println( "sessionId = " + login.getSessionId() );

        // TODO: better approach than this brute force fail through?
        O2Space temp = OxygenSpace.getInstanceById( agent , main.spaceIdentifier );
        if( temp == null )
        {
            temp = OxygenSpace.getInstanceByName( agent , main.spaceIdentifier );
        }

        try
        {
            if( temp == null )
            {
                throw new FileNotFoundException( main.spaceIdentifier + " not found" );
            }
            OxygenSpace space = new OxygenSpace( temp );
            System.out.print( space.getName() + "," + space.getOwner() + "," + space.getId() + ",writable:" + space.canWrite() + "," );
        }catch( FileNotFoundException e )
        {
            System.out.print( e.getMessage() );
        }finally
        {
            agent.logout();
            System.out.print( " [in " + main.calculateRuntime( start ) + " ms]" );
        }
    }

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

    private String validArguments( String args[] )
    {
        StringBuilder result = new StringBuilder();
        if( args.length != 5 )
        {
            result.append( "ERROR: " + args.length + " is the incorrect number of arguments" );
        }else
        {
            for( int i = 0 ; i < args.length ; i++ )
            {
                if( args[i] == null || args[i].isEmpty() )
                {
                    result.append( "ERROR: argument " + i + " is null or empty" );
                    break;
                }
            }
            try
            {
                new URL( args[0] );
            }catch( MalformedURLException e )
            {
                result.append( "ERROR: malformed URL: " + args[0] );
            }
        }
        if( !result.toString().isEmpty() )
        {
            result.append( CORRECTUSAGE );
        }else
        {
            accessUrl = args[0];
            ApiKey = args[1];
            oxygenUserId = args[2];
            oxygenPassword = args[3];
            spaceIdentifier = args[4];
        }
        return result.toString();
    }

} // end class

  • « OxygenSpace
  • HTTP Post multipart form »

Published

Oct 31, 2012

Category

java-oxygencloud

~271 words

Tags

  • java 252
  • oxygencloud 19
  • oxygenuploadcli 1