Search in sources :

Example 31 with CaMgmtException

use of org.xipki.ca.server.mgmt.api.CaMgmtException in project xipki by xipki.

the class CaManagerQueryExecutor method addCaAlias.

// method addCa
void addCaAlias(String aliasName, NameId ca) throws CaMgmtException {
    ParamUtil.requireNonNull("aliasName", aliasName);
    ParamUtil.requireNonNull("ca", ca);
    final String sql = "INSERT INTO CAALIAS (NAME,CA_ID) VALUES (?,?)";
    PreparedStatement ps = null;
    try {
        ps = prepareStatement(sql);
        ps.setString(1, aliasName);
        ps.setInt(2, ca.getId());
        if (ps.executeUpdate() == 0) {
            throw new CaMgmtException("could not add CA alias " + aliasName);
        }
        LOG.info("added CA alias '{}' for CA '{}'", aliasName, ca);
    } catch (SQLException ex) {
        throw new CaMgmtException(datasource, sql, ex);
    } finally {
        datasource.releaseResources(ps, null);
    }
}
Also used : CaMgmtException(org.xipki.ca.server.mgmt.api.CaMgmtException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement)

Example 32 with CaMgmtException

use of org.xipki.ca.server.mgmt.api.CaMgmtException in project xipki by xipki.

the class CaManagerQueryExecutor method getUser.

UserEntry getUser(String username, boolean nullable) throws CaMgmtException {
    ParamUtil.requireNonNull("username", username);
    NameId ident = new NameId(null, username);
    final String sql = sqls.sqlSelectUser;
    ResultSet rs = null;
    PreparedStatement ps = null;
    try {
        ps = prepareStatement(sql);
        int idx = 1;
        ps.setString(idx++, ident.getName());
        rs = ps.executeQuery();
        if (!rs.next()) {
            if (nullable) {
                return null;
            } else {
                throw new CaMgmtException("unknown user " + username);
            }
        }
        int id = rs.getInt("ID");
        ident.setId(id);
        boolean active = rs.getBoolean("ACTIVE");
        String hashedPassword = rs.getString("PASSWORD");
        return new UserEntry(ident, active, hashedPassword);
    } catch (SQLException ex) {
        throw new CaMgmtException(datasource, sql, ex);
    } finally {
        datasource.releaseResources(ps, rs);
    }
}
Also used : CaMgmtException(org.xipki.ca.server.mgmt.api.CaMgmtException) NameId(org.xipki.ca.api.NameId) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) CaHasUserEntry(org.xipki.ca.server.mgmt.api.CaHasUserEntry) ChangeUserEntry(org.xipki.ca.server.mgmt.api.ChangeUserEntry) AddUserEntry(org.xipki.ca.server.mgmt.api.AddUserEntry) UserEntry(org.xipki.ca.server.mgmt.api.UserEntry)

Example 33 with CaMgmtException

use of org.xipki.ca.server.mgmt.api.CaMgmtException in project xipki by xipki.

the class CaManagerQueryExecutor method getCaHasUsersForUser.

// method addUserToCa
Map<String, CaHasUserEntry> getCaHasUsersForUser(String user, CaIdNameMap idNameMap) throws CaMgmtException {
    Integer existingId = getIdForName(sqls.sqlSelectUserId, user);
    if (existingId == null) {
        throw new CaMgmtException(concat("user '", user, " ' does not exist"));
    }
    final String sql = "SELECT CA_ID,PERMISSION,PROFILES FROM CA_HAS_USER WHERE USER_ID=?";
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        ps = prepareStatement(sql);
        ps.setInt(1, existingId);
        rs = ps.executeQuery();
        Map<String, CaHasUserEntry> ret = new HashMap<>();
        while (rs.next()) {
            int permission = rs.getInt("PERMISSION");
            String str = rs.getString("PROFILES");
            List<String> list = StringUtil.splitByComma(str);
            Set<String> profiles = (list == null) ? null : new HashSet<>(list);
            CaHasUserEntry caHasUser = new CaHasUserEntry(new NameId(existingId, user));
            caHasUser.setPermission(permission);
            caHasUser.setProfiles(profiles);
            int caId = rs.getInt("CA_ID");
            String caName = idNameMap.getCaName(caId);
            ret.put(caName, caHasUser);
        }
        return ret;
    } catch (SQLException ex) {
        throw new CaMgmtException(datasource, sql, ex);
    } finally {
        datasource.releaseResources(ps, rs);
    }
}
Also used : CaHasUserEntry(org.xipki.ca.server.mgmt.api.CaHasUserEntry) HashMap(java.util.HashMap) NameId(org.xipki.ca.api.NameId) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CaMgmtException(org.xipki.ca.server.mgmt.api.CaMgmtException) ResultSet(java.sql.ResultSet)

Example 34 with CaMgmtException

use of org.xipki.ca.server.mgmt.api.CaMgmtException in project xipki by xipki.

the class CaManagerQueryExecutor method createResponder.

// method createCmpControl
ResponderEntry createResponder(String name) throws CaMgmtException {
    final String sql = sqls.sqlSelectResponder;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        stmt = prepareStatement(sql);
        stmt.setString(1, name);
        rs = stmt.executeQuery();
        if (!rs.next()) {
            throw new CaMgmtException("unknown responder " + name);
        }
        String type = rs.getString("TYPE");
        String conf = rs.getString("CONF");
        String b64Cert = rs.getString("CERT");
        return new ResponderEntry(name, type, conf, b64Cert);
    } catch (SQLException ex) {
        throw new CaMgmtException(datasource, sql, ex);
    } finally {
        datasource.releaseResources(stmt, rs);
    }
}
Also used : CaMgmtException(org.xipki.ca.server.mgmt.api.CaMgmtException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ResponderEntry(org.xipki.ca.server.mgmt.api.ResponderEntry)

Example 35 with CaMgmtException

use of org.xipki.ca.server.mgmt.api.CaMgmtException in project xipki by xipki.

the class CaManagerQueryExecutor method addEnvParam.

private void addEnvParam(String name, String value, boolean force) throws CaMgmtException {
    ParamUtil.requireNonBlank("name", name);
    ParamUtil.requireNonNull("value", value);
    if (!force) {
        if (CaManagerImpl.ENV_EPOCH.equalsIgnoreCase(name)) {
            throw new CaMgmtException("environment " + name + " is reserved");
        }
    }
    final String sql = "INSERT INTO ENVIRONMENT (NAME,VALUE2) VALUES (?,?)";
    PreparedStatement ps = null;
    try {
        ps = prepareStatement(sql);
        ps.setString(1, name);
        ps.setString(2, value);
        if (ps.executeUpdate() == 0) {
            throw new CaMgmtException("could not add environment param " + name);
        }
        LOG.info("added environment param '{}': {}", name, value);
    } catch (SQLException ex) {
        throw new CaMgmtException(datasource, sql, ex);
    } finally {
        datasource.releaseResources(ps, null);
    }
}
Also used : CaMgmtException(org.xipki.ca.server.mgmt.api.CaMgmtException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement)

Aggregations

CaMgmtException (org.xipki.ca.server.mgmt.api.CaMgmtException)157 PreparedStatement (java.sql.PreparedStatement)63 SQLException (java.sql.SQLException)63 CmdFailure (org.xipki.console.karaf.CmdFailure)52 NameId (org.xipki.ca.api.NameId)31 ResultSet (java.sql.ResultSet)24 OperationException (org.xipki.ca.api.OperationException)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)16 InvalidConfException (org.xipki.common.InvalidConfException)11 DataAccessException (org.xipki.datasource.DataAccessException)11 CertificateEncodingException (java.security.cert.CertificateEncodingException)9 CaHasRequestorEntry (org.xipki.ca.server.mgmt.api.CaHasRequestorEntry)9 CertificateException (java.security.cert.CertificateException)8 ObjectCreationException (org.xipki.common.ObjectCreationException)8 X509Certificate (java.security.cert.X509Certificate)7 Date (java.util.Date)7 X509CaEntry (org.xipki.ca.server.mgmt.api.x509.X509CaEntry)7 IOException (java.io.IOException)6 Statement (java.sql.Statement)6 CaHasUserEntry (org.xipki.ca.server.mgmt.api.CaHasUserEntry)6