Search in sources :

Example 16 with CaMgmtException

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

the class CaManagerQueryExecutor method addRequestor.

// method addCmpControl
void addRequestor(RequestorEntry dbEntry) throws CaMgmtException {
    ParamUtil.requireNonNull("dbEntry", dbEntry);
    try {
        int id = (int) datasource.getMax(null, "REQUESTOR", "ID");
        dbEntry.getIdent().setId(id + 1);
    } catch (DataAccessException ex) {
        throw new CaMgmtException(ex);
    }
    final String sql = "INSERT INTO REQUESTOR (ID,NAME,CERT) VALUES (?,?,?)";
    PreparedStatement ps = null;
    try {
        ps = prepareStatement(sql);
        int idx = 1;
        ps.setInt(idx++, dbEntry.getIdent().getId());
        ps.setString(idx++, dbEntry.getIdent().getName());
        ps.setString(idx++, Base64.encodeToString(dbEntry.getCert().getEncoded()));
        if (ps.executeUpdate() == 0) {
            throw new CaMgmtException("could not add requestor " + dbEntry.getIdent());
        }
        if (LOG.isInfoEnabled()) {
            LOG.info("added requestor '{}': {}", dbEntry.getIdent(), dbEntry.toString(false));
        }
    } catch (SQLException ex) {
        throw new CaMgmtException(datasource, sql, ex);
    } catch (CertificateEncodingException ex) {
        throw new CaMgmtException(ex);
    } finally {
        datasource.releaseResources(ps, null);
    }
}
Also used : CaMgmtException(org.xipki.ca.server.mgmt.api.CaMgmtException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) CertificateEncodingException(java.security.cert.CertificateEncodingException) DataAccessException(org.xipki.datasource.DataAccessException)

Example 17 with CaMgmtException

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

the class CaManagerQueryExecutor method addResponder.

// method revokeCa
void addResponder(ResponderEntry dbEntry) throws CaMgmtException {
    ParamUtil.requireNonNull("dbEntry", dbEntry);
    final String sql = "INSERT INTO RESPONDER (NAME,TYPE,CERT,CONF) VALUES (?,?,?,?)";
    PreparedStatement ps = null;
    try {
        ps = prepareStatement(sql);
        int idx = 1;
        ps.setString(idx++, dbEntry.getName());
        ps.setString(idx++, dbEntry.getType());
        ps.setString(idx++, dbEntry.getBase64Cert());
        ps.setString(idx++, dbEntry.getConf());
        if (ps.executeUpdate() == 0) {
            throw new CaMgmtException("could not add responder " + dbEntry.getName());
        }
        LOG.info("added responder: {}", dbEntry.toString(false, true));
    } 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 18 with CaMgmtException

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

the class CaManagerQueryExecutor method addCertprofileToCa.

// method addCertprofile
void addCertprofileToCa(NameId profile, NameId ca) throws CaMgmtException {
    ParamUtil.requireNonNull("profile", profile);
    ParamUtil.requireNonNull("ca", ca);
    final String sql = "INSERT INTO CA_HAS_PROFILE (CA_ID,PROFILE_ID) VALUES (?,?)";
    PreparedStatement ps = null;
    try {
        ps = prepareStatement(sql);
        ps.setInt(1, ca.getId());
        ps.setInt(2, profile.getId());
        if (ps.executeUpdate() == 0) {
            throw new CaMgmtException("could not add profile " + profile + " to CA " + ca);
        }
        LOG.info("added profile '{}' to CA '{}'", profile, 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 19 with CaMgmtException

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

the class CaManagerQueryExecutor method createCaHasProfiles.

// method createCaHasRequestors
Set<Integer> createCaHasProfiles(NameId ca) throws CaMgmtException {
    final String sql = "SELECT PROFILE_ID FROM CA_HAS_PROFILE WHERE CA_ID=?";
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        stmt = prepareStatement(sql);
        stmt.setInt(1, ca.getId());
        rs = stmt.executeQuery();
        Set<Integer> ret = new HashSet<>();
        while (rs.next()) {
            int id = rs.getInt("PROFILE_ID");
            ret.add(id);
        }
        return ret;
    } catch (SQLException ex) {
        throw new CaMgmtException(datasource, sql, ex);
    } finally {
        datasource.releaseResources(stmt, rs);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CaMgmtException(org.xipki.ca.server.mgmt.api.CaMgmtException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) HashSet(java.util.HashSet)

Example 20 with CaMgmtException

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

the class CaManagerQueryExecutor method changePublisher.

// method changeEnvParam
IdentifiedX509CertPublisher changePublisher(String name, String type, String conf, CaManagerImpl caManager) throws CaMgmtException {
    ParamUtil.requireNonBlank("name", name);
    ParamUtil.requireNonNull("caManager", caManager);
    StringBuilder sqlBuilder = new StringBuilder();
    sqlBuilder.append("UPDATE PUBLISHER SET ");
    String tmpType = type;
    String tmpConf = conf;
    AtomicInteger index = new AtomicInteger(1);
    Integer idxType = addToSqlIfNotNull(sqlBuilder, index, tmpType, "TYPE");
    Integer idxConf = addToSqlIfNotNull(sqlBuilder, index, tmpConf, "CONF");
    sqlBuilder.deleteCharAt(sqlBuilder.length() - 1);
    sqlBuilder.append(" WHERE NAME=?");
    if (index.get() == 1) {
        throw new IllegalArgumentException("nothing to change");
    }
    PublisherEntry currentDbEntry = createPublisher(name);
    if (tmpType == null) {
        tmpType = currentDbEntry.getType();
    }
    if (tmpConf == null) {
        tmpConf = currentDbEntry.getConf();
    }
    PublisherEntry dbEntry = new PublisherEntry(currentDbEntry.getIdent(), tmpType, tmpConf);
    IdentifiedX509CertPublisher publisher = caManager.createPublisher(dbEntry);
    if (publisher == null) {
        throw new CaMgmtException("could not create publisher object");
    }
    final String sql = sqlBuilder.toString();
    PreparedStatement ps = null;
    try {
        StringBuilder sb = new StringBuilder();
        ps = prepareStatement(sql);
        if (idxType != null) {
            sb.append("type: '").append(tmpType).append("'; ");
            ps.setString(idxType, tmpType);
        }
        if (idxConf != null) {
            String txt = getRealString(tmpConf);
            sb.append("conf: '").append(txt).append("'; ");
            ps.setString(idxConf, getRealString(tmpConf));
        }
        ps.setString(index.get(), name);
        if (ps.executeUpdate() == 0) {
            throw new CaMgmtException("could not change publisher " + name);
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1).deleteCharAt(sb.length() - 1);
        }
        LOG.info("changed publisher '{}': {}", name, sb);
        return publisher;
    } catch (SQLException ex) {
        throw new CaMgmtException(datasource, sql, ex);
    } finally {
        datasource.releaseResources(ps, null);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CaMgmtException(org.xipki.ca.server.mgmt.api.CaMgmtException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PublisherEntry(org.xipki.ca.server.mgmt.api.PublisherEntry) 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