// 2012-08-29 johnpfeiffer requires JohnStringUtils, validation-api, bval-core, bval-jsr303, commons-beanutils-core, commons-lang3
// TODO: further validation: host (dns or ip), user
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.constraints.NotNull;
public class NirvanixService
{
protected static final String NIRVANIXSTORAGETYPE = "NirvanixRESTstorageAdaptor";
protected static final String PARAMNIRVANIXISAVAILABLE = "nirvanixrest.isAvailable";
protected static final String PARAMNIRVANIXAPIHOSTNAME = "nirvanixrest.apiHostName";
protected static final String PARAMNIRVANIXUSERNAME = "nirvanixrest.username";
protected static final String PARAMNIRVANIXPASSWORD = "nirvanixrest.password";
protected static final String PARAMNIRVANIXAPPNAME = "nirvanixrest.appName";
protected static final String PARAMNIRVANIXAPPKEY = "nirvanixrest.appKey";
protected static final String PARAMNIRVANIXSTORAGEFOLDER = "nirvanixrest.storageFolder";
protected static final boolean DEFAULTNIRVANIXISAVAILABLE = false;
protected static final String DEFAULTNIRVANIXAPIHOSTNAME = "";
protected static final String DEFAULTNIRVANIXUSERNAME = "";
protected static final String DEFAULTNIRVANIXPASSWORD = "";
protected static final String DEFAULTNIRVANIXAPPKEY = "example-key-here";
protected static final String DEFAULTNIRVANIXAPPNAME = "";
protected static final String DEFAULTNIRVANIXSTORAGEFOLDER = "oxygencloud-storage";
private boolean nirvanixIsAvailable = DEFAULTNIRVANIXISAVAILABLE;
@NotNull( message = "ERROR: APIHostName cannot be null" )
private String nirvanixApiHostName = DEFAULTNIRVANIXAPIHOSTNAME;
@NotNull( message = "ERROR: UserName cannot be null" )
private String nirvanixUsername = DEFAULTNIRVANIXUSERNAME;
@NotNull( message = "ERROR: Password cannot be null" )
private String nirvanixPassword = DEFAULTNIRVANIXPASSWORD;
@NotNull( message = "ERROR: AppKey cannot be null" )
private String nirvanixAppKey = DEFAULTNIRVANIXAPPKEY;
@NotNull( message = "ERROR: AppName cannot be null" )
private String nirvanixAppName = DEFAULTNIRVANIXAPPNAME;
@NotNull( message = "ERROR: StorageFolder cannot be null" )
private String nirvanixStorageFolder = DEFAULTNIRVANIXSTORAGEFOLDER;
public static class Builder
{
private boolean nirvanixIsAvailable = DEFAULTNIRVANIXISAVAILABLE;
@NotNull( message = "ERROR: APIHostName cannot be null" )
private String nirvanixApiHostName = DEFAULTNIRVANIXAPIHOSTNAME;
@NotNull( message = "ERROR: UserName cannot be null" )
private String nirvanixUsername = DEFAULTNIRVANIXUSERNAME;
@NotNull( message = "ERROR: Password cannot be null" )
private String nirvanixPassword = DEFAULTNIRVANIXPASSWORD;
@NotNull( message = "ERROR: AppKey cannot be null" )
private String nirvanixAppKey = DEFAULTNIRVANIXAPPKEY;
@NotNull( message = "ERROR: AppName cannot be null" )
private String nirvanixAppName = DEFAULTNIRVANIXAPPNAME;
@NotNull( message = "ERROR: StorageFolder cannot be null" )
private String nirvanixStorageFolder = DEFAULTNIRVANIXSTORAGEFOLDER;
public Builder isAvailable( boolean value )
{
this.nirvanixIsAvailable = value;
return this;
}
public Builder apihostname( String value )
{
this.nirvanixApiHostName = value;
return this;
}
public Builder user( String value )
{
this.nirvanixUsername = value;
return this;
}
public Builder password( String value )
{
this.nirvanixPassword = value;
return this;
}
public Builder appkey( String value )
{
this.nirvanixAppKey = value;
return this;
}
public Builder appname( String value )
{
this.nirvanixAppName = value;
return this;
}
public NirvanixService build() throws IllegalArgumentException
{
NirvanixService nirvanix = new NirvanixService( this );
NirvanixServiceValidator( nirvanix );
return nirvanix;
}
private static Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
private void NirvanixServiceValidator( NirvanixService nirvanix ) throws IllegalArgumentException
{
Set <ConstraintViolation <NirvanixService>> violations = validator.validate( nirvanix );
if( !violations.isEmpty() )
{
for( ConstraintViolation <?> v : violations )
{
throw new IllegalArgumentException( v.getMessage() );
}
}
// ConstraintViolations ensure all of the values are non null
if( this.nirvanixIsAvailable ) //only test constraints if service is activated
{
if( JohnStringUtils.containsWhiteSpace( this.nirvanixAppKey ) )
{
throw new IllegalArgumentException( "ERROR: AppKey should not contain a space , " + nirvanixAppKey );
}
if( !this.nirvanixAppKey.contains( "-" ) )
{
throw new IllegalArgumentException( "ERROR: AppKey should contain a - , " + nirvanixAppKey );
}
}
}
} // end inner class Builder
private NirvanixService( Builder builder ) throws IllegalArgumentException
{
this.nirvanixIsAvailable = builder.nirvanixIsAvailable;
this.nirvanixApiHostName = builder.nirvanixApiHostName;
this.nirvanixUsername = builder.nirvanixUsername;
this.nirvanixPassword = builder.nirvanixPassword;
this.nirvanixAppKey = builder.nirvanixAppKey;
this.nirvanixAppName = builder.nirvanixAppName;
this.nirvanixStorageFolder = builder.nirvanixStorageFolder;
}
protected String getString( String header )
{
StringBuilder strb = new StringBuilder();
String newline = System.getProperty( "line.separator" );
strb.append( header + PARAMNIRVANIXISAVAILABLE + "=" + nirvanixIsAvailable + newline );
strb.append( header + PARAMNIRVANIXAPIHOSTNAME + "=" + nirvanixApiHostName + newline );
strb.append( header + PARAMNIRVANIXUSERNAME + "=" + nirvanixUsername + newline );
strb.append( header + PARAMNIRVANIXPASSWORD + "=" + nirvanixPassword + newline );
strb.append( header + PARAMNIRVANIXAPPKEY + "=" + nirvanixAppKey + newline );
strb.append( header + PARAMNIRVANIXAPPNAME + "=" + nirvanixAppName + newline );
strb.append( header + PARAMNIRVANIXSTORAGEFOLDER + "=" + nirvanixStorageFolder + newline );
return strb.toString();
}
protected String getHTMLForm()
{
StringBuilder strb = new StringBuilder();
String newline = "<br /><br />" + System.getProperty( "line.separator" );
String space = " ";
strb.append( "<label>Nirvanix Username:</label>" + space + space + "<input type='text' id='" + PARAMNIRVANIXUSERNAME + "' name='" + PARAMNIRVANIXUSERNAME
+ "' size='60' value='" + JohnStringUtils.safeHTML( nirvanixUsername ) + "' /> " + newline );
strb.append( "<label>Nirvanix Password:</label>" + space + space + "<input type='text' id='" + PARAMNIRVANIXPASSWORD + "' name='" + PARAMNIRVANIXPASSWORD
+ "' size='40' value='" + JohnStringUtils.safeHTML( nirvanixPassword ) + "' /> " + newline );
strb.append( "<label>Nirvanix AppName:</label>" + space + space + "<input type='text' id='" + PARAMNIRVANIXAPPNAME + "' name='" + PARAMNIRVANIXAPPNAME
+ "' size='40' value='" + JohnStringUtils.safeHTML( nirvanixAppName ) + "' /> " + newline );
strb.append( "<label>Nirvanix AppKey:</label>" + space + space + "<input type='text' id='" + PARAMNIRVANIXAPPKEY + "' name='" + PARAMNIRVANIXAPPKEY
+ "' size='40' value='" + JohnStringUtils.safeHTML( nirvanixAppKey ) + "' /> " + newline );
strb.append( "<label>Nirvanix API Hostname (only applicable if not using services.nirvanix.com):</label>" + space + space + "<input type='text' name='"
+ PARAMNIRVANIXAPIHOSTNAME + "' size='40' value='" + JohnStringUtils.safeHTML( nirvanixApiHostName ) + "' /> " + newline );
return strb.toString();
}
// TODO: more generic validation from the Data Model
protected static String getValidateFormOnSubmit()
{
StringBuilder strb = new StringBuilder();
String newline = System.getProperty( "line.separator" );
strb.append( newline + "<script type='text/javascript'>" + newline );
strb.append( "function validateFormOnSubmit() {" + newline );
strb.append( "var userinput=document.getElementById( '" + PARAMNIRVANIXUSERNAME + "' ).value;" + newline );
strb.append( "if( userinput==null || userinput=='' )" + newline );
strb.append( "{ alert('Error: " + PARAMNIRVANIXUSERNAME + " cannot be blank');" + newline );
strb.append( " return false; }" + newline );
strb.append( "var userinput=document.getElementById( '" + PARAMNIRVANIXPASSWORD + "' ).value;" + newline );
strb.append( "if( userinput==null || userinput=='' )" + newline );
strb.append( "{ alert('Error: " + PARAMNIRVANIXPASSWORD + " cannot be blank');" + newline );
strb.append( " return false; }" + newline );
strb.append( "var userinput=document.getElementById( '" + PARAMNIRVANIXAPPNAME + "' ).value;" + newline );
strb.append( "if( userinput==null || userinput=='' )" + newline );
strb.append( "{ alert('Error: " + PARAMNIRVANIXAPPNAME + " cannot be blank');" + newline );
strb.append( " return false; }" + newline );
strb.append( "var userinput=document.getElementById( '" + PARAMNIRVANIXAPPKEY + "' ).value;" + newline );
strb.append( "if( userinput==null || userinput=='' )" + newline );
strb.append( "{ alert('Error: " + PARAMNIRVANIXAPPKEY + " cannot be blank');" + newline );
strb.append( " return false; }" + newline );
strb.append( "}" + newline );
strb.append( "</script>" + newline );
return strb.toString();
}
} // end class