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.i18n.I18n;
030    import org.apache.directory.shared.ldap.codec.LdapConstants;
031    
032    
033    /**
034     * And Filter Object to store the And filter.
035     * 
036     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037     * @version $Rev: 912399 $, $Date: 2010-02-21 21:52:31 +0100 (Sun, 21 Feb 2010) $, 
038     */
039    public class AndFilter extends ConnectorFilter
040    {
041        // ~ Methods
042        // ------------------------------------------------------------------------------------
043    
044        /**
045         * The constructor. We wont initialize the ArrayList as they may not be
046         * used.
047         */
048        public AndFilter( int tlvId )
049        {
050            super( tlvId );
051        }
052    
053    
054        /**
055         * The constructor. We wont initialize the ArrayList as they may not be
056         * used.
057         */
058        public AndFilter()
059        {
060            super();
061        }
062    
063    
064        /**
065         * Get the AndFilter.
066         * 
067         * @return Returns the andFilter.
068         */
069        public List<Filter> getAndFilter()
070        {
071            return filterSet;
072        }
073    
074    
075        /**
076         * Compute the AndFilter length 
077         * 
078         * AndFilter : 
079         * 0xA0 L1 super.computeLength()
080         * 
081         * Length(AndFilter) = Length(0xA0) + Length(super.computeLength()) +
082         *          super.computeLength()
083         */
084        public int computeLength()
085        {
086            filtersLength = super.computeLength();
087    
088            return 1 + TLV.getNbBytes( filtersLength ) + filtersLength;
089        }
090    
091    
092        /**
093         * Encode the AndFilter message to a PDU. 
094         * 
095         * AndFilter : 
096         * 0xA0 LL
097         *  filter.encode() ... filter.encode()
098         * 
099         * @param buffer The buffer where to put the PDU
100         * @return The PDU.
101         */
102        public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
103        {
104            if ( buffer == null )
105            {
106                throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
107            }
108    
109            try
110            {
111                // The AndFilter Tag
112                buffer.put( ( byte ) LdapConstants.AND_FILTER_TAG );
113                buffer.put( TLV.getBytes( filtersLength ) );
114            }
115            catch ( BufferOverflowException boe )
116            {
117                throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
118            }
119    
120            super.encode( buffer );
121    
122            return buffer;
123        }
124    
125    
126        /**
127         * Return a string compliant with RFC 2254 representing an AND filter
128         * 
129         * @return The AND filter string
130         */
131        public String toString()
132        {
133            StringBuffer sb = new StringBuffer();
134    
135            sb.append( '&' ).append( super.toString() );
136    
137            return sb.toString();
138        }
139    }