use of com.helger.commons.mutable.MutableBoolean in project phoss-smp by phax.
the class SMPServiceGroupManagerJDBC method createSMPServiceGroup.
@Nonnull
public SMPServiceGroup createSMPServiceGroup(@Nonnull @Nonempty final String sOwnerID, @Nonnull final IParticipantIdentifier aParticipantID, @Nullable final String sExtension, final boolean bCreateInSML) throws SMPServerException {
ValueEnforcer.notEmpty(sOwnerID, "OwnerID");
ValueEnforcer.notNull(aParticipantID, "ParticipantID");
if (LOGGER.isDebugEnabled())
LOGGER.debug("createSMPServiceGroup (" + sOwnerID + ", " + aParticipantID.getURIEncoded() + ", " + (StringHelper.hasText(sExtension) ? "with extension" : "without extension") + ", " + bCreateInSML + ")");
final MutableBoolean aCreatedSGHook = new MutableBoolean(false);
final MutableBoolean aCreatedSGDB = new MutableBoolean(false);
final IRegistrationHook aHook = RegistrationHookFactory.getInstance();
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 IllegalStateException("The service group with ID " + aParticipantID.getURIEncoded() + " already exists!");
if (bCreateInSML) {
// It's a new service group - Create in SML and remember that
// Throws exception in case of an error
aHook.createServiceGroup(aParticipantID);
aCreatedSGHook.set(true);
}
// Did not exist. Create it.
if (aExecutor.insertOrUpdateOrDelete("INSERT INTO smp_service_group (businessIdentifierScheme, businessIdentifier, extension) VALUES (?, ?, ?)", new ConstantPreparedStatementDataProvider(aParticipantID.getScheme(), aParticipantID.getValue(), sExtension)) > 0) {
aCreatedSGDB.set(true);
aExecutor.insertOrUpdateOrDelete("INSERT INTO smp_ownership (businessIdentifierScheme, businessIdentifier, username) VALUES (?, ?, ?)", new ConstantPreparedStatementDataProvider(aParticipantID.getScheme(), aParticipantID.getValue(), sOwnerID));
}
}, aCaughtException::set);
if (aCreatedSGHook.booleanValue() && !aCreatedSGDB.booleanValue()) {
// Undo creation in SML again
try {
aHook.undoCreateServiceGroup(aParticipantID);
} catch (final RegistrationHookException ex) {
LOGGER.error("Failed to undoCreateServiceGroup (" + aParticipantID.getURIEncoded() + ")", ex);
}
}
if (eSuccess.isFailure() || aCaughtException.isSet() || !aCreatedSGDB.booleanValue()) {
AuditHelper.onAuditCreateFailure(SMPServiceGroup.OT, aParticipantID.getURIEncoded(), sOwnerID, sExtension, Boolean.valueOf(bCreateInSML));
// Propagate contained exception
final Exception ex = aCaughtException.get();
if (ex instanceof SMPServerException)
throw (SMPServerException) ex;
if (ex instanceof RegistrationHookException)
throw new SMPSMLException("Failed to create '" + aParticipantID.getURIEncoded() + "' in SML", ex);
throw new SMPInternalErrorException("Error creating ServiceGroup '" + aParticipantID.getURIEncoded() + "'", ex);
}
if (LOGGER.isDebugEnabled())
LOGGER.debug("createSMPServiceGroup succeeded");
AuditHelper.onAuditCreateSuccess(SMPServiceGroup.OT, aParticipantID.getURIEncoded(), sOwnerID, sExtension, Boolean.valueOf(bCreateInSML));
final SMPServiceGroup aServiceGroup = new SMPServiceGroup(sOwnerID, aParticipantID, sExtension);
if (m_aCache != null)
m_aCache.put(aParticipantID.getURIEncoded(), aServiceGroup);
m_aCBs.forEach(x -> x.onSMPServiceGroupCreated(aServiceGroup, bCreateInSML));
return aServiceGroup;
}
use of com.helger.commons.mutable.MutableBoolean 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.commons.mutable.MutableBoolean 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.mutable.MutableBoolean 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.mutable.MutableBoolean in project phoss-smp by phax.
the class SMPBusinessCardManagerJDBC method createOrUpdateSMPBusinessCard.
@Nullable
public ISMPBusinessCard createOrUpdateSMPBusinessCard(@Nonnull final IParticipantIdentifier aParticipantID, @Nonnull final Collection<SMPBusinessCardEntity> aEntities) {
ValueEnforcer.notNull(aParticipantID, "ParticipantID");
ValueEnforcer.notNull(aEntities, "Entities");
if (LOGGER.isDebugEnabled())
LOGGER.debug("createOrUpdateSMPBusinessCard (" + aParticipantID.getURIEncoded() + ", " + aEntities.size() + " entities" + ")");
final MutableBoolean aUpdated = new MutableBoolean(false);
final DBExecutor aExecutor = newExecutor();
final ESuccess eSucces = aExecutor.performInTransaction(() -> {
// Delete all existing entities
final String sPID = aParticipantID.getURIEncoded();
final long nDeleted = aExecutor.insertOrUpdateOrDelete("DELETE FROM smp_bce" + " WHERE pid=?", new ConstantPreparedStatementDataProvider(sPID));
if (nDeleted > 0) {
aUpdated.set(true);
if (LOGGER.isDebugEnabled())
LOGGER.info("Deleted " + nDeleted + " existing DBBusinessCardEntity rows");
}
for (final SMPBusinessCardEntity aEntity : aEntities) {
// Single name only
aExecutor.insertOrUpdateOrDelete("INSERT INTO smp_bce (id, pid, name, country, geoinfo, identifiers, websites, contacts, addon, regdate) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new ConstantPreparedStatementDataProvider(aEntity.getID(), sPID, aEntity.names().getFirst().getName(), aEntity.getCountryCode(), aEntity.getGeographicalInformation(), getBCIAsJson(aEntity.identifiers()).getAsJsonString(JWS), getStringAsJson(aEntity.websiteURIs()).getAsJsonString(JWS), getBCCAsJson(aEntity.contacts()).getAsJsonString(JWS), aEntity.getAdditionalInformation(), aEntity.getRegistrationDate()));
}
});
if (eSucces.isFailure()) {
if (aUpdated.booleanValue())
AuditHelper.onAuditModifyFailure(SMPBusinessCard.OT, "set-all", aParticipantID.getURIEncoded());
else
AuditHelper.onAuditCreateFailure(SMPBusinessCard.OT, aParticipantID.getURIEncoded());
return null;
}
final SMPBusinessCard aNewBusinessCard = new SMPBusinessCard(aParticipantID, aEntities);
if (LOGGER.isDebugEnabled())
LOGGER.debug("Finished createOrUpdateSMPBusinessCard");
if (aUpdated.booleanValue())
AuditHelper.onAuditModifySuccess(SMPBusinessCard.OT, "set-all", aParticipantID.getURIEncoded(), Integer.valueOf(aEntities.size()));
else
AuditHelper.onAuditCreateSuccess(SMPBusinessCard.OT, aParticipantID.getURIEncoded(), Integer.valueOf(aEntities.size()));
// Invoke generic callbacks
m_aCBs.forEach(x -> x.onSMPBusinessCardCreatedOrUpdated(aNewBusinessCard));
return aNewBusinessCard;
}
Aggregations