Search in sources :

Example 1 with WebScoped

use of com.helger.web.scope.mgr.WebScoped in project phoss-smp by phax.

the class V12__MigrateUserGroupsToDB method migrate.

public void migrate(@Nonnull final Context context) throws Exception {
    try (final WebScoped aWS = new WebScoped()) {
        LOGGER.info("Migrating all user groups to the DB");
        final String sFilename = PhotonSecurityManager.FactoryXML.DIRECTORY_SECURITY + PhotonSecurityManager.FactoryXML.FILENAME_USERGROUPS_XML;
        final File aFile = WebFileIO.getDataIO().getFile(sFilename);
        if (aFile.exists()) {
            final IUserManager aUserMgr = new UserManagerJDBC(SMPDBExecutor::new, SMPDBExecutor.TABLE_NAME_CUSTOMIZER);
            final IRoleManager aRoleMgr = new RoleManagerJDBC(SMPDBExecutor::new, SMPDBExecutor.TABLE_NAME_CUSTOMIZER);
            final UserGroupManager aMgrXML = new UserGroupManager(sFilename, aUserMgr, aRoleMgr);
            final ICommonsList<IUserGroup> aUserGroups = aMgrXML.getAll();
            if (aUserGroups.isNotEmpty()) {
                final UserGroupManagerJDBC aMgrNew = new UserGroupManagerJDBC(SMPDBExecutor::new, SMPDBExecutor.TABLE_NAME_CUSTOMIZER, aUserMgr, aRoleMgr);
                for (final IUserGroup aUserGroup : aUserGroups) {
                    // Don't run the callback here
                    if (aMgrNew.internalCreateNewUserGroup((UserGroup) aUserGroup, false, false) == null)
                        LOGGER.error("Failed to migrate user group " + aUserGroup + " to DB");
                }
            }
            // Rename to avoid later inconsistencies
            WebFileIO.getDataIO().renameFile(sFilename, sFilename + ".migrated");
            LOGGER.info("Finished migrating all " + aUserGroups.size() + " user groups to the DB");
        } else {
            LOGGER.warn("No user group file found");
        }
    }
}
Also used : WebScoped(com.helger.web.scope.mgr.WebScoped) UserGroupManager(com.helger.photon.security.usergroup.UserGroupManager) IUserManager(com.helger.photon.security.user.IUserManager) IUserGroup(com.helger.photon.security.usergroup.IUserGroup) SMPDBExecutor(com.helger.phoss.smp.backend.sql.SMPDBExecutor) UserGroup(com.helger.photon.security.usergroup.UserGroup) IUserGroup(com.helger.photon.security.usergroup.IUserGroup) IRoleManager(com.helger.photon.security.role.IRoleManager) UserGroupManagerJDBC(com.helger.photon.jdbc.security.UserGroupManagerJDBC) UserManagerJDBC(com.helger.photon.jdbc.security.UserManagerJDBC) RoleManagerJDBC(com.helger.photon.jdbc.security.RoleManagerJDBC) File(java.io.File)

Example 2 with WebScoped

use of com.helger.web.scope.mgr.WebScoped in project phoss-smp by phax.

the class SMPMetaManager method _performMigrations.

private void _performMigrations() {
    // Required for SQL version
    try (final WebScoped aWS = new WebScoped()) {
        // See issue #128
        PhotonBasicManager.getSystemMigrationMgr().performMigrationIfNecessary("ensure-transport-profiles-128", () -> {
            LOGGER.info("Started running migration to ensure all used transport profiles are automatically created");
            for (final ISMPServiceInformation aSI : m_aServiceInformationMgr.getAllSMPServiceInformation()) for (final ISMPProcess aProc : aSI.getAllProcesses()) for (final ISMPEndpoint aEP : aProc.getAllEndpoints()) {
                final String sTransportProfile = aEP.getTransportProfile();
                if (!m_aTransportProfileMgr.containsSMPTransportProfileWithID(sTransportProfile)) {
                    m_aTransportProfileMgr.createSMPTransportProfile(sTransportProfile, sTransportProfile + " (automatically created)", false);
                    LOGGER.info("Created missing transport profile '" + sTransportProfile + "'");
                }
            }
        });
    }
}
Also used : WebScoped(com.helger.web.scope.mgr.WebScoped) ISMPEndpoint(com.helger.phoss.smp.domain.serviceinfo.ISMPEndpoint) ISMPServiceInformation(com.helger.phoss.smp.domain.serviceinfo.ISMPServiceInformation) ISMPProcess(com.helger.phoss.smp.domain.serviceinfo.ISMPProcess)

Example 3 with WebScoped

use of com.helger.web.scope.mgr.WebScoped in project phoss-smp by phax.

the class V15__MigrateDBUsersToPhotonUsers method migrate.

public void migrate(@Nonnull final Context context) throws Exception {
    try (final WebScoped aWS = new WebScoped()) {
        LOGGER.info("Migrating all old DB users to ph-oton users");
        final EDatabaseType eDBType = SMPDataSourceSingleton.getDatabaseType();
        // Old JDBC user manager
        final SMPUserManagerJDBC aSQLUserMgr = new SMPUserManagerJDBC(SMPDBExecutor::new);
        final ICommonsList<DBUser> aSQLUsers = aSQLUserMgr.getAllUsers();
        LOGGER.info("Found " + aSQLUsers.size() + " DB user to migrate");
        final ICommonsOrderedMap<String, String> aCreatedMappings = new CommonsLinkedHashMap<>();
        // New JDBC user manager
        final IUserManager aPhotonUserMgr = PhotonSecurityManager.getUserMgr();
        for (final DBUser aSQLUser : aSQLUsers) {
            final DBUser aDBUser = aSQLUser;
            IUser aPhotonUser = null;
            int nIndex = 0;
            while (true) {
                final String sUserName = aDBUser.getUserName() + (nIndex > 0 ? Integer.toString(nIndex) : "");
                // The suffix "@example.org" is added to make it an email-address
                final String sEmailAddress = sUserName + "@example.org";
                aPhotonUser = aPhotonUserMgr.createNewUser(sEmailAddress, sEmailAddress, aDBUser.getPassword(), null, sUserName, null, CSMPServer.DEFAULT_LOCALE, null, false);
                if (aPhotonUser != null) {
                    // New user was successfully created
                    break;
                }
                // User name already taken
                ++nIndex;
                if (nIndex > 1000) {
                    // Avoid endless loop
                    throw new IllegalStateException("Too many iterations mapping the DB user '" + aDBUser.getUserName() + "' to a ph-oton user");
                }
            }
            aCreatedMappings.put(aDBUser.getUserName(), aPhotonUser.getID());
            LOGGER.info("Mapped DB user '" + aDBUser.getUserName() + "' to ph-oton user " + aPhotonUser.getID());
        }
        // Update the ownership in "smp_ownership"
        // Remove the table "smp_user"
        aSQLUserMgr.updateOwnershipsAndKillUsers(aCreatedMappings);
        if (XMLMapHandler.writeMap(aCreatedMappings, new FileSystemResource(WebFileIO.getDataIO().getFile("migrations/db-photon-user-mapping-" + eDBType.getID() + ".xml"))).isFailure())
            LOGGER.error("Failed to store mapping of DB users to ph-oton users as XML");
        LOGGER.info("Finished migrating all DB users to ph-oton users");
    }
}
Also used : WebScoped(com.helger.web.scope.mgr.WebScoped) CommonsLinkedHashMap(com.helger.commons.collection.impl.CommonsLinkedHashMap) IUserManager(com.helger.photon.security.user.IUserManager) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) SMPDBExecutor(com.helger.phoss.smp.backend.sql.SMPDBExecutor) IUser(com.helger.photon.security.user.IUser) DBUser(com.helger.phoss.smp.backend.sql.domain.DBUser) EDatabaseType(com.helger.phoss.smp.backend.sql.EDatabaseType)

Example 4 with WebScoped

use of com.helger.web.scope.mgr.WebScoped in project phoss-smp by phax.

the class V5__MigrateTransportProfilesToDB method migrate.

public void migrate(@Nonnull final Context context) throws Exception {
    try (final WebScoped aWS = new WebScoped()) {
        LOGGER.info("Migrating all transport profiles to the DB");
        final String sFilename = "transportprofiles.xml";
        final File aFile = WebFileIO.getDataIO().getFile(sFilename);
        if (aFile.exists()) {
            final SMPTransportProfileManagerXML aMgrXML = new SMPTransportProfileManagerXML(sFilename);
            final ICommonsList<ISMPTransportProfile> aTransportProfiles = aMgrXML.getAll();
            if (aTransportProfiles.isNotEmpty()) {
                final SMPTransportProfileManagerJDBC aMgrNew = new SMPTransportProfileManagerJDBC(SMPDBExecutor::new);
                for (final ISMPTransportProfile aTransportProfile : aTransportProfiles) if (aMgrNew.createSMPTransportProfile(aTransportProfile.getID(), aTransportProfile.getName(), aTransportProfile.isDeprecated()) == null)
                    LOGGER.error("Failed to migrate " + aTransportProfile + " to DB");
            }
            // Rename to avoid later inconsistencies
            WebFileIO.getDataIO().renameFile(sFilename, sFilename + ".migrated");
            LOGGER.info("Finished migrating all " + aTransportProfiles.size() + " transport profiles to the DB");
        } else {
            LOGGER.info("No transport profile file found");
        }
    }
}
Also used : WebScoped(com.helger.web.scope.mgr.WebScoped) ISMPTransportProfile(com.helger.peppol.smp.ISMPTransportProfile) SMPTransportProfileManagerJDBC(com.helger.phoss.smp.backend.sql.mgr.SMPTransportProfileManagerJDBC) SMPTransportProfileManagerXML(com.helger.phoss.smp.domain.transportprofile.SMPTransportProfileManagerXML) File(java.io.File) SMPDBExecutor(com.helger.phoss.smp.backend.sql.SMPDBExecutor)

Example 5 with WebScoped

use of com.helger.web.scope.mgr.WebScoped in project phoss-smp by phax.

the class ServiceMetadataInterfaceFillDBTest method testCreateAndDeleteServiceInformationSMPClient.

@Test
public void testCreateAndDeleteServiceInformationSMPClient() throws SMPClientException {
    final SMPClient aSMPClient = new MockSMPClient();
    final StopWatch aSW = StopWatch.createdStarted();
    int nEndpoints = 0;
    try (final WebScoped aWS = new WebScoped(new MockHttpServletRequest())) {
        final int nCount = 2_000;
        for (int i = 0; i < nCount; ++i) {
            final IParticipantIdentifier aPI_LC = PeppolIdentifierFactory.INSTANCE.createParticipantIdentifierWithDefaultScheme(PID_PREFIX_9999_PHOSS + StringHelper.getLeadingZero(i, 4));
            final PeppolDocumentTypeIdentifier aDT = EPredefinedDocumentTypeIdentifier.INVOICE_EN16931_PEPPOL_V30.getAsDocumentTypeIdentifier();
            final PeppolProcessIdentifier aProcID = EPredefinedProcessIdentifier.BIS3_BILLING.getAsProcessIdentifier();
            for (final ESMPTransportProfile eTP : new ESMPTransportProfile[] { ESMPTransportProfile.TRANSPORT_PROFILE_AS2, ESMPTransportProfile.TRANSPORT_PROFILE_PEPPOL_AS4_V2 }) {
                final ServiceInformationType aSI = new ServiceInformationType();
                aSI.setParticipantIdentifier(new SimpleParticipantIdentifier(aPI_LC));
                aSI.setDocumentIdentifier(aDT);
                {
                    final ProcessListType aPL = new ProcessListType();
                    final ProcessType aProcess = new ProcessType();
                    aProcess.setProcessIdentifier(aProcID);
                    final ServiceEndpointList aSEL = new ServiceEndpointList();
                    final EndpointType aEndpoint = new EndpointType();
                    aEndpoint.setEndpointReference(W3CEndpointReferenceHelper.createEndpointReference("http://test.smpserver/" + eTP.getID()));
                    aEndpoint.setRequireBusinessLevelSignature(false);
                    aEndpoint.setCertificate("blacert");
                    aEndpoint.setServiceDescription("Unit test service");
                    aEndpoint.setTechnicalContactUrl("https://github.com/phax/phoss-smp");
                    aEndpoint.setTransportProfile(eTP.getID());
                    aSEL.addEndpoint(aEndpoint);
                    aProcess.setServiceEndpointList(aSEL);
                    aPL.addProcess(aProcess);
                    aSI.setProcessList(aPL);
                }
                aSMPClient.saveServiceInformation(aSI, CREDENTIALS);
                ++nEndpoints;
            }
        }
    }
    aSW.stop();
    LOGGER.info("Created " + nEndpoints + " ServiceInformation in " + aSW.getDuration() + " (= " + (aSW.getMillis() / nEndpoints) + " ms/entry)");
}
Also used : WebScoped(com.helger.web.scope.mgr.WebScoped) MockHttpServletRequest(com.helger.servlet.mock.MockHttpServletRequest) ESMPTransportProfile(com.helger.peppol.smp.ESMPTransportProfile) PeppolProcessIdentifier(com.helger.peppolid.peppol.process.PeppolProcessIdentifier) ServiceInformationType(com.helger.xsds.peppol.smp1.ServiceInformationType) SimpleParticipantIdentifier(com.helger.peppolid.simple.participant.SimpleParticipantIdentifier) StopWatch(com.helger.commons.timing.StopWatch) MockSMPClient(com.helger.phoss.smp.mock.MockSMPClient) ProcessType(com.helger.xsds.peppol.smp1.ProcessType) MockSMPClient(com.helger.phoss.smp.mock.MockSMPClient) SMPClient(com.helger.smpclient.peppol.SMPClient) PeppolDocumentTypeIdentifier(com.helger.peppolid.peppol.doctype.PeppolDocumentTypeIdentifier) EndpointType(com.helger.xsds.peppol.smp1.EndpointType) ProcessListType(com.helger.xsds.peppol.smp1.ProcessListType) ServiceEndpointList(com.helger.xsds.peppol.smp1.ServiceEndpointList) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier) Test(org.junit.Test)

Aggregations

WebScoped (com.helger.web.scope.mgr.WebScoped)17 File (java.io.File)7 StopWatch (com.helger.commons.timing.StopWatch)6 SMPDBExecutor (com.helger.phoss.smp.backend.sql.SMPDBExecutor)6 MockHttpServletRequest (com.helger.servlet.mock.MockHttpServletRequest)6 PeppolParticipantIdentifier (com.helger.peppolid.peppol.participant.PeppolParticipantIdentifier)5 SimpleParticipantIdentifier (com.helger.peppolid.simple.participant.SimpleParticipantIdentifier)5 Response (javax.ws.rs.core.Response)4 PeppolDocumentTypeIdentifier (com.helger.peppolid.peppol.doctype.PeppolDocumentTypeIdentifier)3 PeppolProcessIdentifier (com.helger.peppolid.peppol.process.PeppolProcessIdentifier)3 WebScopeTestRule (com.helger.web.scope.mock.WebScopeTestRule)3 ObjectFactory (com.helger.xsds.peppol.smp1.ObjectFactory)3 IParticipantIdentifier (com.helger.peppolid.IParticipantIdentifier)2 IAS4ClientBuildMessageCallback (com.helger.phase4.client.IAS4ClientBuildMessageCallback)2 AS4IncomingDumperFileBased (com.helger.phase4.dump.AS4IncomingDumperFileBased)2 AS4OutgoingDumperFileBased (com.helger.phase4.dump.AS4OutgoingDumperFileBased)2 AS4UserMessage (com.helger.phase4.messaging.domain.AS4UserMessage)2 AbstractAS4Message (com.helger.phase4.messaging.domain.AbstractAS4Message)2 PhotonSecurityManagerFactoryMongoDB (com.helger.phoss.smp.backend.mongodb.PhotonSecurityManagerFactoryMongoDB)2 IUserManager (com.helger.photon.security.user.IUserManager)2