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.LdapConstants;
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      // ~ Methods
41      // ------------------------------------------------------------------------------------
42  
43      /**
44       * The constructor. We wont initialize the ArrayList as they may not be
45       * used.
46       */
47      public AndFilter( int tlvId )
48      {
49          super( tlvId );
50      }
51  
52  
53      /**
54       * The constructor. We wont initialize the ArrayList as they may not be
55       * used.
56       */
57      public AndFilter()
58      {
59          super();
60      }
61  
62  
63      /**
64       * Get the AndFilter.
65       * 
66       * @return Returns the andFilter.
67       */
68      public List<Filter> getAndFilter()
69      {
70          return filterSet;
71      }
72  
73  
74      /**
75       * Compute the AndFilter length 
76       * 
77       * AndFilter : 
78       * 0xA0 L1 super.computeLength()
79       * 
80       * Length(AndFilter) = Length(0xA0) + Length(super.computeLength()) +
81       *          super.computeLength()
82       */
83      public int computeLength()
84      {
85          filtersLength = super.computeLength();
86  
87          return 1 + TLV.getNbBytes( filtersLength ) + filtersLength;
88      }
89  
90  
91      /**
92       * Encode the AndFilter message to a PDU. 
93       * 
94       * AndFilter : 
95       * 0xA0 LL
96       *  filter.encode() ... filter.encode()
97       * 
98       * @param buffer The buffer where to put the PDU
99       * @return The PDU.
100      */
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         try
109         {
110             // The AndFilter Tag
111             buffer.put( ( byte ) LdapConstants.AND_FILTER_TAG );
112             buffer.put( TLV.getBytes( filtersLength ) );
113         }
114         catch ( BufferOverflowException boe )
115         {
116             throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
117         }
118 
119         super.encode( buffer );
120 
121         return buffer;
122     }
123 
124 
125     /**
126      * Return a string compliant with RFC 2254 representing an AND filter
127      * 
128      * @return The AND filter string
129      */
130     public String toString()
131     {
132         StringBuffer sb = new StringBuffer();
133 
134         sb.append( '&' ).append( super.toString() );
135 
136         return sb.toString();
137     }
138 }