// 2012-03-01 requires Connection.java , CommandLineParametersRequirements.java
import com.emc.esu.api.EsuApi;
import com.emc.esu.api.EsuException;
import com.emc.esu.api.ServiceInformation ;
import com.emc.esu.api.ObjectId;
import com.emc.esu.api.ObjectInfo;
import com.emc.esu.api.ObjectMetadata;
import com.emc.esu.api.MetadataList;
import com.emc.esu.api.Metadata;
import com.emc.esu.api.DirectoryEntry;
import com.emc.esu.api.ObjectPath;
import com.emc.esu.api.rest.EsuRestApi;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.PatternLayout;
import java.util.List;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Iterator;
public class AtmosConnect
{
static Logger rootLogger = Logger.getRootLogger();
public static void main( String[] args )
{
boolean result = false;
initializeLogger();
CommandLineParameterRequirements required = new CommandLineParameterRequirements( 4, "version 0.3: java -jar AtmosConnect.jar HOST PORT SUBTENANTID/UID SECRETKEY" );
if( ! required.isValid( args , System.out ) )
{ System.exit( 1 );
}
Connection atmos = null;
try{
atmos = new Connection( args[0] , args[1] , args[2] , args[3] );
}catch( Exception e )
{ e.printStackTrace();
System.exit( 1 );
}
System.out.println( "Using: " + atmos.getHost() + " " + atmos.getPort() + " " + atmos.getUser() );
EsuApi myEsuApi = initializeEsuAPI( atmos );
String version = getAtmosVersion( myEsuApi );
if( version != null )
{ System.out.println( "Atmos Version: " + version );
}
String directoryListing = getDirectoryListing( myEsuApi );
if( directoryListing != null )
{ System.out.println( "Directory Listing: " + directoryListing );
}
/* //WORKS but not always necessary
ObjectId myObjectId = null;
myObjectId = createAtmosObject( myObjectId , myEsuApi );
if( myObjectId != null )
{
displayAtmosObject( myObjectId , myEsuApi );
deleteAtmosObject( myObjectId , myEsuApi );
}
*/
rootLogger.info( "Application Successful" );
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
private static void initializeLogger()
{
if( !rootLogger.getAllAppenders().hasMoreElements() )
{
rootLogger.setLevel( Level.INFO );
rootLogger.addAppender( new ConsoleAppender( new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN ) ) );
rootLogger.info( "Starting the application..." );
}
}
private static EsuApi initializeEsuAPI( Connection c )
{
EsuApi myEsuApi = null;
try
{ int portInteger = Integer.parseInt( c.getPort() );
myEsuApi = new EsuRestApi( c.getHost() , portInteger , c.getUser() , c.getPassword() );
}
catch( EsuException ee )
{
rootLogger.error( "EsuRestApi Constructor failed." );
rootLogger.error( ee.getMessage() );
ee.printStackTrace();
System.exit( 1 );
}
return myEsuApi;
}
private static String getAtmosVersion( EsuApi myEsuApi )
{
ServiceInformation atmosServiceInformation = null;
String version = "";
try{
atmosServiceInformation = myEsuApi.getServiceInformation();
version = atmosServiceInformation.getAtmosVersion();
}
catch( EsuException ee )
{ rootLogger.error( "Error: get Atmos Version failed." );
rootLogger.error( ee.getMessage() );
System.exit( 1 );
}
return version;
}
private static String getDirectoryListing( EsuApi myEsuApi )
{
StringBuilder result = new StringBuilder();
List<DirectoryEntry> myDirectoryEntryList = new ArrayList<DirectoryEntry>();
ObjectPath myObjectPath = new ObjectPath( "/" );
System.out.println("Atmos Directory Listing: ");
try{
myDirectoryEntryList = myEsuApi.listDirectory( myObjectPath );
ListIterator<DirectoryEntry> it = myDirectoryEntryList.listIterator();
while ( it.hasNext () )
{
result.append( it.next().toString() + "\n" );
}
}catch( EsuException ee )
{ rootLogger.error( "Error: Directory Listing failed" );
rootLogger.error( ee.getMessage() );
System.exit( 1 );
}
return result.toString();
}
private static ObjectId createAtmosObject( ObjectId myObjectId , EsuApi myEsuApi )
{
try{
myObjectId = myEsuApi.createObject( null , null , null , "application/octet-stream" );
System.out.println( "Created object: " + myObjectId.toString() );
} catch( EsuException ee )
{
rootLogger.error( "Error: Create object failed \n" + ee.getMessage() );
System.exit( 1 );
}
return myObjectId;
}
private static void displayAtmosObject( ObjectId myObjectId , EsuApi myEsuApi )
{
ObjectInfo myObjectInfo = null;
try{
myObjectInfo = myEsuApi.getObjectInfo( myObjectId );
System.out.println( "ObjectInfo as XML: " + myObjectInfo.getRawXml() );
}catch( EsuException ee )
{ rootLogger.error( "Error: get object info failed \n" + ee.getMessage() );
}
ObjectMetadata myObjectMetadata = null;
MetadataList myMetadataList = null;
Metadata metadata = null;
try{
myObjectMetadata = myEsuApi.getAllMetadata( myObjectId );
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 the Object Metadata failed \n" + ee.getMessage());
}
}
private static void deleteAtmosObject( ObjectId myObjectId , EsuApi myEsuApi )
{
try {
System.out.println( "Trying to delete Server Object: " + myObjectId.toString() );
myEsuApi.deleteObject( myObjectId );
System.out.println("Test Object deleted" );
} catch( EsuException ee)
{ rootLogger.error( "Delete Object " + myObjectId.toString() + " failed \n" + ee.getMessage() );
}
}
}// end class