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.extended.ads_impl.storedProcedure;
21
22
23 import java.nio.BufferOverflowException;
24 import java.nio.ByteBuffer;
25 import java.util.LinkedList;
26 import java.util.List;
27
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.i18n.I18n;
33 import org.apache.directory.api.ldap.codec.api.ExtendedRequestDecorator;
34 import org.apache.directory.api.ldap.codec.api.LdapApiService;
35 import org.apache.directory.api.ldap.extras.extended.storedProcedure.StoredProcedureParameter;
36 import org.apache.directory.api.ldap.extras.extended.storedProcedure.StoredProcedureRequest;
37 import org.apache.directory.api.ldap.extras.extended.storedProcedure.StoredProcedureRequestImpl;
38 import org.apache.directory.api.util.Strings;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42
43
44
45
46
47
48 public class StoredProcedureRequestDecorator extends ExtendedRequestDecorator<StoredProcedureRequest>
49 implements StoredProcedureRequest
50 {
51 private static final Logger LOG = LoggerFactory.getLogger( StoredProcedureRequestDecorator.class );
52
53 private StoredProcedureParameter currentParameter;
54
55
56 private int storedProcedureLength;
57
58
59 private int parametersLength;
60
61
62 private List<Integer> parameterLength;
63
64
65 private List<Integer> paramTypeLength;
66
67
68 private List<Integer> paramValueLength;
69
70
71 public StoredProcedureRequestDecorator( LdapApiService codec )
72 {
73 super( codec, new StoredProcedureRequestImpl() );
74 }
75
76
77 public StoredProcedureRequestDecorator( LdapApiService codec, StoredProcedureRequest decoratedRequest )
78 {
79 super( codec, decoratedRequest );
80 if ( decoratedRequest == null )
81 {
82 throw new NullPointerException( "decorated stored procedulre request is null" );
83 }
84 }
85
86
87 public StoredProcedureParameter getCurrentParameter()
88 {
89 return currentParameter;
90 }
91
92
93 public void setCurrentParameter( StoredProcedureParameter currentParameter )
94 {
95 this.currentParameter = currentParameter;
96 }
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126 int computeLengthInternal()
127 {
128
129 byte[] languageBytes = Strings.getBytesUtf8( getDecorated().getLanguage() );
130
131 int languageLength = 1 + TLV.getNbBytes( languageBytes.length )
132 + languageBytes.length;
133
134 byte[] procedure = getDecorated().getProcedure();
135
136
137 int procedureLength = 1 + TLV.getNbBytes( procedure.length )
138 + procedure.length;
139
140
141 if ( getDecorated().getParameters() != null )
142 {
143 parameterLength = new LinkedList<Integer>();
144 paramTypeLength = new LinkedList<Integer>();
145 paramValueLength = new LinkedList<Integer>();
146
147 for ( StoredProcedureParameter spParam : getDecorated().getParameters() )
148 {
149 int localParameterLength = 0;
150 int localParamTypeLength = 0;
151 int localParamValueLength = 0;
152
153 localParamTypeLength = 1 + TLV.getNbBytes( spParam.getType().length ) + spParam.getType().length;
154 localParamValueLength = 1 + TLV.getNbBytes( spParam.getValue().length ) + spParam.getValue().length;
155
156 localParameterLength = localParamTypeLength + localParamValueLength;
157
158 parametersLength += 1 + TLV.getNbBytes( localParameterLength ) + localParameterLength;
159
160 parameterLength.add( localParameterLength );
161 paramTypeLength.add( localParamTypeLength );
162 paramValueLength.add( localParamValueLength );
163 }
164 }
165
166 int localParametersLength = 1 + TLV.getNbBytes( parametersLength ) + parametersLength;
167 storedProcedureLength = languageLength + procedureLength + localParametersLength;
168
169 return 1 + TLV.getNbBytes( storedProcedureLength ) + storedProcedureLength;
170 }
171
172
173
174
175
176
177
178
179 ByteBuffer encodeInternal() throws EncoderException
180 {
181
182 ByteBuffer bb = ByteBuffer.allocate( computeLengthInternal() );
183
184 try
185 {
186
187 bb.put( UniversalTag.SEQUENCE.getValue() );
188 bb.put( TLV.getBytes( storedProcedureLength ) );
189
190
191 BerValue.encode( bb, getDecorated().getLanguage() );
192
193
194 BerValue.encode( bb, getDecorated().getProcedure() );
195
196
197 bb.put( UniversalTag.SEQUENCE.getValue() );
198 bb.put( TLV.getBytes( parametersLength ) );
199
200
201 if ( ( getDecorated().getParameters() != null ) && ( getDecorated().getParameters().size() != 0 ) )
202 {
203 int parameterNumber = 0;
204
205 for ( StoredProcedureParameter spParam : getDecorated().getParameters() )
206 {
207
208 bb.put( UniversalTag.SEQUENCE.getValue() );
209 int localParameterLength = parameterLength.get( parameterNumber );
210 bb.put( TLV.getBytes( localParameterLength ) );
211
212
213 BerValue.encode( bb, spParam.getType() );
214
215
216 BerValue.encode( bb, spParam.getValue() );
217
218
219 parameterNumber++;
220 }
221 }
222 }
223 catch ( BufferOverflowException boe )
224 {
225 throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
226 }
227
228 return bb;
229 }
230
231
232
233
234
235
236
237 public String toString()
238 {
239
240 StringBuffer sb = new StringBuffer();
241
242 sb.append( " StoredProcedure\n" );
243 sb.append( " Language : '" ).append( getDecorated().getLanguage() ).append( "'\n" );
244 sb.append( " Procedure\n" ).append( getDecorated().getProcedureSpecification() ).append( "'\n" );
245
246 if ( ( getDecorated().getParameters() == null ) || ( getDecorated().getParameters().size() == 0 ) )
247 {
248 sb.append( " No parameters\n" );
249 }
250 else
251 {
252 sb.append( " Parameters\n" );
253
254 int i = 1;
255
256 for ( StoredProcedureParameter spParam : getDecorated().getParameters() )
257 {
258 sb.append( " type[" ).append( i ).append( "] : '" ).
259 append( Strings.utf8ToString( spParam.getType() ) ).append( "'\n" );
260 sb.append( " value[" ).append( i ).append( "] : '" ).
261 append( Strings.dumpBytes( spParam.getValue() ) ).append( "'\n" );
262 }
263 }
264
265 return sb.toString();
266 }
267
268
269 public void setProcedure( byte[] procedure )
270 {
271 getDecorated().setProcedure( procedure );
272 }
273
274
275
276
277
278 public void setRequestValue( byte[] payload )
279 {
280 StoredProcedureDecoder decoder = new StoredProcedureDecoder();
281 StoredProcedureContainer container = new StoredProcedureContainer();
282
283 container.setStoredProcedure( this );
284
285 try
286 {
287 decoder.decode( ByteBuffer.wrap( payload ), container );
288 }
289 catch ( Exception e )
290 {
291 LOG.error( I18n.err( I18n.ERR_04165 ), e );
292 throw new RuntimeException( e );
293 }
294 }
295
296
297
298
299
300 public byte[] getRequestValue()
301 {
302 if ( requestValue == null )
303 {
304 try
305 {
306 requestValue = encodeInternal().array();
307 }
308 catch ( EncoderException e )
309 {
310 LOG.error( I18n.err( I18n.ERR_04174 ), e );
311 throw new RuntimeException( e );
312 }
313 }
314
315 return requestValue;
316 }
317
318
319
320
321
322 public String getLanguage()
323 {
324 return getDecorated().getLanguage();
325 }
326
327
328
329
330
331 public void setLanguage( String language )
332 {
333 getDecorated().setLanguage( language );
334 }
335
336
337
338
339
340 public String getProcedureSpecification()
341 {
342 return getDecorated().getProcedureSpecification();
343 }
344
345
346
347
348
349 public int size()
350 {
351 return getDecorated().size();
352 }
353
354
355
356
357
358 public Object getParameterType( int index )
359 {
360 return getDecorated().getParameterType( index );
361 }
362
363
364
365
366
367
368 public Class<?> getJavaParameterType( int index )
369 {
370 return getDecorated().getJavaParameterType( index );
371 }
372
373
374
375
376
377
378 public Object getParameterValue( int index )
379 {
380 return getDecorated().getParameterValue( index );
381 }
382
383
384
385
386
387 public Object getJavaParameterValue( int index )
388 {
389 return getDecorated().getJavaParameterValue( index );
390 }
391
392
393
394
395
396 public void addParameter( Object type, Object value )
397 {
398 getDecorated().addParameter( type, value );
399 }
400
401
402
403
404
405 public byte[] getProcedure()
406 {
407 return getDecorated().getProcedure();
408 }
409
410
411
412
413
414 public List<StoredProcedureParameter> getParameters()
415 {
416 return getDecorated().getParameters();
417 }
418
419
420
421
422
423 public void addParameter( StoredProcedureParameter parameter )
424 {
425 getDecorated().addParameter( parameter );
426 }
427 }