// 2012-10-04 johnpfeiffer requires NirvanixSDK-1-5-5.jar and xom.jar
// TODO: private String getSessionTokenFromXml( String xml )
package net.kittyandbear;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.UUID;
import org.json.JSONException;
import org.json.JSONObject;
import com.nirvanix.sdk.session.NirvanixException;
import com.nirvanix.sdk.session.SDKException;
import com.nirvanix.sdk.session.Session;
import com.nirvanix.sdk.transport.AccountLogin;
import com.nirvanix.sdk.common.VersionInfo;
import com.nirvanix.sdk.session.AccountInfo;
import com.nirvanix.sdk.session.ContactInfo;
public class NirvanixConnection
{
public static final String CLASSVERSION = "0.35";
private URL apiAuthenticationUrl = null;
private String userName;
private String appName;
private Session session = null;
private String sessionToken;
static String getClassVersion()
{
return CLASSVERSION;
}
NirvanixConnection( URL url , String userName , String appName ) throws IllegalArgumentException , InstantiationException , IOException
{
if( url == null )
{
throw new IllegalArgumentException( "ERROR: URL is null" );
}
if( userName == null )
{
throw new IllegalArgumentException( "ERROR: userName is null" );
}
if( appName == null )
{
throw new IllegalArgumentException( "ERROR: appName is null" );
}
this.apiAuthenticationUrl = url;
this.userName = userName;
this.appName = appName;
getSessionREST();
}
private void getSessionREST() throws InstantiationException , IOException
{
URLConnection connection = apiAuthenticationUrl.openConnection();
if( connection == null )
{
throw new InstantiationException( "ERROR: connection to " + apiAuthenticationUrl + " is null" );
}
StringBuilder restResult = new StringBuilder();
BufferedReader in = new BufferedReader( new InputStreamReader( connection.getInputStream() ) );
String line;
while( ( line = in.readLine() ) != null )
{
restResult.append( line );
}
in.close();
getSessionTokenFromJSON( restResult.toString() );
try
{
AccountLogin login = new AccountLogin( sessionToken , userName , appName );
sessionToken = login.getSessionToken();
session = new Session( sessionToken , userName , appName );
}catch( NirvanixException e )
{
throw new InstantiationException( "ERROR: Nirvanix " + e.getErrorCode() + " " + e.getMessage() );
}catch( SDKException e )
{
throw new InstantiationException( "ERROR: Nirvanix SDK " + e.getMessage() );
}
}
private void getSessionTokenFromJSON( String json ) throws InstantiationException
{
if( json == null )
{
throw new IllegalArgumentException( "ERROR: json string is null" );
}
if( json.isEmpty() )
{
throw new IllegalArgumentException( "ERROR: json string is empty" );
}
System.out.println( "DEBUG: " + json );
sessionToken = "";
JSONObject resultJSON = null;
try{
resultJSON = new JSONObject( json.toString() );
sessionToken = resultJSON.getString( "SessionToken" );
} catch( JSONException e )
{
if( resultJSON != null )
{
// resultJSON.get( arg0 )
}
throw new InstantiationException( "ERROR: JSON " + e.getMessage() );
}
}
String getNirvanixSDKVersion()
{
return ( VersionInfo.getNirvanixSDKVersion() );
}
String getSessionToken()
{
return sessionToken;
}
boolean isValidSessionTokenFormat()
{
boolean isValid = true;
try
{
UUID.fromString( sessionToken );
}catch( IllegalArgumentException e )
{
isValid = false;
}
return isValid;
}
String getAccountUserName()
{
StringBuilder strb = new StringBuilder();
if( session == null )
{
strb.append( "ERROR: session is null" );
}else
{
try
{
AccountInfo acctInfo = session.getAccountInfo();
strb.append( acctInfo.getUserName() );
}catch( NirvanixException e )
{
strb.append( "ERROR: Nirvanix " + e.getMessage() );
}catch( SDKException e )
{
strb.append( "ERROR: Nirvanix SDK " + e.getMessage() );
}catch( IOException e )
{
strb.append( "ERROR: IOException " + e.getMessage() );
}
}
return strb.toString();
}
String getAccountUserEmailAddress()
{
StringBuilder strb = new StringBuilder();
if( session == null )
{
strb.append( "ERROR: session is null" );
}else
{
try
{
AccountInfo acctInfo = session.getAccountInfo();
ContactInfo contactInfo = acctInfo.getContactInfo();
strb.append( contactInfo.getEmailAddress() );
}catch( NirvanixException e )
{
strb.append( e.getMessage() );
}catch( SDKException e )
{
strb.append( e.getMessage() );
}catch( IOException e )
{
strb.append( e.getMessage() );
}
}
return strb.toString();
}
void logout()
{
session.logout();
}
} // end class