//2013-02-19 johnpfeiffer
//TODO: apiURL validation of the URL new URI?
//TODO: logging
package com.oxygencloud.sdk.util;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.constraints.NotNull;
import com.oxygen.sdk.O2Agent;
import com.oxygen.sdk.O2AgentFactory;
import com.oxygen.sdk.O2SessionInfo;
import com.oxygen.sdk.exception.O2NetworkException;
import com.oxygen.sdk.exception.O2RuleException;
import com.oxygen.sdk.exception.O2UnexpectedException;
public class OxygenLogin
{
public static final String CLASSVERSION = "0.21";
private O2Agent agent = null;
private String sessionId;
private static long loginDurationInMilliseconds;
@NotNull( message = "ERROR: apiURL cannot be null" )
private String apiURL;
@NotNull( message = "ERROR: apiKey cannot be null" )
private String apiKey;
@NotNull( message = "ERROR: oxygenUserId cannot be null" )
private String oxygenUserId;
@NotNull( message = "ERROR: password cannot be null" )
private String oxygenPassword;
public static class Builder
{
@NotNull( message = "ERROR: apiURL cannot be null" )
private String apiURL;
@NotNull( message = "ERROR: apiKey cannot be null" )
private String apiKey;
@NotNull( message = "ERROR: oxygenUserId cannot be null" )
private String oxygenUserId;
@NotNull( message = "ERROR: oxygenUserId cannot be null" )
private String oxygenPassword;
private O2Agent agent = null;
private O2SessionInfo session = null;
private String sessionId;
O2AgentFactory o2AgentFactory = null;
private long applicationStartTime = System.currentTimeMillis();
public Builder apiURL( String apiURL )
{
this.apiURL = apiURL;
return this;
}
public Builder apiKey( String apiKey )
{
this.apiKey = apiKey;
return this;
}
public Builder oxygenUserId( String userId )
{
this.oxygenUserId = userId;
return this;
}
public Builder oxygenPassword( String password )
{
this.oxygenPassword = password;
return this;
}
//for injection and testability
public Builder O2AgentFactory( O2AgentFactory o2AgentFactory )
{
this.o2AgentFactory = o2AgentFactory;
return this;
}
public OxygenLogin build() throws IllegalArgumentException, O2NetworkException, O2RuleException, O2UnexpectedException
{
OxygenLogin oxygenLogin = new OxygenLogin( this ); // object may exist in an incomplete or illegal state
OxygenLoginValidator( oxygenLogin );
if( o2AgentFactory == null )
{
// TODO: clean this up! O2AgentFactory test = new O2AgentFactory();
agent = O2AgentFactory.createAndroidO2AgentCustomURL( apiURL );
session = agent.login( apiKey , oxygenUserId , oxygenPassword );
sessionId = session.getSessionID();
}
else
{
agent = o2AgentFactory.createAndroidO2AgentCustomURL( apiURL );
session = agent.login( apiKey , oxygenUserId , oxygenPassword );
sessionId = session.getSessionID();
}
loginDurationInMilliseconds = System.currentTimeMillis() - applicationStartTime;
return new OxygenLogin( this );
}
private static Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
private void OxygenLoginValidator( OxygenLogin oxygenLogin ) throws IllegalArgumentException
{
Set <ConstraintViolation <OxygenLogin>> violations = validator.validate( oxygenLogin );
if( !violations.isEmpty() )
{
for( ConstraintViolation <?> v : violations )
{
throw new IllegalArgumentException( v.getMessage() );
}
}
// ConstraintViolations ensure all of the values are non null
}
} // end inner class
//java -jar getoxygenspace-0.1.jar https://api-qa.oxygencloud.com/gateway 3095198fc5bf433799cdc5bdf0d56a62 john-enterprise-qa Sandbox7 Shared
private OxygenLogin( Builder builder )
{
this.apiURL = builder.apiURL;
this.apiKey = builder.apiKey;
this.oxygenUserId = builder.oxygenUserId;
this.oxygenPassword = builder.oxygenPassword;
this.agent = builder.agent;
this.sessionId = builder.sessionId;
}
O2Agent getAgent()
{
return agent;
}
String getSessionId()
{
return sessionId;
}
long getLoginDurationInMilliseconds()
{
return loginDurationInMilliseconds;
}
} // end class