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.cancel;
21
22
23 import java.nio.ByteBuffer;
24
25 import org.apache.directory.api.asn1.Asn1Object;
26 import org.apache.directory.api.asn1.DecoderException;
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.i18n.I18n;
32 import org.apache.directory.api.ldap.codec.api.ExtendedRequestDecorator;
33 import org.apache.directory.api.ldap.codec.api.LdapApiService;
34 import org.apache.directory.api.ldap.extras.extended.cancel.CancelRequest;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38
39
40
41
42
43
44 public class CancelRequestDecorator extends ExtendedRequestDecorator<CancelRequest> implements
45 CancelRequest, Asn1Object
46 {
47 private static final Logger LOG = LoggerFactory.getLogger( CancelRequestDecorator.class );
48
49
50 private CancelRequest cancelRequest;
51
52
53 private int cancelSequenceLength;
54
55
56
57
58
59
60
61
62 public CancelRequestDecorator( LdapApiService codec, CancelRequest decoratedMessage )
63 {
64 super( codec, decoratedMessage );
65 cancelRequest = decoratedMessage;
66 }
67
68
69
70
71
72 @Override
73 public int getCancelId()
74 {
75 return cancelRequest.getCancelId();
76 }
77
78
79
80
81
82 @Override
83 public void setCancelId( int cancelId )
84 {
85 if ( cancelId == cancelRequest.getCancelId() )
86 {
87 return;
88 }
89
90 this.requestValue = null;
91 cancelRequest.setCancelId( cancelId );
92 }
93
94
95
96
97
98 @Override
99 public byte[] getRequestValue()
100 {
101 if ( requestValue == null )
102 {
103 try
104 {
105 requestValue = encodeInternal().array();
106 }
107 catch ( EncoderException e )
108 {
109 LOG.error( I18n.err( I18n.ERR_04164 ), e );
110 throw new RuntimeException( e );
111 }
112 }
113
114 return requestValue;
115 }
116
117
118
119
120
121
122
123 @Override
124 public void setRequestValue( byte[] requestValue )
125 {
126 CancelDecoder decoder = new CancelDecoder();
127
128 try
129 {
130 if ( requestValue != null )
131 {
132 CancelRequest cancel = decoder.decode( requestValue );
133 cancelRequest.setCancelId( cancel.getCancelId() );
134
135 this.requestValue = new byte[requestValue.length];
136 System.arraycopy( requestValue, 0, this.requestValue, 0, requestValue.length );
137 }
138 else
139 {
140 this.requestValue = null;
141 cancelRequest.setCancelId( 0 );
142 }
143
144 }
145 catch ( DecoderException e )
146 {
147 LOG.error( I18n.err( I18n.ERR_04165 ), e );
148 throw new RuntimeException( e );
149 }
150 }
151
152
153
154
155
156
157
158
159
160
161 int computeLengthInternal()
162 {
163
164 cancelSequenceLength = 1 + 1 + BerValue.getNbBytes( cancelRequest.getCancelId() );
165
166
167 return 1 + 1 + cancelSequenceLength;
168 }
169
170
171
172
173
174
175
176
177 ByteBuffer encodeInternal() throws EncoderException
178 {
179
180 ByteBuffer bb = ByteBuffer.allocate( computeLengthInternal() );
181
182
183 bb.put( UniversalTag.SEQUENCE.getValue() );
184 bb.put( TLV.getBytes( cancelSequenceLength ) );
185
186
187 BerValue.encode( bb, cancelRequest.getCancelId() );
188
189 return bb;
190 }
191 }