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.LdapConstants;
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 // ~ Instance fields
41 // ----------------------------------------------------------------------------
42
43 /** The attribute description. */
44 private String attributeDescription;
45
46 /** Temporary storage for attribute description bytes */
47 private byte[] attributeDescriptionBytes;
48
49
50 // ~ Constructors
51 // -------------------------------------------------------------------------------
52
53 /**
54 * The constructor.
55 */
56 public PresentFilter( int tlvId )
57 {
58 super( tlvId );
59 }
60
61
62 /**
63 * The constructor.
64 */
65 public PresentFilter()
66 {
67 super();
68 }
69
70
71 // ~ Methods
72 // ------------------------------------------------------------------------------------
73
74 /**
75 * Get the attribute
76 *
77 * @return Returns the attributeDescription.
78 */
79 public String getAttributeDescription()
80 {
81 return attributeDescription;
82 }
83
84
85 /**
86 * Set the attributeDescription
87 *
88 * @param attributeDescription The attributeDescription to set.
89 */
90 public void setAttributeDescription( String attributeDescription )
91 {
92 this.attributeDescription = attributeDescription;
93 }
94
95
96 /**
97 * Compute the PresentFilter length
98 * PresentFilter :
99 * 0x87 L1 present
100 *
101 * Length(PresentFilter) = Length(0x87) + Length(super.computeLength()) +
102 * super.computeLength()
103 */
104 public int computeLength()
105 {
106 attributeDescriptionBytes = Strings.getBytesUtf8( attributeDescription );
107 return 1 + TLV.getNbBytes( attributeDescriptionBytes.length ) + attributeDescriptionBytes.length;
108 }
109
110
111 /**
112 * Encode the PresentFilter message to a PDU. PresentFilter : 0x87 LL
113 * attributeDescription
114 *
115 * @param buffer The buffer where to put the PDU
116 * @return The PDU.
117 */
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 ) LdapConstants.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 ) );
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 public String toString()
147 {
148
149 StringBuffer sb = new StringBuffer();
150
151 sb.append( attributeDescription ).append( "=*" );
152
153 return sb.toString();
154 }
155 }