001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.shared.ldap.codec.search;
021    
022    
023    import java.nio.BufferOverflowException;
024    import java.nio.ByteBuffer;
025    import java.util.List;
026    
027    import org.apache.directory.shared.asn1.ber.tlv.TLV;
028    import org.apache.directory.shared.asn1.codec.EncoderException;
029    import org.apache.directory.shared.ldap.codec.LdapConstants;
030    
031    
032    /**
033     * And Filter Object to store the And filter.
034     * 
035     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036     * @version $Rev: 664290 $, $Date: 2008-06-07 08:28:06 +0200 (Sam, 07 jui 2008) $, 
037     */
038    public class AndFilter extends ConnectorFilter
039    {
040        // ~ Methods
041        // ------------------------------------------------------------------------------------
042    
043        /**
044         * The constructor. We wont initialize the ArrayList as they may not be
045         * used.
046         */
047        public AndFilter( int tlvId )
048        {
049            super( tlvId );
050        }
051    
052    
053        /**
054         * The constructor. We wont initialize the ArrayList as they may not be
055         * used.
056         */
057        public AndFilter()
058        {
059            super();
060        }
061    
062    
063        /**
064         * Get the AndFilter.
065         * 
066         * @return Returns the andFilter.
067         */
068        public List<Filter> getAndFilter()
069        {
070            return filterSet;
071        }
072    
073    
074        /**
075         * Compute the AndFilter length 
076         * 
077         * AndFilter : 
078         * 0xA0 L1 super.computeLength()
079         * 
080         * Length(AndFilter) = Length(0xA0) + Length(super.computeLength()) +
081         *          super.computeLength()
082         */
083        public int computeLength()
084        {
085            filtersLength = super.computeLength();
086    
087            return 1 + TLV.getNbBytes( filtersLength ) + filtersLength;
088        }
089    
090    
091        /**
092         * Encode the AndFilter message to a PDU. 
093         * 
094         * AndFilter : 
095         * 0xA0 LL
096         *  filter.encode() ... filter.encode()
097         * 
098         * @param buffer The buffer where to put the PDU
099         * @return The PDU.
100         */
101        public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
102        {
103            if ( buffer == null )
104            {
105                throw new EncoderException( "Cannot put a PDU in a null buffer !" );
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( "The PDU buffer size is too small !" );
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    }