View Javadoc

1   /*
2    * Copyright [2007] [University Corporation for Advanced Internet Development, Inc.]
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package edu.internet2.middleware.shibboleth.common.attribute.filtering.provider.match.saml;
18  
19  import org.opensaml.saml2.metadata.EntitiesDescriptor;
20  import org.opensaml.saml2.metadata.EntityDescriptor;
21  import org.opensaml.xml.util.DatatypeHelper;
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import edu.internet2.middleware.shibboleth.common.attribute.filtering.provider.match.basic.AbstractMatchFunctor;
26  
27  /**
28   * Base class for match functors that check if a given entity is in an entity group.
29   */
30  public abstract class AbstractEntityGroupMatchFunctor extends AbstractMatchFunctor {
31  
32      /** Class logger. */
33      private final Logger log = LoggerFactory.getLogger(AbstractEntityGroupMatchFunctor.class);
34  
35      /** The entity group to match against. */
36      private String entityGroup;
37  
38      /**
39       * Gets the entity group to match against.
40       * 
41       * @return entity group to match against
42       */
43      public String getEntityGroup() {
44          return entityGroup;
45      }
46  
47      /**
48       * Sets the entity group to match against.
49       * 
50       * @param group entity group to match against
51       */
52      public void setEntityGroup(String group) {
53          entityGroup = DatatypeHelper.safeTrimOrNullString(group);
54      }
55  
56      /**
57       * Checks if the given entity is in the provided entity group.
58       * 
59       * @param entity the entity to check
60       * 
61       * @return true if the entity is in the group, false if not
62       */
63      protected boolean isEntityInGroup(EntityDescriptor entity) {
64          if (entityGroup == null) {
65              log.debug("No entity group specified, unable to check if entity is in group");
66              return false;
67          }
68  
69          if (entity == null) {
70              log.debug("No entity metadata available, unable to check if entity is in group {}", entityGroup);
71              return false;
72          }
73  
74          EntitiesDescriptor currentGroup = (EntitiesDescriptor) entity.getParent();
75          if (currentGroup == null) {
76              log.debug("Entity descriptor does not have a parent object, unable to check if entity is in group {}",
77                      entityGroup);
78              return false;
79          }
80  
81          do {
82              if (entityGroup.equals(currentGroup.getName())) {
83                  return true;
84              }
85              currentGroup = (EntitiesDescriptor) currentGroup.getParent();
86          } while (currentGroup != null);
87  
88          return false;
89      }
90  }