This project has retired. For details please refer to its
Attic page.
LdapUtils xref
1 package org.apache.archiva.redback.common.ldap;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import javax.naming.NamingEnumeration;
23 import javax.naming.NamingException;
24 import javax.naming.directory.Attribute;
25 import javax.naming.directory.Attributes;
26
27
28
29
30
31 public final class LdapUtils
32 {
33 private LdapUtils()
34 {
35
36 }
37
38 @SuppressWarnings("unchecked")
39 public static String getLabeledUriValue( Attributes attributes, String attrName, String label,
40 String attributeDescription )
41 throws MappingException
42 {
43 if ( attrName == null )
44 {
45 return null;
46 }
47
48 Attribute attribute = attributes.get( attrName );
49 if ( attribute != null )
50 {
51 NamingEnumeration attrs;
52 try
53 {
54 attrs = attribute.getAll();
55 }
56 catch ( NamingException e )
57 {
58 throw new MappingException(
59 "Failed to retrieve " + attributeDescription + " (attribute: \'" + attrName + "\').", e );
60 }
61
62 while ( attrs.hasMoreElements() )
63 {
64 Object value = attrs.nextElement();
65
66 String val = String.valueOf( value );
67
68 if ( val.endsWith( " " + label ) )
69 {
70 return val.substring( 0, val.length() - ( label.length() + 1 ) );
71 }
72 }
73 }
74
75 return null;
76 }
77
78 public static String getAttributeValue( Attributes attributes, String attrName, String attributeDescription )
79 throws MappingException
80 {
81 if ( attrName == null )
82 {
83 return null;
84 }
85
86 Attribute attribute = attributes.get( attrName );
87 if ( attribute != null )
88 {
89 try
90 {
91 Object value = attribute.get();
92
93 return String.valueOf( value );
94 }
95 catch ( NamingException e )
96 {
97 throw new MappingException(
98 "Failed to retrieve " + attributeDescription + " (attribute: \'" + attrName + "\').", e );
99 }
100 }
101
102 return null;
103 }
104
105 public static String getAttributeValueFromByteArray( Attributes attributes, String attrName,
106 String attributeDescription )
107 throws MappingException
108 {
109 if ( attrName == null )
110 {
111 return null;
112 }
113
114 Attribute attribute = attributes.get( attrName );
115 if ( attribute != null )
116 {
117 try
118 {
119 byte[] value = (byte[]) attribute.get();
120
121 return new String( value );
122 }
123 catch ( NamingException e )
124 {
125 throw new MappingException(
126 "Failed to retrieve " + attributeDescription + " (attribute: \'" + attrName + "\').", e );
127 }
128 }
129
130 return null;
131 }
132 }