1 package org.apache.archiva.redback.common.ldap;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import javax.naming.NamingEnumeration;
23 import javax.naming.NamingException;
24 import javax.naming.directory.Attribute;
25 import javax.naming.directory.Attributes;
31 public final class LdapUtils
38 @SuppressWarnings("unchecked")
39 public static String getLabeledUriValue( Attributes attributes, String attrName, String label,
40 String attributeDescription )
41 throws MappingException
43 if ( attrName == null )
48 Attribute attribute = attributes.get( attrName );
49 if ( attribute != null )
51 NamingEnumeration attrs;
54 attrs = attribute.getAll();
56 catch ( NamingException e )
58 throw new MappingException(
59 "Failed to retrieve " + attributeDescription + " (attribute: \'" + attrName + "\').", e );
62 while ( attrs.hasMoreElements() )
64 Object value = attrs.nextElement();
66 String val = String.valueOf( value );
68 if ( val.endsWith( " " + label ) )
70 return val.substring( 0, val.length() - ( label.length() + 1 ) );
78 public static String getAttributeValue( Attributes attributes, String attrName, String attributeDescription )
79 throws MappingException
81 if ( attrName == null )
86 Attribute attribute = attributes.get( attrName );
87 if ( attribute != null )
91 Object value = attribute.get();
93 return String.valueOf( value );
95 catch ( NamingException e )
97 throw new MappingException(
98 "Failed to retrieve " + attributeDescription + " (attribute: \'" + attrName + "\').", e );
105 public static String getAttributeValueFromByteArray( Attributes attributes, String attrName,
106 String attributeDescription )
107 throws MappingException
109 if ( attrName == null )
114 Attribute attribute = attributes.get( attrName );
115 if ( attribute != null )
119 byte[] value = (byte[]) attribute.get();
121 return new String( value );
123 catch ( NamingException e )
125 throw new MappingException(
126 "Failed to retrieve " + attributeDescription + " (attribute: \'" + attrName + "\').", e );