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();
}
}
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));
}
}
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);
}
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();
}
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;
}
Aggregations