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 */ 020package org.apache.directory.api.ldap.codec.search; 021 022 023import java.nio.BufferOverflowException; 024import java.nio.ByteBuffer; 025 026import org.apache.directory.api.asn1.EncoderException; 027import org.apache.directory.api.asn1.ber.tlv.TLV; 028import org.apache.directory.api.i18n.I18n; 029import org.apache.directory.api.ldap.codec.api.LdapCodecConstants; 030import org.apache.directory.api.util.Strings; 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 */ 038public class PresentFilter extends Filter 039{ 040 /** The attribute description. */ 041 private String attributeDescription; 042 043 /** Temporary storage for attribute description bytes */ 044 private byte[] attributeDescriptionBytes; 045 046 047 /** 048 * The constructor. 049 * 050 * @param tlvId The TLV identifier 051 */ 052 public PresentFilter( int tlvId ) 053 { 054 super( tlvId ); 055 } 056 057 058 /** 059 * The constructor. 060 */ 061 public PresentFilter() 062 { 063 super(); 064 } 065 066 067 /** 068 * Get the attribute 069 * 070 * @return Returns the attributeDescription. 071 */ 072 public String getAttributeDescription() 073 { 074 return attributeDescription; 075 } 076 077 078 /** 079 * Set the attributeDescription 080 * 081 * @param attributeDescription The attributeDescription to set. 082 */ 083 public void setAttributeDescription( String attributeDescription ) 084 { 085 this.attributeDescription = attributeDescription; 086 } 087 088 089 /** 090 * Compute the PresentFilter length 091 * <br> 092 * PresentFilter : 093 * <pre> 094 * 0x87 L1 present 095 * 096 * Length(PresentFilter) = Length(0x87) + Length(super.computeLength()) + 097 * super.computeLength() 098 * </pre> 099 * 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}