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.LdapConstants;
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      // ~ Methods
41      // ------------------------------------------------------------------------------------
42  
43      /**
44       * The constructor.
45       */
46      public NotFilter( int tlvId )
47      {
48          super( tlvId );
49      }
50  
51  
52      /**
53       * The constructor.
54       */
55      public NotFilter()
56      {
57          super();
58      }
59  
60  
61      /**
62       * Subclass the addFilterMethod, as this is specific for a NotFilter (we
63       * cannot have more than one elements).
64       * 
65       * @param filter The Filter to add
66       */
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       */
94      public void setNotFilter( Filter notFilter ) throws DecoderException
95      {
96          if ( filterSet != null )
97          {
98              throw new DecoderException( I18n.err( I18n.ERR_04057 ) );
99          }
100 
101         super.addFilter( notFilter );
102     }
103 
104 
105     /**
106      * Compute the NotFilter length 
107      * NotFilter : 
108      * 0xA2 L1 super.computeLength()
109      * 
110      * Length(NotFilter) = Length(0xA2) + Length(super.computeLength()) +
111      *      super.computeLength()
112      */
113     public int computeLength()
114     {
115         filtersLength = super.computeLength();
116 
117         return 1 + TLV.getNbBytes( filtersLength ) + filtersLength;
118     }
119 
120 
121     /**
122      * Encode the NotFilter message to a PDU. 
123      * NotFilter : 
124      * 0xA2 LL filter.encode()
125      * 
126      * @param buffer The buffer where to put the PDU
127      * @return The PDU.
128      */
129     public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
130     {
131         if ( buffer == null )
132         {
133             throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
134         }
135 
136         try
137         {
138             // The NotFilter Tag
139             buffer.put( ( byte ) LdapConstants.NOT_FILTER_TAG );
140             buffer.put( TLV.getBytes( filtersLength ) );
141         }
142         catch ( BufferOverflowException boe )
143         {
144             throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
145         }
146 
147         super.encode( buffer );
148 
149         return buffer;
150     }
151 
152 
153     /**
154      * Return a string compliant with RFC 2254 representing a NOT filter
155      * 
156      * @return The NOT filter string
157      */
158     public String toString()
159     {
160         StringBuffer sb = new StringBuffer();
161 
162         sb.append( '!' ).append( super.toString() );
163 
164         return sb.toString();
165     }
166 }