/** Test method for {@link org.dspace.authenticate.IPMatcher#match(java.lang.String)}. */
 @Test
 public void testIp6MaskedMatch() throws IPMatcherException {
   assertTrue("IPv6 masked match fails", ip6MaskedMatcher.match(IP6_FULL_ADDRESS2));
 }
 /** Test method for {@link org.dspace.authenticate.IPMatcher#match(java.lang.String)}. */
 @Test
 public void testIp6FullMatch() throws IPMatcherException {
   assertTrue("IPv6 full match fails", ip6FullMatcher.match(IP6_FULL_ADDRESS1));
 }
 /** Test method for {@link org.dspace.authenticate.IPMatcher#match(java.lang.String)}. */
 @Test
 public void testIp6MisMatch() throws IPMatcherException {
   assertFalse("IPv6 full nonmatch succeeds", ip6FullMatcher.match(IP6_FULL_ADDRESS2));
 }
  public int[] getSpecialGroups(Context context, HttpServletRequest request) throws SQLException {
    if (request == null) {
      return new int[0];
    }
    List<Integer> groupIDs = new ArrayList<Integer>();

    // Get the user's IP address
    String addr = request.getRemoteAddr();
    if (useProxies == null) {
      useProxies = ConfigurationManager.getBooleanProperty("useProxies", false);
    }
    if (useProxies && request.getHeader("X-Forwarded-For") != null) {
      /* This header is a comma delimited list */
      for (String xfip : request.getHeader("X-Forwarded-For").split(",")) {
        if (!request.getHeader("X-Forwarded-For").contains(addr)) {
          addr = xfip.trim();
        }
      }
    }

    for (IPMatcher ipm : ipMatchers) {
      try {
        if (ipm.match(addr)) {
          // Do we know group ID?
          Integer g = ipMatcherGroupIDs.get(ipm);
          if (g != null) {
            groupIDs.add(g);
          } else {
            // See if we have a group name
            String groupName = ipMatcherGroupNames.get(ipm);

            if (groupName != null) {
              Group group = Group.findByName(context, groupName);
              if (group != null) {
                // Add ID so we won't have to do lookup again
                ipMatcherGroupIDs.put(ipm, Integer.valueOf(group.getID()));
                ipMatcherGroupNames.remove(ipm);

                groupIDs.add(Integer.valueOf(group.getID()));
              } else {
                log.warn(
                    LogManager.getHeader(
                        context, "configuration_error", "unknown_group=" + groupName));
              }
            }
          }
        }
      } catch (IPMatcherException ipme) {
        log.warn(LogManager.getHeader(context, "configuration_error", "bad_ip=" + addr), ipme);
      }
    }

    // Now remove any negative matches
    for (IPMatcher ipm : ipNegativeMatchers) {
      try {
        if (ipm.match(addr)) {
          // Do we know group ID?
          Integer g = ipMatcherGroupIDs.get(ipm);
          if (g != null) {
            groupIDs.remove(g);
          } else {
            // See if we have a group name
            String groupName = ipMatcherGroupNames.get(ipm);

            if (groupName != null) {
              Group group = Group.findByName(context, groupName);
              if (group != null) {
                // Add ID so we won't have to do lookup again
                ipMatcherGroupIDs.put(ipm, Integer.valueOf(group.getID()));
                ipMatcherGroupNames.remove(ipm);

                groupIDs.remove(Integer.valueOf(group.getID()));
              } else {
                log.warn(
                    LogManager.getHeader(
                        context, "configuration_error", "unknown_group=" + groupName));
              }
            }
          }
        }
      } catch (IPMatcherException ipme) {
        log.warn(LogManager.getHeader(context, "configuration_error", "bad_ip=" + addr), ipme);
      }
    }

    int[] results = new int[groupIDs.size()];
    for (int i = 0; i < groupIDs.size(); i++) {
      results[i] = (groupIDs.get(i)).intValue();
    }

    if (log.isDebugEnabled()) {
      StringBuffer gsb = new StringBuffer();

      for (int i = 0; i < results.length; i++) {
        if (i > 0) {
          gsb.append(",");
        }
        gsb.append(results[i]);
      }

      log.debug(LogManager.getHeader(context, "authenticated", "special_groups=" + gsb.toString()));
    }

    return results;
  }