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