protected List<AbstractSession> filterByUserPrincipal(
      Collection<AbstractSession> values, Principal userPrincipal) throws IOException {
    List<AbstractSession> res = null;

    {
      if (values != null) {
        res = new ArrayList<AbstractSession>();

        for (AbstractSession session : values) {
          expandSessionPrincipal(session);
        }

        if (userPrincipal == null) {
          for (AbstractSession session : values) {
            Principal p = session.getUserPrincipal();
            if (p == null) {
              res.add(session);
            }
          }
        } else {
          for (AbstractSession session : values) {
            Principal p = session.getUserPrincipal();
            if (PrincipalUtil.equalsIgnoreRealm(userPrincipal, p)) {
              res.add(session);
            }
          }
        }
      }
    }

    return res;
  }
  public List<String> getSessionIds() throws IOException {
    List<String> res = null;

    {
      synchronized (lookupSessionById) {
        Set<String> keySet = lookupSessionById.keySet();
        if (keySet != null) {
          res = new ArrayList<String>();
          res.addAll(keySet);
        }
      }
    }

    return res;
  }
  protected List<String> joinLists(List<String> v0, List<String> v1) {
    List<String> res = null;

    {
      res = new ArrayList<String>();

      if (v0 != null) {
        res.addAll(v0);
      }

      if (v1 != null) {
        res.addAll(v1);
      }
    }

    return res;
  }
  protected List<String> parseStringList(String list) throws IOException {
    List<String> res = null;

    {
      if (list != null) {
        res = new ArrayList<String>();

        StringTokenizer st = new StringTokenizer(list, ",");
        while (st.hasMoreTokens()) {
          String t = st.nextToken();
          t = t.trim();
          if (t.length() > 0) {
            res.add(t);
          }
        }
      }
    }

    return res;
  }
  public List<Session> getSessions() throws IOException {
    List<Session> res = null;

    {
      synchronized (lookupSessionById) {
        Collection<AbstractSession> values = lookupSessionById.values();
        if (values != null) {
          res = new ArrayList<Session>();

          for (AbstractSession session : values) {
            expandSession(session);
          }

          res.addAll(values);
        }
      }
    }

    return res;
  }
  protected List<Principal> expandGroupPrincipals(AbstractUserAuthorizor.DefaultUser user) {
    List<Principal> res = null;

    {
      if (user != null) {
        res = new ArrayList<Principal>();

        List<Principal> groupPrincipals = user.getGroupPrincipals();

        if (groupPrincipals != null) {
          // Add "User" principal, if required:
          {
            if (matchGroupPrincipal(groupPrincipals, ApplicationUserRoles.BACKEND_USER_A)
                || matchGroupPrincipal(groupPrincipals, ApplicationUserRoles.BACKEND_USER_B)) {
              Principal p = new SimplePrincipal(ApplicationUserRoles.ROLE_USER);
              res.add(p);
            }
          }

          // Add "Administrator" principal, if required:
          {
            if (matchGroupPrincipal(groupPrincipals, ApplicationUserRoles.BACKEND_ADMINISTRATOR_A)
                || matchGroupPrincipal(
                    groupPrincipals, ApplicationUserRoles.BACKEND_ADMINISTRATOR_B)) {
              Principal p = new SimplePrincipal(ApplicationUserRoles.ROLE_ADMINISTRATOR);
              res.add(p);
            }
          }

          // Add all original principals "as is":
          {
            res.addAll(groupPrincipals);
          }
        }
      }
    }

    return res;
  }
  protected Group getJoinedGroup(String userRole, Group group1, Group group2) {
    Group res = null;

    {
      Principal principal = null;
      String id = null;
      String name = null;

      // Set 'principal':
      {
        principal = new SimplePrincipal(userRole);
      }

      // Set 'id':
      {
        id = userRole;
      }

      // Set 'name':
      {
        name = ApplicationUserRoles.getDisplayName(userRole);
      }

      if (group1 == null) {
        if (group2 == null) {
          res = null;
        } else {
          // Set result from 'group2':
          {
            AbstractUserAuthorizor.DefaultGroup g = new AbstractUserAuthorizor.DefaultGroup(group2);

            g.setName(name);
            g.setPrincipal(principal);

            res = g;
          }
        }
      } else {
        if (group2 == null) {
          // Set result from 'group1':
          {
            AbstractUserAuthorizor.DefaultGroup g = new AbstractUserAuthorizor.DefaultGroup(group1);

            g.setName(name);
            g.setPrincipal(principal);

            res = g;
          }
        } else {
          Properties properties = null;
          Map<String, List<String>> attributes = null;
          List<Principal> userPrincipals = null;
          List<Principal> superGroupPrincipals = null;
          List<Principal> groupPrincipals = null;

          // Set 'properties':
          {
            properties = new Properties();

            // Add properties from 'group1':
            {
              addPropertiesWithPrefix(properties, group1);
            }

            // Add properties from 'group2':
            {
              addPropertiesWithPrefix(properties, group2);
            }
          }

          // Set 'attributes':
          {
            Map<String, List<String>> attributes1 = group1.getAttributes();
            Map<String, List<String>> attributes2 = group2.getAttributes();

            attributes = joinAttributes(attributes1, attributes2);
          }

          // Set 'userPrincipals':
          {
            userPrincipals = new ArrayList<Principal>();

            // Add user principals from 'group1':
            {
              List<Principal> l = group1.getUserPrincipals();
              if (l != null) {
                userPrincipals.addAll(l);
              }
            }

            // Add user principals from 'group2':
            {
              List<Principal> l = group2.getUserPrincipals();
              if (l != null) {
                userPrincipals.addAll(l);
              }
            }
          }

          // Set 'superGroupPrincipals':
          {
            superGroupPrincipals = new ArrayList<Principal>();

            // Add super-group principals from 'group1':
            {
              List<Principal> l = group1.getSuperGroupPrincipals();
              if (l != null) {
                superGroupPrincipals.addAll(l);
              }
            }

            // Add super-group principals from 'group2':
            {
              List<Principal> l = group2.getSuperGroupPrincipals();
              if (l != null) {
                superGroupPrincipals.addAll(l);
              }
            }
          }

          // Set 'groupPrincipals':
          {
            groupPrincipals = new ArrayList<Principal>();

            // Add group principals from 'group1':
            {
              List<Principal> l = group1.getGroupPrincipals();
              if (l != null) {
                groupPrincipals.addAll(l);
              }
            }

            // Add group principals from 'group2':
            {
              List<Principal> l = group2.getGroupPrincipals();
              if (l != null) {
                groupPrincipals.addAll(l);
              }
            }
          }

          res =
              new AbstractUserAuthorizor.DefaultGroup(
                  principal,
                  id,
                  name,
                  properties,
                  attributes,
                  userPrincipals,
                  superGroupPrincipals,
                  groupPrincipals);
        }
      }
    }

    return res;
  }
  public User getUser(Principal principal, Properties properties) throws IOException {
    User res = null;

    {
      AbstractUserAuthorizor.DefaultUser user = null; // user created by this

      // Set 'user':
      {
        User u = super.getUser(principal, properties); // get user returned from nested resource

        if (u == null) {
          user = new AbstractUserAuthorizor.DefaultUser();

          // Set principal:
          {
            String userName = PrincipalUtil.getNameStripped(principal);
            Principal userPrincipal = new SimplePrincipal(userName);
            user.setPrincipal(userPrincipal);
          }
        } else {
          user = new AbstractUserAuthorizor.DefaultUser(u);
        }
      }

      // Override group principals:
      {
        List<Principal> l = expandGroupPrincipals(user); // new list of group-principals
        user.setGroupPrincipals(l); // overwrite the original group principals
      }

      // Override user roles:
      {
        List<String> userRoles = new ArrayList<String>();

        // Add basic user roles:
        {
          List<String> l = user.getUserRoles();
          if (l != null) {
            userRoles.addAll(l);
          }
        }

        // Add special, additional user roles:
        {
          List<String> l = getIncludeUserRolesFromConfig(principal);
          if (l != null) {
            userRoles.addAll(l); // add!
          }
        }

        // Expand all user roles:
        {
          userRoles =
              ApplicationUserRoles.expandUserRoles(
                  userRoles); // expanded user-roles, sorted, duplicates removed!
        }

        // Remove special, excluded user roles:
        {
          List<String> l = getExcludeUserRolesFromConfig(principal);
          if (l != null) {
            l =
                ApplicationUserRoles.expandUserRoles(
                    l); // expanded user-roles, sorted, duplicates removed!
            userRoles.removeAll(l); // remove!
          }
        }

        user.setUserRoles(userRoles); // overwrite the original user roles
      }

      // Override full name:
      {
        Principal userPrincipal = user.getPrincipal();
        String fullName = getUserPresentationNameFromConfig(userPrincipal);
        if (fullName != null) {
          fullName = fullName.trim();

          if (fullName.length() > 0) {
            user.setFullName(fullName);
          }
        }
      }

      res = user;
    }

    return res;
  }