use of com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider in project phoss-smp by phax.
the class SMPRedirectManagerJDBC method deleteAllSMPRedirectsOfServiceGroup.
@Nonnull
public EChange deleteAllSMPRedirectsOfServiceGroup(@Nullable final ISMPServiceGroup aServiceGroup) {
if (aServiceGroup == null)
return EChange.UNCHANGED;
// Remember all existing
final ICommonsList<ISMPRedirect> aDeletedRedirects = getAllSMPRedirectsOfServiceGroup(aServiceGroup);
// Now delete
final IParticipantIdentifier aParticipantID = aServiceGroup.getParticipantIdentifier();
final long nDeleted = newExecutor().insertOrUpdateOrDelete("DELETE FROM smp_service_metadata_red" + " WHERE businessIdentifierScheme=? AND businessIdentifier=?", new ConstantPreparedStatementDataProvider(aParticipantID.getScheme(), aParticipantID.getValue()));
if (nDeleted == 0) {
return EChange.UNCHANGED;
}
// Callback only, if all were deleted
if (nDeleted == aDeletedRedirects.size()) {
for (final ISMPRedirect aSMPRedirect : aDeletedRedirects) {
AuditHelper.onAuditDeleteSuccess(SMPRedirect.OT, aSMPRedirect.getID(), aSMPRedirect.getServiceGroupID(), aSMPRedirect.getDocumentTypeIdentifier().getURIEncoded());
m_aCallbacks.forEach(x -> x.onSMPRedirectDeleted(aSMPRedirect));
}
} else {
LOGGER.warn(nDeleted + " SMP redirects were deleted, but " + aDeletedRedirects.size() + " were found previously. Because of this inconsistency, no callbacks are triggered");
}
return EChange.CHANGED;
}
use of com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider in project phoss-smp by phax.
the class SMPRedirectManagerJDBC method createOrUpdateSMPRedirect.
@Nullable
public ISMPRedirect createOrUpdateSMPRedirect(@Nonnull final ISMPServiceGroup aServiceGroup, @Nonnull final IDocumentTypeIdentifier aDocTypeID, @Nonnull @Nonempty final String sRedirectUrl, @Nonnull @Nonempty final String sSubjectUniqueIdentifier, @Nullable final X509Certificate aCertificate, @Nullable final String sExtension) {
ValueEnforcer.notNull(aServiceGroup, "ServiceGroup");
ValueEnforcer.notNull(aDocTypeID, "DocumentTypeIdentifier");
final MutableBoolean aCreatedNew = new MutableBoolean(true);
final DBExecutor aExecutor = newExecutor();
final ESuccess eSuccess = aExecutor.performInTransaction(() -> {
final ISMPRedirect aDBRedirect = getSMPRedirectOfServiceGroupAndDocumentType(aServiceGroup, aDocTypeID);
final IParticipantIdentifier aParticipantID = aServiceGroup.getParticipantIdentifier();
final String sCertificate = aCertificate == null ? null : CertificateHelper.getPEMEncodedCertificate(aCertificate);
if (aDBRedirect == null) {
// Create new
final long nCreated = aExecutor.insertOrUpdateOrDelete("INSERT INTO smp_service_metadata_red (businessIdentifierScheme, businessIdentifier, documentIdentifierScheme, documentIdentifier, redirectionUrl, certificateUID, certificate, extension) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", new ConstantPreparedStatementDataProvider(aParticipantID.getScheme(), aParticipantID.getValue(), aDocTypeID.getScheme(), aDocTypeID.getValue(), sRedirectUrl, sSubjectUniqueIdentifier, sCertificate, sExtension));
if (nCreated != 1)
throw new IllegalStateException("Failed to create new DB entry (" + nCreated + ")");
aCreatedNew.set(true);
} else {
// Update existing
final long nUpdated = aExecutor.insertOrUpdateOrDelete("UPDATE smp_service_metadata_red" + " SET redirectionUrl=?, certificateUID=?, certificate=?, extension=?" + " WHERE businessIdentifierScheme=? AND businessIdentifier=? AND documentIdentifierScheme=? AND documentIdentifier=?", new ConstantPreparedStatementDataProvider(sRedirectUrl, sSubjectUniqueIdentifier, sCertificate, sExtension, aParticipantID.getScheme(), aParticipantID.getValue(), aDocTypeID.getScheme(), aDocTypeID.getValue()));
if (nUpdated != 1)
throw new IllegalStateException("Failed to update existing DB entry (" + nUpdated + ")");
aCreatedNew.set(false);
}
});
if (eSuccess.isFailure()) {
return null;
}
final SMPRedirect aSMPRedirect = new SMPRedirect(aServiceGroup, aDocTypeID, sRedirectUrl, sSubjectUniqueIdentifier, aCertificate, sExtension);
if (aCreatedNew.booleanValue()) {
AuditHelper.onAuditCreateSuccess(SMPRedirect.OT, aSMPRedirect.getID(), aSMPRedirect.getServiceGroupID(), aSMPRedirect.getDocumentTypeIdentifier().getURIEncoded(), aSMPRedirect.getTargetHref(), aSMPRedirect.getSubjectUniqueIdentifier(), aSMPRedirect.getCertificate(), aSMPRedirect.getExtensionsAsString());
m_aCallbacks.forEach(x -> x.onSMPRedirectCreated(aSMPRedirect));
} else {
AuditHelper.onAuditModifySuccess(SMPRedirect.OT, "set-all", aSMPRedirect.getID(), aSMPRedirect.getServiceGroupID(), aSMPRedirect.getDocumentTypeIdentifier().getURIEncoded(), aSMPRedirect.getTargetHref(), aSMPRedirect.getSubjectUniqueIdentifier(), aSMPRedirect.getCertificate(), aSMPRedirect.getExtensionsAsString());
m_aCallbacks.forEach(x -> x.onSMPRedirectUpdated(aSMPRedirect));
}
return aSMPRedirect;
}
use of com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider in project phoss-smp by phax.
the class SMPServiceGroupManagerJDBC method getSMPServiceGroupOfID.
@Nullable
public SMPServiceGroup getSMPServiceGroupOfID(@Nullable final IParticipantIdentifier aParticipantID) {
if (LOGGER.isDebugEnabled())
LOGGER.debug("getSMPServiceGroupOfID(" + (aParticipantID == null ? "null" : aParticipantID.getURIEncoded()) + ")");
if (aParticipantID == null)
return null;
// Use cache
SMPServiceGroup ret = m_aCache == null ? null : m_aCache.get(aParticipantID.getURIEncoded());
if (ret != null)
return ret;
// Not in cache
final Wrapper<DBResultRow> aResult = new Wrapper<>();
newExecutor().querySingle("SELECT sg.extension, so.username" + " FROM smp_service_group sg, smp_ownership so" + " WHERE sg.businessIdentifierScheme=? AND sg.businessIdentifier=?" + " AND so.businessIdentifierScheme=sg.businessIdentifierScheme AND so.businessIdentifier=sg.businessIdentifier", new ConstantPreparedStatementDataProvider(aParticipantID.getScheme(), aParticipantID.getValue()), aResult::set);
if (aResult.isNotSet())
return null;
ret = new SMPServiceGroup(aResult.get().getAsString(1), aParticipantID, aResult.get().getAsString(0));
if (m_aCache != null)
m_aCache.put(aParticipantID.getURIEncoded(), ret);
return ret;
}
use of com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider in project phoss-smp by phax.
the class SMPServiceGroupManagerJDBC method updateSMPServiceGroup.
@Nonnull
public EChange updateSMPServiceGroup(@Nonnull final IParticipantIdentifier aParticipantID, @Nonnull @Nonempty final String sNewOwnerID, @Nullable final String sNewExtension) throws SMPServerException {
ValueEnforcer.notNull(aParticipantID, "ParticipantID");
ValueEnforcer.notEmpty(sNewOwnerID, "NewOwnerID");
if (LOGGER.isDebugEnabled())
LOGGER.debug("updateSMPServiceGroup (" + aParticipantID.getURIEncoded() + ", " + sNewOwnerID + ", " + (StringHelper.hasText(sNewExtension) ? "with extension" : "without extension") + ")");
final Wrapper<EChange> aWrappedChange = new Wrapper<>(EChange.UNCHANGED);
final Wrapper<Exception> aCaughtException = new Wrapper<>();
final DBExecutor aExecutor = newExecutor();
final ESuccess eSuccess = aExecutor.performInTransaction(() -> {
// Check if the passed service group ID is already in use
final SMPServiceGroup aDBServiceGroup = getSMPServiceGroupOfID(aParticipantID);
if (aDBServiceGroup == null)
throw new SMPNotFoundException("The service group with ID " + aParticipantID.getURIEncoded() + " does not exist!");
if (!EqualsHelper.equals(sNewOwnerID, aDBServiceGroup.getOwnerID())) {
// Update ownership
final long nCount = aExecutor.insertOrUpdateOrDelete("UPDATE smp_ownership SET username=? WHERE businessIdentifierScheme=? AND businessIdentifier=?", new ConstantPreparedStatementDataProvider(sNewOwnerID, aDBServiceGroup.getParticipantIdentifier().getScheme(), aDBServiceGroup.getParticipantIdentifier().getValue()));
if (nCount != 1)
throw new IllegalStateException("Failed to update the ownership username to '" + sNewOwnerID + "'");
aWrappedChange.set(EChange.CHANGED);
}
if (!EqualsHelper.equals(sNewExtension, aDBServiceGroup.getExtensionsAsString())) {
// Update extension
final long nCount = aExecutor.insertOrUpdateOrDelete("UPDATE smp_service_group SET extension=? WHERE businessIdentifierScheme=? AND businessIdentifier=?", new ConstantPreparedStatementDataProvider(sNewExtension, aDBServiceGroup.getParticipantIdentifier().getScheme(), aDBServiceGroup.getParticipantIdentifier().getValue()));
if (nCount != 1)
throw new IllegalStateException("Failed to update the service_group extension to '" + sNewExtension + "'");
aWrappedChange.set(EChange.CHANGED);
}
}, aCaughtException::set);
if (eSuccess.isFailure() || aCaughtException.isSet()) {
AuditHelper.onAuditModifyFailure(SMPServiceGroup.OT, "set-all", aParticipantID.getURIEncoded(), sNewOwnerID, sNewExtension);
final Exception ex = aCaughtException.get();
if (ex instanceof SMPServerException)
throw (SMPServerException) ex;
throw new SMPInternalErrorException("Failed to update ServiceGroup '" + aParticipantID.getURIEncoded() + "'", ex);
}
final EChange eChange = aWrappedChange.get();
if (LOGGER.isDebugEnabled())
LOGGER.debug("updateSMPServiceGroup succeeded. Change=" + eChange.isChanged());
AuditHelper.onAuditModifySuccess(SMPServiceGroup.OT, "set-all", aParticipantID.getURIEncoded(), sNewOwnerID, sNewExtension);
// Callback only if something changed
if (eChange.isChanged())
m_aCBs.forEach(x -> x.onSMPServiceGroupUpdated(aParticipantID));
return eChange;
}
use of com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider in project phoss-smp by phax.
the class SMPServiceGroupManagerJDBC method deleteSMPServiceGroup.
@Nonnull
public EChange deleteSMPServiceGroup(@Nonnull final IParticipantIdentifier aParticipantID, final boolean bDeleteInSML) throws SMPServerException {
ValueEnforcer.notNull(aParticipantID, "ParticipantID");
if (LOGGER.isDebugEnabled())
LOGGER.debug("deleteSMPServiceGroup (" + aParticipantID.getURIEncoded() + ")");
final IRegistrationHook aHook = RegistrationHookFactory.getInstance();
final MutableBoolean aDeletedServiceGroupInSML = new MutableBoolean(false);
final Wrapper<EChange> aWrappedChange = new Wrapper<>(EChange.UNCHANGED);
final Wrapper<Exception> aCaughtException = new Wrapper<>();
final DBExecutor aExecutor = newExecutor();
final ESuccess eSuccess = aExecutor.performInTransaction(() -> {
// Check if the passed service group ID is already in use
final SMPServiceGroup aDBServiceGroup = getSMPServiceGroupOfID(aParticipantID);
if (aDBServiceGroup == null)
throw new SMPNotFoundException("The service group with ID " + aParticipantID.getURIEncoded() + " does not exist!");
if (bDeleteInSML) {
// Delete in SML - and remember that
// throws exception in case of error
aHook.deleteServiceGroup(aParticipantID);
aDeletedServiceGroupInSML.set(true);
}
final long nCount = aExecutor.insertOrUpdateOrDelete("DELETE FROM smp_service_group" + " WHERE businessIdentifierScheme=? AND businessIdentifier=?", new ConstantPreparedStatementDataProvider(aParticipantID.getScheme(), aParticipantID.getValue()));
if (nCount != 1)
throw new IllegalStateException("Failed to delete service group");
aWrappedChange.set(EChange.CHANGED);
}, aCaughtException::set);
if (eSuccess.isFailure()) {
// Error writing to the DB
if (bDeleteInSML && aDeletedServiceGroupInSML.booleanValue()) {
// Undo deletion in SML!
try {
aHook.undoDeleteServiceGroup(aParticipantID);
} catch (final RegistrationHookException ex) {
LOGGER.error("Failed to undoDeleteServiceGroup (" + aParticipantID.getURIEncoded() + ")", ex);
}
}
}
if (aCaughtException.isSet()) {
AuditHelper.onAuditDeleteFailure(SMPServiceGroup.OT, aParticipantID.getURIEncoded(), "database-error");
final Exception ex = aCaughtException.get();
if (ex instanceof SMPServerException)
throw (SMPServerException) ex;
if (ex instanceof RegistrationHookException)
throw new SMPSMLException("Failed to delete '" + aParticipantID.getURIEncoded() + "' in SML", ex);
throw new SMPInternalErrorException("Failed to delete ServiceGroup '" + aParticipantID.getURIEncoded() + "'", ex);
}
final EChange eChange = aWrappedChange.get();
if (LOGGER.isDebugEnabled())
LOGGER.debug("deleteSMPServiceGroup succeeded. Change=" + eChange.isChanged());
if (eChange.isChanged()) {
AuditHelper.onAuditDeleteSuccess(SMPServiceGroup.OT, aParticipantID.getURIEncoded());
if (m_aCache != null)
m_aCache.remove(aParticipantID.getURIEncoded());
m_aCBs.forEach(x -> x.onSMPServiceGroupDeleted(aParticipantID, bDeleteInSML));
}
return eChange;
}
Aggregations