Debian Codegraph @

java-sdk/ietfldap/org/ietf/ldap/LDAPAttribute.java

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is mozilla.org code.
17 *
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1999
21 * the Initial Developer. All Rights Reserved.
22 *
23 * Contributor(s):
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38 package org.ietf.ldap;
39
40 import java.io.IOException;
41 import java.io.Serializable;
42 import java.util.Enumeration;
43 import java.util.StringTokenizer;
44 import java.util.Vector;
45
46 import org.ietf.ldap.ber.stream.BERElement;
47 import org.ietf.ldap.ber.stream.BEROctetString;
48 import org.ietf.ldap.ber.stream.BERSequence;
49 import org.ietf.ldap.ber.stream.BERSet;
50
51 /**
52 * Represents the name and values of an attribute in an entry.
53 *
54 * @version 1.0
55 * @see org.ietf.ldap.LDAPAttributeSet
56 */
57 public class LDAPAttribute implements Cloneable, Serializable {
58
59 static final long serialVersionUID = -4594745735452202600L;
60 private String name = null;
61 private byte[] nameBuf = null;
62 /**
63 * Internally, this is a list of "byte[]"-based attribute values.
64 */
65 private Object values[] = new Object[0];
66
67 /**
68 * Constructs an attribute from another existing attribute.
69 * Effectively, this makes a copy of the existing attribute.
70 * @param attr the attribute to copy
71 */
72 public LDAPAttribute( LDAPAttribute attr ) {
73 name = attr.name;
74 nameBuf = attr.nameBuf;
75 values = new Object[attr.values.length];
76 for (int i = 0; i < attr.values.length; i++) {
77 values[i] = new byte[((byte[])attr.values[i]).length];
78 System.arraycopy(attr.values[i], 0, values[i], 0,
79 ((byte[])attr.values[i]).length);
80 }
81 }
82
83 /**
84 * Constructs an attribute with no values.
85 * @param attrName name of the attribute
86 */
87 public LDAPAttribute( String attrName ) {
88 name = attrName;
89 }
90
91 /**
92 * Constructs an attribute with a byte-formatted value.
93 * @param attrName name of the attribute
94 * @param attrValue value of the attribute in byte format
95 */
96 public LDAPAttribute( String attrName, byte[] attrValue ) {
97 name = attrName;
98 addValue(attrValue);
99 }
100
101 /**
102 * Constructs an attribute that has a single string value.
103 * @param attrName name of the attribute
104 * @param attrValue value of the attribute in String format
105 */
106 public LDAPAttribute( String attrName, String attrValue ) {
107 name = attrName;
108 addValue( attrValue );
109 }
110
111 /**
112 * Constructs an attribute that has an array of string values.
113 * @param attrName name of the attribute
114 * @param attrValues the list of string values for this attribute
115 */
116 public LDAPAttribute( String attrName, String[] attrValues ) {
117 name = attrName;
118 if (attrValues != null) {
119 setValues( attrValues );
120 }
121 }
122
123 /**
124 * Constructs an attribute from a BER (Basic Encoding Rules) element.
125 * (The protocol elements of LDAP are encoded for exchange using the
126 * Basic Encoding Rules.)
127 * @param element element that you want translated into an attribute
128 * @exception IOException The attribute could not be created from
129 * the specified element.
130 */
131 public LDAPAttribute(BERElement element) throws IOException {
132 BERSequence seq = (BERSequence)element;
133 BEROctetString type = (BEROctetString)seq.elementAt(0);
134 nameBuf = type.getValue();
135 BERSet set = (BERSet)seq.elementAt(1);
136 if (set.size() > 0) {
137 Object[] vals = new Object[set.size()];
138 for (int i = 0; i < set.size(); i++) {
139 vals[i] = ((BEROctetString)set.elementAt(i)).getValue();
140 if (vals[i] == null) {
141 vals[i] = new byte[0];
142 }
143 }
144 setValues( vals );
145 }
146 }
147
148 /**
149 * Returns the number of values of the attribute.
150 * @return number of values for this attribute.
151 */
152 public int size() {
153 return values.length;
154 }
155
156 /**
157 * Returns an enumerator for the string values of an attribute.
158 * @return enumerator for the string values.
159 */
160 public Enumeration<String> getStringValues() {
161 Vector<String> v = new Vector<>();
162 synchronized(this) {
163 try {
164 for (int i=0; i<values.length; i++) {
165 if ( values[i] != null ) {
166 v.addElement(new String ((byte[])values[i], "UTF8"));
167 } else {
168 v.addElement( new String( "" ) );
169 }
170 }
171 } catch ( Exception e ) {
172 return null;
173 }
174 }
175 return v.elements();
176 }
177
178 /**
179 * Returns the values of the attribute as an array of <CODE>String</CODE>
180 * objects.
181 * @return array of attribute values. Each element in the array
182 * is a <CODE>String</CODE> object.
183 */
184 public String[] getStringValueArray() {
185
186 String s[] = new String[values.length];
187 synchronized(this) {
188 try {
189 for (int i=0; i < values.length; i++) {
190 if ( values[i] !=null ) {
191 s[i] = new String((byte[])values[i], "UTF8");
192 } else {
193 s[i] = new String("");
194 }
195 }
196 } catch (Exception e) {
197 return null;
198 }
199 }
200 return s;
201 }
202
203 /**
204 * Returns an enumerator for the values of the attribute in <CODE>byte[]</CODE>
205 * format.
206 * @return a set of attribute values. Each element in the enumeration
207 * is of type <CODE>byte[]</CODE>.
208 */
209 public Enumeration<byte[]> getByteValues() {
210 Vector<byte[]> v = new Vector<>();
211 synchronized(this) {
212 for (int i=0; i<values.length; i++) {
213 if ( values[i] != null ) {
214 v.addElement((byte[])values[i]);
215 } else {
216 v.addElement( new byte[0] );
217 }
218 }
219 }
220 return v.elements();
221 }
222
223 /**
224 * Returns the values of the attribute in an array of <CODE>byte[]</CODE>
225 * format.
226 * @return array of attribute values. Each element in the array
227 * will be of type <CODE>byte[]</CODE>.
228 */
229 public byte[][] getByteValueArray() {
230 byte b[][] = new byte[values.length][];
231 synchronized(this) {
232 try {
233 for (int i=0; i < values.length; i++) {
234 b[i] = new byte[((byte[])(values[i])).length];
235 System.arraycopy(values[i], 0, b[i], 0,
236 ((byte[])(values[i])).length);
237 }
238 } catch (Exception e) {
239 return null;
240 }
241 }
242 return b;
243
244 }
245
246 /**
247 * Returns the name of the attribute.
248 * @return name of the attribute.
249 */
250 public String getName() {
251 if ((name == null) && (nameBuf != null)) {
252 try{
253 name = new String(nameBuf, "UTF8");
254 } catch(Throwable x) {}
255 }
256 return name;
257 }
258
259 /**
260 * Extracts the subtypes from the specified attribute name.
261 * For example, if the attribute name is <CODE>cn;lang-ja;phonetic</CODE>,
262 * this method returns an array containing <CODE>lang-ja</CODE>
263 * and <CODE>phonetic</CODE>.
264 * <P>
265 *
266 * @param attrName name of the attribute from which to extract the subtypes
267 * @return array of subtypes, or null (if the name has no subtypes).
268 * @see org.ietf.ldap.LDAPAttribute#getBaseName
269 */
270 public static String[] getSubtypes(String attrName) {
271 StringTokenizer st = new StringTokenizer(attrName, ";");
272 if( st.hasMoreElements() ) {
273 // First element is base name
274 st.nextElement();
275 String[] subtypes = new String[st.countTokens()];
276 int i = 0;
277 // Extract the types
278 while( st.hasMoreElements() )
279 subtypes[i++] = (String)st.nextElement();
280 return subtypes;
281 }
282 return null;
283 }
284
285 /**
286 * Extracts the subtypes from the attribute name of the current
287 * <CODE>LDAPAttribute</CODE> object. For example, if the attribute
288 * name is <CODE>cn;lang-ja;phonetic</CODE>, this method returns an array
289 * containing <CODE>lang-ja</CODE> and <CODE>phonetic</CODE>.
290 *<P>
291 *
292 * @return array of subtypes, or null (if the name has no subtypes).
293 */
294 public String[] getSubtypes() {
295 return getSubtypes(getName());
296 }
297
298 /**
299 * Extracts the language subtype from the attribute name of the
300 * <CODE>LDAPAttribute</CODE> object, if any. For example, if the
301 * attribute name is <CODE>cn;lang-ja;phonetic</CODE>, this method
302 * returns the String <CODE>lang-ja</CODE>.
303 *<P>
304 *
305 * @return the language subtype, or null (if the name has no
306 * language subtype).
307 */
308 public String getLangSubtype() {
309 String[] subTypes = getSubtypes();
310 if ( subTypes != null ) {
311 for( int i = 0; i < subTypes.length; i++ ) {
312 if ((subTypes[i].length() >= 5) &&
313 (subTypes[i].substring(0, 5).equalsIgnoreCase("lang-")))
314 return subTypes[i];
315 }
316 }
317 return null;
318 }
319
320 /**
321 * Extracts the base name from the specified attribute name.
322 * For example, if the attribute name is <CODE>cn;lang-ja;phonetic</CODE>,
323 * this method returns <CODE>cn</CODE>.
324 * <P>
325 *
326 * @param attrName name of the attribute from which to extract the base name
327 * @return base name (the attribute name without any subtypes).
328 * @see org.ietf.ldap.LDAPAttribute#getSubtypes
329 */
330 public static String getBaseName( String attrName ) {
331 String basename = attrName;
332 StringTokenizer st = new StringTokenizer(attrName, ";");
333 if( st.hasMoreElements() )
334 // First element is base name
335 basename = (String)st.nextElement();
336 return basename;
337 }
338
339 /**
340 * Extracts the base name from the attribute name of the current
341 * <CODE>LDAPAttribute</CODE> object. For example, if the attribute
342 * name is <CODE>cn;lang-ja;phonetic</CODE>, this method returns
343 * <CODE>cn</CODE>.
344 * <P>
345 *
346 * @return base name (the attribute name without any subtypes).
347 * @see org.ietf.ldap.LDAPAttribute#getSubtypes
348 */
349 public String getBaseName() {
350 return getBaseName(getName());
351 }
352
353 /**
354 * Reports whether the attribute name contains the specified subtype.
355 * For example, if you check for the subtype <CODE>lang-en</CODE>
356 * and the attribute name is <CODE>cn;lang-en</CODE>, this method
357 * returns <CODE>true</CODE>.
358 * <P>
359 *
360 * @param subtype the single subtype for which you want to check
361 * @return true if the attribute name contains the specified subtype.
362 * @see org.ietf.ldap.LDAPAttribute#getSubtypes
363 */
364 public boolean hasSubtype( String subtype ) {
365 String[] mytypes = getSubtypes();
366 for(int i = 0; i < mytypes.length; i++) {
367 if( subtype.equalsIgnoreCase( mytypes[i] ) )
368 return true;
369 }
370 return false;
371 }
372
373 /**
374 * Reports if the attribute name contains all specified subtypes
375 * For example, if you check for the subtypes <CODE>lang-en</CODE>
376 * and <CODE>phonetic</CODE> and the attribute name is
377 * <CODE>cn;lang-en;phonetic</CODE>, this method returns <CODE>true</CODE>.
378 * If the attribute name is <CODE>cn;phonetic</CODE> or
379 * <CODE>cn;lang-en</CODE>, this method returns <CODE>false</CODE>.
380 * <P>
381 * @param subtypes an array of subtypes to check
382 * @return true if the attribute name contains all subtypes
383 * @see org.ietf.ldap.LDAPAttribute#getSubtypes
384 */
385 public boolean hasSubtypes( String[] subtypes ) {
386 for(int i = 0; i < subtypes.length; i++) {
387 if( !hasSubtype(subtypes[i]) )
388 return false;
389 }
390 return true;
391 }
392
393 /**
394 * Adds a string value to the attribute.
395 * @param attrValue the string value to add to the attribute
396 */
397 public synchronized void addValue( String attrValue ) {
398 if (attrValue != null) {
399 try {
400 byte[] b = attrValue.getBytes("UTF8");
401 addValue( b );
402 } catch(Throwable x)
403 {}
404 }
405 }
406
407 /**
408 * Sets the string values as the attribute's values.
409 * @param attrValues the string values to use in the attribute
410 */
411 protected void setValues( String[] attrValues ) {
412 Object[] vals;
413 if (attrValues != null) {
414 vals = new Object[attrValues.length];
415 for (int i = 0; i < vals.length; i++) {
416 try {
417 vals[i] = attrValues[i].getBytes("UTF8");
418 } catch(Throwable x)
419 { vals[i] = new byte[0]; }
420 }
421 } else {
422 vals = new Object[0];
423 }
424 setValues(vals);
425 }
426
427 /**
428 * Adds a <CODE>byte[]</CODE>-formatted value to the attribute.
429 * @param attrValue the <CODE>byte[]</CODE>-formatted value to
430 * add to the attribute
431 */
432 public synchronized void addValue( byte[] attrValue ) {
433 if (attrValue != null) {
434 Object[] vals = new Object[values.length+1];
435 for (int i = 0; i < values.length; i++)
436 vals[i] = values[i];
437 vals[values.length] = attrValue;
438 values = vals;
439 }
440 }
441
442 /**
443 * Sets the byte[] values as the attribute's values.
444 * @param attrValues the values to use in the attribute
445 */
446 protected synchronized void setValues( Object[] attrValues ) {
447 values = attrValues;
448 }
449
450 /**
451 * Removes a string value from the attribute.
452 * @param attrValue the string value to remove
453 */
454 public synchronized void removeValue( String attrValue) {
455 if (attrValue != null) {
456 try{
457 byte b[] = attrValue.getBytes("UTF8");
458 removeValue ( b );
459 } catch(Throwable x)
460 {}
461 }
462 }
463
464 /**
465 * Removes a <CODE>byte[]</CODE>-formatted value from the attribute.
466 * @param attrValue <CODE>byte[]</CODE>-formatted value to remove
467 */
468 public synchronized void removeValue( byte[] attrValue) {
469 if ((attrValue == null) || (values == null)|| (values.length < 1))
470 return;
471 int ind = -1;
472 for (int i=0; i<values.length; i++) {
473 if (equalValue(attrValue, (byte[])values[i])) {
474 ind = i;
475 break;
476 }
477 }
478 if (ind >= 0) {
479 Object[] vals = new Object[values.length-1];
480 int j = 0;
481 for (int i = 0; i < values.length; i++) {
482 if (i != ind) {
483 vals[j++] = values[i];
484 }
485 }
486 values = vals;
487 }
488 }
489
490 private static boolean equalValue(byte[] a, byte[] b) {
491 if (a.length != b.length)
492 return false;
493
494 for (int i=0; i<a.length; i++) {
495 if (a[i] != b[i])
496 return false;
497 }
498 return true;
499 }
500
501 /**
502 * Retrieves the BER (Basic Encoding Rules) representation of an attribute.
503 * (The protocol elements of LDAP are encoded for exchange using the
504 * Basic Encoding Rules.)
505 * @return the BER representation of the attribute.
506 */
507 public BERElement getBERElement() {
508 try {
509 BERSequence seq = new BERSequence();
510 seq.addElement(new BEROctetString(getName()));
511 BERSet set = new BERSet();
512 for (int i = 0; i < values.length; i++) {
513 set.addElement(new BEROctetString((byte[])values[i]));
514 }
515 seq.addElement(set);
516 return seq;
517 } catch (IOException e) {
518 return null;
519 }
520 }
521
522 /**
523 * Retrieves the string representation of attribute parameters.
524 * @return string representation parameters.
525 */
526 private String getParamString() {
527 StringBuffer sb = new StringBuffer();
528
529 if ( values.length > 0 ) {
530 for (int i = 0; i < values.length; i++) {
531 if (i != 0) {
532 sb.append(",");
533 }
534 byte[] val = (byte[])values[i];
535 try {
536 String sval = new String(val, "UTF8");
537 if (sval.length() == 0 && val.length > 0) {
538 sb.append("<binary value, length:");
539 sb.append(val.length);
540 sb.append(">");
541 }
542 else {
543 sb.append(sval);
544 }
545
546 } catch (Exception e) {
547 if (val != null) {
548 sb.append("<binary value, length:");
549 sb.append(val.length);
550 sb.append(">");
551 }
552 else {
553 sb.append("null value");
554 }
555 }
556 }
557 }
558 return "{type='" + getName() + "', values='" + sb.toString() + "'}";
559 }
560
561 /**
562 * Creates and returns a new <CODE>LDAPAttribute</CODE> object that
563 * contains the same information as this one. The cloned object has
564 * a deep copy of the contents of the original.
565 *
566 * @return A deep copy of this object
567 */
568 public synchronized Object clone() {
569 return new LDAPAttribute( this );
570 }
571
572 /**
573 * Retrieves the string representation of an attribute
574 * in an LDAP entry. For example:
575 *
576 * <PRE>LDAPAttribute {type='cn', values='Barbara Jensen,Babs Jensen'}</PRE>
577 *
578 * @return string representation of the attribute.
579 */
580 public String toString() {
581 return "LDAPAttribute " + getParamString();
582 }
583 }