use of com.helger.phoss.smp.exception.SMPServerException 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.phoss.smp.exception.SMPServerException 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.phoss.smp.exception.SMPServerException in project phoss-smp by phax.
the class APIExecutorMigrationInboundFromPathPut method migrationInbound.
public static void migrationInbound(@Nonnull final String sServiceGroupID, @Nonnull final String sMigrationKey, @Nonnull final String sLogPrefix, @Nonnull final IRequestWebScopeWithoutResponse aRequestScope, @Nonnull final UnifiedResponse aUnifiedResponse) throws SMPServerException, GeneralSecurityException {
LOGGER.info(sLogPrefix + "Starting inbound migration for Service Group ID '" + sServiceGroupID + "' and migration key '" + sMigrationKey + "'");
// Only authenticated user may do so
final BasicAuthClientCredentials aBasicAuth = getMandatoryAuth(aRequestScope.headers());
final IUser aOwningUser = SMPUserManagerPhoton.validateUserCredentials(aBasicAuth);
final ISMPServerAPIDataProvider aDataProvider = new SMPRestDataProvider(aRequestScope, sServiceGroupID);
final ISMPSettings aSettings = SMPMetaManager.getSettings();
final ISMLInfo aSMLInfo = aSettings.getSMLInfo();
final IIdentifierFactory aIdentifierFactory = SMPMetaManager.getIdentifierFactory();
final ISMPServiceGroupManager aServiceGroupMgr = SMPMetaManager.getServiceGroupMgr();
final ISMPParticipantMigrationManager aParticipantMigrationMgr = SMPMetaManager.getParticipantMigrationMgr();
if (aSMLInfo == null) {
throw new SMPPreconditionFailedException("Currently no SML is available. Please select it in the UI at the 'SMP Settings' page", aDataProvider.getCurrentURI());
}
if (!aSettings.isSMLEnabled()) {
throw new SMPPreconditionFailedException("SML Connection is not enabled hence no participant can be migrated", aDataProvider.getCurrentURI());
}
final IParticipantIdentifier aParticipantID = aIdentifierFactory.parseParticipantIdentifier(sServiceGroupID);
if (aParticipantID == null) {
// Invalid identifier
throw SMPBadRequestException.failedToParseSG(sServiceGroupID, aDataProvider.getCurrentURI());
}
// Check that service group does not exist yet
if (aServiceGroupMgr.containsSMPServiceGroupWithID(aParticipantID)) {
throw new SMPBadRequestException("The Service Group '" + sServiceGroupID + "' already exists.", aDataProvider.getCurrentURI());
}
if (false) {
// valid
if (aParticipantMigrationMgr.containsInboundMigration(aParticipantID)) {
throw new SMPBadRequestException("The inbound migration of the Service Group '" + sServiceGroupID + "' is already contained.", aDataProvider.getCurrentURI());
}
}
// create the Service Group locally
try {
final ManageParticipantIdentifierServiceCaller aCaller = new ManageParticipantIdentifierServiceCaller(aSettings.getSMLInfo());
aCaller.setSSLSocketFactory(SMPKeyManager.getInstance().createSSLContext().getSocketFactory());
// SML call
aCaller.migrate(aParticipantID, sMigrationKey, SMPServerConfiguration.getSMLSMPID());
LOGGER.info(sLogPrefix + "Successfully migrated '" + aParticipantID.getURIEncoded() + "' in the SML to this SMP using migration key '" + sMigrationKey + "'");
} catch (final BadRequestFault | InternalErrorFault | NotFoundFault | UnauthorizedFault | ClientTransportException ex) {
throw new SMPSMLException("Failed to confirm the migration for participant '" + aParticipantID.getURIEncoded() + "' in SML, hence the migration failed." + " Please check the participant identifier and the migration key.", ex);
}
// Now create the service group locally (it was already checked that the
// PID is available on this SMP)
ISMPServiceGroup aSG = null;
Exception aCaughtEx = null;
try {
// Do not allow any Extension here
// Do NOT create in SMK/SML
aSG = aServiceGroupMgr.createSMPServiceGroup(aOwningUser.getID(), aParticipantID, (String) null, false);
} catch (final Exception ex) {
aCaughtEx = ex;
}
if (aSG != null) {
LOGGER.info(sLogPrefix + "The new SMP Service Group for participant '" + aParticipantID.getURIEncoded() + "' was successfully created.");
} else {
// No exception here
LOGGER.error(sLogPrefix + "Error creating the new SMP Service Group for participant '" + aParticipantID.getURIEncoded() + "'.", aCaughtEx);
}
// Remember internally
final ISMPParticipantMigration aMigration = aParticipantMigrationMgr.createInboundParticipantMigration(aParticipantID, sMigrationKey);
if (aMigration != null) {
LOGGER.info(sLogPrefix + "The participant migration for '" + aParticipantID.getURIEncoded() + "' with migration key '" + sMigrationKey + "' was successfully performed. Please inform the source SMP that the migration was successful.");
} else {
// No exception here
LOGGER.error(sLogPrefix + "Failed to store the participant migration for '" + aParticipantID.getURIEncoded() + "'.");
}
final IMicroDocument aResponseDoc = new MicroDocument();
final IMicroElement eRoot = aResponseDoc.appendElement("migrationInboundResponse");
eRoot.setAttribute("success", aSG != null && aMigration != null);
eRoot.setAttribute("serviceGroupCreated", aSG != null);
eRoot.setAttribute("migrationCreated", aMigration != null);
final XMLWriterSettings aXWS = new XMLWriterSettings().setIndent(EXMLSerializeIndent.INDENT_AND_ALIGN);
aUnifiedResponse.setContentAndCharset(MicroWriter.getNodeAsString(aResponseDoc, aXWS), aXWS.getCharset()).setMimeType(new MimeType(CMimeType.APPLICATION_XML).addParameter(CMimeType.PARAMETER_NAME_CHARSET, aXWS.getCharset().name())).disableCaching();
}
Aggregations