View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.shared.kerberos.components;
21  
22  import java.security.MessageDigest;
23  import java.util.Arrays;
24  
25  import org.apache.directory.api.util.Strings;
26  import org.apache.directory.shared.kerberos.codec.types.AuthorizationType;
27  
28  
29  /**
30   * The class storing the individual AuthorizationDatas
31   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
32   */
33  public class AuthorizationDataEntry
34  {
35      /** the type of authorization data */
36      private AuthorizationType adType;
37  
38      /** the authorization data */
39      private byte[] adData;
40  
41  
42      /**
43       * Creates a new instance of AD entry
44       */
45      public AuthorizationDataEntry()
46      {
47  
48      }
49  
50  
51      /**
52       * Creates a new Instance of AD entry
53       * 
54       * @param adType The AuthorizationData type
55       * @param adData The AuthorizationData data
56       */
57      public AuthorizationDataEntry( AuthorizationType adType, byte[] adData )
58      {
59          this.adType = adType;
60  
61          if ( adData != null )
62          {
63              this.adData = new byte[adData.length];
64              System.arraycopy( adData, 0, this.adData, 0, adData.length );
65          }
66      }
67  
68  
69      /**
70       * @return the adType
71       */
72      public AuthorizationType getAdType()
73      {
74          return adType;
75      }
76  
77  
78      /**
79       * @param adType the adType to set
80       */
81      public void setAdType( AuthorizationType adType )
82      {
83          this.adType = adType;
84      }
85  
86  
87      /**
88       * @return a copy of adData
89       */
90      public byte[] getAdData()
91      {
92          if ( Strings.isEmpty( adData ) )
93          {
94              return Strings.EMPTY_BYTES;
95          }
96          else
97          {
98              byte[] copy = new byte[adData.length];
99  
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./org/apache/directory/shared/kerberos/components/AuthorizationDataEntry.html#AuthorizationDataEntry">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 }