john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

AtmosService

// 2012-06-19 johnpfeiffer requires hibernate-validator-4.3 , jboss-logging, validation-api , outbound internet connection for InetAddress.getByName(String)

import java.net.InetAddress;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

public class AtmosService
{
    protected static final String ATMOSSTORAGETYPE = "EMCstorageAdaptor";
    protected static final String PARAMEMCIPADDRESS = "emcIpAddress";
    protected static final String PARAMEMCPORT = "emcPortNumber";
    protected static final String PARAMEMCUID = "emcUid";
    protected static final String PARAMEMCSHAREDSECRET = "emcSharedSecret";
    protected static final String PARAMEMCSTORAGEFOLDER = "emcStorageFolder";

    protected static final String DEFAULTEMCIPADDRESS = "storage.synaptic.att.com";
    protected static final int DEFAULTEMCPORT = 443;
    protected static final String DEFAULTUID = "subtenant/uid";
    protected static final String DEFAULTSHAREDSECRET = "sharedsecret=";
    protected static final String DEFAULTEMCFOLDER = "oxygencloud-storage";

    @NotNull( message = "ERROR: Atmos hostname cannot be null" )
    private String emcIpAddress = DEFAULTEMCIPADDRESS;

    //Not sure why Hibernate throws a runtime java.lang.Integer error when using @Size
    @Min( 0 )
    @Max( 65535 )
    private int emcPortNumber = DEFAULTEMCPORT;

    @NotNull( message = "ERROR: Atmos UID cannot be null" )
    private String emcUid = DEFAULTUID;

    @NotNull( message = "ERROR: Atmos SharedSecret cannot be null" )
    private String emcSharedSecret = DEFAULTSHAREDSECRET;

    @NotNull( message = "ERROR: Atmos storageFolder cannot be null" )
    private String emcStorageFolder = DEFAULTEMCFOLDER;

    public static class Builder
    {
        private static Validator validator = Validation.buildDefaultValidatorFactory().getValidator();

        @NotNull( message = "ERROR: Atmos hostname cannot be null" )
        private String emcIpAddress = DEFAULTEMCIPADDRESS;

        @Size( min = 0 , max = 65535 , message = "ERROR: port number is out of range" )
        private int emcPortNumber = DEFAULTEMCPORT;

        @NotNull( message = "ERROR: Atmos UID cannot be null" )
        private String emcUid = DEFAULTUID;

        @NotNull( message = "ERROR: Atmos SharedSecret cannot be null" )
        private String emcSharedSecret = DEFAULTSHAREDSECRET;

        @NotNull( message = "ERROR: Atmos storageFolder cannot be null" )
        private String emcStorageFolder = DEFAULTEMCFOLDER;

        public Builder hostname( String value )
        {
            this.emcIpAddress = value;
            return this;
        }

        public Builder port( int value )
        {
            this.emcPortNumber = value;
            return this;
        }

        public Builder uid( String value )
        {
            this.emcUid = value;
            return this;
        }

        public Builder sharedSecret( String value )
        {
            this.emcSharedSecret = value;
            return this;
        }

        public AtmosService build() throws IllegalArgumentException
        {
            AtmosService atmos = new AtmosService( this );      //object may exist in an incomplete or illegal state
            AtmosValidator( atmos );

            return new AtmosService( this );
        }

        private void AtmosValidator( AtmosService atmos )
        {
            Set <ConstraintViolation <AtmosService>> violations = validator.validate( atmos );
            if( !violations.isEmpty() )
            {
                for( ConstraintViolation <?> v : violations )
                {
                    throw new IllegalArgumentException( v.getMessage() );
                }
            }
            //ConstraintViolations ensure all of the values are non null

            if( this.emcIpAddress.isEmpty() )
            {
                throw new IllegalArgumentException( "ERROR: Atmos hostname cannot be empty" );
            }
            try
            {
                InetAddress.getByName( this.emcIpAddress );
            }catch( Exception e )
            {
                throw new IllegalArgumentException( "ERROR: Atmos hostname " + e.getMessage() );
            }

            if( StringUtils.containsWhiteSpace( this.emcUid ) )
            {
                throw new IllegalArgumentException( "ERROR: Atmos UID cannot contain whitespace" );
            }

            if( StringUtils.containsWhiteSpace( this.emcSharedSecret ) )
            {
                throw new IllegalArgumentException( "ERROR: Atmos SharedSecret cannot contain whitespace" );
            }
            if( !this.emcSharedSecret.trim().endsWith( "=" ) )
            {
                throw new IllegalArgumentException( "ERROR: Atmos SharedSecret must end with an =" );
            }
        }

    } //end inner clas


    private AtmosService( Builder builder ) throws IllegalArgumentException
    {
        this.emcIpAddress = builder.emcIpAddress;
        this.emcPortNumber = builder.emcPortNumber;
        this.emcUid = builder.emcUid;
        this.emcSharedSecret = builder.emcSharedSecret;
        this.emcStorageFolder = builder.emcStorageFolder;
    }

    protected String getString( String header )
    {
        StringBuilder strb = new StringBuilder();
        String newline = System.getProperty( "line.separator" );
        strb.append( header + PARAMEMCIPADDRESS + "=" + emcIpAddress + newline );
        strb.append( header + PARAMEMCPORT + "=" + emcPortNumber + newline );
        strb.append( header + PARAMEMCUID + "=" + emcUid + newline );
        strb.append( header + PARAMEMCSHAREDSECRET + "=" + emcSharedSecret + newline );
        strb.append( header + PARAMEMCSTORAGEFOLDER + "=" + emcStorageFolder + newline );
        return strb.toString();
    }

    protected String getHTMLForm()
    {
        StringBuilder strb = new StringBuilder();
        String newline = "<br /><br />" + System.getProperty( "line.separator" );
        String space = "&#xA0;";

        strb.append( "<label>Atmos Hostname or IP Address:</label>" + space + space + "<input type='text' name='" + PARAMEMCIPADDRESS + "' size='40' value='"
                + StringUtils.safeHTML( emcIpAddress ) + "' /> " + newline );
        strb.append( "<label>Port Number: </label> " );
        strb.append( "<input type='radio' name='" + PARAMEMCPORT + "' value='80' " );
        if( emcPortNumber == 80 )
        {
            strb.append( "checked='checked'" );
        }
        strb.append( " /> <label>80</label>" );
        strb.append( space + space );
        strb.append( "<input type='radio' name='" + PARAMEMCPORT + "' value='443' " );
        if( emcPortNumber == 443 )
        {
            strb.append( "checked='checked'" );
        }
        strb.append( " /> <label> 443 </label> " + newline );
        strb.append( "<label>Atmos Subtenant/UID:</label>" + space + space + "<input type='text' name='" + PARAMEMCUID + "' size='60' value='"
                + StringUtils.safeHTML( emcUid ) + "' /> " + newline );
        strb.append( "<label>Atmos Shared Secret:</label>" + space + space + "<input type='text' name='" + PARAMEMCSHAREDSECRET + "' size='40' value='"
                + StringUtils.safeHTML( emcSharedSecret ) + "' /> " + newline );
        return strb.toString();
    }

} // end class

  • « AtmosServiceTest
  • eclipse default format john »

Published

Jun 20, 2012

Category

java-classes

~558 words

Tags

  • atmos 11
  • atmosservice 2
  • classes 92
  • java 252