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");
}
}
}
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 + "'");
}
}
});
}
}
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");
}
}
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");
}
}
}
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)");
}
Aggregations