1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.shared.kerberos.components;
21
22
23 import java.nio.BufferOverflowException;
24 import java.nio.ByteBuffer;
25 import java.util.Arrays;
26
27 import org.apache.directory.api.asn1.Asn1Object;
28 import org.apache.directory.api.asn1.EncoderException;
29 import org.apache.directory.api.asn1.ber.tlv.BerValue;
30 import org.apache.directory.api.asn1.ber.tlv.TLV;
31 import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
32 import org.apache.directory.api.util.Strings;
33 import org.apache.directory.server.i18n.I18n;
34 import org.apache.directory.shared.kerberos.KerberosConstants;
35 import org.apache.directory.shared.kerberos.codec.types.TransitedEncodingType;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public class TransitedEncoding implements Asn1Object
54 {
55
56 private static final Logger log = LoggerFactory.getLogger( TransitedEncoding.class );
57
58
59 private static final boolean IS_DEBUG = log.isDebugEnabled();
60
61
62
63
64
65
66 private TransitedEncodingType trType;
67
68
69 private byte[] contents;
70
71
72 private int trTypeLength;
73 private int contentsLength;
74 private int transitedEncodingLength;
75
76
77
78
79
80 public TransitedEncoding()
81 {
82 trType = TransitedEncodingType.NULL;
83 contents = Strings.EMPTY_BYTES;
84 }
85
86
87
88
89
90
91
92 public byte[] getContents()
93 {
94 return contents;
95 }
96
97
98
99
100
101
102 public void setContents( byte[] contents )
103 {
104 this.contents = contents;
105 }
106
107
108
109
110
111
112
113 public TransitedEncodingType getTrType()
114 {
115 return trType;
116 }
117
118
119
120
121
122
123 public void setTrType( TransitedEncodingType trType )
124 {
125 this.trType = trType;
126 }
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152 public int computeLength()
153 {
154
155 trTypeLength = 1 + 1 + BerValue.getNbBytes( trType.getValue() );
156 transitedEncodingLength = 1 + TLV.getNbBytes( trTypeLength ) + trTypeLength;
157
158
159 if ( contents == null )
160 {
161 contentsLength = 1 + 1;
162 }
163 else
164 {
165 contentsLength = 1 + TLV.getNbBytes( contents.length ) + contents.length;
166 }
167
168 transitedEncodingLength += 1 + TLV.getNbBytes( contentsLength ) + contentsLength;
169
170
171 return 1 + TLV.getNbBytes( transitedEncodingLength ) + transitedEncodingLength;
172 }
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
191 {
192 if ( buffer == null )
193 {
194 throw new EncoderException( I18n.err( I18n.ERR_148 ) );
195 }
196
197 try
198 {
199
200 buffer.put( UniversalTag.SEQUENCE.getValue() );
201 buffer.put( TLV.getBytes( transitedEncodingLength ) );
202
203
204 buffer.put( ( byte ) KerberosConstants.TRANSITED_ENCODING_TR_TYPE_TAG );
205 buffer.put( TLV.getBytes( trTypeLength ) );
206 BerValue.encode( buffer, trType.getValue() );
207
208
209 buffer.put( ( byte ) KerberosConstants.TRANSITED_ENCODING_CONTENTS_TAG );
210 buffer.put( TLV.getBytes( contentsLength ) );
211 BerValue.encode( buffer, contents );
212 }
213 catch ( BufferOverflowException boe )
214 {
215 log.error( I18n.err( I18n.ERR_147, 1 + TLV.getNbBytes( transitedEncodingLength )
216 + transitedEncodingLength, buffer.capacity() ) );
217 throw new EncoderException( I18n.err( I18n.ERR_138 ), boe );
218 }
219
220 if ( IS_DEBUG )
221 {
222 log.debug( "TransitedEncoding encoding : {}", Strings.dumpBytes( buffer.array() ) );
223 log.debug( "TransitedEncoding initial value : {}", this );
224 }
225
226 return buffer;
227 }
228
229
230
231
232
233 @Override
234 public int hashCode()
235 {
236 final int prime = 31;
237 int result = 1;
238 result = prime * result + Arrays.hashCode( contents );
239 result = prime * result + ( ( trType == null ) ? 0 : trType.hashCode() );
240 return result;
241 }
242
243
244
245
246
247 @Override
248 public boolean equals( Object obj )
249 {
250 if ( this == obj )
251 {
252 return true;
253 }
254
255 if ( !( obj instanceof TransitedEncoding ) )
256 {
257 return false;
258 }
259
260 TransitedEncoding/../../org/apache/directory/shared/kerberos/components/TransitedEncoding.html#TransitedEncoding">TransitedEncoding other = ( TransitedEncoding ) obj;
261
262 if ( !Arrays.equals( contents, other.contents ) )
263 {
264 return false;
265 }
266
267 return trType == other.trType;
268 }
269
270
271
272
273
274 public String toString()
275 {
276 StringBuilder sb = new StringBuilder();
277
278 sb.append( "TransitedEncoding : {\n" );
279 sb.append( " tr-type: " ).append( trType ).append( '\n' );
280
281 sb.append( " contents: " ).append( Strings.dumpBytes( contents ) ).append( "\n}\n" );
282
283 return sb.toString();
284 }
285 }