1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.api.ldap.extras.controls.ad_impl;
21
22
23 import java.util.Set;
24
25 import org.apache.directory.api.asn1.DecoderException;
26 import org.apache.directory.api.asn1.ber.grammar.AbstractGrammar;
27 import org.apache.directory.api.asn1.ber.grammar.Grammar;
28 import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
29 import org.apache.directory.api.asn1.ber.grammar.GrammarTransition;
30 import org.apache.directory.api.asn1.ber.tlv.BerValue;
31 import org.apache.directory.api.asn1.ber.tlv.IntegerDecoder;
32 import org.apache.directory.api.asn1.ber.tlv.IntegerDecoderException;
33 import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
34 import org.apache.directory.api.ldap.extras.controls.ad.AdDirSyncFlag;
35 import org.apache.directory.api.util.Strings;
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
54
55
56
57 public final class AdDirSyncGrammar extends AbstractGrammar<AdDirSyncContainer>
58 {
59
60
61 private static final Logger LOG = LoggerFactory.getLogger( AdDirSyncGrammar.class );
62
63
64 private static final boolean IS_DEBUG = LOG.isDebugEnabled();
65
66
67 private static final AdDirSyncGrammar INSTANCE = new AdDirSyncGrammar();
68
69
70
71
72
73
74
75 @SuppressWarnings("unchecked")
76 private AdDirSyncGrammar()
77 {
78 setName( AdDirSyncGrammar.class.getName() );
79
80 super.transitions = new GrammarTransition[AdDirSyncStatesEnum.LAST_AD_DIR_SYNC_STATE.ordinal()][256];
81
82
83
84
85
86
87
88
89 super.transitions[AdDirSyncStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
90 new GrammarTransition<AdDirSyncContainer>(
91 AdDirSyncStatesEnum.START_STATE, AdDirSyncStatesEnum.AD_DIR_SYNC_SEQUENCE_STATE,
92 UniversalTag.SEQUENCE.getValue(),
93 new GrammarAction<AdDirSyncContainer>( "Initialization" )
94 {
95 public void action( AdDirSyncContainer container ) throws DecoderException
96 {
97 }
98 } );
99
100
101
102
103
104
105
106
107
108 super.transitions[AdDirSyncStatesEnum.AD_DIR_SYNC_SEQUENCE_STATE.ordinal()][UniversalTag.INTEGER
109 .getValue()] =
110 new GrammarTransition<AdDirSyncContainer>( AdDirSyncStatesEnum.AD_DIR_SYNC_SEQUENCE_STATE,
111 AdDirSyncStatesEnum.FLAG_STATE, UniversalTag.INTEGER.getValue(),
112 new GrammarAction<AdDirSyncContainer>( "Set AdDirSyncControl parentFirst" )
113 {
114 public void action( AdDirSyncContainer container ) throws DecoderException
115 {
116 BerValue value = container.getCurrentTLV().getValue();
117
118 try
119 {
120 int flagValue = IntegerDecoder.parse( value );
121
122 Set<AdDirSyncFlag> flags = AdDirSyncFlag.getFlags( flagValue );
123
124 if ( flags == null )
125 {
126 String msg = "Error while decoding the AdDirSync flag, unknown value : " + flagValue;
127 LOG.error( msg );
128 throw new DecoderException( msg );
129 }
130
131 if ( IS_DEBUG )
132 {
133 LOG.debug( "flags = {}", flags.toString() );
134 }
135
136 container.getAdDirSyncControl().setFlags( flags );
137 }
138 catch ( IntegerDecoderException ide )
139 {
140 String msg = "Error while decoding the AdDirSync flag : " + ide.getMessage();
141 LOG.error( msg, ide );
142 throw new DecoderException( msg, ide );
143 }
144 }
145 } );
146
147
148
149
150
151
152
153
154
155
156 super.transitions[AdDirSyncStatesEnum.FLAG_STATE.ordinal()][UniversalTag.INTEGER
157 .getValue()] =
158 new GrammarTransition<AdDirSyncContainer>( AdDirSyncStatesEnum.FLAG_STATE,
159 AdDirSyncStatesEnum.MAX_RETURN_LENGTH_STATE, UniversalTag.INTEGER.getValue(),
160 new GrammarAction<AdDirSyncContainer>( "Set AdDirSyncControl maxReturnLength" )
161 {
162 public void action( AdDirSyncContainer container ) throws DecoderException
163 {
164 BerValue value = container.getCurrentTLV().getValue();
165
166 try
167 {
168 int maxReturnLength = IntegerDecoder.parse( value );
169
170 if ( IS_DEBUG )
171 {
172 LOG.debug( "maxReturnLength = {}", maxReturnLength );
173 }
174
175 container.getAdDirSyncControl().setMaxReturnLength( maxReturnLength );
176 }
177 catch ( IntegerDecoderException ide )
178 {
179 String msg = "Error while decoding the AdDirSync maxReturnLength : " + ide.getMessage();
180 LOG.error( msg, ide );
181 throw new DecoderException( msg, ide );
182 }
183 }
184 } );
185
186
187
188
189
190
191
192
193
194 super.transitions[AdDirSyncStatesEnum.MAX_RETURN_LENGTH_STATE.ordinal()][UniversalTag.OCTET_STRING
195 .getValue()] =
196 new GrammarTransition<AdDirSyncContainer>( AdDirSyncStatesEnum.MAX_RETURN_LENGTH_STATE,
197 AdDirSyncStatesEnum.COOKIE_STATE, UniversalTag.OCTET_STRING.getValue(),
198 new GrammarAction<AdDirSyncContainer>( "Set AdDirSyncControl cookie" )
199 {
200 public void action( AdDirSyncContainer container ) throws DecoderException
201 {
202 BerValue value = container.getCurrentTLV().getValue();
203
204 byte[] cookie = value.getData();
205
206 if ( IS_DEBUG )
207 {
208 LOG.debug( "cookie = {}", Strings.dumpBytes( cookie ) );
209 }
210
211 container.getAdDirSyncControl().setCookie( cookie );
212
213 container.setGrammarEndAllowed( true );
214 }
215 } );
216 }
217
218
219
220
221
222 public static Grammar<AdDirSyncContainer> getInstance()
223 {
224 return INSTANCE;
225 }
226 }