use of org.xipki.datasource.DataAccessException in project xipki by xipki.
the class CaManagerQueryExecutor method addCa.
// method deleteRows
void addCa(CaEntry caEntry) throws CaMgmtException {
ParamUtil.requireNonNull("caEntry", caEntry);
if (!(caEntry instanceof X509CaEntry)) {
throw new CaMgmtException("unsupported CAEntry " + caEntry.getClass().getName());
}
try {
int id = (int) datasource.getMax(null, "CA", "ID");
caEntry.getIdent().setId(id + 1);
} catch (DataAccessException ex) {
throw new CaMgmtException(ex);
}
X509CaEntry entry = (X509CaEntry) caEntry;
final String sql = "INSERT INTO CA (ID,NAME,ART,SUBJECT,SN_SIZE,NEXT_CRLNO,STATUS,CRL_URIS," + "DELTACRL_URIS,OCSP_URIS,CACERT_URIS,MAX_VALIDITY,CERT,SIGNER_TYPE,CRLSIGNER_NAME," + "RESPONDER_NAME,CMPCONTROL_NAME,DUPLICATE_KEY,DUPLICATE_SUBJECT,SAVE_REQ,PERMISSION," + "NUM_CRLS,EXPIRATION_PERIOD,KEEP_EXPIRED_CERT_DAYS,VALIDITY_MODE,EXTRA_CONTROL," + "SIGNER_CONF) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
// insert to table ca
PreparedStatement ps = null;
try {
ps = prepareStatement(sql);
int idx = 1;
ps.setInt(idx++, entry.getIdent().getId());
ps.setString(idx++, entry.getIdent().getName());
ps.setInt(idx++, CertArt.X509PKC.getCode());
ps.setString(idx++, entry.getSubject());
ps.setInt(idx++, entry.getSerialNoBitLen());
ps.setLong(idx++, entry.getNextCrlNumber());
ps.setString(idx++, entry.getStatus().getStatus());
ps.setString(idx++, entry.getCrlUrisAsString());
ps.setString(idx++, entry.getDeltaCrlUrisAsString());
ps.setString(idx++, entry.getOcspUrisAsString());
ps.setString(idx++, entry.getCaCertUrisAsString());
ps.setString(idx++, entry.getMaxValidity().toString());
byte[] encodedCert = entry.getCert().getEncoded();
ps.setString(idx++, Base64.encodeToString(encodedCert));
ps.setString(idx++, entry.getSignerType());
ps.setString(idx++, entry.getCrlSignerName());
ps.setString(idx++, entry.getResponderName());
ps.setString(idx++, entry.getCmpControlName());
setBoolean(ps, idx++, entry.isDuplicateKeyPermitted());
setBoolean(ps, idx++, entry.isDuplicateSubjectPermitted());
setBoolean(ps, idx++, entry.isSaveRequest());
ps.setInt(idx++, entry.getPermission());
ps.setInt(idx++, entry.getNumCrls());
ps.setInt(idx++, entry.getExpirationPeriod());
ps.setInt(idx++, entry.getKeepExpiredCertInDays());
ps.setString(idx++, entry.getValidityMode().name());
ConfPairs extraControl = entry.getExtraControl();
String encodedExtraCtrl = (extraControl == null) ? null : extraControl.getEncoded();
if (StringUtil.isBlank(encodedExtraCtrl)) {
ps.setString(idx++, null);
} else {
ps.setString(idx++, encodedExtraCtrl);
}
ps.setString(idx++, entry.getSignerConf());
if (ps.executeUpdate() == 0) {
throw new CaMgmtException("could not add CA " + entry.getIdent());
}
if (LOG.isInfoEnabled()) {
LOG.info("add CA '{}': {}", entry.getIdent(), entry.toString(false, true));
}
} catch (SQLException ex) {
throw new CaMgmtException(datasource, sql, ex);
} catch (CertificateEncodingException ex) {
throw new CaMgmtException(ex);
} finally {
datasource.releaseResources(ps, null);
}
}
use of org.xipki.datasource.DataAccessException in project xipki by xipki.
the class CaManagerQueryExecutor method addUser.
private void addUser(String name, boolean active, String hashedPassword) throws CaMgmtException {
Integer existingId = getIdForName(sqls.sqlSelectUserId, name);
if (existingId != null) {
throw new CaMgmtException(concat("user named '", name, " ' already exists"));
}
long id;
try {
long maxId = datasource.getMax(null, "TUSER", "ID");
id = maxId + 1;
} catch (DataAccessException ex) {
throw new CaMgmtException(ex);
}
final String sql = "INSERT INTO TUSER (ID,NAME,ACTIVE,PASSWORD) VALUES (?,?,?,?)";
PreparedStatement ps = null;
try {
ps = prepareStatement(sql);
int idx = 1;
ps.setLong(idx++, id);
ps.setString(idx++, name);
setBoolean(ps, idx++, active);
ps.setString(idx++, hashedPassword);
if (ps.executeUpdate() == 0) {
throw new CaMgmtException("could not add user " + name);
}
} catch (SQLException ex) {
throw new CaMgmtException(datasource, sql, ex);
} finally {
datasource.releaseResources(ps, null);
}
LOG.info("added user '{}'", name);
}
use of org.xipki.datasource.DataAccessException in project xipki by xipki.
the class CaManagerQueryExecutor method addUserToCa.
// method removeRequestorFromCa
void addUserToCa(CaHasUserEntry user, NameId ca) throws CaMgmtException {
ParamUtil.requireNonNull("user", user);
ParamUtil.requireNonNull("ca", ca);
final NameId userIdent = user.getUserIdent();
Integer existingId = getIdForName(sqls.sqlSelectUserId, userIdent.getName());
if (existingId == null) {
throw new CaMgmtException(concat("user '", userIdent.getName(), " ' does not exist"));
}
userIdent.setId(existingId);
PreparedStatement ps = null;
final String sql = "INSERT INTO CA_HAS_USER (ID,CA_ID,USER_ID, PERMISSION,PROFILES)" + " VALUES (?,?,?,?,?)";
long maxId;
try {
maxId = datasource.getMax(null, "CA_HAS_USER", "ID");
} catch (DataAccessException ex) {
throw new CaMgmtException(ex);
}
try {
ps = prepareStatement(sql);
int idx = 1;
ps.setLong(idx++, maxId + 1);
ps.setInt(idx++, ca.getId());
ps.setInt(idx++, userIdent.getId());
ps.setInt(idx++, user.getPermission());
String profilesText = StringUtil.collectionAsStringByComma(user.getProfiles());
ps.setString(idx++, profilesText);
if (ps.executeUpdate() == 0) {
throw new CaMgmtException("could not add user " + userIdent + " to CA " + ca);
}
LOG.info("added user '{}' to CA '{}': permission: {}; profile: {}", userIdent, ca, user.getPermission(), profilesText);
} catch (SQLException ex) {
throw new CaMgmtException(datasource, sql, ex);
} finally {
datasource.releaseResources(ps, null);
}
}
use of org.xipki.datasource.DataAccessException in project xipki by xipki.
the class CaManagerQueryExecutor method addRequestorIfNeeded.
// method addRequestor
void addRequestorIfNeeded(String requestorName) throws CaMgmtException {
String sql = sqls.sqlSelectRequestorId;
ResultSet rs = null;
PreparedStatement stmt = null;
try {
stmt = prepareStatement(sql);
stmt.setString(1, requestorName);
rs = stmt.executeQuery();
if (rs.next()) {
return;
}
datasource.releaseResources(stmt, rs);
stmt = null;
rs = null;
int id = (int) datasource.getMax(null, "REQUESTOR", "ID");
sql = "INSERT INTO REQUESTOR (ID,NAME) VALUES (?,?)";
stmt = prepareStatement(sql);
stmt.setInt(1, id + 1);
stmt.setString(2, requestorName);
stmt.executeUpdate();
LOG.info("added requestor '{}'", requestorName);
} catch (SQLException ex) {
throw new CaMgmtException(datasource, sql, ex);
} catch (DataAccessException ex) {
throw new CaMgmtException(ex);
} finally {
datasource.releaseResources(stmt, rs);
}
}
use of org.xipki.datasource.DataAccessException in project xipki by xipki.
the class ResponseCacher method storeOcspResponse.
void storeOcspResponse(int issuerId, BigInteger serialNumber, long thisUpdate, Long nextUpdate, AlgorithmCode sigAlgCode, byte[] response) {
byte[] identBytes = buildIdent(serialNumber, sigAlgCode);
String ident = Base64.encodeToString(identBytes);
try {
long id = deriveId(issuerId, identBytes);
Connection conn = datasource.getConnection();
try {
String sql = SQL_ADD_RESP;
PreparedStatement ps = datasource.prepareStatement(conn, sql);
String b64Response = Base64.encodeToString(response);
Boolean dataIntegrityViolationException = null;
try {
int idx = 1;
ps.setLong(idx++, id);
ps.setInt(idx++, issuerId);
ps.setString(idx++, ident);
ps.setLong(idx++, thisUpdate);
if (nextUpdate != null && nextUpdate > 0) {
ps.setLong(idx++, nextUpdate);
} else {
ps.setNull(idx++, java.sql.Types.BIGINT);
}
ps.setString(idx++, b64Response);
ps.execute();
} catch (SQLException ex) {
DataAccessException dex = datasource.translate(sql, ex);
if (dex.getReason().isDescendantOrSelfOf(Reason.DataIntegrityViolation)) {
dataIntegrityViolationException = Boolean.TRUE;
} else {
throw dex;
}
} finally {
datasource.releaseResources(ps, null, false);
}
if (dataIntegrityViolationException == null) {
LOG.debug("added cached OCSP response iid={}, ident={}", issuerId, ident);
return;
}
sql = SQL_UPDATE_RESP;
ps = datasource.prepareStatement(conn, sql);
try {
int idx = 1;
ps.setLong(idx++, thisUpdate);
if (nextUpdate != null && nextUpdate > 0) {
ps.setLong(idx++, nextUpdate);
} else {
ps.setNull(idx++, java.sql.Types.BIGINT);
}
ps.setString(idx++, b64Response);
ps.setLong(idx++, id);
ps.executeUpdate();
} catch (SQLException ex) {
throw datasource.translate(sql, ex);
} finally {
datasource.releaseResources(ps, null, false);
}
} finally {
datasource.returnConnection(conn);
}
} catch (DataAccessException ex) {
LOG.info("could not cache OCSP response iid={}, ident={}", issuerId, ident);
if (LOG.isDebugEnabled()) {
LOG.debug("could not cache OCSP response iid=" + issuerId + ", ident=" + ident, ex);
}
}
}
Aggregations