buildframework/helium/sf/java/core/src/com/nokia/helium/core/LDAPHelper.java
changeset 628 7c4a911dc066
equal deleted inserted replaced
588:c7c26511138f 628:7c4a911dc066
       
     1 /*
       
     2  * Copyright (c) 2010-2011 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the License "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description:  
       
    15  *
       
    16  */
       
    17 package com.nokia.helium.core;
       
    18 
       
    19 import java.util.Hashtable;
       
    20 
       
    21 import javax.naming.Context;
       
    22 import javax.naming.NameNotFoundException;
       
    23 import javax.naming.NamingEnumeration;
       
    24 import javax.naming.NamingException;
       
    25 import javax.naming.directory.DirContext;
       
    26 import javax.naming.directory.InitialDirContext;
       
    27 import javax.naming.directory.SearchControls;
       
    28 import javax.naming.directory.SearchResult;
       
    29 
       
    30 /**
       
    31  * Simpler LDAP interface to retrieve user base attribute. 
       
    32  *
       
    33  */
       
    34 public class LDAPHelper {
       
    35     public static final String EMAIL_ATTRIBUTE_NAME = "mail";
       
    36     private String ldapURL;
       
    37     private String rootdn;
       
    38     
       
    39     /**
       
    40      * Construct a LDAP helper for a specific server.
       
    41      * @param ldapURL the ldap url (e.g: ldap://server:389)
       
    42      * @param rootdn the rootdn value.
       
    43      */
       
    44     public LDAPHelper(String ldapURL, String rootdn) {
       
    45         if (ldapURL == null) {
       
    46             throw new IllegalArgumentException("The ldap server url cannot be null.");
       
    47         }
       
    48         if (rootdn == null) {
       
    49             throw new IllegalArgumentException("The rootdn cannot be null.");
       
    50         }
       
    51         this.ldapURL = ldapURL;
       
    52         this.rootdn = rootdn;
       
    53     }
       
    54     
       
    55     /**
       
    56      * Get the value of the LDAP attribute for a filter.
       
    57      * @param filter the filter to search for
       
    58      * @param attribute the attribute name.
       
    59      * @return the value of the attribute as a string., if the value is not a String then it is 
       
    60      *         stringify using toString. Returns null if not found.
       
    61      * @throws LDAPException
       
    62      */
       
    63     public String getAttributeAsString(String filter, String attribute) throws LDAPException {
       
    64         if (filter == null) {
       
    65             throw new IllegalArgumentException("filter cannot be null.");
       
    66         }
       
    67         if (attribute == null) {
       
    68             throw new IllegalArgumentException("attribute cannot be null.");
       
    69         }
       
    70         // Set up environment for creating initial context
       
    71         Hashtable<String, String> env = new Hashtable<String, String>(11);
       
    72         env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
       
    73         env.put(Context.PROVIDER_URL, ldapURL + "/" + rootdn);
       
    74 
       
    75         // Create initial context
       
    76         try {
       
    77             DirContext ctx = new InitialDirContext(env);
       
    78             SearchControls controls = new SearchControls();
       
    79             controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
       
    80             NamingEnumeration<SearchResult> en = ctx.search("", filter, controls);
       
    81             if (en.hasMore()) {
       
    82                 SearchResult sr = en.next();
       
    83                 if (sr.getAttributes().get(attribute) != null) {
       
    84                     Object value = sr.getAttributes().get(attribute).get();
       
    85                     if (value instanceof String) {
       
    86                         return (String)value;
       
    87                     } else {
       
    88                         return value.toString();
       
    89                     }
       
    90                 }
       
    91             }
       
    92         } catch (NameNotFoundException ex) {
       
    93             throw new LDAPException("Could not find " +  filter + " attribute " + attribute + " in LDAP: " + ex.getMessage(), ex);
       
    94         } catch (NamingException ex) {
       
    95             throw new LDAPException("Could not find " +  filter + " attribute " + attribute + " in LDAP: " + ex.getMessage(), ex);
       
    96         }
       
    97         return null;
       
    98     }
       
    99 
       
   100     /**
       
   101      * Get the value of the LDAP attribute for current user. (based on user.name).
       
   102      * @param attribute the attribute name.
       
   103      * @return the value of the attribute as a string., if the value is not a String then it is 
       
   104      *         stringify using toString. Returns null if not found.
       
   105      * @throws LDAPException
       
   106      */
       
   107     public String getUserAttributeAsString(String attribute) throws LDAPException {
       
   108         return getUserAttributeAsString(System.getProperty("user.name"), attribute);
       
   109     }
       
   110 
       
   111     /**
       
   112      * Get the value of the LDAP attribute for username.
       
   113      * @param username the user to search for
       
   114      * @param attribute the attribute name.
       
   115      * @return the value of the attribute as a string., if the value is not a String then it is 
       
   116      *         stringify using toString. Returns null if not found.
       
   117      * @throws LDAPException
       
   118      */
       
   119     public String getUserAttributeAsString(String username, String attribute) throws LDAPException {
       
   120         return getAttributeAsString("uid=" + username, attribute);
       
   121     }
       
   122 
       
   123 }