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 * https://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; 021 022 023import org.apache.directory.api.i18n.I18n; 024import org.apache.directory.api.ldap.codec.api.LdapCodecConstants; 025import org.apache.directory.api.ldap.model.entry.Value; 026import org.apache.directory.api.util.Strings; 027 028 029/** 030 * A class to store an attribute value assertion. 031 * The grammar is : 032 * 033 * AttributeValueAssertion ::= SEQUENCE { 034 * attributeDesc AttributeDescription, 035 * assertionValue AssertionValue } 036 * 037 * AttributeDescription ::= LDAPString 038 * 039 * AssertionValue ::= OCTET STRING 040 * 041 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 042 */ 043public class AttributeValueAssertion 044{ 045 /** The attribute description */ 046 private String attributeDesc; 047 048 /** The assertion as we received it */ 049 private byte[] assertion; 050 051 052 /** 053 * Helper method to render an object which can be a String or a byte[] 054 * 055 * @param object the Object to render 056 * @return A string representing the object 057 */ 058 public static String dumpObject( Object object ) 059 { 060 if ( object != null ) 061 { 062 if ( object instanceof String ) 063 { 064 return ( String ) object; 065 } 066 else if ( object instanceof byte[] ) 067 { 068 return Strings.dumpBytes( ( byte[] ) object ); 069 } 070 else if ( object instanceof Value ) 071 { 072 return ( ( Value ) object ).getString(); 073 } 074 else 075 { 076 return "<unknown type>"; 077 } 078 } 079 else 080 { 081 return ""; 082 } 083 } 084 085 086 /** 087 * Get the attribute description 088 * 089 * @return Returns the attributeDesc. 090 */ 091 public String getAttributeDesc() 092 { 093 return attributeDesc; 094 } 095 096 097 /** 098 * Set the attribute description 099 * 100 * @param attributeDesc The attributeDesc to set. 101 */ 102 public void setAttributeDesc( String attributeDesc ) 103 { 104 this.attributeDesc = attributeDesc; 105 } 106 107 108 /** 109 * Get a String representation of an AttributeValueAssertion 110 * 111 * @param tabs The spacing to be put before the string 112 * @return An AttributeValueAssertion String 113 */ 114 public String toString( String tabs ) 115 { 116 StringBuilder sb = new StringBuilder(); 117 118 sb.append( tabs ).append( "AttributeValueAssertion\n" ); 119 sb.append( tabs ).append( " Assertion description : '" ); 120 sb.append( attributeDesc != null ? attributeDesc : "null" ); 121 sb.append( "'\n" ); 122 sb.append( tabs ).append( " Assertion value : '" ).append( dumpObject( assertion ) ).append( "'\n" ); 123 124 return sb.toString(); 125 } 126 127 128 /** 129 * Get a String representation of an AttributeValueAssertion, as of RFC 130 * 2254. 131 * 132 * @param filterType The filter type 133 * @return An AttributeValueAssertion String 134 */ 135 public String toStringRFC2254( int filterType ) 136 { 137 StringBuilder sb = new StringBuilder(); 138 139 sb.append( attributeDesc ); 140 141 switch ( filterType ) 142 { 143 case LdapCodecConstants.EQUALITY_MATCH_FILTER: 144 sb.append( '=' ); 145 break; 146 147 case LdapCodecConstants.LESS_OR_EQUAL_FILTER: 148 sb.append( "<=" ); 149 break; 150 151 case LdapCodecConstants.GREATER_OR_EQUAL_FILTER: 152 sb.append( ">=" ); 153 break; 154 155 case LdapCodecConstants.APPROX_MATCH_FILTER: 156 sb.append( "~=" ); 157 break; 158 159 default: 160 throw new IllegalArgumentException( I18n.err( I18n.ERR_05503_UNEXPECTED_FILTER_TYPE, filterType ) ); 161 } 162 163 sb.append( dumpObject( assertion ) ); 164 165 return sb.toString(); 166 } 167 168 169 /** 170 * @return the assertion 171 */ 172 public byte[] getAssertion() 173 { 174 return assertion; 175 } 176 177 178 /** 179 * @param assertion the assertion to set 180 */ 181 public void setAssertion( byte[] assertion ) 182 { 183 if ( assertion != null ) 184 { 185 this.assertion = new byte[assertion.length]; 186 System.arraycopy( assertion, 0, this.assertion, 0, assertion.length ); 187 } 188 } 189 190 191 /** 192 * Get a String representation of an AttributeValueAssertion 193 * 194 * @return An AttributeValueAssertion String 195 */ 196 @Override 197 public String toString() 198 { 199 return toString( "" ); 200 } 201}