// 2012-06-28 johnpfeiffer requires JohnStringUtils, validation-api, bval-core, bval-jsr303, commons-beanutils-core, commons-lang3
// TODO: javascript form validation
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.constraints.NotNull;
public class CIFSService
{
protected static final String CIFSTORAGETYPE = "CIFSstorageAdaptor";
protected static final String PARAMCIFSSHARE = "cifsServerHost";
protected static final String PARAMCIFSUSER = "username";
protected static final String PARAMCIFSPASSWORD = "password";
protected static final String DEFAULTHOSTNAME = "10.10.10.250";
protected static final String DEFAULTHSHARE = "sharename";
protected static final String DEFAULTCIFSSHARE = "//" + DEFAULTHOSTNAME + "/" + DEFAULTHSHARE;
protected static final String DEFAULTCIFSUSER = "DOMAIN/winshareuser";
protected static final String DEFAULTCIFSPASSWORD = "ExamplePassword1";
@NotNull( message = "ERROR: CIFS share cannot be null" )
private String cifsshare = DEFAULTCIFSSHARE;
@NotNull( message = "ERROR: CIFS user cannot be null" )
private String user = DEFAULTCIFSUSER;
@NotNull( message = "ERROR: CIFS password cannot be null" )
private String password = DEFAULTCIFSPASSWORD;
public static class Builder
{
@NotNull( message = "ERROR: CIFS hostname cannot be null" )
private String cifsshare = DEFAULTCIFSSHARE;
@NotNull( message = "ERROR: CIFS user cannot be null" )
private String user = DEFAULTCIFSUSER;
@NotNull( message = "ERROR: CIFS password cannot be null" )
private String password = DEFAULTCIFSPASSWORD;
public Builder cifsshare( String value )
{
this.cifsshare = value;
return this;
}
public Builder user( String value )
{
this.user = value;
return this;
}
public Builder password( String value )
{
this.password = value;
return this;
}
public CIFSService build() throws IllegalArgumentException
{
CIFSService cifs = new CIFSService( this );
CIFSServiceValidator( cifs );
return cifs;
}
private static Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
private void CIFSServiceValidator( CIFSService cifs ) throws IllegalArgumentException
{
Set <ConstraintViolation <CIFSService>> violations = validator.validate( cifs );
if( !violations.isEmpty() )
{
for( ConstraintViolation <?> v : violations )
{
throw new IllegalArgumentException( v.getMessage() );
}
}
// ConstraintViolations ensure all of the values are non null
if( this.cifsshare.isEmpty() )
{
throw new IllegalArgumentException( "ERROR: CIFS share cannot be empty" );
}
if( JohnStringUtils.containsWhiteSpace( this.cifsshare ) )
{
throw new IllegalArgumentException( "ERROR: CIFS share cannot contain whitespace" );
}
if( JohnStringUtils.containsWhiteSpace( this.password ) )
{
throw new IllegalArgumentException( "ERROR: CIFS Password cannot contain whitespace" );
}
}
} // end inner class Builder
private CIFSService( Builder builder ) throws IllegalArgumentException
{
this.cifsshare = builder.cifsshare;
this.user = builder.user;
this.password = builder.password;
}
protected String getCIFSShare()
{
return cifsshare;
}
protected String getUser()
{
return user;
}
protected String getPassword()
{
return password;
}
protected String getHTMLForm()
{
StringBuilder strb = new StringBuilder();
String newline = "<br /><br />" + System.getProperty( "line.separator" );
String space = " ";
strb.append( "<label>CIFS Server Address: </label>" + space + space + "<input type='text' name='" + PARAMCIFSSHARE + "' size='40' value='"
+ JohnStringUtils.safeHTML( cifsshare ) + "' /> " + newline );
strb.append( "<label>CIFS Username: </label>" + space + space + "<input type='text' name='" + PARAMCIFSUSER + "' size='40' value='"
+ JohnStringUtils.safeHTML( user ) + "' /> " + newline );
strb.append( "<label>CIFS Password: </label>" + space + space + "<input type='password' name='" + PARAMCIFSPASSWORD + "' size='40' /> " + newline );
return strb.toString();
}
} // end class