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  
26  import org.apache.directory.api.asn1.DecoderException;
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   * Not Filter Object to store the Not filter.
35   * 
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   */
38  public class NotFilter extends ConnectorFilter
39  {
40      /**
41       * The constructor.
42       * 
43       * @param tlvId The TLV identifier
44       */
45      public NotFilter( int tlvId )
46      {
47          super( tlvId );
48      }
49  
50  
51      /**
52       * The constructor.
53       */
54      public NotFilter()
55      {
56          super();
57      }
58  
59  
60      /**
61       * Subclass the addFilterMethod, as this is specific for a NotFilter (we
62       * cannot have more than one elements).
63       * 
64       * @param filter The Filter to add
65       */
66      @Override
67      public void addFilter( Filter filter ) throws DecoderException
68      {
69          if ( filterSet != null )
70          {
71              throw new DecoderException( I18n.err( I18n.ERR_04057 ) );
72          }
73  
74          super.addFilter( filter );
75      }
76  
77  
78      /**
79       * Get the NotFilter
80       * 
81       * @return Returns the notFilter.
82       */
83      public Filter getNotFilter()
84      {
85          return filterSet.get( 0 );
86      }
87  
88  
89      /**
90       * Set the NotFilter
91       * 
92       * @param notFilter The notFilter to set.
93       * @throws DecoderException If the NotFilter is already containing a filter
94       */
95      public void setNotFilter( Filter notFilter ) throws DecoderException
96      {
97          if ( filterSet != null )
98          {
99              throw new DecoderException( I18n.err( I18n.ERR_04057 ) );
100         }
101 
102         super.addFilter( notFilter );
103     }
104 
105 
106     /**
107      * Compute the NotFilter length 
108      * <br>
109      * NotFilter :
110      * <pre> 
111      * 0xA2 L1 super.computeLength()
112      * 
113      * Length(NotFilter) = Length(0xA2) + Length(super.computeLength()) +
114      *      super.computeLength()
115      * </pre>
116      * 
117      * @return The encoded length
118      */
119     @Override
120     public int computeLength()
121     {
122         filtersLength = super.computeLength();
123 
124         return 1 + TLV.getNbBytes( filtersLength ) + filtersLength;
125     }
126 
127 
128     /**
129      * Encode the NotFilter message to a PDU. 
130      * <br>
131      * NotFilter :
132      * <pre> 
133      * 0xA2 LL filter.encode()
134      * </pre>
135      * 
136      * @param buffer The buffer where to put the PDU
137      * @return The PDU.
138      */
139     @Override
140     public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
141     {
142         if ( buffer == null )
143         {
144             throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
145         }
146 
147         try
148         {
149             // The NotFilter Tag
150             buffer.put( ( byte ) LdapCodecConstants.NOT_FILTER_TAG );
151             buffer.put( TLV.getBytes( filtersLength ) );
152         }
153         catch ( BufferOverflowException boe )
154         {
155             throw new EncoderException( I18n.err( I18n.ERR_04005 ), boe );
156         }
157 
158         super.encode( buffer );
159 
160         return buffer;
161     }
162 
163 
164     /**
165      * Return a string compliant with RFC 2254 representing a NOT filter
166      * 
167      * @return The NOT filter string
168      */
169     @Override
170     public String toString()
171     {
172         StringBuilder sb = new StringBuilder();
173 
174         sb.append( '!' ).append( super.toString() );
175 
176         return sb.toString();
177     }
178 }