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.EncoderException;
27  import org.apache.directory.api.asn1.ber.tlv.TLV;
28  import org.apache.directory.api.i18n.I18n;
29  import org.apache.directory.api.ldap.codec.api.LdapCodecConstants;
30  import org.apache.directory.api.util.Strings;
31  
32  
33  /**
34   * Object to store the filter. A filter is seen as a tree with a root.
35   * 
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   */
38  public class PresentFilter extends Filter
39  {
40      /** The attribute description. */
41      private String attributeDescription;
42  
43      /** Temporary storage for attribute description bytes */
44      private byte[] attributeDescriptionBytes;
45  
46  
47      /**
48       * The constructor.
49       * 
50       * @param tlvId The TLV identifier
51       */
52      public PresentFilter( int tlvId )
53      {
54          super( tlvId );
55      }
56  
57  
58      /**
59       * The constructor.
60       */
61      public PresentFilter()
62      {
63          super();
64      }
65  
66  
67      /**
68       * Get the attribute
69       * 
70       * @return Returns the attributeDescription.
71       */
72      public String getAttributeDescription()
73      {
74          return attributeDescription;
75      }
76  
77  
78      /**
79       * Set the attributeDescription
80       * 
81       * @param attributeDescription The attributeDescription to set.
82       */
83      public void setAttributeDescription( String attributeDescription )
84      {
85          this.attributeDescription = attributeDescription;
86      }
87  
88  
89      /**
90       * Compute the PresentFilter length 
91       * <br>
92       * PresentFilter :
93       * <pre> 
94       * 0x87 L1 present
95       * 
96       * Length(PresentFilter) = Length(0x87) + Length(super.computeLength()) +
97       *      super.computeLength()
98       * </pre>
99       * 
100      * @return The encoded length
101      */
102     @Override
103     public int computeLength()
104     {
105         attributeDescriptionBytes = Strings.getBytesUtf8( attributeDescription );
106         return 1 + TLV.getNbBytes( attributeDescriptionBytes.length ) + attributeDescriptionBytes.length;
107     }
108 
109 
110     /**
111      * Encode the PresentFilter message to a PDU. PresentFilter : 0x87 LL
112      * attributeDescription
113      * 
114      * @param buffer The buffer where to put the PDU
115      * @return The PDU.
116      */
117     @Override
118     public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
119     {
120         if ( buffer == null )
121         {
122             throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
123         }
124 
125         try
126         {
127             // The PresentFilter Tag
128             buffer.put( ( byte ) LdapCodecConstants.PRESENT_FILTER_TAG );
129             buffer.put( TLV.getBytes( attributeDescriptionBytes.length ) );
130             buffer.put( attributeDescriptionBytes );
131         }
132         catch ( BufferOverflowException boe )
133         {
134             throw new EncoderException( I18n.err( I18n.ERR_04005 ), boe );
135         }
136 
137         return buffer;
138     }
139 
140 
141     /**
142      * Return a string compliant with RFC 2254 representing a Present filter
143      * 
144      * @return The Present filter string
145      */
146     @Override
147     public String toString()
148     {
149 
150         StringBuilder sb = new StringBuilder();
151 
152         sb.append( attributeDescription ).append( "=*" );
153 
154         return sb.toString();
155     }
156 }