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 import java.util.ArrayList;
25 import java.util.List;
26
27 import org.apache.directory.api.asn1.Asn1Object;
28 import org.apache.directory.api.asn1.DecoderException;
29 import org.apache.directory.api.asn1.EncoderException;
30 import org.apache.directory.api.asn1.ber.Asn1Decoder;
31 import org.apache.directory.api.asn1.ber.tlv.BerValue;
32 import org.apache.directory.api.asn1.ber.tlv.TLV;
33 import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
34 import org.apache.directory.api.asn1.util.Asn1StringUtils;
35 import org.apache.directory.api.i18n.I18n;
36 import org.apache.directory.api.ldap.codec.api.ControlDecorator;
37 import org.apache.directory.api.ldap.codec.api.LdapApiService;
38 import org.apache.directory.api.ldap.model.message.controls.SortKey;
39 import org.apache.directory.api.ldap.model.message.controls.SortRequest;
40 import org.apache.directory.api.ldap.model.message.controls.SortRequestControlImpl;
41 import org.apache.directory.api.util.Strings;
42
43
44
45
46
47
48
49 public class SortRequestDecorator extends ControlDecorator<SortRequest> implements SortRequest
50 {
51 private static final Asn1Decoder DECODER = new Asn1Decoder();
52
53 private int sortReqLen = 0;
54
55 private List<Integer> sortKeyLenList = new ArrayList<>();
56
57
58 public static final int ORDERING_RULE_TAG = 0x80;
59
60
61 public static final int REVERSE_ORDER_TAG = 0x81;
62
63
64
65
66
67
68
69 public SortRequestDecorator( LdapApiService codec )
70 {
71 super( codec, new SortRequestControlImpl() );
72 }
73
74
75
76
77
78
79
80
81 public SortRequestDecorator( LdapApiService codec, SortRequest control )
82 {
83 super( codec, control );
84 }
85
86
87
88
89
90 @Override
91 public int computeLength()
92 {
93 sortReqLen = 0;
94 sortKeyLenList.clear();
95 valueLength = 0;
96
97 for ( SortKey sk : getSortKeys() )
98 {
99 int skLen = 0;
100
101 byte[] atBytes = Strings.getBytesUtf8( sk.getAttributeTypeDesc() );
102 skLen += 1 + TLV.getNbBytes( atBytes.length ) + atBytes.length;
103
104 if ( sk.getMatchingRuleId() != null )
105 {
106 byte[] mrBytes = Strings.getBytesUtf8( sk.getMatchingRuleId() );
107 skLen += 1 + TLV.getNbBytes( mrBytes.length ) + mrBytes.length;
108 }
109
110 if ( sk.isReverseOrder() )
111 {
112
113 skLen += 1 + 1 + 1;
114 }
115
116 sortKeyLenList.add( skLen );
117
118
119 sortReqLen += 1 + TLV.getNbBytes( skLen ) + skLen;
120 }
121
122 valueLength = 1 + TLV.getNbBytes( sortReqLen ) + sortReqLen;
123
124 return valueLength;
125 }
126
127
128 @Override
129 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
130 {
131 if ( buffer == null )
132 {
133 throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
134 }
135
136 buffer.put( UniversalTag.SEQUENCE.getValue() );
137 buffer.put( TLV.getBytes( sortReqLen ) );
138
139 List<SortKey> lst = getSortKeys();
140
141 for ( int i = 0; i < lst.size(); i++ )
142 {
143 SortKey sk = lst.get( i );
144 int skLen = sortKeyLenList.get( i );
145
146 buffer.put( UniversalTag.SEQUENCE.getValue() );
147 buffer.put( TLV.getBytes( skLen ) );
148
149 BerValue.encode( buffer, sk.getAttributeTypeDesc() );
150
151 String mrId = sk.getMatchingRuleId();
152 if ( mrId != null )
153 {
154 buffer.put( ( byte ) ORDERING_RULE_TAG );
155 byte[] value = Asn1StringUtils.getBytesUtf8( mrId );
156
157 buffer.put( TLV.getBytes( value.length ) );
158 buffer.put( value );
159 }
160
161 if ( sk.isReverseOrder() )
162 {
163 buffer.put( ( byte ) REVERSE_ORDER_TAG );
164 buffer.put( ( byte ) 0x01 );
165 buffer.put( BerValue.TRUE_VALUE );
166 }
167 }
168
169 return buffer;
170 }
171
172
173 @Override
174 public Asn1Object decode( byte[] controlBytes ) throws DecoderException
175 {
176 ByteBuffer buffer = ByteBuffer.wrap( controlBytes );
177 SortRequestContainer container = new SortRequestContainer( getCodecService(), this );
178 DECODER.decode( buffer, container );
179 return this;
180 }
181
182
183
184
185
186 @Override
187 public byte[] getValue()
188 {
189 if ( value == null )
190 {
191 try
192 {
193 computeLength();
194 ByteBuffer buffer = ByteBuffer.allocate( valueLength );
195
196 value = encode( buffer ).array();
197 }
198 catch ( Exception e )
199 {
200 return null;
201 }
202 }
203
204 return value;
205 }
206
207
208 @Override
209 public void setSortKeys( List<SortKey> sortKeys )
210 {
211 getDecorated().setSortKeys( sortKeys );
212 }
213
214
215 @Override
216 public List<SortKey> getSortKeys()
217 {
218 return getDecorated().getSortKeys();
219 }
220
221
222 @Override
223 public void addSortKey( SortKey sortKey )
224 {
225 getDecorated().addSortKey( sortKey );
226 }
227
228 }