Search in sources :

Example 11 with Schedule

use of javax.ejb.Schedule in project muikku by otavanopisto.

the class ChatUserSyncScheduler method updateChatUsers.

@Schedule(second = "0", minute = "*/15", hour = "*", persistent = false)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void updateChatUsers() {
    String enabledUsersCsv = pluginSettingsController.getPluginSetting("chat", "enabledUsers");
    if (enabledUsersCsv == null) {
        return;
    }
    List<String> enabledUsers = Arrays.asList(enabledUsersCsv.split(","));
    String openfireToken = pluginSettingsController.getPluginSetting("chat", "openfireToken");
    if (openfireToken == null) {
        logger.log(Level.INFO, "No openfire token set, skipping user sync");
        return;
    }
    String openfireUrl = pluginSettingsController.getPluginSetting("chat", "openfireUrl");
    if (openfireUrl == null) {
        logger.log(Level.INFO, "No openfire url set, skipping user sync");
        return;
    }
    String openfirePort = pluginSettingsController.getPluginSetting("chat", "openfirePort");
    if (openfirePort == null) {
        logger.log(Level.INFO, "No openfire port set, skipping user sync");
        return;
    }
    if (!StringUtils.isNumeric(openfirePort)) {
        logger.log(Level.WARNING, "Invalid openfire port, skipping user sync");
        return;
    }
    AuthenticationToken token = new AuthenticationToken(openfireToken);
    RestApiClient client = new RestApiClient(openfireUrl, Integer.parseInt(openfirePort, 10), token);
    SecureRandom random = new SecureRandom();
    for (String enabledUser : enabledUsers) {
        try {
            // Checking before creating is subject to a race condition, but in the worst case
            // the creation just fails, resulting in a log entry
            UserEntity userEntity = client.getUser(enabledUser);
            if (userEntity == null) {
                logger.log(Level.INFO, "Syncing chat user " + enabledUser);
                SchoolDataIdentifier identifier = SchoolDataIdentifier.fromId(enabledUser);
                if (identifier == null) {
                    logger.log(Level.WARNING, "Invalid user identifier " + enabledUser + ", skipping...");
                    continue;
                }
                User user = userController.findUserByIdentifier(identifier);
                if (user == null) {
                    logger.log(Level.WARNING, "No user found for identifier " + enabledUser + ", skipping...");
                    continue;
                }
                // Can't leave the password empty, so next best thing is random passwords
                // The passwords are not actually used
                byte[] passwordBytes = new byte[20];
                random.nextBytes(passwordBytes);
                String password = Base64.encodeBase64String(passwordBytes);
                userEntity = new UserEntity(enabledUser, user.getDisplayName(), "", password);
                client.createUser(userEntity);
            }
        } catch (Exception e) {
            logger.log(Level.INFO, "Exception when syncing user " + enabledUser, e);
        }
    }
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) AuthenticationToken(fi.otavanopisto.muikku.openfire.rest.client.entity.AuthenticationToken) User(fi.otavanopisto.muikku.schooldata.entity.User) SecureRandom(java.security.SecureRandom) RestApiClient(fi.otavanopisto.muikku.openfire.rest.client.RestApiClient) UserEntity(fi.otavanopisto.muikku.openfire.rest.client.entity.UserEntity) TransactionAttribute(javax.ejb.TransactionAttribute) Schedule(javax.ejb.Schedule)

Aggregations

Schedule (javax.ejb.Schedule)11 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)3 TransactionAttribute (javax.ejb.TransactionAttribute)3 Employee (com.artezio.arttime.datamodel.Employee)2 Period (com.artezio.arttime.datamodel.Period)2 RestApiClient (fi.otavanopisto.muikku.openfire.rest.client.RestApiClient)2 AuthenticationToken (fi.otavanopisto.muikku.openfire.rest.client.entity.AuthenticationToken)2 SchoolDataIdentifier (fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 SyndEntry (com.rometools.rome.feed.synd.SyndEntry)1 SyndFeed (com.rometools.rome.feed.synd.SyndFeed)1 FeedException (com.rometools.rome.io.FeedException)1 SyndFeedInput (com.rometools.rome.io.SyndFeedInput)1 XmlReader (com.rometools.rome.io.XmlReader)1 HiddenMonitor (eu.ggnet.saft.api.progress.HiddenMonitor)1 MUCRoomEntity (fi.otavanopisto.muikku.openfire.rest.client.entity.MUCRoomEntity)1 UserEntity (fi.otavanopisto.muikku.openfire.rest.client.entity.UserEntity)1 Feed (fi.otavanopisto.muikku.plugins.feed.model.Feed)1 WorkspaceNode (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode)1