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 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 public byte[] getValue()
139 {
140 if ( value == null )
141 {
142 try
143 {
144 computeLength();
145 ByteBuffer buffer = ByteBuffer.allocate( valueLength );
146
147 value = encode( buffer ).array();
148 }
149 catch ( Exception e )
150 {
151 return null;
152 }
153 }
154
155 return value;
156 }
157
158
159 @Override
160 public void setSortResult( SortResultCode result )
161 {
162 getDecorated().setSortResult( result );
163 }
164
165
166 @Override
167 public SortResultCode getSortResult()
168 {
169 return getDecorated().getSortResult();
170 }
171
172
173 @Override
174 public void setAttributeName( String attributeName )
175 {
176 getDecorated().setAttributeName( attributeName );
177 }
178
179
180 @Override
181 public String getAttributeName()
182 {
183 return getDecorated().getAttributeName();
184 }
185
186 }