/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.server.realm;

import java.security.cert.X509Certificate;
import java.io.*;
import java.util.Enumeration;

import org.apache.log4j.Logger;

import org.bodington.server.BuildingServerException;
import org.bodington.database.*;
import org.bodington.sqldatabase.*;
import org.bodington.util.*;

/**
 *
 * @author  jrm
 */
public class UserX509 extends org.bodington.sqldatabase.SqlPersistentObject
{
    
    private static Logger log = Logger.getLogger(UserX509.class);
    private PrimaryKey user_x509_id;
    private PrimaryKey user_id;
    
    private X509Certificate cert;

    private int serial_number;
    private String distinguished_name;
    
    
    public static UserX509 findUserX509( PrimaryKey key )
    throws BuildingServerException
    {
        return (UserX509)findPersistentObject( key, "org.bodington.server.realm.UserX509" );
    }

    public static Enumeration findUserX509s( int serial, String dn )
    throws BuildingServerException
    {
        String dn_spec;
        if ( dn.length() > 255 )
            dn_spec = dn.substring( 0, 255 );
        else
            dn_spec = dn;
        return findPersistentObjects( 
            "serial_number = " + serial + " AND distinguished_name = " + SqlDatabase.quotedSQL( dn_spec ), 
            "org.bodington.server.realm.UserX509" );
    }
    
    
    /** Creates a new instance of X509Certificate */
    public UserX509()
    {
    }
    
    public org.bodington.database.PrimaryKey getPrimaryKey()
    {
        return getUserX509Id();
    }
    
    public void setPrimaryKey(org.bodington.database.PrimaryKey key)
    {
        setUserX509Id( key );
    }
    
    public org.bodington.database.PrimaryKey getUserX509Id()
    {
        return user_x509_id;
    }
    
    public void setUserX509Id(org.bodington.database.PrimaryKey key)
    {
        setUnsaved();
        user_x509_id = key;
    }
    
    public org.bodington.database.PrimaryKey getUserId()
    {
        return user_id;
    }
    
    public void setUserId(org.bodington.database.PrimaryKey key)
    {
        setUnsaved();
        user_id = key;
    }
    
    public int getSerialNumber()
    {
        return serial_number;
    }
    
    public void setSerialNumber( int s )
    {
        setUnsaved();
        serial_number = s;
    }
    
    public String getDistinguishedName()
    {
        return distinguished_name;
    }
    
    public void setDistinguishedName( String s )
    {
        setUnsaved();
        if ( s.length() > 255 )
            distinguished_name = s.substring( 0, 255 );
        else
            distinguished_name = s;
    }
    
    public String getSerializedCertificate()
    {
        if ( cert == null )
            return null;
        
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        try
        {
            ObjectOutputStream objout = new ObjectOutputStream( bout );
            objout.writeObject( cert );
            objout.close();
        }
        catch ( IOException ioe )
        {
            log.error( ioe.getMessage(), ioe );
            return null;
        }
        Base64Encoder encoder = new Base64Encoder();
        return encoder.encode( bout.toByteArray() );
    }
    
    public void setSerializedCertificate( String s )
    {
        setUnsaved();
        cert=null;
        if ( s == null || s.length() == 0 )
            return;
        
        try
        {
            Base64Decoder decoder = new Base64Decoder();
            byte[] binary = decoder.decodeBuffer( s );
            ByteArrayInputStream bin = new ByteArrayInputStream( binary );
            ObjectInputStream objin = new ObjectInputStream( bin );
            cert = (X509Certificate)objin.readObject();
        }
        catch ( Exception e )
        {
            log.error( e.getMessage(), e );
        }
    }
    
    public X509Certificate getX509Certificate()
    {
        return cert;
    }
    
    public void setX509Certificate( X509Certificate c )
    {
        setUnsaved();
        cert = c;
    }
}

