john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

SSLCertificate

// 2012-07-09 johnpfeiffer requires FileSystem
//TODO: Unit Tests
import java.io.IOException;

public class SSLCertificate
{
    private static final String CLASSVERSION = "0.54";
    private String sslcertificate;

    SSLCertificate()
    {
        sslcertificate = "";
    }

    SSLCertificate( String certificate )
    {
        if( certificate == null )
        {
            throw new IllegalArgumentException( "ERROR: certificate cannot be null" );
        }
        if( certificate.isEmpty() )
        {
            throw new IllegalArgumentException( "ERROR: certificate cannot be empty" );
        }
        sslcertificate = certificate.trim();
    }

    protected String getVersion()
    {
        return CLASSVERSION;
    }

    protected String get()
    {
        return sslcertificate;
    }

    // TODO enhanced by java security or openssl test
    protected boolean isValid()
    {
        boolean result = false;
        if( sslcertificate.startsWith( "-----BEGIN CERTIFICATE-----" ) )
        {
            result = true;
        }
        return result;
    }

    protected boolean isValid( String certificate )
    {
        if( certificate == null )
        {
            throw new IllegalArgumentException( "ERROR: certificate cannot be null" );
        }
        if( certificate.isEmpty() )
        {
            throw new IllegalArgumentException( "ERROR: certificate cannot be empty" );
        }
        boolean result = false;
        if( certificate.startsWith( "-----BEGIN CERTIFICATE-----" ) )
        {
            result = true;
        }
        return result;
    }

    protected boolean readFromFile( String pathFilename ) throws IllegalArgumentException
    {
        if( pathFilename == null )
        {
            throw new IllegalArgumentException( "ERROR: pathFilename cannot be null" );
        }
        if( pathFilename.isEmpty() )
        {
            throw new IllegalArgumentException( "ERROR: pathFilename cannot be empty" );
        }
        boolean result = false;
        FileSystem fs = new FileSystem();
        String contents;
        try
        {
            pathFilename = fs.convertFileSystemPath( pathFilename );
            contents = fs.stringFromFile( pathFilename );
        }catch( IOException e )
        {
            throw new IllegalArgumentException( "ERROR: IOException on " + pathFilename + " " + e.getMessage() );
        }
        contents = contents.trim();
        if( contents != null && !contents.isEmpty() && isValid( contents ) )
        {
            sslcertificate = contents;
            result = true;
        }
        return result;
    }

    protected void writeToFile( String pathFilename ) throws IllegalArgumentException, IOException
    {
        if( pathFilename == null )
        {
            throw new IllegalArgumentException( "ERROR: pathFilename cannot be null" );
        }
        if( pathFilename.isEmpty() )
        {
            throw new IllegalArgumentException( "ERROR: pathFilename cannot be empty" );
        }
        FileSystem fs = new FileSystem();
        pathFilename = fs.convertFileSystemPath( pathFilename );
        fs.writeStringToFile( sslcertificate , pathFilename );
    }

} // end class

  • « drupal 05 taxonomy vocabulary tags cloud
  • SSLKey »

Published

Jul 9, 2012

Category

java-classes

~228 words

Tags

  • classes 92
  • java 252
  • sslcertificate 1