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.codec.controls.sort;
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.Asn1Decoder;
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.ControlDecorator;
34 import org.apache.directory.api.ldap.codec.api.LdapApiService;
35 import org.apache.directory.api.ldap.model.message.controls.SortResponse;
36 import org.apache.directory.api.ldap.model.message.controls.SortResponseControlImpl;
37 import org.apache.directory.api.ldap.model.message.controls.SortResultCode;
38 import org.apache.directory.api.util.Strings;
39
40
41
42
43
44
45
46 public class SortResponseDecorator extends ControlDecorator<SortResponse> implements SortResponse
47 {
48 private static final Asn1Decoder DECODER = new Asn1Decoder();
49
50 private int sortRespLen = 0;
51
52
53
54
55
56
57
58 public SortResponseDecorator( LdapApiService codec )
59 {
60 super( codec, new SortResponseControlImpl() );
61 }
62
63
64
65
66
67
68
69
70 public SortResponseDecorator( LdapApiService codec, SortResponse control )
71 {
72 super( codec, control );
73 }
74
75
76
77
78
79 @Override
80 public int computeLength()
81 {
82 sortRespLen = 0;
83 valueLength = 0;
84
85
86 sortRespLen += 1 + 1 + 1;
87
88 if ( getAttributeName() != null )
89 {
90 byte[] data = Strings.getBytesUtf8( getAttributeName() );
91 sortRespLen += 1 + TLV.getNbBytes( data.length ) + data.length;
92 }
93
94 valueLength = 1 + TLV.getNbBytes( sortRespLen ) + sortRespLen;
95
96 return valueLength;
97 }
98
99
100 @Override
101 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
102 {
103 if ( buffer == null )
104 {
105 throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
106 }
107
108 buffer.put( UniversalTag.SEQUENCE.getValue() );
109 buffer.put( TLV.getBytes( sortRespLen ) );
110
111 BerValue.encodeEnumerated( buffer, getSortResult().getVal() );
112
113 if ( getAttributeName() != null )
114 {
115 BerValue.encode( buffer, getAttributeName() );
116 }
117
118 return buffer;
119 }
120
121
122
123
124
125 @Override
126 public Asn1Object decode( byte[] controlBytes ) throws DecoderException
127 {
128 ByteBuffer buffer = ByteBuffer.wrap( controlBytes );
129 SortResponseContainer container = new SortResponseContainer( getCodecService(), this );
130 DECODER.decode( buffer, container );
131 return this;
132 }
133
134
135
136
137
138 @Override
139 public byte[] getValue()
140 {
141 if ( value == null )
142 {
143 try
144 {
145 computeLength();
146 ByteBuffer buffer = ByteBuffer.allocate( valueLength );
147
148 value = encode( buffer ).array();
149 }
150 catch ( Exception e )
151 {
152 return null;
153 }
154 }
155
156 return value;
157 }
158
159
160 @Override
161 public void setSortResult( SortResultCode result )
162 {
163 getDecorated().setSortResult( result );
164 }
165
166
167 @Override
168 public SortResultCode getSortResult()
169 {
170 return getDecorated().getSortResult();
171 }
172
173
174 @Override
175 public void setAttributeName( String attributeName )
176 {
177 getDecorated().setAttributeName( attributeName );
178 }
179
180
181 @Override
182 public String getAttributeName()
183 {
184 return getDecorated().getAttributeName();
185 }
186
187 }