Search in sources :

Example 56 with ErrorInformation

use of password.pwm.error.ErrorInformation in project pwm by pwm-project.

the class AbstractJDBCLocalDB method removeAll.

public void removeAll(final LocalDB.DB db, final Collection<String> keys) throws LocalDBException {
    preCheck(true);
    final String sqlString = "DELETE FROM " + db.toString() + " WHERE " + KEY_COLUMN + "=?";
    PreparedStatement statement = null;
    try {
        lock.writeLock().lock();
        try {
            statement = dbConnection.prepareStatement(sqlString);
            for (final String loopKey : keys) {
                statement.clearParameters();
                statement.setString(1, loopKey);
                statement.addBatch();
            }
            statement.executeBatch();
            dbConnection.commit();
        } catch (final SQLException ex) {
            throw new LocalDBException(new ErrorInformation(PwmError.ERROR_LOCALDB_UNAVAILABLE, ex.getMessage()));
        } finally {
            close(statement);
        }
    } finally {
        lock.writeLock().unlock();
    }
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement)

Example 57 with ErrorInformation

use of password.pwm.error.ErrorInformation in project pwm by pwm-project.

the class DerbyLocalDB method openConnection.

@Override
Connection openConnection(final File databaseDirectory, final String driverClasspath, final Map<String, String> initOptions) throws LocalDBException {
    final String filePath = databaseDirectory.getAbsolutePath() + File.separator + "derby-db";
    final String baseConnectionURL = "jdbc:derby:" + filePath;
    final String connectionURL = baseConnectionURL + ";create=true";
    try {
        // load driver.
        driver = (Driver) Class.forName(driverClasspath).newInstance();
        final Connection connection = driver.connect(connectionURL, new Properties());
        connection.setAutoCommit(false);
        if (aggressiveCompact) {
            reclaimAllSpace(connection);
        }
        return connection;
    } catch (Throwable e) {
        final String errorMsg;
        if (e instanceof SQLException) {
            final SQLException sqlException = (SQLException) e;
            final SQLException nextException = sqlException.getNextException();
            if (nextException != null) {
                if ("XSDB6".equals(nextException.getSQLState())) {
                    errorMsg = "unable to open LocalDB, the LocalDB is already opened in a different instance: " + nextException.getMessage();
                } else {
                    errorMsg = "unable to open LocalDB, error=" + e.getMessage() + ", nextError=" + nextException.getMessage();
                }
            } else {
                errorMsg = "unable to open LocalDB, error=" + e.getMessage();
            }
        } else {
            errorMsg = "error opening DB: " + e.getMessage();
        }
        LOGGER.error(errorMsg, e);
        throw new LocalDBException(new ErrorInformation(PwmError.ERROR_LOCALDB_UNAVAILABLE, errorMsg));
    }
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) SQLException(java.sql.SQLException) Connection(java.sql.Connection) Properties(java.util.Properties)

Example 58 with ErrorInformation

use of password.pwm.error.ErrorInformation in project pwm by pwm-project.

the class JDBCDriverLoader method loadDriver.

static DriverWrapper loadDriver(final PwmApplication pwmApplication, final DBConfiguration dbConfiguration) throws DatabaseException {
    final List<ClassLoaderStrategy> strategies = dbConfiguration.getClassLoaderStrategies();
    LOGGER.trace("attempting to load jdbc driver using strategies: " + JsonUtil.serializeCollection(strategies));
    final List<String> errorMsgs = new ArrayList<>();
    for (final ClassLoaderStrategy strategy : strategies) {
        final DriverLoader loader = strategy.getJdbcDriverDriverLoader();
        try {
            final Driver driver = loader.loadDriver(pwmApplication, dbConfiguration);
            if (driver != null) {
                return new DriverWrapper(driver, loader);
            }
        } catch (DatabaseException e) {
            errorMsgs.add(strategy + " error: " + e.getMessage());
        }
    }
    final String errorMsg = " unable to load database driver: " + JsonUtil.serializeCollection(errorMsgs);
    final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_DB_UNAVAILABLE, errorMsg);
    LOGGER.error(errorMsg);
    throw new DatabaseException(errorInformation);
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) ArrayList(java.util.ArrayList) Driver(java.sql.Driver)

Example 59 with ErrorInformation

use of password.pwm.error.ErrorInformation in project pwm by pwm-project.

the class FormUtility method ldapSearchFilterForForm.

public static String ldapSearchFilterForForm(final PwmApplication pwmApplication, final Collection<FormConfiguration> formElements) throws PwmUnrecoverableException {
    if (formElements == null || formElements.isEmpty()) {
        final String errorMsg = "can not auto-generate ldap search filter for form with no required form items";
        final ErrorInformation errorInformation = new ErrorInformation(PwmError.CONFIG_FORMAT_ERROR, null, new String[] { errorMsg });
        throw new PwmUnrecoverableException(errorInformation);
    }
    final StringBuilder sb = new StringBuilder();
    sb.append("(&");
    final List<String> objectClasses = pwmApplication.getConfig().readSettingAsStringArray(PwmSetting.DEFAULT_OBJECT_CLASSES);
    if (objectClasses != null && !objectClasses.isEmpty()) {
        if (objectClasses.size() == 1) {
            sb.append("(objectclass=");
            sb.append(objectClasses.iterator().next());
            sb.append(")");
        } else {
            sb.append("(|");
            for (final String objectClassValue : objectClasses) {
                sb.append("(objectclass=");
                sb.append(objectClassValue);
                sb.append(")");
            }
            sb.append(")");
        }
    }
    for (final FormConfiguration formConfiguration : formElements) {
        final String formElementName = formConfiguration.getName();
        sb.append("(");
        sb.append(formElementName);
        sb.append("=");
        sb.append("%").append(formElementName).append("%");
        sb.append(")");
    }
    sb.append(")");
    return sb.toString();
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) FormConfiguration(password.pwm.config.value.data.FormConfiguration)

Example 60 with ErrorInformation

use of password.pwm.error.ErrorInformation in project pwm by pwm-project.

the class FormUtility method readFormValuesFromMap.

public static Map<FormConfiguration, String> readFormValuesFromMap(final Map<String, String> inputMap, final Collection<FormConfiguration> formItems, final Locale locale) throws PwmDataValidationException, PwmUnrecoverableException {
    if (formItems == null || formItems.isEmpty()) {
        return Collections.emptyMap();
    }
    final Map<FormConfiguration, String> returnMap = new LinkedHashMap<>();
    if (inputMap == null) {
        return returnMap;
    }
    for (final FormConfiguration formItem : formItems) {
        final String keyName = formItem.getName();
        final String value = inputMap.get(keyName);
        if (formItem.isRequired() && !formItem.isReadonly()) {
            if (StringUtil.isEmpty(value)) {
                final String errorMsg = "missing required value for field '" + formItem.getName() + "'";
                final ErrorInformation error = new ErrorInformation(PwmError.ERROR_FIELD_REQUIRED, errorMsg, new String[] { formItem.getLabel(locale) });
                throw new PwmDataValidationException(error);
            }
        }
        if (formItem.isConfirmationRequired()) {
            final String confirmValue = inputMap.get(keyName + Validator.PARAM_CONFIRM_SUFFIX);
            if (confirmValue == null || !confirmValue.equals(value)) {
                final String errorMsg = "incorrect confirmation value for field '" + formItem.getName() + "'";
                final ErrorInformation error = new ErrorInformation(PwmError.ERROR_FIELD_BAD_CONFIRM, errorMsg, new String[] { formItem.getLabel(locale) });
                throw new PwmDataValidationException(error);
            }
        }
        if (formItem.getType() == FormConfiguration.Type.checkbox) {
            final String parsedValue = parseInputValueToFormValue(formItem, value);
            returnMap.put(formItem, parsedValue);
        } else if (value != null && !formItem.isReadonly()) {
            final String parsedValue = parseInputValueToFormValue(formItem, value);
            returnMap.put(formItem, parsedValue);
        }
    }
    return returnMap;
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) PwmDataValidationException(password.pwm.error.PwmDataValidationException) FormConfiguration(password.pwm.config.value.data.FormConfiguration) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

ErrorInformation (password.pwm.error.ErrorInformation)325 PwmUnrecoverableException (password.pwm.error.PwmUnrecoverableException)216 PwmOperationalException (password.pwm.error.PwmOperationalException)125 PwmException (password.pwm.error.PwmException)67 UserIdentity (password.pwm.bean.UserIdentity)62 IOException (java.io.IOException)58 PwmApplication (password.pwm.PwmApplication)54 ChaiUnavailableException (com.novell.ldapchai.exception.ChaiUnavailableException)53 ChaiUser (com.novell.ldapchai.ChaiUser)38 PwmSession (password.pwm.http.PwmSession)38 LinkedHashMap (java.util.LinkedHashMap)35 Configuration (password.pwm.config.Configuration)33 ChaiOperationException (com.novell.ldapchai.exception.ChaiOperationException)32 Map (java.util.Map)32 Instant (java.time.Instant)30 ArrayList (java.util.ArrayList)30 FormConfiguration (password.pwm.config.value.data.FormConfiguration)29 ServletException (javax.servlet.ServletException)28 RestResultBean (password.pwm.ws.server.RestResultBean)26 UserInfo (password.pwm.ldap.UserInfo)23