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