/* Files/Objects are downloaded in binary and may not come with the end of file newline */
import com.emc.esu.api.*;
import com.emc.esu.api.rest.*;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.PatternLayout;
import java.util.Iterator;
import java.io.*;
public class AtmosGet
{
private static Logger rootLogger = Logger.getRootLogger();
private static int MAXBYTESLIMIT;
public static void main( String[] args )
{
if( !rootLogger.getAllAppenders().hasMoreElements() )
{
rootLogger.setLevel( Level.INFO );
rootLogger.addAppender( new ConsoleAppender( new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN ) ) );
rootLogger.info("Starting application...");
}
if( args.length != 6 )
{
System.out.println( args.length + " does not equal the 6 required arguments.");
System.out.println( "version 0.1: java -jar atmosdisplaybypath.jar HOST PORT SUBTENANTID/UID SECRETKEY /PATH/ MAXLIMIT" );
System.out.println( "example: java -jar atmosdisplaybypath.jar storage.synaptic.att.com 443 1a2b3c/uid 9z8y7x / 10" );
System.out.println( "example: java -jar atmosdisplaybypath.jar storage.synaptic.att.com 443 1a2b3c/uid 9z8y7x /foldername/filename 1" );
System.exit( 1 );
}
String HOST = args[0];
int PORT = Integer.parseInt( args[1] );
String FULLTOKENID = args[2];
String SECRETKEY = args[3];
String PATH = args[4];
MAXBYTESLIMIT = Integer.parseInt( args[5] ); //max number of ListOptions.getToken() to recursively use
displayConnectionCredentials( HOST , PORT , FULLTOKENID , SECRETKEY , PATH , MAXBYTESLIMIT );
EsuApi myEsuApi = null;
try
{ myEsuApi = new EsuRestApi( HOST, PORT, FULLTOKENID, SECRETKEY );
}catch( EsuException e )
{
System.out.println( "EsuRestApi Constructor failed. " + e.getMessage() );
e.printStackTrace();
}
displayAtmosInformation( myEsuApi );
ObjectPath objectPath = new ObjectPath( PATH );
if( objectPath.isDirectory() )
{ rootLogger.info("Cannot download " + objectPath.getName() + " as it is a directory." );
}
else
{ displayAtmosObjectByPath( objectPath , myEsuApi );
getObjectByPath( objectPath , myEsuApi );
}
rootLogger.info("Application Finished Successfully");
} // end main()
private static void displayConnectionCredentials( String HOST , int PORT , String FULLTOKENID , String SECRETKEY , String PATH , int MAXBYTESLIMIT )
{
System.out.println( "Connecting to Host: " + HOST );
System.out.println( "Connecting on Port: " + PORT );
System.out.println( "Full Token ID: " + FULLTOKENID );
System.out.println( "Secret Key: **************" );
System.out.println( "Namespace Path: " + PATH );
System.out.println( "Maximum number of bytes to return: " + MAXBYTESLIMIT );
}
private static void displayAtmosInformation( EsuApi myEsuApi )
{
try{
ServiceInformation atmosServiceInformation = myEsuApi.getServiceInformation();
String atmosVersion = atmosServiceInformation.getAtmosVersion();
System.out.println( "Atmos Version: " + atmosVersion );
}
catch( EsuException ee )
{ rootLogger.error( "Get Atmos system version failed. " + ee + ". AtmosCode: " + ee.getAtmosCode() );
}
catch( Exception e )
{ System.out.println( "Get Atmos system version failed. " + e );
System.out.println( e );
}
}
private static void displayAtmosObjectByPath( Identifier id , EsuApi myEsuApi )
{
ObjectInfo myObjectInfo = null;
try{
myObjectInfo = myEsuApi.getObjectInfo( id );
System.out.println( "ObjectInfo as XML: " + myObjectInfo.getRawXml() );
}
catch( EsuException ee )
{ rootLogger.error( "Get Object Info failed. " + ee + " AtmosCode: " + ee.getAtmosCode() );
}
catch( Exception e )
{ System.out.println( "Get Object Info failed. " + e );
}
ObjectMetadata myObjectMetadata = null;
MetadataList myMetadataList = null;
Metadata metadata = null;
try{
myObjectMetadata = myEsuApi.getAllMetadata( id );
myMetadataList = myObjectMetadata.getMetadata();
System.out.println( "Number of Metadata Tags: " + myMetadataList.count() );
Iterator<Metadata> iterator = myMetadataList.iterator();
while( iterator.hasNext() )
{
metadata = iterator.next();
System.out.println( metadata.getName() + "," + metadata.getValue() + "," + metadata.isListable());
}
}
catch( EsuException ee )
{ rootLogger.error( "Getting and listing all Object Metadata failed. " + ee + ". AtmosCode: " + ee.getAtmosCode() );
}
catch( Exception e )
{ System.out.println("Getting and listing all Object Metadata failed. " + e );
}
}
private static String getAtmosObjectSize( Identifier id , EsuApi myEsuApi )
{
String size = null;
try{
ObjectMetadata objectMetadata = myEsuApi.getAllMetadata( id );
MetadataList metadataList = objectMetadata.getMetadata();
Metadata metadataSize = metadataList.getMetadata("size");
if( metadataSize != null)
{ size = metadataSize.getValue();
}
}
catch( EsuException ee )
{ rootLogger.error( "Getting object size from metadata failed. " + ee + ". AtmosCode: " + ee.getAtmosCode() );
}
catch( Exception e )
{ System.out.println("Getting object size from metadata failed. " + e );
}
return size;
}
private static void getObjectByPath( ObjectPath objectPath , EsuApi myEsuApi )
{
Extent myExtent = new Extent( -1 , -1 ); //offset = -1 and size = -1 means entire object
BufferedReader in = null;
FileOutputStream target = null;
String objectSizeString = null;
int objectSize = 0;
byte[] buffer = null;
int bytes_read = 0;
try{
objectSizeString = getAtmosObjectSize( objectPath , myEsuApi );
objectSize = Integer.parseInt( objectSizeString ); //assuming this returns in bytes
System.out.println( "Starting to download " + objectPath.getName() + " with size: " + objectSize + " bytes.");
File newfile = new File( objectPath.getName() );
target = new FileOutputStream( newfile );
while( bytes_read < objectSize && bytes_read < MAXBYTESLIMIT )
{
buffer = myEsuApi.readObject( objectPath, myExtent, buffer );
target.write( buffer , 0 , buffer.length );
System.out.println( buffer.length + " bytes downloaded." );
bytes_read += buffer.length;
}
}catch( EsuException ee )
{ rootLogger.error( "Reading Atmos object failed. " + ee + ". AtmosCode: " + ee.getAtmosCode() );
}
catch( NumberFormatException nf )
{ rootLogger.error( nf );
}
catch( IOException ie )
{ rootLogger.error( "IO Exception on Atmos object read. " + ie );
}
catch( Exception e )
{ System.out.println( "IO Exception on Atmos object read. " + e );
}
finally
{ if( in !=null )
{ try{ in.close(); } catch( IOException e){ rootLogger.error( e ); }
}
if( target !=null )
{ try{ target.close(); } catch( IOException e){ rootLogger.error( e ); }
}
}
} //end getObjectByPath()
}// end class