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.i18n.I18n;
029 import org.apache.directory.shared.ldap.codec.LdapConstants;
030 import org.apache.directory.shared.ldap.util.StringTools;
031
032
033 /**
034 * Object to store the filter. A filter is seen as a tree with a root.
035 *
036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037 * @version $Rev: 912399 $, $Date: 2010-02-21 22:52:31 +0200 (Sun, 21 Feb 2010) $,
038 */
039 public class PresentFilter extends Filter
040 {
041 // ~ Instance fields
042 // ----------------------------------------------------------------------------
043
044 /** The attribute description. */
045 private String attributeDescription;
046
047 /** Temporary storage for attribute description bytes */
048 private byte[] attributeDescriptionBytes;
049
050
051 // ~ Constructors
052 // -------------------------------------------------------------------------------
053
054 /**
055 * The constructor.
056 */
057 public PresentFilter( int tlvId )
058 {
059 super( tlvId );
060 }
061
062
063 /**
064 * The constructor.
065 */
066 public PresentFilter()
067 {
068 super();
069 }
070
071
072 // ~ Methods
073 // ------------------------------------------------------------------------------------
074
075 /**
076 * Get the attribute
077 *
078 * @return Returns the attributeDescription.
079 */
080 public String getAttributeDescription()
081 {
082 return attributeDescription;
083 }
084
085
086 /**
087 * Set the attributeDescription
088 *
089 * @param attributeDescription The attributeDescription to set.
090 */
091 public void setAttributeDescription( String attributeDescription )
092 {
093 this.attributeDescription = attributeDescription;
094 }
095
096
097 /**
098 * Compute the PresentFilter length
099 * PresentFilter :
100 * 0x87 L1 present
101 *
102 * Length(PresentFilter) = Length(0x87) + Length(super.computeLength()) +
103 * super.computeLength()
104 */
105 public int computeLength()
106 {
107 attributeDescriptionBytes = StringTools.getBytesUtf8( attributeDescription );
108 return 1 + TLV.getNbBytes( attributeDescriptionBytes.length ) + attributeDescriptionBytes.length;
109 }
110
111
112 /**
113 * Encode the PresentFilter message to a PDU. PresentFilter : 0x87 LL
114 * attributeDescription
115 *
116 * @param buffer The buffer where to put the PDU
117 * @return The PDU.
118 */
119 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
120 {
121 if ( buffer == null )
122 {
123 throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
124 }
125
126 try
127 {
128 // The PresentFilter Tag
129 buffer.put( ( byte ) LdapConstants.PRESENT_FILTER_TAG );
130 buffer.put( TLV.getBytes( attributeDescriptionBytes.length ) );
131 buffer.put( attributeDescriptionBytes );
132 }
133 catch ( BufferOverflowException boe )
134 {
135 throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
136 }
137
138 super.encode( buffer );
139
140 return buffer;
141 }
142
143
144 /**
145 * Return a string compliant with RFC 2254 representing a Present filter
146 *
147 * @return The Present filter string
148 */
149 public String toString()
150 {
151
152 StringBuffer sb = new StringBuffer();
153
154 sb.append( attributeDescription ).append( "=*" );
155
156 return sb.toString();
157 }
158 }