use of com.helger.commons.state.EChange in project phoss-smp by phax.
the class SMPServerAPI method deleteServiceRegistrations.
public void deleteServiceRegistrations(@Nonnull final String sPathServiceGroupID, @Nonnull final BasicAuthClientCredentials aCredentials) throws SMPServerException {
final String sLog = LOG_PREFIX + "DELETE /" + sPathServiceGroupID + "/services/";
final String sAction = "deleteServiceRegistrations";
if (LOGGER.isInfoEnabled())
LOGGER.info(sLog);
STATS_COUNTER_INVOCATION.increment(sAction);
try {
final IIdentifierFactory aIdentifierFactory = SMPMetaManager.getIdentifierFactory();
final IParticipantIdentifier aPathServiceGroupID = aIdentifierFactory.parseParticipantIdentifier(sPathServiceGroupID);
if (aPathServiceGroupID == null) {
// Invalid identifier
throw SMPBadRequestException.failedToParseSG(sPathServiceGroupID, m_aAPIDataProvider.getCurrentURI());
}
final IUser aSMPUser = SMPUserManagerPhoton.validateUserCredentials(aCredentials);
SMPUserManagerPhoton.verifyOwnership(aPathServiceGroupID, aSMPUser);
final ISMPServiceGroupManager aServiceGroupMgr = SMPMetaManager.getServiceGroupMgr();
final ISMPServiceGroup aServiceGroup = aServiceGroupMgr.getSMPServiceGroupOfID(aPathServiceGroupID);
if (aServiceGroup == null) {
throw new SMPNotFoundException("Service Group '" + sPathServiceGroupID + "' is not on this SMP", m_aAPIDataProvider.getCurrentURI());
}
final ISMPServiceInformationManager aServiceInfoMgr = SMPMetaManager.getServiceInformationMgr();
EChange eChange = aServiceInfoMgr.deleteAllSMPServiceInformationOfServiceGroup(aServiceGroup);
final ISMPRedirectManager aRedirectMgr = SMPMetaManager.getRedirectMgr();
eChange = eChange.or(aRedirectMgr.deleteAllSMPRedirectsOfServiceGroup(aServiceGroup));
if (LOGGER.isInfoEnabled())
LOGGER.info(sLog + " SUCCESS - " + eChange);
STATS_COUNTER_SUCCESS.increment(sAction);
} catch (final SMPServerException ex) {
if (LOGGER.isWarnEnabled())
LOGGER.warn(sLog + " ERROR - " + ex.getMessage());
STATS_COUNTER_ERROR.increment(sAction);
throw ex;
}
}
use of com.helger.commons.state.EChange 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.commons.state.EChange 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;
}
use of com.helger.commons.state.EChange in project phoss-smp by phax.
the class SMPServiceInformationManagerJDBC method mergeSMPServiceInformation.
@Nonnull
public ESuccess mergeSMPServiceInformation(@Nonnull final ISMPServiceInformation aSMPServiceInformation) {
ValueEnforcer.notNull(aSMPServiceInformation, "ServiceInformation");
final MutableBoolean aUpdated = new MutableBoolean(false);
final DBExecutor aExecutor = newExecutor();
final ESuccess eSuccess = aExecutor.performInTransaction(() -> {
// Simply delete the old one
final EChange eDeleted = _deleteSMPServiceInformationNoCallback(aSMPServiceInformation);
aUpdated.set(eDeleted.isChanged());
// Insert new processes
final IParticipantIdentifier aPID = aSMPServiceInformation.getServiceGroup().getParticipantIdentifier();
final IDocumentTypeIdentifier aDocTypeID = aSMPServiceInformation.getDocumentTypeIdentifier();
aExecutor.insertOrUpdateOrDelete("INSERT INTO smp_service_metadata (businessIdentifierScheme, businessIdentifier, documentIdentifierScheme, documentIdentifier, extension) VALUES (?, ?, ?, ?, ?)", new ConstantPreparedStatementDataProvider(aPID.getScheme(), aPID.getValue(), aDocTypeID.getScheme(), aDocTypeID.getValue(), aSMPServiceInformation.getExtensionsAsString()));
for (final ISMPProcess aProcess : aSMPServiceInformation.getAllProcesses()) {
final IProcessIdentifier aProcessID = aProcess.getProcessIdentifier();
aExecutor.insertOrUpdateOrDelete("INSERT INTO smp_process (businessIdentifierScheme, businessIdentifier, documentIdentifierScheme, documentIdentifier, processIdentifierType, processIdentifier, extension) VALUES (?, ?, ?, ?, ?, ?, ?)", new ConstantPreparedStatementDataProvider(aPID.getScheme(), aPID.getValue(), aDocTypeID.getScheme(), aDocTypeID.getValue(), aProcessID.getScheme(), aProcessID.getValue(), aProcess.getExtensionsAsString()));
// Insert new endpoints
for (final ISMPEndpoint aEndpoint : aProcess.getAllEndpoints()) {
aExecutor.insertOrUpdateOrDelete("INSERT INTO smp_endpoint (businessIdentifierScheme, businessIdentifier, documentIdentifierScheme, documentIdentifier, processIdentifierType, processIdentifier," + " certificate, endpointReference, minimumAuthenticationLevel, requireBusinessLevelSignature, serviceActivationDate, serviceDescription, serviceExpirationDate, technicalContactUrl, technicalInformationUrl, transportProfile," + " extension) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new ConstantPreparedStatementDataProvider(aPID.getScheme(), aPID.getValue(), aDocTypeID.getScheme(), aDocTypeID.getValue(), aProcessID.getScheme(), aProcessID.getValue(), aEndpoint.getCertificate(), aEndpoint.getEndpointReference(), aEndpoint.getMinimumAuthenticationLevel(), Boolean.valueOf(aEndpoint.isRequireBusinessLevelSignature()), DBValueHelper.toTimestamp(aEndpoint.getServiceActivationDateTime()), aEndpoint.getServiceDescription(), DBValueHelper.toTimestamp(aEndpoint.getServiceExpirationDateTime()), aEndpoint.getTechnicalContactUrl(), aEndpoint.getTechnicalInformationUrl(), aEndpoint.getTransportProfile(), aEndpoint.getExtensionsAsString()));
}
}
});
if (eSuccess.isFailure())
return ESuccess.FAILURE;
// Callback outside of transaction
if (aUpdated.booleanValue()) {
AuditHelper.onAuditModifySuccess(SMPServiceInformation.OT, "set-all", aSMPServiceInformation.getID(), aSMPServiceInformation.getServiceGroupID(), aSMPServiceInformation.getDocumentTypeIdentifier().getURIEncoded(), aSMPServiceInformation.getAllProcesses(), aSMPServiceInformation.getExtensionsAsString());
m_aCBs.forEach(x -> x.onSMPServiceInformationUpdated(aSMPServiceInformation));
} else {
AuditHelper.onAuditCreateSuccess(SMPServiceInformation.OT, aSMPServiceInformation.getID(), aSMPServiceInformation.getServiceGroupID(), aSMPServiceInformation.getDocumentTypeIdentifier().getURIEncoded(), aSMPServiceInformation.getAllProcesses(), aSMPServiceInformation.getExtensionsAsString());
m_aCBs.forEach(x -> x.onSMPServiceInformationCreated(aSMPServiceInformation));
}
return ESuccess.SUCCESS;
}
use of com.helger.commons.state.EChange in project phoss-smp by phax.
the class SMPServiceInformationManagerMongoDB method deleteSMPProcess.
@Nonnull
public EChange deleteSMPProcess(@Nullable final ISMPServiceInformation aSMPServiceInformation, @Nullable final ISMPProcess aProcess) {
if (LOGGER.isDebugEnabled())
LOGGER.debug("deleteSMPProcess (" + aSMPServiceInformation + ", " + aProcess + ")");
if (aSMPServiceInformation == null || aProcess == null) {
if (LOGGER.isDebugEnabled())
LOGGER.debug("deleteSMPProcess - failure");
return EChange.UNCHANGED;
}
// Find implementation object
final SMPServiceInformation aRealServiceInformation = getCollection().find(new Document(BSON_ID, aSMPServiceInformation.getID())).map(x -> toServiceInformation(x, true)).first();
if (aRealServiceInformation == null) {
AuditHelper.onAuditDeleteFailure(SMPServiceInformation.OT, aSMPServiceInformation.getID(), "no-such-id");
if (LOGGER.isDebugEnabled())
LOGGER.debug("deleteSMPProcess - failure");
return EChange.UNCHANGED;
}
// Main deletion in write lock
if (aRealServiceInformation.deleteProcess(aProcess).isUnchanged()) {
AuditHelper.onAuditDeleteFailure(SMPServiceInformation.OT, aSMPServiceInformation.getID(), aProcess.getProcessIdentifier().getURIEncoded(), "no-such-process");
if (LOGGER.isDebugEnabled())
LOGGER.debug("deleteSMPProcess - failure");
return EChange.UNCHANGED;
}
// Save new one
getCollection().replaceOne(new Document(BSON_ID, aSMPServiceInformation.getID()), toBson(aRealServiceInformation));
AuditHelper.onAuditDeleteSuccess(SMPServiceInformation.OT, aSMPServiceInformation.getID(), aProcess.getProcessIdentifier().getURIEncoded());
if (LOGGER.isDebugEnabled())
LOGGER.debug("deleteSMPProcess - success");
return EChange.CHANGED;
}
Aggregations