This project has retired. For details please refer to its Attic page.
LdapUtils xref
View Javadoc

1   package org.apache.archiva.redback.common.ldap;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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          // no op
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 }