View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    https://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.codec;
21  
22  
23  import org.apache.directory.api.i18n.I18n;
24  import org.apache.directory.api.ldap.codec.api.LdapCodecConstants;
25  import org.apache.directory.api.ldap.model.entry.Value;
26  import org.apache.directory.api.util.Strings;
27  
28  
29  /**
30   * A class to store an attribute value assertion. 
31   * The grammar is :
32   * 
33   * AttributeValueAssertion ::= SEQUENCE {
34   *           attributeDesc   AttributeDescription,
35   *           assertionValue  AssertionValue }
36   *
37   * AttributeDescription ::= LDAPString
38   * 
39   * AssertionValue ::= OCTET STRING
40   * 
41   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
42   */
43  public class AttributeValueAssertion
44  {
45      /** The attribute description */
46      private String attributeDesc;
47  
48      /** The assertion as we received it */
49      private byte[] assertion;
50  
51  
52      /**
53       * Helper method to render an object which can be a String or a byte[]
54       *
55       * @param object the Object to render
56       * @return A string representing the object
57       */
58      public static String dumpObject( Object object )
59      {
60          if ( object != null )
61          {
62              if ( object instanceof String )
63              {
64                  return ( String ) object;
65              }
66              else if ( object instanceof byte[] )
67              {
68                  return Strings.dumpBytes( ( byte[] ) object );
69              }
70              else if ( object instanceof Value )
71              {
72                  return ( ( Value ) object ).getString();
73              }
74              else
75              {
76                  return "<unknown type>";
77              }
78          }
79          else
80          {
81              return "";
82          }
83      }
84  
85  
86      /**
87       * Get the attribute description
88       * 
89       * @return Returns the attributeDesc.
90       */
91      public String getAttributeDesc()
92      {
93          return attributeDesc;
94      }
95  
96  
97      /**
98       * Set the attribute description
99       * 
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 }