View Javadoc
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  import java.util.List;
26  
27  import org.apache.directory.api.asn1.EncoderException;
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.api.LdapCodecConstants;
31  
32  
33  /**
34   * And Filter Object to store the And filter.
35   * 
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   */
38  public class AndFilter extends ConnectorFilter
39  {
40      /**
41       * The constructor. We wont initialize the ArrayList as they may not be
42       * used.
43       * 
44       * @param tlvId The TLV identifier
45       */
46      public AndFilter( int tlvId )
47      {
48          super( tlvId );
49      }
50  
51  
52      /**
53       * The constructor. We wont initialize the ArrayList as they may not be
54       * used.
55       */
56      public AndFilter()
57      {
58          super();
59      }
60  
61  
62      /**
63       * Get the AndFilter.
64       * 
65       * @return Returns the andFilter.
66       */
67      public List<Filter> getAndFilter()
68      {
69          return filterSet;
70      }
71  
72  
73      /**
74       * Compute the AndFilter length 
75       * <br>
76       * AndFilter :
77       * <pre> 
78       * 0xA0 L1 super.computeLength()
79       * 
80       * Length(AndFilter) = Length(0xA0) + Length(super.computeLength()) +
81       *          super.computeLength()
82       * </pre>
83       * 
84       * @return The encoded length
85       */
86      @Override
87      public int computeLength()
88      {
89          filtersLength = super.computeLength();
90  
91          return 1 + TLV.getNbBytes( filtersLength ) + filtersLength;
92      }
93  
94  
95      /**
96       * Encode the AndFilter message to a PDU. 
97       * <br>
98       * AndFilter :
99       * <pre> 
100      * 0xA0 LL
101      *  filter.encode() ... filter.encode()
102      * </pre>
103      * 
104      * @param buffer The buffer where to put the PDU
105      * @return The PDU.
106      */
107     @Override
108     public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
109     {
110         if ( buffer == null )
111         {
112             throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
113         }
114 
115         try
116         {
117             // The AndFilter Tag
118             buffer.put( ( byte ) LdapCodecConstants.AND_FILTER_TAG );
119             buffer.put( TLV.getBytes( filtersLength ) );
120         }
121         catch ( BufferOverflowException boe )
122         {
123             throw new EncoderException( I18n.err( I18n.ERR_04005 ), boe );
124         }
125 
126         super.encode( buffer );
127 
128         return buffer;
129     }
130 
131 
132     /**
133      * Return a string compliant with RFC 2254 representing an AND filter
134      * 
135      * @return The AND filter string
136      */
137     @Override
138     public String toString()
139     {
140         StringBuilder sb = new StringBuilder();
141 
142         sb.append( '&' ).append( super.toString() );
143 
144         return sb.toString();
145     }
146 }