private void refreshConfigurationFormValues() {
    room.lock.readLock().lock();
    try {
      FormField field = configurationForm.getField("muc#roomconfig_roomname");
      field.clearValues();
      field.addValue(room.getNaturalLanguageName());

      field = configurationForm.getField("muc#roomconfig_roomdesc");
      field.clearValues();
      field.addValue(room.getDescription());

      field = configurationForm.getField("muc#roomconfig_changesubject");
      field.clearValues();
      field.addValue((room.canOccupantsChangeSubject() ? "1" : "0"));

      field = configurationForm.getField("muc#roomconfig_maxusers");
      field.clearValues();
      field.addValue(Integer.toString(room.getMaxUsers()));

      field = configurationForm.getField("muc#roomconfig_presencebroadcast");
      field.clearValues();
      for (String roleToBroadcast : room.getRolesToBroadcastPresence()) {
        field.addValue(roleToBroadcast);
      }

      field = configurationForm.getField("muc#roomconfig_publicroom");
      field.clearValues();
      field.addValue((room.isPublicRoom() ? "1" : "0"));

      field = configurationForm.getField("muc#roomconfig_persistentroom");
      field.clearValues();
      field.addValue((room.isPersistent() ? "1" : "0"));

      field = configurationForm.getField("muc#roomconfig_moderatedroom");
      field.clearValues();
      field.addValue((room.isModerated() ? "1" : "0"));

      field = configurationForm.getField("muc#roomconfig_membersonly");
      field.clearValues();
      field.addValue((room.isMembersOnly() ? "1" : "0"));

      field = configurationForm.getField("muc#roomconfig_allowinvites");
      field.clearValues();
      field.addValue((room.canOccupantsInvite() ? "1" : "0"));

      field = configurationForm.getField("muc#roomconfig_passwordprotectedroom");
      field.clearValues();
      field.addValue((room.isPasswordProtected() ? "1" : "0"));

      field = configurationForm.getField("muc#roomconfig_roomsecret");
      field.clearValues();
      field.addValue(room.getPassword());

      field = configurationForm.getField("muc#roomconfig_whois");
      field.clearValues();
      field.addValue((room.canAnyoneDiscoverJID() ? "anyone" : "moderators"));

      field = configurationForm.getField("muc#roomconfig_enablelogging");
      field.clearValues();
      field.addValue((room.isLogEnabled() ? "1" : "0"));

      field = configurationForm.getField("x-muc#roomconfig_reservednick");
      field.clearValues();
      field.addValue((room.isLoginRestrictedToNickname() ? "1" : "0"));

      field = configurationForm.getField("x-muc#roomconfig_canchangenick");
      field.clearValues();
      field.addValue((room.canChangeNickname() ? "1" : "0"));

      field = configurationForm.getField("x-muc#roomconfig_registration");
      field.clearValues();
      field.addValue((room.isRegistrationEnabled() ? "1" : "0"));

      field = configurationForm.getField("muc#roomconfig_roomadmins");
      field.clearValues();
      for (JID jid : room.getAdmins()) {
        field.addValue(jid.toString());
      }

      field = configurationForm.getField("muc#roomconfig_roomowners");
      field.clearValues();
      for (JID jid : room.getOwners()) {
        field.addValue(jid.toString());
      }

      // Remove the old element
      probeResult.remove(probeResult.element(QName.get("x", "jabber:x:data")));
      // Add the new representation of configurationForm as an element
      probeResult.add(configurationForm.getElement());

    } finally {
      room.lock.readLock().unlock();
    }
  }
  private void init() {
    Element element =
        DocumentHelper.createElement(QName.get("query", "http://jabber.org/protocol/muc#owner"));

    configurationForm = new DataForm(DataForm.Type.form);
    configurationForm.setTitle(LocaleUtils.getLocalizedString("muc.form.conf.title"));
    List<String> params = new ArrayList<String>();
    params.add(room.getName());
    configurationForm.addInstruction(
        LocaleUtils.getLocalizedString("muc.form.conf.instruction", params));

    configurationForm
        .addField("FORM_TYPE", null, Type.hidden)
        .addValue("http://jabber.org/protocol/muc#roomconfig");

    configurationForm.addField(
        "muc#roomconfig_roomname",
        LocaleUtils.getLocalizedString("muc.form.conf.owner_roomname"),
        Type.text_single);

    configurationForm.addField(
        "muc#roomconfig_roomdesc",
        LocaleUtils.getLocalizedString("muc.form.conf.owner_roomdesc"),
        Type.text_single);

    configurationForm.addField(
        "muc#roomconfig_changesubject",
        LocaleUtils.getLocalizedString("muc.form.conf.owner_changesubject"),
        Type.boolean_type);

    final FormField maxUsers =
        configurationForm.addField(
            "muc#roomconfig_maxusers",
            LocaleUtils.getLocalizedString("muc.form.conf.owner_maxusers"),
            Type.list_single);
    maxUsers.addOption("10", "10");
    maxUsers.addOption("20", "20");
    maxUsers.addOption("30", "30");
    maxUsers.addOption("40", "40");
    maxUsers.addOption("50", "50");
    maxUsers.addOption(LocaleUtils.getLocalizedString("muc.form.conf.none"), "0");

    final FormField broadcast =
        configurationForm.addField(
            "muc#roomconfig_presencebroadcast",
            LocaleUtils.getLocalizedString("muc.form.conf.owner_presencebroadcast"),
            Type.list_multi);
    broadcast.addOption(LocaleUtils.getLocalizedString("muc.form.conf.moderator"), "moderator");
    broadcast.addOption(LocaleUtils.getLocalizedString("muc.form.conf.participant"), "participant");
    broadcast.addOption(LocaleUtils.getLocalizedString("muc.form.conf.visitor"), "visitor");

    configurationForm.addField(
        "muc#roomconfig_publicroom",
        LocaleUtils.getLocalizedString("muc.form.conf.owner_publicroom"),
        Type.boolean_type);

    configurationForm.addField(
        "muc#roomconfig_persistentroom",
        LocaleUtils.getLocalizedString("muc.form.conf.owner_persistentroom"),
        Type.boolean_type);

    configurationForm.addField(
        "muc#roomconfig_moderatedroom",
        LocaleUtils.getLocalizedString("muc.form.conf.owner_moderatedroom"),
        Type.boolean_type);

    configurationForm.addField(
        "muc#roomconfig_membersonly",
        LocaleUtils.getLocalizedString("muc.form.conf.owner_membersonly"),
        Type.boolean_type);

    configurationForm
        .addField(null, null, Type.fixed)
        .addValue(LocaleUtils.getLocalizedString("muc.form.conf.allowinvitesfixed"));

    configurationForm.addField(
        "muc#roomconfig_allowinvites",
        LocaleUtils.getLocalizedString("muc.form.conf.owner_allowinvites"),
        Type.boolean_type);

    configurationForm.addField(
        "muc#roomconfig_passwordprotectedroom",
        LocaleUtils.getLocalizedString("muc.form.conf.owner_passwordprotectedroom"),
        Type.boolean_type);

    configurationForm
        .addField(null, null, Type.fixed)
        .addValue(LocaleUtils.getLocalizedString("muc.form.conf.roomsecretfixed"));

    configurationForm.addField(
        "muc#roomconfig_roomsecret",
        LocaleUtils.getLocalizedString("muc.form.conf.owner_roomsecret"),
        Type.text_private);

    final FormField whois =
        configurationForm.addField(
            "muc#roomconfig_whois",
            LocaleUtils.getLocalizedString("muc.form.conf.owner_whois"),
            Type.list_single);
    whois.addOption(LocaleUtils.getLocalizedString("muc.form.conf.moderator"), "moderators");
    whois.addOption(LocaleUtils.getLocalizedString("muc.form.conf.anyone"), "anyone");

    configurationForm.addField(
        "muc#roomconfig_enablelogging",
        LocaleUtils.getLocalizedString("muc.form.conf.owner_enablelogging"),
        Type.boolean_type);

    configurationForm.addField(
        "x-muc#roomconfig_reservednick",
        LocaleUtils.getLocalizedString("muc.form.conf.owner_reservednick"),
        Type.boolean_type);

    configurationForm.addField(
        "x-muc#roomconfig_canchangenick",
        LocaleUtils.getLocalizedString("muc.form.conf.owner_canchangenick"),
        Type.boolean_type);

    configurationForm
        .addField(null, null, Type.fixed)
        .addValue(LocaleUtils.getLocalizedString("muc.form.conf.owner_registration"));

    configurationForm.addField(
        "x-muc#roomconfig_registration",
        LocaleUtils.getLocalizedString("muc.form.conf.owner_registration"),
        Type.boolean_type);

    configurationForm
        .addField(null, null, Type.fixed)
        .addValue(LocaleUtils.getLocalizedString("muc.form.conf.roomadminsfixed"));

    configurationForm.addField(
        "muc#roomconfig_roomadmins",
        LocaleUtils.getLocalizedString("muc.form.conf.owner_roomadmins"),
        Type.jid_multi);

    configurationForm
        .addField(null, null, Type.fixed)
        .addValue(LocaleUtils.getLocalizedString("muc.form.conf.roomownersfixed"));

    configurationForm.addField(
        "muc#roomconfig_roomowners",
        LocaleUtils.getLocalizedString("muc.form.conf.owner_roomowners"),
        Type.jid_multi);

    // Create the probeResult and add the basic info together with the configuration form
    probeResult = element;
    probeResult.add(configurationForm.getElement());
  }
  /**
   * Handles a IQ-register 'get' request, which is to be interpreted as a request for a registration
   * form template. The template will be prefilled with data, if the requestee has a current
   * registration with the gateway.
   *
   * @param packet the IQ-register 'get' stanza.
   * @throws UnauthorizedException if the user is not allowed to make use of the gateway.
   */
  private void getRegistrationForm(IQ packet) throws UnauthorizedException {
    final JID from = packet.getFrom();
    final IQ result = IQ.createResultIQ(packet);

    // search for existing registrations
    String curUsername = null;
    String curPassword = null;
    String curNickname = null;
    Boolean registered = false;
    final Collection<Registration> registrations =
        RegistrationManager.getInstance().getRegistrations(from, parent.transportType);
    if (registrations.iterator().hasNext()) {
      Registration registration = registrations.iterator().next();
      curUsername = registration.getUsername();
      curPassword = registration.getPassword();
      curNickname = registration.getNickname();
      registered = true;
    }

    // Verify that the user is allowed to make use of the gateway.
    if (!registered && !parent.permissionManager.hasAccess(from)) {
      // User does not have permission to register with transport.
      // We want to allow them to change settings if they are already
      // registered.
      throw new UnauthorizedException(
          LocaleUtils.getLocalizedString("gateway.base.registrationdeniedbyacls", "kraken"));
    }

    // generate a template registration form.
    final Element response =
        DocumentHelper.createElement(QName.get("query", NameSpace.IQ_REGISTER));
    final DataForm form = new DataForm(DataForm.Type.form);
    form.addInstruction(parent.getTerminologyRegistration());

    final FormField usernameField = form.addField();
    usernameField.setLabel(parent.getTerminologyUsername());
    usernameField.setVariable("username");
    usernameField.setType(FormField.Type.text_single);
    if (curUsername != null) {
      usernameField.addValue(curUsername);
    }

    final FormField passwordField = form.addField();
    passwordField.setLabel(parent.getTerminologyPassword());
    passwordField.setVariable("password");
    passwordField.setType(FormField.Type.text_private);
    if (curPassword != null) {
      passwordField.addValue(curPassword);
    }

    final String nicknameTerm = parent.getTerminologyNickname();
    if (nicknameTerm != null) {
      FormField nicknameField = form.addField();
      nicknameField.setLabel(nicknameTerm);
      nicknameField.setVariable("nick");
      nicknameField.setType(FormField.Type.text_single);
      if (curNickname != null) {
        nicknameField.addValue(curNickname);
      }
    }

    response.add(form.getElement());
    response.addElement("instructions").addText(parent.getTerminologyRegistration());

    // prefill the template with existing data if a registration already
    // exists.
    if (registered) {
      response.addElement("registered");
      response.addElement("username").addText(curUsername);
      if (curPassword == null) {
        response.addElement("password");
      } else {
        response.addElement("password").addText(curPassword);
      }
      if (nicknameTerm != null) {
        if (curNickname == null) {
          response.addElement("nick");
        } else {
          response.addElement("nick").addText(curNickname);
        }
      }
    } else {
      response.addElement("username");
      response.addElement("password");
      if (nicknameTerm != null) {
        response.addElement("nick");
      }
    }

    // Add special indicator for rosterless gateway handling.
    response.addElement("x").addNamespace("", NameSpace.IQ_GATEWAY_REGISTER);

    result.setChildElement(response);

    parent.sendPacket(result);
  }