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 public CancelRequestDecorator( LdapApiService codec, CancelRequest decoratedMessage )
57 {
58 super( codec, decoratedMessage );
59 cancelRequest = decoratedMessage;
60 }
61
62
63
64
65
66 public int getCancelId()
67 {
68 return cancelRequest.getCancelId();
69 }
70
71
72
73
74
75 public void setCancelId( int cancelId )
76 {
77 if ( cancelId == cancelRequest.getCancelId() )
78 {
79 return;
80 }
81
82 this.requestValue = null;
83 cancelRequest.setCancelId( cancelId );
84 }
85
86
87
88
89
90 public byte[] getRequestValue()
91 {
92 if ( requestValue == null )
93 {
94 try
95 {
96 requestValue = encodeInternal().array();
97 }
98 catch ( EncoderException e )
99 {
100 LOG.error( I18n.err( I18n.ERR_04164 ), e );
101 throw new RuntimeException( e );
102 }
103 }
104
105 return requestValue;
106 }
107
108
109
110
111
112
113
114 @Override
115 public void setRequestValue( byte[] requestValue )
116 {
117 CancelDecoder decoder = new CancelDecoder();
118
119 try
120 {
121 CancelRequest cancel = decoder.decode( requestValue );
122
123 if ( requestValue != null )
124 {
125 this.requestValue = new byte[requestValue.length];
126 System.arraycopy( requestValue, 0, this.requestValue, 0, requestValue.length );
127 }
128 else
129 {
130 this.requestValue = null;
131 }
132
133 cancelRequest.setCancelId( cancel.getCancelId() );
134 }
135 catch ( DecoderException e )
136 {
137 LOG.error( I18n.err( I18n.ERR_04165 ), e );
138 throw new RuntimeException( e );
139 }
140 }
141
142
143
144
145
146
147
148
149
150 int computeLengthInternal()
151 {
152
153 cancelSequenceLength = 1 + 1 + BerValue.getNbBytes( cancelRequest.getCancelId() );
154
155
156 return 1 + 1 + cancelSequenceLength;
157 }
158
159
160
161
162
163
164
165
166 ByteBuffer encodeInternal() throws EncoderException
167 {
168
169 ByteBuffer bb = ByteBuffer.allocate( computeLengthInternal() );
170
171
172 bb.put( UniversalTag.SEQUENCE.getValue() );
173 bb.put( TLV.getBytes( cancelSequenceLength ) );
174
175
176 BerValue.encode( bb, cancelRequest.getCancelId() );
177
178 return bb;
179 }
180 }