1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.kerberos.shared.messages.value;
21
22
23 import java.nio.BufferOverflowException;
24 import java.nio.ByteBuffer;
25
26 import org.apache.directory.server.kerberos.shared.messages.value.types.AuthorizationType;
27 import org.apache.directory.shared.asn1.AbstractAsn1Object;
28 import org.apache.directory.shared.asn1.ber.tlv.TLV;
29 import org.apache.directory.shared.asn1.ber.tlv.UniversalTag;
30 import org.apache.directory.shared.asn1.ber.tlv.Value;
31 import org.apache.directory.shared.asn1.codec.EncoderException;
32 import org.apache.directory.shared.ldap.util.StringTools;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 public class AuthorizationDataEntry extends AbstractAsn1Object
52 {
53
54 private static final Logger log = LoggerFactory.getLogger( AuthorizationDataEntry.class );
55
56
57 private static final boolean IS_DEBUG = log.isDebugEnabled();
58
59
60
61
62
63
64
65 private AuthorizationType adType;
66
67
68 private byte[] adData;
69
70
71 private transient int adTypeLength;
72 private transient int adDataLength;
73 private transient int authorizationDataEntryLength;
74
75
76
77
78
79 public AuthorizationDataEntry()
80 {
81 }
82
83
84
85
86
87
88
89
90 public AuthorizationDataEntry( AuthorizationType adType, byte[] adData )
91 {
92 this.adType = adType;
93 this.adData = adData;
94 }
95
96
97
98
99
100
101
102 public byte[] getAdData()
103 {
104 return adData;
105 }
106
107
108
109
110
111
112
113 public void setAdData( byte[] adData )
114 {
115 this.adData = adData;
116 }
117
118
119
120
121
122
123
124 public AuthorizationType getAdType()
125 {
126 return adType;
127 }
128
129
130
131
132
133
134 public void setAdType( int adType )
135 {
136 this.adType = AuthorizationType.getTypeByOrdinal( adType );
137 }
138
139
140
141
142
143
144 public void setAdType( AuthorizationType adType )
145 {
146 this.adType = adType;
147 }
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171 public int computeLength()
172 {
173
174 adTypeLength = 1 + 1 + Value.getNbBytes( adType.getOrdinal() );
175 authorizationDataEntryLength = 1 + TLV.getNbBytes( adTypeLength ) + adTypeLength;
176
177
178 if ( adData == null )
179 {
180 adDataLength = 1 + 1;
181 }
182 else
183 {
184 adDataLength = 1 + TLV.getNbBytes( adData.length ) + adData.length;
185 }
186
187 authorizationDataEntryLength += 1 + TLV.getNbBytes( adDataLength ) + adDataLength;
188
189
190 int authorizationDataEntrySeqLength = 1 + Value.getNbBytes( authorizationDataEntryLength )
191 + authorizationDataEntryLength;
192
193 return authorizationDataEntrySeqLength;
194
195 }
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
214 {
215 if ( buffer == null )
216 {
217 throw new EncoderException( "Cannot put a PDU in a null buffer !" );
218 }
219
220 try
221 {
222
223 buffer.put( UniversalTag.SEQUENCE_TAG );
224 buffer.put( TLV.getBytes( authorizationDataEntryLength ) );
225
226
227 buffer.put( ( byte ) 0xA0 );
228 buffer.put( TLV.getBytes( adTypeLength ) );
229 Value.encode( buffer, adType.getOrdinal() );
230
231
232 buffer.put( ( byte ) 0xA1 );
233 buffer.put( TLV.getBytes( adDataLength ) );
234 Value.encode( buffer, adData );
235 }
236 catch ( BufferOverflowException boe )
237 {
238 log
239 .error(
240 "Cannot encode the AuthorizationDataEntry object, the PDU size is {} when only {} bytes has been allocated",
241 1 + TLV.getNbBytes( authorizationDataEntryLength ) + authorizationDataEntryLength, buffer
242 .capacity() );
243 throw new EncoderException( "The PDU buffer size is too small !" );
244 }
245
246 if ( IS_DEBUG )
247 {
248 log.debug( "AuthorizationDataEntry encoding : {}", StringTools.dumpBytes( buffer.array() ) );
249 log.debug( "AuthorizationDataEntry initial value : {}", toString() );
250 }
251
252 return buffer;
253 }
254
255
256
257
258
259 public String toString()
260 {
261 return toString( " " );
262 }
263
264
265
266
267
268 public String toString( String tabs )
269 {
270 StringBuilder sb = new StringBuilder();
271
272 sb.append( tabs ).append( "AuthorizationDataEntry : {\n" );
273 sb.append( tabs ).append( " ad-type: " ).append( adType ).append( '\n' );
274
275 sb.append( tabs ).append( " ad-data: " ).append( StringTools.dumpBytes( adData ) )
276 .append( "\n" + tabs + "}" );
277
278 return sb.toString();
279 }
280 }