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     * Or Filter Object to store the Or 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 OrFilter extends ConnectorFilter
039    {
040        // ~ Constructors
041        // -------------------------------------------------------------------------------
042    
043        /**
044         * The constructor. We wont initialize the ArrayList as they may not be
045         * used.
046         */
047        public OrFilter( 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 OrFilter()
058        {
059            super();
060        }
061    
062    
063        // ~ Methods
064        // ------------------------------------------------------------------------------------
065    
066        /**
067         * Get the OrFilter
068         * 
069         * @return Returns the orFilter.
070         */
071        public List<Filter> getOrFilter()
072        {
073            return filterSet;
074        }
075    
076    
077        /**
078         * Compute the OrFilter length 
079         * 
080         * OrFilter : 
081         * 0xA1 L1 super.computeLength()
082         * 
083         * Length(OrFilter) = Length(0xA1) + Length(super.computeLength()) +
084         *      super.computeLength()
085         */
086        public int computeLength()
087        {
088            filtersLength = super.computeLength();
089    
090            return 1 + TLV.getNbBytes( filtersLength ) + filtersLength;
091        }
092    
093    
094        /**
095         * Encode the OrFilter message to a PDU. 
096         * OrFilter : 
097         *   0xA1 LL 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( "Cannot put a PDU in a null buffer !" );
107            }
108    
109            try
110            {
111                // The OrFilter Tag
112                buffer.put( ( byte ) LdapConstants.OR_FILTER_TAG );
113                buffer.put( TLV.getBytes( filtersLength ) );
114            }
115            catch ( BufferOverflowException boe )
116            {
117                throw new EncoderException( "The PDU buffer size is too small !" );
118            }
119    
120            super.encode( buffer );
121    
122            return buffer;
123        }
124    
125    
126        /**
127         * Return a string compliant with RFC 2254 representing an OR filter
128         * 
129         * @return The OR filter string
130         */
131        public String toString()
132        {
133    
134            StringBuffer sb = new StringBuffer();
135    
136            sb.append( '|' ).append( super.toString() );
137    
138            return sb.toString();
139        }
140    }