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.LdapCodecConstants;
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      /**
41       * The constructor. We wont initialize the ArrayList as they may not be
42       * used.
43       * 
44       * @param tlvId The TLV identifier
45       */
46      public OrFilter( int tlvId )
47      {
48          super( tlvId );
49      }
50  
51  
52      /**
53       * The constructor. We wont initialize the ArrayList as they may not be
54       * used.
55       */
56      public OrFilter()
57      {
58          super();
59      }
60  
61  
62      /**
63       * Get the OrFilter
64       * 
65       * @return Returns the orFilter.
66       */
67      public List<Filter> getOrFilter()
68      {
69          return filterSet;
70      }
71  
72  
73      /**
74       * Compute the OrFilter length 
75       * <br>
76       * OrFilter :
77       * <pre> 
78       * 0xA1 L1 super.computeLength()
79       * 
80       * Length(OrFilter) = Length(0xA1) + Length(super.computeLength()) +
81       *      super.computeLength()
82       * </pre>
83       * 
84       * @return The encoded length
85       */
86      @Override
87      public int computeLength()
88      {
89          filtersLength = super.computeLength();
90  
91          return 1 + TLV.getNbBytes( filtersLength ) + filtersLength;
92      }
93  
94  
95      /**
96       * Encode the OrFilter message to a PDU. 
97       * <br>
98       * OrFilter :
99       * <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 }