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
26 import org.apache.directory.api.asn1.Asn1Object;
27 import org.apache.directory.api.asn1.EncoderException;
28 import org.apache.directory.api.asn1.ber.tlv.BerValue;
29 import org.apache.directory.api.asn1.ber.tlv.TLV;
30 import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
31 import org.apache.directory.api.util.Strings;
32 import org.apache.directory.server.i18n.I18n;
33 import org.apache.directory.shared.kerberos.KerberosConstants;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class AdAndOr implements Asn1Object
51 {
52
53 private static final Logger LOG = LoggerFactory.getLogger( AdAndOr.class );
54
55
56 private static final boolean IS_DEBUG = LOG.isDebugEnabled();
57
58
59 private int conditionCount;
60
61
62 private AuthorizationData elements;
63
64
65 private int conditionCountTagLength;
66 private int elementsTagLength;
67 private int adAndOrSeqLength;
68
69
70
71
72
73 public AdAndOr()
74 {
75 }
76
77
78
79
80
81 public int getConditionCount()
82 {
83 return conditionCount;
84 }
85
86
87
88
89
90 public void setConditionCount( int conditionCount )
91 {
92 this.conditionCount = conditionCount;
93 }
94
95
96
97
98
99 public AuthorizationData getElements()
100 {
101 return elements;
102 }
103
104
105
106
107
108 public void setElements( AuthorizationData elements )
109 {
110 this.elements = elements;
111 }
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128 @Override
129 public int computeLength()
130 {
131
132 int conditionCountLength = BerValue.getNbBytes( conditionCount );
133 conditionCountTagLength = 1 + TLV.getNbBytes( conditionCountLength ) + conditionCountLength;
134 adAndOrSeqLength = 1 + TLV.getNbBytes( conditionCountTagLength ) + conditionCountTagLength;
135
136
137 elementsTagLength = elements.computeLength();
138 adAndOrSeqLength += 1 + TLV.getNbBytes( elementsTagLength ) + elementsTagLength;
139
140
141 return 1 + TLV.getNbBytes( adAndOrSeqLength ) + adAndOrSeqLength;
142 }
143
144
145
146
147
148
149
150
151
152 @Override
153 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
154 {
155 if ( buffer == null )
156 {
157 throw new EncoderException( I18n.err( I18n.ERR_148 ) );
158 }
159
160 try
161 {
162
163 buffer.put( UniversalTag.SEQUENCE.getValue() );
164 buffer.put( TLV.getBytes( adAndOrSeqLength ) );
165
166
167 buffer.put( ( byte ) KerberosConstants.AD_AND_OR_CONDITION_COUNT_TAG );
168 buffer.put( ( byte ) conditionCountTagLength );
169 BerValue.encode( buffer, conditionCount );
170
171
172 buffer.put( ( byte ) KerberosConstants.AD_AND_OR_ELEMENTS_TAG );
173 buffer.put( ( byte ) elementsTagLength );
174
175 elements.encode( buffer );
176 }
177 catch ( BufferOverflowException boe )
178 {
179 LOG.error( I18n.err( I18n.ERR_139, 1 + TLV.getNbBytes( adAndOrSeqLength )
180 + adAndOrSeqLength, buffer.capacity() ) );
181 throw new EncoderException( I18n.err( I18n.ERR_138 ), boe );
182 }
183
184 if ( IS_DEBUG )
185 {
186 LOG.debug( "AD-AND-OR encoding : {}", Strings.dumpBytes( buffer.array() ) );
187 LOG.debug( "AD-AND-OR initial value : {}", this );
188 }
189
190 return buffer;
191 }
192
193
194
195
196
197 public String toString()
198 {
199 return toString( "" );
200 }
201
202
203
204
205
206 public String toString( String tabs )
207 {
208 StringBuilder sb = new StringBuilder();
209
210 sb.append( tabs ).append( "AD-AND-OR : {\n" );
211 sb.append( tabs ).append( " condition-count: " ).append( conditionCount ).append( '\n' );
212 sb.append( tabs + " elements:" ).append( elements ).append( '\n' );
213 sb.append( tabs + "}\n" );
214
215 return sb.toString();
216 }
217 }