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.dsmlv2.request;
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      // ~ Instance fields
46      // ----------------------------------------------------------------------------
47  
48      /** The attribute description */
49      private String attributeDesc;
50  
51      /** The assertion value */
52      private Value assertionValue;
53  
54  
55      /**
56       *
57       * Helper method to render an object which can be a String or a byte[]
58       *
59       * @param object The Value to dump
60       * @return A string representing the object
61       */
62      public static String dumpObject( Object object )
63      {
64          if ( object != null )
65          {
66              if ( object instanceof String )
67              {
68                  return ( String ) object;
69              }
70              else if ( object instanceof byte[] )
71              {
72                  return Strings.dumpBytes( ( byte[] ) object );
73              }
74              else if ( object instanceof Value )
75              {
76                  return ( ( Value ) object ).getString();
77              }
78              else
79              {
80                  return "<unknown type>";
81              }
82          }
83          else
84          {
85              return Strings.EMPTY_STRING;
86          }
87      }
88  
89  
90      // ~ Methods
91      // ------------------------------------------------------------------------------------
92  
93      /**
94       * Get the assertion value
95       * 
96       * @return Returns the assertionValue.
97       */
98      public Value getAssertionValue()
99      {
100         return assertionValue;
101     }
102 
103 
104     /**
105      * Set the assertion value
106      * 
107      * @param assertionValue The assertionValue to set.
108      */
109     public void setAssertionValue( Value assertionValue )
110     {
111         this.assertionValue = assertionValue;
112     }
113 
114 
115     /**
116      * Get the attribute description
117      * 
118      * @return Returns the attributeDesc.
119      */
120     public String getAttributeDesc()
121     {
122         return attributeDesc;
123     }
124 
125 
126     /**
127      * Set the attribute description
128      * 
129      * @param attributeDesc The attributeDesc to set.
130      */
131     public void setAttributeDesc( String attributeDesc )
132     {
133         this.attributeDesc = attributeDesc;
134     }
135 
136 
137     /**
138      * Get a String representation of an AttributeValueAssertion
139      * 
140      * @param tabs The spacing to be put before the string
141      * @return An AttributeValueAssertion String
142      */
143     public String toString( String tabs )
144     {
145         StringBuilder sb = new StringBuilder();
146 
147         sb.append( tabs ).append( "AttributeValueAssertion\n" );
148         sb.append( tabs ).append( "    Assertion description : '" );
149         sb.append( attributeDesc != null ? attributeDesc : "null" );
150         sb.append( "'\n" );
151         sb.append( tabs ).append( "    Assertion value : '" ).append( dumpObject( assertionValue ) ).append( "'\n" );
152 
153         return sb.toString();
154     }
155 
156 
157     /**
158      * Get a String representation of an AttributeValueAssertion, as of RFC
159      * 2254.
160      * 
161      * @param filterType The filter type
162      * @return An AttributeValueAssertion String
163      */
164     public String toStringRFC2254( int filterType )
165     {
166         StringBuilder sb = new StringBuilder();
167 
168         sb.append( attributeDesc );
169 
170         switch ( filterType )
171         {
172             case LdapCodecConstants.EQUALITY_MATCH_FILTER:
173                 sb.append( '=' );
174                 break;
175 
176             case LdapCodecConstants.LESS_OR_EQUAL_FILTER:
177                 sb.append( "<=" );
178                 break;
179 
180             case LdapCodecConstants.GREATER_OR_EQUAL_FILTER:
181                 sb.append( ">=" );
182                 break;
183 
184             case LdapCodecConstants.APPROX_MATCH_FILTER:
185                 sb.append( "~=" );
186                 break;
187 
188             default:
189                 throw new IllegalStateException( I18n.err( I18n.ERR_03037_UNEXPECTED_FILTER_TYPE, filterType ) );
190         }
191 
192         sb.append( dumpObject( assertionValue ) );
193 
194         return sb.toString();
195     }
196 
197 
198     /**
199      * Get a String representation of an AttributeValueAssertion
200      * 
201      * @return An AttributeValueAssertion String
202      */
203     @Override
204     public String toString()
205     {
206         return toString( Strings.EMPTY_STRING );
207     }
208 }