@Override
 public boolean equals(final Object o) {
   if (o == this) return true;
   if (!(o instanceof UAProfile)) return false;
   final UAProfile rhs = (UAProfile) o;
   return EqualsUtils.equals(m_sProfileUrl, rhs.m_sProfileUrl)
       && EqualsUtils.equals(m_aProfileDiffData, rhs.m_aProfileDiffData);
 }
 @Override
 public boolean equals(final Object o) {
   if (o == this) return true;
   if (!(o instanceof ReadonlyPair<?, ?>)) return false;
   final ReadonlyPair<?, ?> rhs = (ReadonlyPair<?, ?>) o;
   return EqualsUtils.equals(m_aFirst, rhs.m_aFirst)
       && EqualsUtils.equals(m_aSecond, rhs.m_aSecond);
 }
 @Override
 public boolean equals(final Object o) {
   if (o == this) return true;
   if (o == null || !getClass().equals(o.getClass())) return false;
   final ResourceLocation rhs = (ResourceLocation) o;
   return EqualsUtils.equals(m_sResourceID, rhs.m_sResourceID)
       && m_nLineNumber == rhs.m_nLineNumber
       && m_nColumnNumber == rhs.m_nColumnNumber
       && EqualsUtils.equals(m_sField, rhs.m_sField);
 }
 @Override
 public boolean equals(final Object o) {
   if (o == this) return true;
   if (!super.equals(o)) return false;
   final AbstractJSInvocation<?> rhs = (AbstractJSInvocation<?>) o;
   return EqualsUtils.equals(this.m_aObject, rhs.m_aObject)
       && EqualsUtils.equals(this.m_aCtorType, rhs.m_aCtorType)
       && EqualsUtils.equals(this.m_aCallee, rhs.m_aCallee)
       && EqualsUtils.equals(this.m_sName, rhs.m_sName)
       && this.m_aArgs.equals(rhs.m_aArgs);
 }
 @Override
 public boolean equals(final Object o) {
   if (o == this) return true;
   if (!(o instanceof MutableBigInteger)) return false;
   final MutableBigInteger rhs = (MutableBigInteger) o;
   return EqualsUtils.equals(m_aValue, rhs.m_aValue);
 }
 @Override
 public boolean equals(final Object o) {
   if (o == this) return true;
   if (!(o instanceof MicroDataAware)) return false;
   final MicroDataAware rhs = (MicroDataAware) o;
   return EqualsUtils.equals(m_aSB, rhs.m_aSB);
 }
 @Override
 public boolean equals(final Object o) {
   if (o == this) return true;
   if (!(o instanceof URLResource)) return false;
   final URLResource rhs = (URLResource) o;
   return EqualsUtils.equals(m_aURL, rhs.m_aURL);
 }
 @Override
 public boolean equals(final Object o) {
   if (o == this) return true;
   if (!(o instanceof FilterElementWithNamespaceAndLocalName)) return false;
   final FilterElementWithNamespaceAndLocalName rhs = (FilterElementWithNamespaceAndLocalName) o;
   return EqualsUtils.equals(m_sNamespaceURI, rhs.m_sNamespaceURI)
       && m_sLocalName.equals(rhs.m_sLocalName);
 }
  /**
   * Rename a directory.
   *
   * @param aSourceDir The original directory name. May not be <code>null</code>.
   * @param aTargetDir The destination directory name. May not be <code>null</code>.
   * @return A non-<code>null</code> error code.
   */
  @Nonnull
  public static FileIOError renameDir(
      @Nonnull final File aSourceDir, @Nonnull final File aTargetDir) {
    ValueEnforcer.notNull(aSourceDir, "SourceDirectory");
    ValueEnforcer.notNull(aTargetDir, "TargetDirectory");

    // Does the source directory exist?
    if (!FileUtils.existsDir(aSourceDir))
      return EFileIOErrorCode.SOURCE_DOES_NOT_EXIST.getAsIOError(
          EFileIOOperation.RENAME_DIR, aSourceDir);

    // Are source and target different?
    if (EqualsUtils.equals(aSourceDir, aTargetDir))
      return EFileIOErrorCode.SOURCE_EQUALS_TARGET.getAsIOError(
          EFileIOOperation.RENAME_DIR, aSourceDir);

    // Does the target directory already exist?
    if (aTargetDir.exists())
      return EFileIOErrorCode.TARGET_ALREADY_EXISTS.getAsIOError(
          EFileIOOperation.RENAME_DIR, aTargetDir);

    // Is the source a parent of target?
    if (FileUtils.isParentDirectory(aSourceDir, aTargetDir))
      return EFileIOErrorCode.TARGET_IS_CHILD_OF_SOURCE.getAsIOError(
          EFileIOOperation.RENAME_DIR, aSourceDir, aTargetDir);

    // Is the source parent directory writable?
    final File aSourceParentDir = aSourceDir.getParentFile();
    if (aSourceParentDir != null && !FileUtils.canWrite(aSourceParentDir))
      return EFileIOErrorCode.SOURCE_PARENT_NOT_WRITABLE.getAsIOError(
          EFileIOOperation.RENAME_DIR, aSourceDir);

    // Is the target parent directory writable?
    final File aTargetParentDir = aTargetDir.getParentFile();
    if (aTargetParentDir != null
        && aTargetParentDir.exists()
        && !FileUtils.canWrite(aTargetParentDir))
      return EFileIOErrorCode.TARGET_PARENT_NOT_WRITABLE.getAsIOError(
          EFileIOOperation.RENAME_DIR, aTargetDir);

    // Ensure parent of target directory is present
    FileUtils.ensureParentDirectoryIsPresent(aTargetDir);

    try {
      final EFileIOErrorCode eError =
          aSourceDir.renameTo(aTargetDir)
              ? EFileIOErrorCode.NO_ERROR
              : EFileIOErrorCode.OPERATION_FAILED;
      return eError.getAsIOError(EFileIOOperation.RENAME_DIR, aSourceDir, aTargetDir);
    } catch (final SecurityException ex) {
      return EFileIOErrorCode.getAsIOError(EFileIOOperation.RENAME_DIR, ex);
    }
  }
  /**
   * Copies the source file to the target file.
   *
   * @param aSourceFile The source file to use. May not be <code>null</code>. Needs to be an
   *     existing file.
   * @param aTargetFile The destination files. May not be <code>null</code> and may not be an
   *     existing file.
   * @return A non-<code>null</code> error code.
   */
  @Nonnull
  public static FileIOError copyFile(
      @Nonnull final File aSourceFile, @Nonnull final File aTargetFile) {
    ValueEnforcer.notNull(aSourceFile, "SourceFile");
    ValueEnforcer.notNull(aTargetFile, "TargetFile");

    // Does the source file exist?
    if (!FileUtils.existsFile(aSourceFile))
      return EFileIOErrorCode.SOURCE_DOES_NOT_EXIST.getAsIOError(
          EFileIOOperation.COPY_FILE, aSourceFile);

    // Are source and target different?
    if (EqualsUtils.equals(aSourceFile, aTargetFile))
      return EFileIOErrorCode.SOURCE_EQUALS_TARGET.getAsIOError(
          EFileIOOperation.COPY_FILE, aSourceFile);

    // Does the target file already exist?
    if (aTargetFile.exists())
      return EFileIOErrorCode.TARGET_ALREADY_EXISTS.getAsIOError(
          EFileIOOperation.COPY_FILE, aTargetFile);

    // Is the source file readable?
    if (!FileUtils.canRead(aSourceFile))
      return EFileIOErrorCode.SOURCE_NOT_READABLE.getAsIOError(
          EFileIOOperation.COPY_FILE, aSourceFile);

    // Is the target parent directory writable?
    final File aTargetParentDir = aTargetFile.getParentFile();
    if (aTargetParentDir != null
        && aTargetParentDir.exists()
        && !FileUtils.canWrite(aTargetParentDir))
      return EFileIOErrorCode.TARGET_PARENT_NOT_WRITABLE.getAsIOError(
          EFileIOOperation.COPY_FILE, aTargetFile);

    // Ensure the targets parent directory is present
    FileUtils.ensureParentDirectoryIsPresent(aTargetFile);

    // Used FileChannel for better performance
    final EFileIOErrorCode eError =
        _copyFile(aSourceFile, aTargetFile).isSuccess()
            ? EFileIOErrorCode.NO_ERROR
            : EFileIOErrorCode.OPERATION_FAILED;
    return eError.getAsIOError(EFileIOOperation.COPY_FILE, aSourceFile, aTargetFile);
  }
Exemple #11
0
 public HCRadioButton(@Nonnull final IHCRequestField aRF, @Nullable final String sValue) {
   this(aRF.getFieldName(), sValue, EqualsUtils.equals(sValue, aRF.getRequestValue()));
 }
 @Override
 public boolean hasAttributeValue(
     @Nullable final String sName, @Nullable final String sDesiredValue, final boolean bDefault) {
   final String sValue = getAttributeAsString(sName);
   return sValue == null ? bDefault : EqualsUtils.equals(sValue, sDesiredValue);
 }
 @Override
 public boolean hasAttributeValue(
     @Nullable final String sName, @Nullable final String sDesiredValue) {
   return EqualsUtils.equals(getAttributeAsString(sName), sDesiredValue);
 }
  @Override
  @SuppressWarnings("null")
  protected void validateAndSaveInputParameters(
      @Nonnull final WebPageExecutionContext aWPEC,
      @Nullable final IUser aSelectedObject,
      @Nonnull final FormErrors aFormErrors,
      final boolean bEdit) {
    final HCNodeList aNodeList = aWPEC.getNodeList();
    final Locale aDisplayLocale = aWPEC.getDisplayLocale();
    final boolean bIsAdministrator = aSelectedObject != null && aSelectedObject.isAdministrator();
    final AccessManager aAccessMgr = AccessManager.getInstance();
    String sLoginName = aWPEC.getAttr(FIELD_LOGINNAME);
    final String sFirstName = aWPEC.getAttr(FIELD_FIRSTNAME);
    final String sLastName = aWPEC.getAttr(FIELD_LASTNAME);
    final String sEmailAddress = aWPEC.getAttr(FIELD_EMAILADDRESS);
    final String sPassword = aWPEC.getAttr(FIELD_PASSWORD);
    final String sPasswordConf = aWPEC.getAttr(FIELD_PASSWORD_CONFIRM);
    final boolean bEnabled =
        bIsAdministrator ? true : aWPEC.getCheckBoxAttr(FIELD_ENABLED, DEFAULT_ENABLED);
    final Collection<String> aUserGroupIDs =
        bIsAdministrator
            ? aAccessMgr.getAllUserGroupIDsWithAssignedUser(aSelectedObject.getID())
            : aWPEC.getAttrs(FIELD_USERGROUPS);

    if (useEmailAddressAsLoginName()) {
      sLoginName = sEmailAddress;
    } else {
      if (StringHelper.hasNoText(sLoginName))
        aFormErrors.addFieldError(
            FIELD_LOGINNAME, EText.ERROR_LOGINNAME_REQUIRED.getDisplayText(aDisplayLocale));
    }

    if (StringHelper.hasNoText(sLastName)) {
      if (isLastNameMandatory())
        aFormErrors.addFieldError(
            FIELD_LASTNAME, EText.ERROR_LASTNAME_REQUIRED.getDisplayText(aDisplayLocale));
    }

    if (StringHelper.hasNoText(sEmailAddress)) {
      if (isEmailMandatory())
        aFormErrors.addFieldError(
            FIELD_EMAILADDRESS, EText.ERROR_EMAIL_REQUIRED.getDisplayText(aDisplayLocale));
    } else if (!EmailAddressUtils.isValid(sEmailAddress))
      aFormErrors.addFieldError(
          FIELD_EMAILADDRESS, EText.ERROR_EMAIL_INVALID.getDisplayText(aDisplayLocale));
    else {
      final IUser aSameLoginUser = aAccessMgr.getUserOfLoginName(sEmailAddress);
      if (aSameLoginUser != null)
        if (!bEdit || !aSameLoginUser.equals(aSelectedObject))
          aFormErrors.addFieldError(
              FIELD_EMAILADDRESS, EText.ERROR_EMAIL_IN_USE.getDisplayText(aDisplayLocale));
    }

    if (!bEdit) {
      final List<String> aPasswordErrors =
          GlobalPasswordSettings.getPasswordConstraintList()
              .getInvalidPasswordDescriptions(sPassword, aDisplayLocale);
      for (final String sPasswordError : aPasswordErrors)
        aFormErrors.addFieldError(FIELD_PASSWORD, sPasswordError);
      if (!EqualsUtils.equals(sPassword, sPasswordConf))
        aFormErrors.addFieldError(
            FIELD_PASSWORD_CONFIRM,
            EText.ERROR_PASSWORDS_DONT_MATCH.getDisplayText(aDisplayLocale));
    }

    if (ContainerHelper.isEmpty(aUserGroupIDs))
      aFormErrors.addFieldError(
          FIELD_USERGROUPS, EText.ERROR_NO_USERGROUP.getDisplayText(aDisplayLocale));
    else if (!aAccessMgr.containsAllUserGroupsWithID(aUserGroupIDs))
      aFormErrors.addFieldError(
          FIELD_USERGROUPS, EText.ERROR_INVALID_USERGROUPS.getDisplayText(aDisplayLocale));

    // Call custom method
    final Map<String, String> aCustomAttrMap =
        validateCustomParameters(aWPEC, aSelectedObject, aFormErrors, bEdit);

    if (aFormErrors.isEmpty()) {
      // All fields are valid -> save
      if (bEdit) {
        final String sUserID = aSelectedObject.getID();

        final Map<String, Object> aAttrMap = aSelectedObject.getAllAttributes();
        if (aCustomAttrMap != null) aAttrMap.putAll(aCustomAttrMap);

        // We're editing an existing object
        aAccessMgr.setUserData(
            sUserID,
            sLoginName,
            sEmailAddress,
            sFirstName,
            sLastName,
            m_aDefaultUserLocale,
            aAttrMap,
            !bEnabled);
        aNodeList.addChild(
            getStyler().createSuccessBox(EText.SUCCESS_EDIT.getDisplayText(aDisplayLocale)));

        // assign to the matching user groups
        final Collection<String> aPrevUserGroupIDs =
            aAccessMgr.getAllUserGroupIDsWithAssignedUser(sUserID);
        // Create all missing assignments
        final Set<String> aUserGroupsToBeAssigned =
            ContainerHelper.getDifference(aUserGroupIDs, aPrevUserGroupIDs);
        for (final String sUserGroupID : aUserGroupsToBeAssigned)
          aAccessMgr.assignUserToUserGroup(sUserGroupID, sUserID);

        // Delete all old assignments
        final Set<String> aUserGroupsToBeUnassigned =
            ContainerHelper.getDifference(aPrevUserGroupIDs, aUserGroupIDs);
        for (final String sUserGroupID : aUserGroupsToBeUnassigned)
          aAccessMgr.unassignUserFromUserGroup(sUserGroupID, sUserID);

      } else {
        // We're creating a new object
        final IUser aNewUser =
            aAccessMgr.createNewUser(
                sLoginName,
                sEmailAddress,
                sPassword,
                sFirstName,
                sLastName,
                m_aDefaultUserLocale,
                aCustomAttrMap,
                !bEnabled);
        if (aNewUser != null) {
          aNodeList.addChild(
              getStyler().createSuccessBox(EText.SUCCESS_CREATE.getDisplayText(aDisplayLocale)));

          // assign to the matching internal user groups
          for (final String sUserGroupID : aUserGroupIDs)
            aAccessMgr.assignUserToUserGroup(sUserGroupID, aNewUser.getID());
        } else
          aNodeList.addChild(
              getStyler().createErrorBox(EText.FAILURE_CREATE.getDisplayText(aDisplayLocale)));
      }
    }
  }
  /**
   * Copy a directory including all child objects.
   *
   * @param aSourceDir The source directory to be copied. May not be <code>null</code>.
   * @param aTargetDir The destination directory where to be copied. This directory may not be
   *     existing. May not be <code>null</code>.
   * @return A non-<code>null</code> error code.
   */
  @Nonnull
  public static FileIOError copyDirRecursive(
      @Nonnull final File aSourceDir, @Nonnull final File aTargetDir) {
    ValueEnforcer.notNull(aSourceDir, "SourceDirectory");
    ValueEnforcer.notNull(aTargetDir, "TargetDirectory");

    // Does the source directory exist?
    if (!FileUtils.existsDir(aSourceDir))
      return EFileIOErrorCode.SOURCE_DOES_NOT_EXIST.getAsIOError(
          EFileIOOperation.COPY_DIR_RECURSIVE, aSourceDir);

    // Are source and target different?
    if (EqualsUtils.equals(aSourceDir, aTargetDir))
      return EFileIOErrorCode.SOURCE_EQUALS_TARGET.getAsIOError(
          EFileIOOperation.COPY_DIR_RECURSIVE, aSourceDir);

    // Is the source a parent of target?
    if (FileUtils.isParentDirectory(aSourceDir, aTargetDir))
      return EFileIOErrorCode.TARGET_IS_CHILD_OF_SOURCE.getAsIOError(
          EFileIOOperation.COPY_DIR_RECURSIVE, aSourceDir, aTargetDir);

    // Does the target directory already exist?
    if (aTargetDir.exists())
      return EFileIOErrorCode.TARGET_ALREADY_EXISTS.getAsIOError(
          EFileIOOperation.COPY_DIR_RECURSIVE, aTargetDir);

    // Is the source directory readable?
    if (!FileUtils.canRead(aSourceDir))
      return EFileIOErrorCode.SOURCE_NOT_READABLE.getAsIOError(
          EFileIOOperation.COPY_DIR_RECURSIVE, aSourceDir);

    // Is the target parent directory writable?
    final File aTargetParentDir = aTargetDir.getParentFile();
    if (aTargetParentDir != null
        && aTargetParentDir.exists()
        && !FileUtils.canWrite(aTargetParentDir))
      return EFileIOErrorCode.TARGET_PARENT_NOT_WRITABLE.getAsIOError(
          EFileIOOperation.COPY_DIR_RECURSIVE, aTargetDir);

    FileIOError eCode;

    // Ensure the targets parent directory is present
    eCode = createDirRecursive(aTargetDir);
    if (eCode.isFailure()) return eCode;

    for (final File aChild : FileUtils.getDirectoryContent(aSourceDir)) {
      if (aChild.isDirectory()) {
        // Skip "." and ".."
        if (FilenameHelper.isSystemInternalDirectory(aChild.getName())) continue;

        // Copy directory
        eCode = copyDirRecursive(aChild, new File(aTargetDir, aChild.getName()));
        if (eCode.isFailure()) return eCode;
      } else if (aChild.isFile()) {
        // Copy a file
        eCode = copyFile(aChild, new File(aTargetDir, aChild.getName()));
        if (eCode.isFailure()) return eCode;
      } else {
        // Neither directory not file - don't know how to handle
        return EFileIOErrorCode.OBJECT_CANNOT_BE_HANDLED.getAsIOError(
            EFileIOOperation.COPY_DIR_RECURSIVE, aChild);
      }
    }

    // Done
    return EFileIOErrorCode.NO_ERROR.getAsIOError(
        EFileIOOperation.COPY_DIR_RECURSIVE, aSourceDir, aTargetDir);
  }
  @Override
  protected boolean handleCustomActions(
      @Nonnull final WebPageExecutionContext aWPEC, @Nullable final IUser aSelectedObject) {
    if (aWPEC.hasAction(ACTION_RESET_PASSWORD) && aSelectedObject != null) {
      if (!canResetPassword(aSelectedObject)) throw new IllegalStateException("Won't work!");

      final Locale aDisplayLocale = aWPEC.getDisplayLocale();
      final boolean bShowForm = true;
      final FormErrors aFormErrors = new FormErrors();
      if (aWPEC.hasSubAction(ACTION_PERFORM)) {
        final String sPlainTextPassword = aWPEC.getAttr(FIELD_PASSWORD);
        final String sPlainTextPasswordConfirm = aWPEC.getAttr(FIELD_PASSWORD_CONFIRM);

        final List<String> aPasswordErrors =
            GlobalPasswordSettings.getPasswordConstraintList()
                .getInvalidPasswordDescriptions(sPlainTextPassword, aDisplayLocale);
        for (final String sPasswordError : aPasswordErrors)
          aFormErrors.addFieldError(FIELD_PASSWORD, sPasswordError);
        if (!EqualsUtils.equals(sPlainTextPassword, sPlainTextPasswordConfirm))
          aFormErrors.addFieldError(
              FIELD_PASSWORD_CONFIRM,
              EText.ERROR_PASSWORDS_DONT_MATCH.getDisplayText(aDisplayLocale));

        if (aFormErrors.isEmpty()) {
          AccessManager.getInstance().setUserPassword(aSelectedObject.getID(), sPlainTextPassword);
          aWPEC
              .getNodeList()
              .addChild(
                  getStyler()
                      .createSuccessBox(
                          EText.SUCCESS_RESET_PASSWORD.getDisplayTextWithArgs(
                              aDisplayLocale,
                              SecurityUI.getUserDisplayName(aSelectedObject, aDisplayLocale))));
          return true;
        }
      }
      if (bShowForm) {
        // Show input form
        final boolean bHasAnyPasswordConstraint =
            GlobalPasswordSettings.getPasswordConstraintList().hasConstraints();
        final HCForm aForm = aWPEC.getNodeList().addAndReturnChild(createFormSelf());
        final IHCTableForm<?> aTable =
            aForm.addAndReturnChild(
                getStyler().createTableForm(new HCCol(200), HCCol.star(), new HCCol(20)));
        aTable.setSpanningHeaderContent(
            EText.TITLE_RESET_PASSWORD.getDisplayTextWithArgs(
                aDisplayLocale, SecurityUI.getUserDisplayName(aSelectedObject, aDisplayLocale)));

        final String sPassword = EText.LABEL_PASSWORD.getDisplayText(aDisplayLocale);
        aTable
            .createItemRow()
            .setLabel(
                sPassword, bHasAnyPasswordConstraint ? ELabelType.MANDATORY : ELabelType.OPTIONAL)
            .setCtrl(new HCEditPassword(FIELD_PASSWORD).setPlaceholder(sPassword))
            .setNote(SecurityUI.createPasswordConstraintTip(aDisplayLocale))
            .setErrorList(aFormErrors.getListOfField(FIELD_PASSWORD));

        final String sPasswordConfirm = EText.LABEL_PASSWORD_CONFIRM.getDisplayText(aDisplayLocale);
        aTable
            .createItemRow()
            .setLabel(
                sPasswordConfirm,
                bHasAnyPasswordConstraint ? ELabelType.MANDATORY : ELabelType.OPTIONAL)
            .setCtrl(new HCEditPassword(FIELD_PASSWORD_CONFIRM).setPlaceholder(sPasswordConfirm))
            .setNote(SecurityUI.createPasswordConstraintTip(aDisplayLocale))
            .setErrorList(aFormErrors.getListOfField(FIELD_PASSWORD_CONFIRM));

        final IButtonToolbar<?> aToolbar = aForm.addAndReturnChild(getStyler().createToolbar());
        aToolbar.addHiddenField(CHCParam.PARAM_ACTION, ACTION_RESET_PASSWORD);
        aToolbar.addHiddenField(CHCParam.PARAM_OBJECT, aSelectedObject.getID());
        aToolbar.addHiddenField(CHCParam.PARAM_SUBACTION, ACTION_PERFORM);
        aToolbar.addSubmitButtonSave(aDisplayLocale);
        aToolbar.addButtonCancel(aDisplayLocale);
      }
      return false;
    }
    return true;
  }
 @Nonnull
 public EChange set(@Nonnull final BigInteger aValue) {
   if (EqualsUtils.equals(aValue, m_aValue)) return EChange.UNCHANGED;
   m_aValue = aValue;
   return EChange.CHANGED;
 }