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.shared.kerberos.components;
021
022import java.security.MessageDigest;
023import java.util.Arrays;
024
025import org.apache.directory.api.util.Strings;
026import org.apache.directory.shared.kerberos.codec.types.AuthorizationType;
027
028
029/**
030 * The class storing the individual AuthorizationDatas
031 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
032 */
033public class AuthorizationDataEntry
034{
035    /** the type of authorization data */
036    private AuthorizationType adType;
037
038    /** the authorization data */
039    private byte[] adData;
040
041
042    /**
043     * Creates a new instance of AD entry
044     */
045    public AuthorizationDataEntry()
046    {
047
048    }
049
050
051    /**
052     * Creates a new Instance of AD entry
053     * 
054     * @param adType The AuthorizationData type
055     * @param adData The AuthorizationData data
056     */
057    public AuthorizationDataEntry( AuthorizationType adType, byte[] adData )
058    {
059        this.adType = adType;
060
061        if ( adData != null )
062        {
063            this.adData = new byte[adData.length];
064            System.arraycopy( adData, 0, this.adData, 0, adData.length );
065        }
066    }
067
068
069    /**
070     * @return the adType
071     */
072    public AuthorizationType getAdType()
073    {
074        return adType;
075    }
076
077
078    /**
079     * @param adType the adType to set
080     */
081    public void setAdType( AuthorizationType adType )
082    {
083        this.adType = adType;
084    }
085
086
087    /**
088     * @return a copy of adData
089     */
090    public byte[] getAdData()
091    {
092        if ( Strings.isEmpty( adData ) )
093        {
094            return Strings.EMPTY_BYTES;
095        }
096        else
097        {
098            byte[] copy = new byte[adData.length];
099
100            System.arraycopy( adData, 0, copy, 0, adData.length );
101
102            return copy;
103        }
104    }
105
106
107    /**
108     * @return the reference on adData
109     */
110    public byte[] getAdDataRef()
111    {
112        return adData;
113    }
114
115
116    /**
117     * @param adData the adData to set
118     */
119    public void setAdData( byte[] adData )
120    {
121        if ( Strings.isEmpty( adData ) )
122        {
123            this.adData = Strings.EMPTY_BYTES;
124        }
125        else
126        {
127            this.adData = new byte[adData.length];
128
129            System.arraycopy( adData, 0, this.adData, 0, adData.length );
130        }
131    }
132
133
134    /**
135     * {@inheritDoc}
136     */
137    @Override
138    public int hashCode()
139    {
140        final int prime = 31;
141        int result = 17;
142        result = prime * result + Arrays.hashCode( adData );
143        result = prime * result + ( ( adType == null ) ? 0 : adType.hashCode() );
144        return result;
145    }
146
147
148    /**
149     * {@inheritDoc}
150     */
151    @Override
152    public boolean equals( Object obj )
153    {
154        if ( this == obj )
155        {
156            return true;
157        }
158
159        if ( !( obj instanceof AuthorizationDataEntry ) )
160        {
161            return false;
162        }
163
164        AuthorizationDataEntry other = ( AuthorizationDataEntry ) obj;
165
166        if ( !MessageDigest.isEqual( adData, other.adData ) )
167        {
168            return false;
169        }
170
171        return adType == other.adType;
172    }
173
174
175    /**
176     * @see Object#toString()
177     */
178    public String toString( String tabs )
179    {
180        StringBuilder sb = new StringBuilder();
181
182        sb.append( tabs ).append( "AuthorizationDataEntry : {\n" );
183        sb.append( tabs ).append( "    adType : " ).append( adType ).append( "\n" );
184        sb.append( tabs ).append( "    adData : " ).append( Strings.dumpBytes( adData ) ).append( "\n" );
185        sb.append( tabs ).append( "}" );
186        return sb.toString();
187    }
188
189
190    /**
191     * @see Object#toString()
192     */
193    public String toString()
194    {
195        return toString( "" );
196    }
197}