john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

AppProperties AtmosService

// 2012-08-29 johnpfeiffer requires JohnStringUtils, validation-api, bval-core, bval-jsr303, commons-beanutils-core, commons-lang3
// outbound internet connection for InetAddress.getByName(String)
// TODO: javascript validation
// TODO: atmosIpAddress = URI not String, renamed to hostname

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;

public class AtmosService
{
    protected static final String ATMOSSTORAGETYPE = "EMCstorageAdaptor";
    protected static final String PARAMATMOSISAVALAIBLE = "emc.isAvailable";
    protected static final String PARAMEMCIPADDRESS = "emc.IpAddress";
    protected static final String PARAMEMCPORT = "emc.portNumber";
    protected static final String PARAMEMCUID = "emc.uid";
    protected static final String PARAMEMCSHAREDSECRET = "emc.sharedSecret";
    protected static final String PARAMEMCSTORAGEFOLDER = "emc.storageFolder";

    protected static final boolean DEFAULTATMOSISAVAILABLE = false;
    protected static final String DEFAULTATMOSIPADDRESS = "storage.synaptic.att.com";
    protected static final int DEFAULTATMOSPORT = 443;
    protected static final String DEFAULTATMOSUID = "subtenant/uid";
    protected static final String DEFAULTATMOSSHAREDSECRET = "sharedsecret=";
    protected static final String DEFAULTATMOSFOLDER = "oxygencloud-storage";

    private boolean atmosIsAvailable = DEFAULTATMOSISAVAILABLE;
    @NotNull( message = "ERROR: Atmos hostname cannot be null" )
    private String atmosIpAddress = DEFAULTATMOSIPADDRESS;
    @Min( value = 0 , message = "ERROR: port number too small, must be between 0 and 65535" )
    @Max( value = 65535 , message = "ERROR: port number too large, must be between 0 and 65535" )
    private int atmosPortNumber = DEFAULTATMOSPORT;
    @NotNull( message = "ERROR: Atmos UID cannot be null" )
    private String atmosUid = DEFAULTATMOSUID;
    @NotNull( message = "ERROR: Atmos SharedSecret cannot be null" )
    private String atmosSharedSecret = DEFAULTATMOSSHAREDSECRET;
    @NotNull( message = "ERROR: Atmos storageFolder cannot be null" )
    private String atmosStorageFolder = DEFAULTATMOSFOLDER;

    public static class Builder
    {
        private boolean atmosIsAvailable = DEFAULTATMOSISAVAILABLE;
        private String atmosIpAddress = DEFAULTATMOSIPADDRESS;
        private int atmosPortNumber = DEFAULTATMOSPORT;
        private String atmosUid = DEFAULTATMOSUID;
        private String atmosSharedSecret = DEFAULTATMOSSHAREDSECRET;
        private String atmosStorageFolder = DEFAULTATMOSFOLDER;

        public Builder isAvailable( boolean value )
        {
            this.atmosIsAvailable = value;
            return this;
        }

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

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

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

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

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

        private static Validator validator = Validation.buildDefaultValidatorFactory().getValidator();

        private void AtmosServiceValidator( AtmosService atmos ) throws IllegalArgumentException
        {
            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.atmosIsAvailable )
            {

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

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

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

    } // end inner class Builder

    private AtmosService( Builder builder ) throws IllegalArgumentException
    {
        this.atmosIsAvailable = builder.atmosIsAvailable;
        this.atmosIpAddress = builder.atmosIpAddress;
        this.atmosPortNumber = builder.atmosPortNumber;
        this.atmosUid = builder.atmosUid;
        this.atmosSharedSecret = builder.atmosSharedSecret;
        this.atmosStorageFolder = builder.atmosStorageFolder;
    }

    protected String getString( String header )
    {
        StringBuilder strb = new StringBuilder();
        String newline = System.getProperty( "line.separator" );
        strb.append( header + PARAMATMOSISAVALAIBLE + "=" + atmosIsAvailable + newline );
        strb.append( header + PARAMEMCIPADDRESS + "=" + atmosIpAddress + newline );
        strb.append( header + PARAMEMCPORT + "=" + atmosPortNumber + newline );
        strb.append( header + PARAMEMCUID + "=" + atmosUid + newline );
        strb.append( header + PARAMEMCSHAREDSECRET + "=" + atmosSharedSecret + newline );
        strb.append( header + PARAMEMCSTORAGEFOLDER + "=" + atmosStorageFolder + 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' id='" + PARAMEMCIPADDRESS + "' name='"
                + PARAMEMCIPADDRESS + "' size='40' value='" + JohnStringUtils.safeHTML( atmosIpAddress ) + "' /> " + newline );
        strb.append( "<label>Port Number: </label> " );
        strb.append( "<input type='radio' name='" + PARAMEMCPORT + "' value='80' " );
        if( atmosPortNumber == 80 )
        {
            strb.append( "checked='checked'" );
        }
        strb.append( " /> <label>80</label>" );
        strb.append( space + space );
        strb.append( "<input type='radio' name='" + PARAMEMCPORT + "' value='443' " );
        if( atmosPortNumber == 443 )
        {
            strb.append( "checked='checked'" );
        }
        strb.append( " /> <label> 443 </label> " + newline );
        strb.append( "<label>Atmos Subtenant/UID:</label>" + space + space + "<input type='text' id='" + PARAMEMCUID + "' name='" + PARAMEMCUID
                + "' size='60' value='" + JohnStringUtils.safeHTML( atmosUid ) + "' /> " + newline );
        strb.append( "<label>Atmos Shared Secret:</label>" + space + space + "<input type='text' id='" + PARAMEMCSHAREDSECRET + "' name='" + PARAMEMCSHAREDSECRET
                + "' size='40' value='" + JohnStringUtils.safeHTML( atmosSharedSecret ) + "' /> " + 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( '" + PARAMEMCIPADDRESS + "' ).value;" + newline );
        strb.append( "if( userinput==null || userinput=='' )" + newline );
        strb.append( "{ alert('Error: " + PARAMEMCIPADDRESS + " cannot be blank');" + newline );
        strb.append( " return false; }" + newline );

        strb.append( "var userinput=document.getElementById( '" + PARAMEMCUID + "' ).value;" + newline );
        strb.append( "if( userinput==null || userinput=='' )" + newline );
        strb.append( "{ alert('Error: " + PARAMEMCUID + " cannot be blank');" + newline );
        strb.append( " return false; }" + newline );

        strb.append( "var userinput=document.getElementById( '" + PARAMEMCSHAREDSECRET + "' ).value;" + newline );
        strb.append( "if( userinput==null || userinput=='' )" + newline );
        strb.append( "{ alert('Error: " + PARAMEMCSHAREDSECRET + " cannot be blank');" + newline );
        strb.append( " return false; }" + newline );

        strb.append( "}" + newline );
        strb.append( "</script>" + newline );
        return strb.toString();
    }

} // end class

  • « AppProperties 1.2
  • AppProperties AuthgatewayService »

Published

Aug 30, 2012

Category

java-servlet

~686 words

Tags

  • appproperties 18
  • atmosservice 2
  • java-servlet 61