001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.server.core.api;
021
022
023import java.io.IOException;
024import java.io.ObjectInput;
025import java.io.ObjectOutput;
026
027import org.apache.directory.api.ldap.model.constants.AuthenticationLevel;
028import org.apache.directory.api.ldap.model.name.Dn;
029import org.apache.directory.api.ldap.model.schema.SchemaManager;
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032
033
034/**
035 * A helper class which serialize and deserialize a LdapPrincipal.
036 *
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 */
039public final class LdapPrincipalSerializer
040{
041    /** The LoggerFactory used by this class */
042    protected static final Logger LOG = LoggerFactory.getLogger( LdapPrincipalSerializer.class );
043
044
045    /**
046     * Private constructor.
047     */
048    private LdapPrincipalSerializer()
049    {
050    }
051
052
053    /**
054     * Serializes a LdapPrincipal instance.
055     * 
056     * @param principal The LdapPrincipal instance to serialize
057     * @param out The stream into which we will write the serialized instance
058     * @throws IOException If the stream can't be written
059     */
060    public static void serialize( LdapPrincipal principal, ObjectOutput out ) throws IOException
061    {
062        // The Authentication level
063        out.writeInt( principal.getAuthenticationLevel().getLevel() );
064
065        // The principal's DN
066        if ( principal.getDn() == null )
067        {
068            Dn.EMPTY_DN.writeExternal( out );
069        }
070        else
071        {
072            principal.getDn().writeExternal( out );
073        }
074    }
075
076
077    /**
078     * Deserializes a LdapPrincipal instance.
079     * 
080     * @param schemaManager The SchemaManager (can be null)
081     * @param in The input stream from which the LdapPrincipal is read
082     * @return a deserialized LdapPrincipal
083     * @throws IOException If the stream can't be read
084     */
085    public static LdapPrincipal deserialize( SchemaManager schemaManager, ObjectInput in )
086        throws IOException
087    {
088        // Read the authenyication level
089        AuthenticationLevel authenticationLevel = AuthenticationLevel.getLevel( in.readInt() );
090
091        // Read the principal's DN
092        Dn dn = new Dn( schemaManager );
093
094        try
095        {
096            dn.readExternal( in );
097        }
098        catch ( ClassNotFoundException cnfe )
099        {
100            IOException ioe = new IOException( cnfe.getMessage() );
101            ioe.initCause( cnfe );
102            throw ioe;
103        }
104
105        return new LdapPrincipal( schemaManager, dn, authenticationLevel );
106    }
107}