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.TransitedEncodingType;
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 TransitedEncoding extends AbstractAsn1Object
52 {
53
54 private static final Logger log = LoggerFactory.getLogger( TransitedEncoding.class );
55
56
57 private static final boolean IS_DEBUG = log.isDebugEnabled();
58
59
60
61
62
63 private TransitedEncodingType trType;
64
65
66 private byte[] contents;
67
68
69 private transient int trTypeLength;
70 private transient int contentsLength;
71 private transient int transitedEncodingLength;
72
73
74
75
76
77 public TransitedEncoding()
78 {
79 trType = TransitedEncodingType.NULL;
80 contents = new byte[0];
81 }
82
83
84
85
86
87
88
89
90 public TransitedEncoding( TransitedEncodingType trType, byte[] contents )
91 {
92 this.trType = trType;
93 this.contents = contents;
94 }
95
96
97
98
99
100
101
102 public byte[] getContents()
103 {
104 return contents;
105 }
106
107
108
109
110
111
112 public void setContents( byte[] contents )
113 {
114 this.contents = contents;
115 }
116
117
118
119
120
121
122
123 public TransitedEncodingType getTrType()
124 {
125 return trType;
126 }
127
128
129
130
131
132
133 public void setTrType( TransitedEncodingType trType )
134 {
135 this.trType = trType;
136 }
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160 public int computeLength()
161 {
162
163 trTypeLength = 1 + 1 + Value.getNbBytes( trType.getOrdinal() );
164 transitedEncodingLength = 1 + TLV.getNbBytes( trTypeLength ) + trTypeLength;
165
166
167 if ( contents == null )
168 {
169 contentsLength = 1 + 1;
170 }
171 else
172 {
173 contentsLength = 1 + TLV.getNbBytes( contents.length ) + contents.length;
174 }
175
176 transitedEncodingLength += 1 + TLV.getNbBytes( contentsLength ) + contentsLength;
177
178
179 int transitedEncodingSeqLength = 1 + Value.getNbBytes( transitedEncodingLength ) + transitedEncodingLength;
180
181 return transitedEncodingSeqLength;
182
183 }
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
202 {
203 if ( buffer == null )
204 {
205 throw new EncoderException( "Cannot put a PDU in a null buffer !" );
206 }
207
208 try
209 {
210
211 buffer.put( UniversalTag.SEQUENCE_TAG );
212 buffer.put( TLV.getBytes( transitedEncodingLength ) );
213
214
215 buffer.put( ( byte ) 0xA0 );
216 buffer.put( TLV.getBytes( trTypeLength ) );
217 Value.encode( buffer, trType.getOrdinal() );
218
219
220 buffer.put( ( byte ) 0xA1 );
221 buffer.put( TLV.getBytes( contentsLength ) );
222 Value.encode( buffer, contents );
223 }
224 catch ( BufferOverflowException boe )
225 {
226 log.error(
227 "Cannot encode the TransitedEncoding object, the PDU size is {} when only {} bytes has been allocated",
228 1 + TLV.getNbBytes( transitedEncodingLength ) + transitedEncodingLength, buffer.capacity() );
229 throw new EncoderException( "The PDU buffer size is too small !" );
230 }
231
232 if ( IS_DEBUG )
233 {
234 log.debug( "TransitedEncoding encoding : {}", StringTools.dumpBytes( buffer.array() ) );
235 log.debug( "TransitedEncoding initial value : {}", toString() );
236 }
237
238 return buffer;
239 }
240
241
242
243
244
245 public String toString()
246 {
247 StringBuilder sb = new StringBuilder();
248
249 sb.append( "TransitedEncoding : {\n" );
250 sb.append( " tr-type: " ).append( trType ).append( '\n' );
251
252 sb.append( " contents: " ).append( StringTools.dumpBytes( contents ) ).append( "\n}\n" );
253
254 return sb.toString();
255 }
256 }