private String getGluuCustomPersonAttributesWithValues(GluuCustomPerson gluuCustomPerson) {
    StringBuilder sb = new StringBuilder();
    sb.append("dn: '").append(gluuCustomPerson.getDn()).append("'\n");
    sb.append("inum: '").append(gluuCustomPerson.getInum()).append("',\n");
    sb.append("gluuStatus: '").append(gluuCustomPerson.getStatus()).append("'");

    for (GluuCustomAttribute customAttribute : gluuCustomPerson.getCustomAttributes()) {
      sb.append("\n").append(customAttribute.getName()).append(": '");
      if ((customAttribute.getValues() != null) && (customAttribute.getValues().length > 1)) {
        sb.append(Arrays.toString(customAttribute.getValues()));
      } else {
        sb.append(customAttribute.getValue());
      }
      sb.append("'");
    }

    return sb.toString();
  }
  @PUT
  @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
  public Response deleteAssociation(
      @HeaderParam("Authorization") String authorization, PersonAssociation personAssociation)
      throws Exception {

    personService = PersonService.instance();
    clientService = ClientService.instance();

    Response authorizationResponse = processAuthorization(authorization);
    if (authorizationResponse != null) {
      return authorizationResponse;
    }

    try {
      log.info("Creating an instance of GluuCustomPerson");
      GluuCustomPerson person =
          personService.getPersonByInum(personAssociation.getUserAssociation().replaceAll(" ", ""));
      log.info("getting a list of clientDNs");
      List<String> listClientDNs = new ArrayList<String>();

      boolean isACDNsEmpty = person.getAssociatedClient() == null;

      if (!isACDNsEmpty) {
        for (String dn : person.getAssociatedClient()) {
          log.info("isACDNsEmpty = false");
          if (dn != null && !dn.equalsIgnoreCase("")) {
            listClientDNs.add(dn.replaceAll(" ", ""));
          }
        }
      }

      log.info("getting a list of clean clients");

      List<String> cleanPACDNs = new ArrayList<String>();
      for (String dn : personAssociation.getEntryAssociations()) {
        if (dn != null && !dn.equalsIgnoreCase("")) {
          cleanPACDNs.add(dn);
        }
      }

      log.info("removing clientdns");

      for (String clientdn : cleanPACDNs) {

        if (listClientDNs.contains(clientdn)) {
          listClientDNs.remove(clientdn);
        }
      }

      log.info("geting a cleanlist");

      List<String> cleanList = new ArrayList<String>();
      for (String cDn : listClientDNs) {
        if (cDn != null && !cDn.equalsIgnoreCase("")) {
          cleanList.add(cDn);
        }
      }
      log.info("setting AssociatedClientDNs");
      if (cleanList.size() < 1) {
        person.setAssociatedClient(null);
      } else {
        person.setAssociatedClient(cleanList);
      }

      log.info("Updating person");

      personService.updatePerson(person);

      log.info("deleting user dn from clients");

      List<String> EntryAssociations = new ArrayList<String>();

      for (String dn : personAssociation.getEntryAssociations()) {
        if (dn != null && !dn.equalsIgnoreCase("")) {
          EntryAssociations.add(dn.replaceAll(" ", ""));
        }
      }

      for (String clientDn : EntryAssociations) {
        log.info("getting a client");

        OxAuthCustomClient client =
            clientService.getClientByAttributeCustom(
                applicationConfiguration.getClientAssociationAttribute(),
                clientDn.replaceAll(" ", ""));
        // String[] personDNS =
        // client.getAttributes("associatedPersonDN");
        log.info("checking if the associatedPerson is empty");
        log.info("client dn : ", client.getDn());
        boolean isAPDNsEmpty = client.getAttributes("associatedPerson") == null;
        log.info("new ArrayList");
        List<String> list = new ArrayList<String>();
        if (!isAPDNsEmpty) {
          log.info("!isAPDNsEmpty");
          // list =
          // Arrays.asList(client.getAttributes("associatedPersonDN"));
          for (int i = 0; i < client.getAttributes("associatedPerson").length; i++) {
            if (client.getAttributes("associatedPerson")[i] != null
                && !client.getAttributes("associatedPerson")[i].equalsIgnoreCase("")) {
              list.add(client.getAttributes("associatedPerson")[i]);
            }
          }
          /*
           * for(String dn : client.getAssociatedPersonDNs()){ if(dn
           * != null && !dn.equalsIgnoreCase("")){list.add(dn);} }
           */
        }
        log.info("getting personDN");
        String personInum = personAssociation.getUserAssociation().replaceAll(" ", "");

        if (list.contains(personInum)) {
          log.info("removing person's dn");
          list.remove(personInum);
        }

        log.info("Creating a clean list");

        List<String> cleanPersonList = new ArrayList<String>();
        for (String cDn : list) {
          if (cDn != null && cDn.equalsIgnoreCase("")) {
            cleanPersonList.add(cDn);
          }
        }
        log.info("Setting AssociatedPersonDNs");
        if (cleanPersonList.size() < 1) {
          String[] nullArray = null;
          client.setAttribute("associatedPerson", nullArray);
        } else {
          String[] arrayPersonDns = new String[cleanPersonList.size()];
          for (int i = 0; i < cleanPersonList.size(); i++) {
            arrayPersonDns[i] = cleanPersonList.get(i);
          }
          client.setAttribute("associatedPerson", arrayPersonDns);
        }
        clientService.updateCustomClient(client);
      }
      log.info("returning result;");

      return Response.ok().build();
    } catch (Exception ex) {
      log.info("Exception: ", ex);
      log.error("Exception: ", ex);
      return getErrorResponse(
          "Unexpected processing error, please check the input parameters",
          Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
  }
  @Path("/Associate/")
  @POST
  @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
  @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
  public Response createAssociation(
      @HeaderParam("Authorization") String authorization, PersonAssociation personAssociation)
      throws Exception {

    personService = PersonService.instance();
    clientService = ClientService.instance();

    Response authorizationResponse = processAuthorization(authorization);
    if (authorizationResponse != null) {
      return authorizationResponse;
    }

    try {
      log.info("creating an instance of gluuCustomperson");
      GluuCustomPerson person =
          personService.getPersonByInum(personAssociation.getUserAssociation().replaceAll(" ", ""));

      log.info("setting AssociatedClientDNs");
      List<String> cleanCDNList = new ArrayList<String>();
      for (String dn : personAssociation.getEntryAssociations()) {
        cleanCDNList.add(dn.replaceAll(" ", ""));
      }

      person.setAssociatedClient(cleanCDNList);

      log.info("updating person");

      personService.updatePerson(person);

      log.info("setting user in clients");
      for (String clientDn : personAssociation.getEntryAssociations()) {
        log.info("getting a client");
        OxAuthCustomClient client =
            clientService.getClientByAttributeCustom(
                applicationConfiguration.getClientAssociationAttribute(),
                clientDn.replaceAll(" ", ""));

        log.info("the inum of the client ", client.getInum());

        log.info("checking if the list is empty");
        boolean isAPDNsEmpty = client.getAttributes("associatedPerson") == null;

        log.info("instantiating a new arraylist");

        List<String> listOfpersons = new ArrayList<String>();
        log.info("getting AssociatedPersonDN");
        if (!isAPDNsEmpty) {
          listOfpersons = new ArrayList(Arrays.asList(client.getAttributes("associatedPerson")));
          /*
           * for(String dn :
           * client.getAttributes("associatedPersonDN")){ if(dn !=
           * null && !dn.equalsIgnoreCase("")){listOfpersons.add(dn);}
           * }
           */

        }
        log.info("getting persons dn");
        String personInum = personAssociation.getUserAssociation().replaceAll(" ", "");

        if (isAPDNsEmpty || !listOfpersons.contains(personInum)) {
          log.info("adding person");
          listOfpersons.add(personInum);
        }

        String[] arrayOfpersons = new String[listOfpersons.size()];
        for (int i = 0; i < listOfpersons.size(); i++) {
          arrayOfpersons[i] = listOfpersons.get(i);
        }
        log.info("setting list of AssociatedPersonDns");
        client.setAttribute("associatedPerson", arrayOfpersons);
        log.info("Updating client");
        clientService.updateCustomClient(client);
      }

      String uri = "/ClientAssociation/Associate/" + person.getInum();
      log.info("returning response");
      return Response.created(URI.create(uri)).entity(personAssociation).build();

    } catch (Exception ex) {
      log.error("Failed to add Association", ex);
      // log.info("Failed to add Association" , ex);
      return getErrorResponse(
          "Unexpected processing error, please check the input parameters",
          Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
  }
  public void validateInterceptorScript() {
    String result = update();
    if (!OxTrustConstants.RESULT_SUCCESS.equals(result)) {
      return;
    }

    // Reinit dialog
    init();

    this.showInterceptorValidationDialog = true;

    boolean loadedScripts = externalCacheRefreshService.getCustomScriptConfigurations().size() > 0;
    if (!loadedScripts) {
      String message = "Can't load Cache Refresh scripts. Using default script";
      log.error(message);
      this.interceptorValidationMessage = message;

      return;
    }

    // Prepare data for dummy entry
    String targetInum = inumService.generateInums(OxTrustConstants.INUM_TYPE_PEOPLE_SLUG, false);
    String targetPersonDn = personService.getDnForPerson(targetInum);
    String[] targetCustomObjectClasses = applicationConfiguration.getPersonObjectClassTypes();

    // Collect all attributes
    String[] keyAttributesWithoutValues =
        getCompoundKeyAttributesWithoutValues(cacheRefreshConfiguration);
    String[] sourceAttributes = getSourceAttributes(cacheRefreshConfiguration);

    // Merge all attributes into one set
    Set<String> allAttributes = new HashSet<String>();
    for (String attribute : keyAttributesWithoutValues) {
      allAttributes.add(attribute);
    }

    for (String attribute : sourceAttributes) {
      allAttributes.add(attribute);
    }

    // Prepare source person entry with default attributes values
    GluuSimplePerson sourcePerson = new GluuSimplePerson();
    List<GluuCustomAttribute> customAttributes = sourcePerson.getCustomAttributes();
    for (String attribute : allAttributes) {
      customAttributes.add(new GluuCustomAttribute(attribute, "Test value"));
    }

    // Prepare target person
    GluuCustomPerson targetPerson = new GluuCustomPerson();
    targetPerson.setDn(targetPersonDn);
    targetPerson.setInum(targetInum);
    targetPerson.setStatus(GluuStatus.ACTIVE);
    targetPerson.setCustomObjectClasses(targetCustomObjectClasses);

    // Execute mapping according to configuration
    Map<String, String> targetServerAttributesMapping =
        getTargetServerAttributesMapping(cacheRefreshConfiguration);
    cacheRefreshService.setTargetEntryAttributes(
        sourcePerson, targetServerAttributesMapping, targetPerson);

    // Execute interceptor script
    boolean executionResult =
        externalCacheRefreshService.executeExternalUpdateUserMethods(targetPerson);
    if (!executionResult) {
      String message = "Can't execute Cache Refresh scripts.";
      log.error(message);
      this.interceptorValidationMessage = message;

      return;
    }

    log.info(
        "Script has been executed successfully.\n\nSample source entry is:\n'{0}'.\n\nSample result entry is:\n'{1}'",
        getGluuSimplePersonAttributesWithValues(sourcePerson),
        getGluuCustomPersonAttributesWithValues(targetPerson));
    this.interceptorValidationMessage =
        String.format(
            "Script has been executed successfully.\n\nSample source entry is:\n%s.\n\nSample result entry is:\n%s",
            getGluuSimplePersonAttributesWithValues(sourcePerson),
            getGluuCustomPersonAttributesWithValues(targetPerson));
  }