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.LdapConstants; 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 // ~ 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 = 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}