001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.shared.ldap.codec.search;
021
022
023 import java.nio.BufferOverflowException;
024 import java.nio.ByteBuffer;
025
026 import org.apache.directory.shared.asn1.ber.tlv.TLV;
027 import org.apache.directory.shared.asn1.codec.EncoderException;
028 import org.apache.directory.shared.ldap.codec.LdapConstants;
029 import org.apache.directory.shared.ldap.util.StringTools;
030
031
032 /**
033 * Object to store the filter. A filter is seen as a tree with a root.
034 *
035 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036 * @version $Rev: 664290 $, $Date: 2008-06-07 08:28:06 +0200 (Sam, 07 jui 2008) $,
037 */
038 public class PresentFilter extends Filter
039 {
040 // ~ Instance fields
041 // ----------------------------------------------------------------------------
042
043 /** The attribute description. */
044 private String attributeDescription;
045
046 /** Temporary storage for attribute description bytes */
047 private byte[] attributeDescriptionBytes;
048
049
050 // ~ Constructors
051 // -------------------------------------------------------------------------------
052
053 /**
054 * The constructor.
055 */
056 public PresentFilter( int tlvId )
057 {
058 super( tlvId );
059 }
060
061
062 /**
063 * The constructor.
064 */
065 public PresentFilter()
066 {
067 super();
068 }
069
070
071 // ~ Methods
072 // ------------------------------------------------------------------------------------
073
074 /**
075 * Get the attribute
076 *
077 * @return Returns the attributeDescription.
078 */
079 public String getAttributeDescription()
080 {
081 return attributeDescription;
082 }
083
084
085 /**
086 * Set the attributeDescription
087 *
088 * @param attributeDescription The attributeDescription to set.
089 */
090 public void setAttributeDescription( String attributeDescription )
091 {
092 this.attributeDescription = attributeDescription;
093 }
094
095
096 /**
097 * Compute the PresentFilter length
098 * PresentFilter :
099 * 0x87 L1 present
100 *
101 * Length(PresentFilter) = Length(0x87) + Length(super.computeLength()) +
102 * super.computeLength()
103 */
104 public int computeLength()
105 {
106 attributeDescriptionBytes = StringTools.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( "Cannot put a PDU in a null buffer !" );
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( "The PDU buffer size is too small !" );
135 }
136
137 super.encode( buffer );
138
139 return buffer;
140 }
141
142
143 /**
144 * Return a string compliant with RFC 2254 representing a Present filter
145 *
146 * @return The Present filter string
147 */
148 public String toString()
149 {
150
151 StringBuffer sb = new StringBuffer();
152
153 sb.append( attributeDescription ).append( "=*" );
154
155 return sb.toString();
156 }
157 }