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     * Or Filter Object to store the Or 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 OrFilter extends ConnectorFilter
040    {
041        // ~ Constructors
042        // -------------------------------------------------------------------------------
043    
044        /**
045         * The constructor. We wont initialize the ArrayList as they may not be
046         * used.
047         */
048        public OrFilter( 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 OrFilter()
059        {
060            super();
061        }
062    
063    
064        // ~ Methods
065        // ------------------------------------------------------------------------------------
066    
067        /**
068         * Get the OrFilter
069         * 
070         * @return Returns the orFilter.
071         */
072        public List<Filter> getOrFilter()
073        {
074            return filterSet;
075        }
076    
077    
078        /**
079         * Compute the OrFilter length 
080         * 
081         * OrFilter : 
082         * 0xA1 L1 super.computeLength()
083         * 
084         * Length(OrFilter) = Length(0xA1) + Length(super.computeLength()) +
085         *      super.computeLength()
086         */
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         * OrFilter : 
098         *   0xA1 LL filter.encode()
099         * 
100         * @param buffer The buffer where to put the PDU
101         * @return The PDU.
102         */
103        public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
104        {
105            if ( buffer == null )
106            {
107                throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
108            }
109    
110            try
111            {
112                // The OrFilter Tag
113                buffer.put( ( byte ) LdapConstants.OR_FILTER_TAG );
114                buffer.put( TLV.getBytes( filtersLength ) );
115            }
116            catch ( BufferOverflowException boe )
117            {
118                throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
119            }
120    
121            super.encode( buffer );
122    
123            return buffer;
124        }
125    
126    
127        /**
128         * Return a string compliant with RFC 2254 representing an OR filter
129         * 
130         * @return The OR filter string
131         */
132        public String toString()
133        {
134    
135            StringBuffer sb = new StringBuffer();
136    
137            sb.append( '|' ).append( super.toString() );
138    
139            return sb.toString();
140        }
141    }