/**
  * Add attribute to ldap entry.
  *
  * @param request HTTP request.
  * @param response HTTP response.
  * @param context request context
  * @throws IdentityException if a system error occurs preventing the action
  * @throws IOException if error writing to the buffer
  * @throws NamingException if an LDAP naming exception occurs
  * @throws SQLException
  * @throws CredentialPolicyException
  */
 private void executeModifyUserAttribute(
     HttpServletRequest request,
     HttpServletResponse response,
     RequestContext context,
     boolean isAddAttributeRequest)
     throws IdentityException, IOException, NamingException, SQLException,
         CredentialPolicyException {
   String mimeType = "application/json";
   String filter = Val.chkStr(request.getParameter("q"));
   String attributeName = Val.chkStr(request.getParameter("an"));
   String attributeValue = Val.chkStr(request.getParameter("av"));
   if (filter.length() == 0) {
     response.getWriter().write("{ \"response\" : \"noResults\" }");
     return;
   }
   IdentityAdapter idAdapter = context.newIdentityAdapter();
   Users users = idAdapter.readUsers(filter, null);
   for (User u : users.values()) {
     if (isAddAttributeRequest) {
       try {
         idAdapter.addAttribute(u.getDistinguishedName(), attributeName, attributeValue);
       } catch (AttributeInUseException aiue) {
         // TODO : do nothing if attribute exists ? or overwrite ?
       }
     } else {
       idAdapter.removeAttribute(u.getDistinguishedName(), attributeName, attributeValue);
     }
   }
   writeCharacterResponse(
       response,
       "{ \"response\" : \"User attribute modification was successful.\" }",
       "UTF-8",
       mimeType + ";charset=UTF-8");
 }
  /**
   * Builds list of ldap users matching filter.
   *
   * @param context the current request context (contains the active user)
   * @param filter the user search filter for ldap
   * @return the list of users matching filter
   * @throws IdentityException if a system error occurs preventing the action
   * @throws NamingException if an LDAP naming exception occurs
   */
  protected HashMap<String, Object> buildUsersList(
      RequestContext context, String filter, String attributeName)
      throws IdentityException, NamingException {
    HashMap<String, Object> resultsMap = new HashMap<String, Object>();
    IdentityAdapter idAdapter = context.newIdentityAdapter();
    String searchLimit =
        Val.chkStr(
            context
                .getCatalogConfiguration()
                .getParameters()
                .getValue("ldap.identity.search.maxResults"));
    int srchLimit = -1;
    if (searchLimit.length() > 0) {
      srchLimit = Integer.parseInt(searchLimit);
    }
    Users users = idAdapter.readUsers(filter, attributeName);
    users.sort();
    int totalMatches = users.size();
    resultsMap.put("totalMatches", totalMatches);
    if (srchLimit == -1) {
      resultsMap.put("topUserMatches", users);
      return resultsMap;
    }

    if (attributeName != null) {
      resultsMap.put("topUserMatches", users);
      return resultsMap;
    }
    Users topUserMatches = new Users();
    int count = 0;
    for (User user : users.values()) {
      count++;
      if (count <= srchLimit) {
        topUserMatches.add(user);
      } else {
        break;
      }
    }
    resultsMap.put("topUserMatches", topUserMatches);
    return resultsMap;
  }