Search in sources :

Example 26 with SMPServerException

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;
}
Also used : ESuccess(com.helger.commons.state.ESuccess) ESuccess(com.helger.commons.state.ESuccess) SMPInternalErrorException(com.helger.phoss.smp.exception.SMPInternalErrorException) Nonnegative(javax.annotation.Nonnegative) ICommonsSet(com.helger.commons.collection.impl.ICommonsSet) CIdentifier(com.helger.peppolid.CIdentifier) LoggerFactory(org.slf4j.LoggerFactory) SMPSMLException(com.helger.phoss.smp.exception.SMPSMLException) EChange(com.helger.commons.state.EChange) CheckForSigned(javax.annotation.CheckForSigned) Supplier(java.util.function.Supplier) CallbackList(com.helger.commons.callback.CallbackList) AbstractJDBCEnabledManager(com.helger.db.jdbc.mgr.AbstractJDBCEnabledManager) ISMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup) MutableBoolean(com.helger.commons.mutable.MutableBoolean) SMPServerException(com.helger.phoss.smp.exception.SMPServerException) Nonempty(com.helger.commons.annotation.Nonempty) RegistrationHookException(com.helger.phoss.smp.smlhook.RegistrationHookException) DBExecutor(com.helger.db.jdbc.executor.DBExecutor) ReturnsMutableCopy(com.helger.commons.annotation.ReturnsMutableCopy) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) RegistrationHookFactory(com.helger.phoss.smp.smlhook.RegistrationHookFactory) Logger(org.slf4j.Logger) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) ConstantPreparedStatementDataProvider(com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider) StringHelper(com.helger.commons.string.StringHelper) SimpleParticipantIdentifier(com.helger.peppolid.simple.participant.SimpleParticipantIdentifier) ISMPServiceGroupCallback(com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroupCallback) ExpiringMap(net.jodah.expiringmap.ExpiringMap) EqualsHelper(com.helger.commons.equals.EqualsHelper) ValueEnforcer(com.helger.commons.ValueEnforcer) DBResultRow(com.helger.db.jdbc.executor.DBResultRow) TimeUnit(java.util.concurrent.TimeUnit) SMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.SMPServiceGroup) IRegistrationHook(com.helger.phoss.smp.smlhook.IRegistrationHook) AuditHelper(com.helger.photon.audit.AuditHelper) SMPNotFoundException(com.helger.phoss.smp.exception.SMPNotFoundException) CommonsHashSet(com.helger.commons.collection.impl.CommonsHashSet) ICommonsList(com.helger.commons.collection.impl.ICommonsList) Wrapper(com.helger.commons.wrapper.Wrapper) ReturnsMutableObject(com.helger.commons.annotation.ReturnsMutableObject) ExpirationPolicy(net.jodah.expiringmap.ExpirationPolicy) ISMPServiceGroupManager(com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroupManager) Wrapper(com.helger.commons.wrapper.Wrapper) SMPInternalErrorException(com.helger.phoss.smp.exception.SMPInternalErrorException) SMPSMLException(com.helger.phoss.smp.exception.SMPSMLException) SMPServerException(com.helger.phoss.smp.exception.SMPServerException) RegistrationHookException(com.helger.phoss.smp.smlhook.RegistrationHookException) SMPNotFoundException(com.helger.phoss.smp.exception.SMPNotFoundException) SMPNotFoundException(com.helger.phoss.smp.exception.SMPNotFoundException) SMPInternalErrorException(com.helger.phoss.smp.exception.SMPInternalErrorException) DBExecutor(com.helger.db.jdbc.executor.DBExecutor) ISMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup) SMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.SMPServiceGroup) ConstantPreparedStatementDataProvider(com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider) EChange(com.helger.commons.state.EChange) SMPServerException(com.helger.phoss.smp.exception.SMPServerException) Nonnull(javax.annotation.Nonnull)

Example 27 with SMPServerException

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;
}
Also used : ESuccess(com.helger.commons.state.ESuccess) Wrapper(com.helger.commons.wrapper.Wrapper) RegistrationHookException(com.helger.phoss.smp.smlhook.RegistrationHookException) MutableBoolean(com.helger.commons.mutable.MutableBoolean) SMPInternalErrorException(com.helger.phoss.smp.exception.SMPInternalErrorException) SMPSMLException(com.helger.phoss.smp.exception.SMPSMLException) SMPServerException(com.helger.phoss.smp.exception.SMPServerException) RegistrationHookException(com.helger.phoss.smp.smlhook.RegistrationHookException) SMPNotFoundException(com.helger.phoss.smp.exception.SMPNotFoundException) SMPSMLException(com.helger.phoss.smp.exception.SMPSMLException) IRegistrationHook(com.helger.phoss.smp.smlhook.IRegistrationHook) SMPNotFoundException(com.helger.phoss.smp.exception.SMPNotFoundException) SMPInternalErrorException(com.helger.phoss.smp.exception.SMPInternalErrorException) DBExecutor(com.helger.db.jdbc.executor.DBExecutor) ISMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup) SMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.SMPServiceGroup) ConstantPreparedStatementDataProvider(com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider) EChange(com.helger.commons.state.EChange) SMPServerException(com.helger.phoss.smp.exception.SMPServerException) Nonnull(javax.annotation.Nonnull)

Example 28 with SMPServerException

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();
}
Also used : ClientTransportException(com.sun.xml.ws.client.ClientTransportException) ISMPServiceGroupManager(com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroupManager) BadRequestFault(com.helger.peppol.smlclient.participant.BadRequestFault) ISMLInfo(com.helger.peppol.sml.ISMLInfo) NotFoundFault(com.helger.peppol.smlclient.participant.NotFoundFault) SMPSMLException(com.helger.phoss.smp.exception.SMPSMLException) CMimeType(com.helger.commons.mime.CMimeType) MimeType(com.helger.commons.mime.MimeType) IMicroDocument(com.helger.xml.microdom.IMicroDocument) MicroDocument(com.helger.xml.microdom.MicroDocument) ISMPParticipantMigration(com.helger.phoss.smp.domain.pmigration.ISMPParticipantMigration) ISMPServerAPIDataProvider(com.helger.phoss.smp.restapi.ISMPServerAPIDataProvider) IUser(com.helger.photon.security.user.IUser) ISMPParticipantMigrationManager(com.helger.phoss.smp.domain.pmigration.ISMPParticipantMigrationManager) IIdentifierFactory(com.helger.peppolid.factory.IIdentifierFactory) SMPBadRequestException(com.helger.phoss.smp.exception.SMPBadRequestException) XMLWriterSettings(com.helger.xml.serialize.write.XMLWriterSettings) ISMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup) ManageParticipantIdentifierServiceCaller(com.helger.peppol.smlclient.ManageParticipantIdentifierServiceCaller) SMPBadRequestException(com.helger.phoss.smp.exception.SMPBadRequestException) SMPSMLException(com.helger.phoss.smp.exception.SMPSMLException) GeneralSecurityException(java.security.GeneralSecurityException) SMPServerException(com.helger.phoss.smp.exception.SMPServerException) SMPPreconditionFailedException(com.helger.phoss.smp.exception.SMPPreconditionFailedException) ClientTransportException(com.sun.xml.ws.client.ClientTransportException) UnauthorizedFault(com.helger.peppol.smlclient.participant.UnauthorizedFault) SMPPreconditionFailedException(com.helger.phoss.smp.exception.SMPPreconditionFailedException) BasicAuthClientCredentials(com.helger.http.basicauth.BasicAuthClientCredentials) ISMPSettings(com.helger.phoss.smp.settings.ISMPSettings) IMicroElement(com.helger.xml.microdom.IMicroElement) IMicroDocument(com.helger.xml.microdom.IMicroDocument) InternalErrorFault(com.helger.peppol.smlclient.participant.InternalErrorFault) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier)

Aggregations

SMPServerException (com.helger.phoss.smp.exception.SMPServerException)28 IParticipantIdentifier (com.helger.peppolid.IParticipantIdentifier)24 ISMPServiceGroupManager (com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroupManager)23 IIdentifierFactory (com.helger.peppolid.factory.IIdentifierFactory)22 ISMPServiceGroup (com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup)22 SMPNotFoundException (com.helger.phoss.smp.exception.SMPNotFoundException)18 Nonnull (javax.annotation.Nonnull)18 IUser (com.helger.photon.security.user.IUser)17 ISMPServiceInformationManager (com.helger.phoss.smp.domain.serviceinfo.ISMPServiceInformationManager)13 ISMPServiceInformation (com.helger.phoss.smp.domain.serviceinfo.ISMPServiceInformation)11 IDocumentTypeIdentifier (com.helger.peppolid.IDocumentTypeIdentifier)10 ISMPRedirectManager (com.helger.phoss.smp.domain.redirect.ISMPRedirectManager)9 SMPBadRequestException (com.helger.phoss.smp.exception.SMPBadRequestException)9 EChange (com.helger.commons.state.EChange)8 ISMPRedirect (com.helger.phoss.smp.domain.redirect.ISMPRedirect)5 ICommonsList (com.helger.commons.collection.impl.ICommonsList)4 ISMPBusinessCardManager (com.helger.phoss.smp.domain.businesscard.ISMPBusinessCardManager)4 RegistrationHookException (com.helger.phoss.smp.smlhook.RegistrationHookException)4 MutableBoolean (com.helger.commons.mutable.MutableBoolean)3 ESuccess (com.helger.commons.state.ESuccess)3