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.search;
21
22
23 import java.nio.BufferOverflowException;
24 import java.nio.ByteBuffer;
25
26 import org.apache.directory.api.asn1.EncoderException;
27 import org.apache.directory.api.asn1.ber.tlv.BerValue;
28 import org.apache.directory.api.asn1.ber.tlv.TLV;
29 import org.apache.directory.api.i18n.I18n;
30 import org.apache.directory.api.ldap.codec.AttributeValueAssertion;
31 import org.apache.directory.api.ldap.codec.api.LdapCodecConstants;
32
33
34
35
36
37
38
39 public class AttributeValueAssertionFilter extends Filter
40 {
41
42 private AttributeValueAssertion assertion;
43
44
45 private int filterType;
46
47
48 private int avaLength;
49
50
51
52
53
54
55
56
57 public AttributeValueAssertionFilter( int tlvId, int filterType )
58 {
59 super( tlvId );
60 this.filterType = filterType;
61 }
62
63
64
65
66
67
68
69 public AttributeValueAssertionFilter( int filterType )
70 {
71 super();
72 this.filterType = filterType;
73 }
74
75
76
77
78
79
80
81 public AttributeValueAssertion getAssertion()
82 {
83 return assertion;
84 }
85
86
87
88
89
90
91
92 public void setAssertion( AttributeValueAssertion assertion )
93 {
94 this.assertion = assertion;
95 }
96
97
98
99
100
101
102
103 public int getFilterType()
104 {
105 return filterType;
106 }
107
108
109
110
111
112
113
114 public void setFilterType( int filterType )
115 {
116 this.filterType = filterType;
117 }
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143 @Override
144 public int computeLength()
145 {
146 avaLength = 0;
147 int attributeDescLength = assertion.getAttributeDesc().length();
148
149 avaLength = 1 + TLV.getNbBytes( attributeDescLength ) + attributeDescLength;
150
151 org.apache.directory.api.ldap.model.entry.Value<?> assertionValue = assertion.getAssertionValue();
152
153 int assertionValueLength;
154
155 assertionValueLength = assertionValue.getBytes().length;
156
157 avaLength += 1 + TLV.getNbBytes( assertionValueLength ) + assertionValueLength;
158
159 return 1 + TLV.getNbBytes( avaLength ) + avaLength;
160 }
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184 @Override
185 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
186 {
187 if ( buffer == null )
188 {
189 throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
190 }
191
192 try
193 {
194
195 switch ( filterType )
196 {
197 case LdapCodecConstants.EQUALITY_MATCH_FILTER:
198 buffer.put( ( byte ) LdapCodecConstants.EQUALITY_MATCH_FILTER_TAG );
199 break;
200
201 case LdapCodecConstants.LESS_OR_EQUAL_FILTER:
202 buffer.put( ( byte ) LdapCodecConstants.LESS_OR_EQUAL_FILTER_TAG );
203 break;
204
205 case LdapCodecConstants.GREATER_OR_EQUAL_FILTER:
206 buffer.put( ( byte ) LdapCodecConstants.GREATER_OR_EQUAL_FILTER_TAG );
207 break;
208
209 case LdapCodecConstants.APPROX_MATCH_FILTER:
210 buffer.put( ( byte ) LdapCodecConstants.APPROX_MATCH_FILTER_TAG );
211 break;
212
213 default:
214 throw new IllegalArgumentException( "Unexpected filter type: " + filterType );
215 }
216
217 buffer.put( TLV.getBytes( avaLength ) );
218 }
219 catch ( BufferOverflowException boe )
220 {
221 throw new EncoderException( I18n.err( I18n.ERR_04005 ), boe );
222 }
223
224
225 BerValue.encode( buffer, assertion.getAttributeDesc() );
226
227
228 if ( assertion.getAssertionValue().isHumanReadable() )
229 {
230 BerValue.encode( buffer, assertion.getAssertionValue().getString() );
231 }
232 else
233 {
234 BerValue.encode( buffer, assertion.getAssertionValue().getBytes() );
235 }
236
237 return buffer;
238 }
239
240
241
242
243
244
245
246 @Override
247 public String toString()
248 {
249 return assertion != null ? assertion.toStringRFC2254( filterType ) : "";
250 }
251 }