1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.directory.api.ldap.extras.extended.ads_impl.storedProcedure;
22
23
24 import org.apache.directory.api.asn1.DecoderException;
25 import org.apache.directory.api.asn1.ber.grammar.AbstractGrammar;
26 import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
27 import org.apache.directory.api.asn1.ber.grammar.GrammarTransition;
28 import org.apache.directory.api.asn1.ber.tlv.TLV;
29 import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
30 import org.apache.directory.api.i18n.I18n;
31 import org.apache.directory.api.ldap.extras.extended.storedProcedure.StoredProcedureParameter;
32 import org.apache.directory.api.ldap.extras.extended.storedProcedure.StoredProcedureRequest;
33 import org.apache.directory.api.util.Strings;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37
38
39
40
41
42
43 public final class StoredProcedureRequestGrammar extends AbstractGrammar<StoredProcedureRequestContainer>
44 {
45
46 static final Logger LOG = LoggerFactory.getLogger( StoredProcedureRequestGrammar.class );
47
48
49 private static StoredProcedureRequestGrammar instance = new StoredProcedureRequestGrammar();
50
51
52
53
54
55
56
57 @SuppressWarnings("unchecked")
58 private StoredProcedureRequestGrammar()
59 {
60 setName( StoredProcedureRequestGrammar.class.getName() );
61
62
63 super.transitions = new GrammarTransition[StoredProcedureStatesEnum.LAST_STORED_PROCEDURE_STATE.ordinal()][256];
64
65
66
67
68
69
70
71 super.transitions[StoredProcedureStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
72 new GrammarTransition<StoredProcedureRequestContainer>( StoredProcedureStatesEnum.START_STATE,
73 StoredProcedureStatesEnum.STORED_PROCEDURE_STATE,
74 UniversalTag.SEQUENCE.getValue(),
75 null );
76
77
78
79
80
81 super.transitions[StoredProcedureStatesEnum.STORED_PROCEDURE_STATE.ordinal()][UniversalTag.OCTET_STRING
82 .getValue()] =
83 new GrammarTransition<StoredProcedureRequestContainer>( StoredProcedureStatesEnum.STORED_PROCEDURE_STATE,
84 StoredProcedureStatesEnum.LANGUAGE_STATE,
85 UniversalTag.OCTET_STRING.getValue(),
86 new GrammarAction<StoredProcedureRequestContainer>( "Stores the language" )
87 {
88 public void action( StoredProcedureRequestContainer container ) throws DecoderException
89 {
90 TLV tlv = container.getCurrentTLV();
91
92
93 if ( tlv.getLength() == 0 )
94 {
95
96 String msg = I18n.err( I18n.ERR_08207_SP_LANGUAGE_NULL );
97 LOG.error( msg );
98 throw new DecoderException( msg );
99 }
100 else
101 {
102
103 String language = Strings.utf8ToString( tlv.getValue().getData() );
104
105 if ( LOG.isDebugEnabled() )
106 {
107 LOG.debug( I18n.msg( I18n.MSG_08213_SP_LANGUAGE_FOUND, language ) );
108 }
109
110 container.getStoredProcedure().setLanguage( language );
111 }
112 }
113 } );
114
115
116
117
118 super.transitions[StoredProcedureStatesEnum.LANGUAGE_STATE.ordinal()][UniversalTag.OCTET_STRING.getValue()] =
119 new GrammarTransition<StoredProcedureRequestContainer>( StoredProcedureStatesEnum.LANGUAGE_STATE,
120 StoredProcedureStatesEnum.PROCEDURE_STATE,
121 UniversalTag.OCTET_STRING.getValue(),
122 new GrammarAction<StoredProcedureRequestContainer>( "Stores the procedure" )
123 {
124 public void action( StoredProcedureRequestContainer container ) throws DecoderException
125 {
126 TLV tlv = container.getCurrentTLV();
127
128
129 if ( tlv.getLength() == 0 )
130 {
131
132 String msg = I18n.err( I18n.ERR_08208_NULL_PROCEDURE );
133 LOG.error( msg );
134 throw new DecoderException( msg );
135 }
136 else
137 {
138 byte[] procedure = tlv.getValue().getData();
139
140 container.getStoredProcedure().setProcedure( procedure );
141 }
142
143 if ( LOG.isDebugEnabled() )
144 {
145 LOG.debug( I18n.msg( I18n.MSG_08212_PROCEDURE_FOUND,
146 container.getStoredProcedure().getProcedureSpecification() ) );
147 }
148 }
149 } );
150
151
152
153
154
155 super.transitions[StoredProcedureStatesEnum.PROCEDURE_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
156 new GrammarTransition<StoredProcedureRequestContainer>( StoredProcedureStatesEnum.PROCEDURE_STATE,
157 StoredProcedureStatesEnum.PARAMETERS_STATE,
158 UniversalTag.SEQUENCE.getValue(),
159 new GrammarAction<StoredProcedureRequestContainer>( "Stores the parameters" )
160 {
161 public void action( StoredProcedureRequestContainer container )
162 {
163 container.setGrammarEndAllowed( true );
164 }
165 } );
166
167
168
169
170 super.transitions[StoredProcedureStatesEnum.PARAMETERS_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
171 new GrammarTransition<StoredProcedureRequestContainer>( StoredProcedureStatesEnum.PARAMETERS_STATE,
172 StoredProcedureStatesEnum.PARAMETER_STATE,
173 UniversalTag.SEQUENCE.getValue(),
174 null );
175
176
177
178
179
180
181 super.transitions[StoredProcedureStatesEnum.PARAMETER_STATE.ordinal()][UniversalTag.OCTET_STRING.getValue()] =
182 new GrammarTransition<StoredProcedureRequestContainer>( StoredProcedureStatesEnum.PARAMETER_STATE,
183 StoredProcedureStatesEnum.PARAMETER_TYPE_STATE,
184 UniversalTag.OCTET_STRING.getValue(),
185 new GrammarAction<StoredProcedureRequestContainer>( "Store parameter type" )
186 {
187 public void action( StoredProcedureRequestContainer container ) throws DecoderException
188 {
189 TLV tlv = container.getCurrentTLV();
190
191 if ( tlv.getLength() == 0 )
192 {
193
194 String msg = I18n.err( I18n.ERR_08209_NULL_PARAMETER_TYPE );
195 LOG.error( msg );
196 throw new DecoderException( msg );
197 }
198 else
199 {
200 StoredProcedureParameter parameter = new StoredProcedureParameter();
201
202 byte[] parameterType = tlv.getValue().getData();
203
204 parameter.setType( parameterType );
205
206
207 container.setCurrentParameter( parameter );
208
209 if ( LOG.isDebugEnabled() )
210 {
211 LOG.debug( I18n.msg( I18n.MSG_08210_PARAMETER_TYPE_FOUND, Strings.dumpBytes( parameterType ) ) );
212 }
213
214 }
215 }
216 } );
217
218
219
220
221
222
223 super.transitions[StoredProcedureStatesEnum.PARAMETER_TYPE_STATE.ordinal()][UniversalTag.OCTET_STRING
224 .getValue()] =
225 new GrammarTransition<StoredProcedureRequestContainer>( StoredProcedureStatesEnum.PARAMETER_TYPE_STATE,
226 StoredProcedureStatesEnum.PARAMETER_VALUE_STATE,
227 UniversalTag.OCTET_STRING.getValue(),
228 new GrammarAction<StoredProcedureRequestContainer>( "Store parameter value" )
229 {
230 public void action( StoredProcedureRequestContainer container ) throws DecoderException
231 {
232 TLV tlv = container.getCurrentTLV();
233 StoredProcedureRequest storedProcedure = container.getStoredProcedure();
234
235
236 if ( tlv.getLength() == 0 )
237 {
238
239 String msg = I18n.err( I18n.ERR_08210_NULL_PARAMETER_VALUE );
240 LOG.error( msg );
241 throw new DecoderException( msg );
242 }
243 else
244 {
245 byte[] parameterValue = tlv.getValue().getData();
246
247 if ( parameterValue.length != 0 )
248 {
249 StoredProcedureParameter parameter = container.getCurrentParameter();
250 parameter.setValue( parameterValue );
251
252
253 storedProcedure.addParameter( parameter );
254
255 if ( LOG.isDebugEnabled() )
256 {
257 LOG.debug( I18n.msg( I18n.MSG_08211_PARAMETER_VALUE_FOUND, Strings.dumpBytes( parameterValue ) ) );
258 }
259 }
260 else
261 {
262 String msg = I18n.err( I18n.ERR_08211_EMPTY_PARAMETER_VALUE );
263 LOG.error( msg );
264 throw new DecoderException( msg );
265 }
266 }
267
268
269 container.setGrammarEndAllowed( true );
270 }
271 } );
272
273
274
275
276 super.transitions[StoredProcedureStatesEnum.PARAMETER_VALUE_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
277 new GrammarTransition<StoredProcedureRequestContainer>( StoredProcedureStatesEnum.PARAMETER_VALUE_STATE,
278 StoredProcedureStatesEnum.PARAMETER_STATE,
279 UniversalTag.SEQUENCE.getValue(),
280 null );
281 }
282
283
284
285
286
287
288
289
290
291 public static StoredProcedureRequestGrammar getInstance()
292 {
293 return instance;
294 }
295 }