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 */
020package org.apache.directory.api.ldap.codec.search;
021
022
023import java.nio.BufferOverflowException;
024import java.nio.ByteBuffer;
025import java.util.List;
026
027import org.apache.directory.api.asn1.EncoderException;
028import org.apache.directory.api.asn1.ber.tlv.TLV;
029import org.apache.directory.api.i18n.I18n;
030import org.apache.directory.api.ldap.codec.api.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 */
038public 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( 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}