1 /*
2  * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 /**
27   * Supports checking an attribute set satisfies a filter
28   * that is specified as a set of "matching" attributes.
29   * Checking is done by determining whether the given attribute set
30   * is a superset of the matching ones.
31   *
32   * @author Rosanna Lee
33   */
34 
35 package com.sun.jndi.toolkit.dir;
36 
37 import javax.naming.*;
38 import javax.naming.directory.*;
39 
40 public class ContainmentFilter implements AttrFilter {
41     private Attributes matchingAttrs;
42 
ContainmentFilter(Attributes match)43     public ContainmentFilter(Attributes match) {
44         matchingAttrs = match;
45     }
46 
check(Attributes attrs)47     public boolean check(Attributes attrs) throws NamingException {
48         return matchingAttrs == null ||
49             matchingAttrs.size() == 0 ||
50             contains(attrs, matchingAttrs);
51     }
52 
53     // returns true if superset contains subset
contains(Attributes superset, Attributes subset)54     public static boolean contains(Attributes superset, Attributes subset)
55         throws NamingException {
56           if (subset == null)
57             return true;  // an empty set is always a subset
58 
59             NamingEnumeration<? extends Attribute> m = subset.getAll();
60             while (m.hasMore()) {
61                 if (superset == null) {
62                     return false;  // contains nothing
63                 }
64                 Attribute target = m.next();
65                 Attribute fromSuper = superset.get(target.getID());
66                 if (fromSuper == null) {
67                     return false;
68                 } else {
69                     // check whether attribute values match
70                     if (target.size() > 0) {
71                         NamingEnumeration<?> vals = target.getAll();
72                         while (vals.hasMore()) {
73                             if (!fromSuper.contains(vals.next())) {
74                                 return false;
75                             }
76                         }
77                     }
78                 }
79             }
80             return true;
81         }
82 
83 }
84