001 /*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License"). You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at
010 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012 * See the License for the specific language governing permissions
013 * and limitations under the License.
014 *
015 * When distributing Covered Code, include this CDDL HEADER in each
016 * file and include the License file at
017 * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
018 * add the following below this CDDL HEADER, with the fields enclosed
019 * by brackets "[]" replaced with your own identifying information:
020 * Portions Copyright [yyyy] [name of copyright owner]
021 *
022 * CDDL HEADER END
023 *
024 *
025 * Copyright 2006-2008 Sun Microsystems, Inc.
026 */
027 package org.opends.server.schema;
028
029
030
031 import java.util.Arrays;
032
033 import org.opends.server.admin.std.server.EqualityMatchingRuleCfg;
034 import org.opends.server.api.EqualityMatchingRule;
035 import org.opends.server.config.ConfigException;
036 import org.opends.server.core.DirectoryServer;
037
038 import org.opends.server.protocols.asn1.ASN1OctetString;
039 import org.opends.server.types.ByteString;
040 import org.opends.server.types.DirectoryException;
041 import org.opends.server.types.InitializationException;
042 import org.opends.server.types.ResultCode;
043
044 import static org.opends.server.loggers.ErrorLogger.*;
045 import static org.opends.messages.SchemaMessages.*;
046 import org.opends.messages.Message;
047 import static org.opends.server.schema.SchemaConstants.*;
048
049
050 /**
051 * This class defines the bitStringMatch matching rule defined in X.520 and
052 * referenced in RFC 2252.
053 */
054 public class BitStringEqualityMatchingRule
055 extends EqualityMatchingRule
056 {
057 /**
058 * Creates a new instance of this bitStringMatch matching rule.
059 */
060 public BitStringEqualityMatchingRule()
061 {
062 super();
063 }
064
065
066
067 /**
068 * {@inheritDoc}
069 */
070 public void initializeMatchingRule(EqualityMatchingRuleCfg configuration)
071 throws ConfigException, InitializationException
072 {
073 // No initialization is required.
074 }
075
076
077
078 /**
079 * Retrieves the common name for this matching rule.
080 *
081 * @return The common name for this matching rule, or <CODE>null</CODE> if
082 * it does not have a name.
083 */
084 public String getName()
085 {
086 return EMR_BIT_STRING_NAME;
087 }
088
089
090
091 /**
092 * Retrieves the OID for this matching rule.
093 *
094 * @return The OID for this matching rule.
095 */
096 public String getOID()
097 {
098 return EMR_BIT_STRING_OID;
099 }
100
101
102
103 /**
104 * Retrieves the description for this matching rule.
105 *
106 * @return The description for this matching rule, or <CODE>null</CODE> if
107 * there is none.
108 */
109 public String getDescription()
110 {
111 // There is no standard description for this matching rule.
112 return null;
113 }
114
115
116
117 /**
118 * Retrieves the OID of the syntax with which this matching rule is
119 * associated.
120 *
121 * @return The OID of the syntax with which this matching rule is associated.
122 */
123 public String getSyntaxOID()
124 {
125 return SYNTAX_BIT_STRING_OID;
126 }
127
128
129
130 /**
131 * Retrieves the normalized form of the provided value, which is best suited
132 * for efficiently performing matching operations on that value.
133 *
134 * @param value The value to be normalized.
135 *
136 * @return The normalized version of the provided value.
137 *
138 * @throws DirectoryException If the provided value is invalid according to
139 * the associated attribute syntax.
140 */
141 public ByteString normalizeValue(ByteString value)
142 throws DirectoryException
143 {
144 String valueString = value.stringValue().toUpperCase();
145
146 int length = valueString.length();
147 if (length < 3)
148 {
149
150 Message message = WARN_ATTR_SYNTAX_BIT_STRING_TOO_SHORT.get(
151 value.stringValue());
152 switch (DirectoryServer.getSyntaxEnforcementPolicy())
153 {
154 case REJECT:
155 throw new DirectoryException(ResultCode.INVALID_ATTRIBUTE_SYNTAX,
156 message);
157 case WARN:
158 logError(message);
159 return new ASN1OctetString(valueString);
160 default:
161 return new ASN1OctetString(valueString);
162 }
163 }
164
165
166 if ((valueString.charAt(0) != '\'') ||
167 (valueString.charAt(length-2) != '\'') ||
168 (valueString.charAt(length-1) != 'B'))
169 {
170
171 Message message = WARN_ATTR_SYNTAX_BIT_STRING_NOT_QUOTED.get(
172 value.stringValue());
173
174 switch (DirectoryServer.getSyntaxEnforcementPolicy())
175 {
176 case REJECT:
177 throw new DirectoryException(ResultCode.INVALID_ATTRIBUTE_SYNTAX,
178 message);
179 case WARN:
180 logError(
181 message);
182 return new ASN1OctetString(valueString);
183 default:
184 return new ASN1OctetString(valueString);
185 }
186 }
187
188
189 for (int i=1; i < (length-2); i++)
190 {
191 switch (valueString.charAt(i))
192 {
193 case '0':
194 case '1':
195 // These characters are fine.
196 break;
197 default:
198
199 Message message = WARN_ATTR_SYNTAX_BIT_STRING_INVALID_BIT.get(
200 value.stringValue(), String.valueOf(valueString.charAt(i)));
201
202 switch (DirectoryServer.getSyntaxEnforcementPolicy())
203 {
204 case REJECT:
205 throw new DirectoryException(ResultCode.INVALID_ATTRIBUTE_SYNTAX,
206 message);
207 case WARN:
208 logError(message);
209 return new ASN1OctetString(valueString);
210 default:
211 return new ASN1OctetString(valueString);
212 }
213 }
214 }
215
216 return new ASN1OctetString(valueString);
217 }
218
219
220
221 /**
222 * Indicates whether the two provided normalized values are equal to each
223 * other.
224 *
225 * @param value1 The normalized form of the first value to compare.
226 * @param value2 The normalized form of the second value to compare.
227 *
228 * @return <CODE>true</CODE> if the provided values are equal, or
229 * <CODE>false</CODE> if not.
230 */
231 public boolean areEqual(ByteString value1, ByteString value2)
232 {
233 // Since the values are already normalized, we just need to compare the
234 // associated byte arrays.
235 return Arrays.equals(value1.value(), value2.value());
236 }
237 }
238