1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
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.LdapConstants;
32
33
34 /**
35 * Object to store the filter. A filter is seen as a tree with a root.
36 *
37 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38 */
39 public class AttributeValueAssertionFilter extends Filter
40 {
41 // ~ Instance fields
42 // ----------------------------------------------------------------------------
43
44 /** The assertion. */
45 private AttributeValueAssertion assertion;
46
47 /** The filter type */
48 private int filterType;
49
50 /** The attributeValueAssertion length */
51 private int avaLength;
52
53
54 // ~ Constructors
55 // -------------------------------------------------------------------------------
56
57 /**
58 * The constructor.
59 *
60 * @param filterType The filter type
61 */
62 public AttributeValueAssertionFilter( int tlvId, int filterType )
63 {
64 super( tlvId );
65 this.filterType = filterType;
66 }
67
68
69 /**
70 * The constructor.
71 *
72 * @param filterType The filter type
73 */
74 public AttributeValueAssertionFilter( int filterType )
75 {
76 super();
77 this.filterType = filterType;
78 }
79
80
81 // ~ Methods
82 // ------------------------------------------------------------------------------------
83
84 /**
85 * Get the assertion
86 *
87 * @return Returns the assertion.
88 */
89 public AttributeValueAssertion getAssertion()
90 {
91 return assertion;
92 }
93
94
95 /**
96 * Set the assertion
97 *
98 * @param assertion The assertion to set.
99 */
100 public void setAssertion( AttributeValueAssertion assertion )
101 {
102 this.assertion = assertion;
103 }
104
105
106 /**
107 * Get the filter type
108 *
109 * @return Returns the filterType.
110 */
111 public int getFilterType()
112 {
113 return filterType;
114 }
115
116
117 /**
118 * Set the filter type
119 *
120 * @param filterType The filterType to set.
121 */
122 public void setFilterType( int filterType )
123 {
124 this.filterType = filterType;
125 }
126
127
128 /**
129 * Compute the AttributeValueFilter length
130 *
131 * AttributeValueFilter :
132 *
133 * 0xA(3, 5, 6, 8) L1
134 * |
135 * +--> 0x04 L2 attributeDesc
136 * +--> 0x04 L3 assertionValue
137 *
138 *
139 * L2 = Length(attributeDesc)
140 * L3 = Length(assertionValue)
141 * L1 = 1 + Length(L2) + L2
142 * + 1 + Length(L3) + L3
143 *
144 * Length(AttributeValueFilter) = Length(0xA?) + Length(L1)
145 * + 1 + Length(L2) + L2
146 * + 1 + Length(L3) + L3
147 */
148 public int computeLength()
149 {
150 avaLength = 0;
151 int attributeDescLength = assertion.getAttributeDesc().length();
152
153 avaLength = 1 + TLV.getNbBytes( attributeDescLength ) + attributeDescLength;
154
155 org.apache.directory.api.ldap.model.entry.Value<?> assertionValue = assertion.getAssertionValue();
156
157 int assertionValueLength = 0;
158
159 assertionValueLength = assertionValue.getBytes().length;
160
161 avaLength += 1 + TLV.getNbBytes( assertionValueLength ) + assertionValueLength;
162
163 return 1 + TLV.getNbBytes( avaLength ) + avaLength;
164 }
165
166
167 /**
168 * Encode the AttributeValueAssertion Filters to a PDU. The
169 * following filters are to be encoded :
170 * - equality match
171 * - greater or equal
172 * - less or equal
173 * - approx match
174 *
175 * AttributeValueAssertion filters :
176 *
177 * 0xA[3, 5, 6, 8] LL
178 * 0x04 LL attributeDesc
179 * 0x04 LL assertionValue
180 *
181 * @param buffer The buffer where to put the PDU
182 * @return The PDU.
183 */
184 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
185 {
186 if ( buffer == null )
187 {
188 throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
189 }
190
191 try
192 {
193 // The AttributeValueAssertion Tag
194 switch ( filterType )
195 {
196 case LdapConstants.EQUALITY_MATCH_FILTER:
197 buffer.put( ( byte ) LdapConstants.EQUALITY_MATCH_FILTER_TAG );
198 break;
199
200 case LdapConstants.LESS_OR_EQUAL_FILTER:
201 buffer.put( ( byte ) LdapConstants.LESS_OR_EQUAL_FILTER_TAG );
202 break;
203
204 case LdapConstants.GREATER_OR_EQUAL_FILTER:
205 buffer.put( ( byte ) LdapConstants.GREATER_OR_EQUAL_FILTER_TAG );
206 break;
207
208 case LdapConstants.APPROX_MATCH_FILTER:
209 buffer.put( ( byte ) LdapConstants.APPROX_MATCH_FILTER_TAG );
210 break;
211 }
212
213 buffer.put( TLV.getBytes( avaLength ) );
214 }
215 catch ( BufferOverflowException boe )
216 {
217 throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
218 }
219
220 // The attribute desc
221 BerValue.encode( buffer, assertion.getAttributeDesc() );
222
223 // The assertion desc
224 if ( assertion.getAssertionValue().isHumanReadable() )
225 {
226 BerValue.encode( buffer, assertion.getAssertionValue().getString() );
227 }
228 else
229 {
230 BerValue.encode( buffer, assertion.getAssertionValue().getBytes() );
231 }
232
233 return buffer;
234 }
235
236
237 /**
238 * Return a string compliant with RFC 2254 representing an item filter
239 *
240 * @return The item filter string
241 */
242 public String toString()
243 {
244 return assertion != null ? assertion.toStringRFC2254( filterType ) : "";
245 }
246 }