View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.codec.search;
21  
22  
23  import java.nio.BufferOverflowException;
24  import java.nio.ByteBuffer;
25  import java.util.List;
26  
27  import org.apache.directory.api.asn1.EncoderException;
28  import org.apache.directory.api.asn1.ber.tlv.TLV;
29  import org.apache.directory.api.i18n.I18n;
30  import org.apache.directory.api.ldap.codec.api.LdapConstants;
31  
32  
33  /**
34   * Or Filter Object to store the Or filter.
35   * 
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   */
38  public class OrFilter extends ConnectorFilter
39  {
40      // ~ Constructors
41      // -------------------------------------------------------------------------------
42  
43      /**
44       * The constructor. We wont initialize the ArrayList as they may not be
45       * used.
46       */
47      public OrFilter( int tlvId )
48      {
49          super( tlvId );
50      }
51  
52  
53      /**
54       * The constructor. We wont initialize the ArrayList as they may not be
55       * used.
56       */
57      public OrFilter()
58      {
59          super();
60      }
61  
62  
63      // ~ Methods
64      // ------------------------------------------------------------------------------------
65  
66      /**
67       * Get the OrFilter
68       * 
69       * @return Returns the orFilter.
70       */
71      public List<Filter> getOrFilter()
72      {
73          return filterSet;
74      }
75  
76  
77      /**
78       * Compute the OrFilter length 
79       * 
80       * OrFilter : 
81       * 0xA1 L1 super.computeLength()
82       * 
83       * Length(OrFilter) = Length(0xA1) + Length(super.computeLength()) +
84       *      super.computeLength()
85       */
86      public int computeLength()
87      {
88          filtersLength = super.computeLength();
89  
90          return 1 + TLV.getNbBytes( filtersLength ) + filtersLength;
91      }
92  
93  
94      /**
95       * Encode the OrFilter message to a PDU. 
96       * OrFilter : 
97       *   0xA1 LL filter.encode()
98       * 
99       * @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 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( 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 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 }