LdapGroupTester.java 6.15 KB
Newer Older
Gaston Dombiak's avatar
Gaston Dombiak committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision: $
 * $Date: $
 *
6
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
Gaston Dombiak's avatar
Gaston Dombiak committed
7
 *
8 9 10 11 12 13 14 15 16 17 18
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
Gaston Dombiak's avatar
Gaston Dombiak committed
19 20 21 22 23
 */

package org.jivesoftware.admin;

import org.jivesoftware.util.Log;
24
import org.jivesoftware.openfire.ldap.LdapManager;
Gaston Dombiak's avatar
Gaston Dombiak committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.Control;
import javax.naming.ldap.LdapContext;
import javax.naming.ldap.SortControl;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;

/**
 * Class that assists during the testing of the ldap groups.
 *
 * @author Gaston Dombiak
 */
public class LdapGroupTester {

    private LdapManager manager;

    public LdapGroupTester(LdapManager manager) {
        this.manager = manager;
    }

    /**
     * Returns fist N groups found in LDAP. The returned groups are only able to return their name,
53
     * description and count of members. Count of members is considering all values that were found
Gaston Dombiak's avatar
Gaston Dombiak committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
     * in the member field.
     *
     * @param maxGroups max number of groups to return.
     * @return fist N groups found in the LDAP.
     */
    public Collection<Group> getGroups(int maxGroups) {
        Collection<Group> groups = new ArrayList<Group>();
        LdapContext ctx = null;
        try {
            ctx = manager.getContext();
            // Sort on group name field.
            Control[] searchControl = new Control[]{
                new SortControl(new String[]{manager.getGroupNameField()}, Control.NONCRITICAL)
            };
            ctx.setRequestControls(searchControl);

            SearchControls searchControls = new SearchControls();
            // See if recursive searching is enabled. Otherwise, only search one level.
            if (manager.isSubTreeSearch()) {
                searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
            }
            else {
                searchControls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
            }
            // Attributes to return for each group
            String[] standardAttributes = new String[3];
            standardAttributes[0] = manager.getGroupNameField();
            standardAttributes[1] = manager.getGroupDescriptionField();
            standardAttributes[2] = manager.getGroupMemberField();
            searchControls.setReturningAttributes(standardAttributes);
            // Limit results to those we'll need to process
            searchControls.setCountLimit(maxGroups);

            String filter = MessageFormat.format(manager.getGroupSearchFilter(), "*");
            NamingEnumeration answer = ctx.search("", filter, searchControls);
            while (answer.hasMoreElements()) {
                // Get the next group.
                Attributes attributes = ((SearchResult) answer.next()).getAttributes();
                String groupName = (String) attributes.get(manager.getGroupNameField()).get();
                String description = "";
                int elements = 0;
                try {
                    description = ((String) attributes.get(manager.getGroupDescriptionField()).get());
                } catch (NullPointerException e) {
                    // Do nothing since the group description field was not found
                } catch (Exception e) {
                    Log.error("Error retrieving group description", e);
                }

                Attribute memberField = attributes.get(manager.getGroupMemberField());
                if (memberField != null) {
                    NamingEnumeration ne = memberField.getAll();
                    while (ne.hasMore()) {
                        ne.next();
                        elements = elements + 1;
                    }
                }
                // Build Group with found information
                groups.add(new Group(groupName, description, elements));
            }
            // Close the enumeration.
            answer.close();
        }
        catch (Exception e) {
118
            Log.error(e.getMessage(), e);
Gaston Dombiak's avatar
Gaston Dombiak committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
        }
        finally {
            try {
                if (ctx != null) {
                    ctx.setRequestControls(null);
                    ctx.close();
                }
            }
            catch (Exception ignored) {
                // Ignore.
            }
        }
        return groups;
    }

    /**
     * Representation of a group found in LDAP. This representatio is read-only and only provides
136 137
     * some basic information: name, description and number of members in the group. Note that
     * group members are not validated (i.e. checked that they are valid JIDs and that the JID belongs
Gaston Dombiak's avatar
Gaston Dombiak committed
138 139 140 141 142 143 144 145 146
     * to an existing user).
     */
    public static class Group {
        private String name;
        private String description;
        /**
         * Elements that the group contains. This includes admins, members or anything listed
         * in the <tt>member field</tt>. At this point JIDs are not validated.
         */
147
        private int members;
Gaston Dombiak's avatar
Gaston Dombiak committed
148 149


150
        public Group(String name, String description, int members) {
Gaston Dombiak's avatar
Gaston Dombiak committed
151 152
            this.name = name;
            this.description = description;
153
            this.members = members;
Gaston Dombiak's avatar
Gaston Dombiak committed
154 155 156 157 158 159 160 161 162 163 164
        }


        public String getName() {
            return name;
        }

        public String getDescription() {
            return description;
        }

165 166
        public int getMembers() {
            return members;
Gaston Dombiak's avatar
Gaston Dombiak committed
167 168 169
        }
    }
}