Search in sources :

Example 6 with UserManager

use of org.jivesoftware.openfire.user.UserManager in project Openfire by igniterealtime.

the class EmailSenderUtility method sendEmail.

public void sendEmail() {
    ByteArrayOutputStream outputStream = null;
    try {
        String host = JiveGlobals.getProperty("mail.smtp.host", "localhost");
        String port = JiveGlobals.getProperty("mail.smtp.port", "25");
        String username = JiveGlobals.getProperty("mail.smtp.username");
        String password = JiveGlobals.getProperty("mail.smtp.password");
        String debugEnabled = JiveGlobals.getProperty("mail.debug");
        boolean sslEnabled = JiveGlobals.getBooleanProperty("mail.smtp.ssl", true);
        Properties props = new Properties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.auth", port);
        props.setProperty("mail.smtp.sendpartial", "true");
        props.setProperty("mail.debug", debugEnabled);
        if (sslEnabled) {
            // Register with security provider.
            Security.setProperty("ssl.SocketFactory.provider", SSL_FACTORY);
            props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
            props.setProperty("mail.smtp.socketFactory.fallback", "true");
        }
        if (username != null) {
            props.put("mail.smtp.auth", "true");
        }
        Session session = Session.getInstance(props);
        outputStream = new ByteArrayOutputStream();
        createPdfAttachment(outputStream);
        byte[] bytes = outputStream.toByteArray();
        ByteArrayDataSource dataSource = new ByteArrayDataSource(bytes, "application/pdf");
        MimeBodyPart pdfBodyPart = new MimeBodyPart();
        pdfBodyPart.setDataHandler(new DataHandler(dataSource));
        pdfBodyPart.setFileName("ResultSummary.pdf");
        MimeMultipart multipart = new MimeMultipart();
        multipart.addBodyPart(pdfBodyPart);
        MimeMessage msg = new MimeMessage(session);
        DefaultAdminProvider defaultAdminProvider = new DefaultAdminProvider();
        java.util.List<JID> adminList = defaultAdminProvider.getAdmins();
        java.util.List<String> adminListEmails = new ArrayList<String>();
        UserManager manager = UserManager.getInstance();
        Log.info("Number of Admins " + adminList.size());
        for (int i = 0; i < adminList.size(); i++) {
            User user;
            try {
                user = manager.getUser(adminList.get(i).getNode().toString());
                Log.info("Admin Emails: " + user.getEmail());
                adminListEmails.add(user.getEmail());
            } catch (Exception ex) {
                continue;
            }
        }
        // java.util.List<String> recipientsList=Arrays.asList("", "", "");
        InternetAddress[] recipients = new InternetAddress[adminListEmails.size()];
        for (int i = 0; i < adminListEmails.size(); i++) {
            recipients[i] = new InternetAddress(adminListEmails.get(i).toString());
        }
        msg.setFrom(new InternetAddress("no-reply@openfire.org", "Openfire Admin"));
        msg.setRecipients(javax.mail.Message.RecipientType.TO, recipients);
        msg.setSubject("MONITORING REPORT - " + new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new Date()));
        msg.setContent(multipart);
        if (username != null) {
            URLName url = new URLName("smtp", host, Integer.parseInt(port), "", username, password);
            Transport transport = new com.sun.mail.smtp.SMTPTransport(session, url);
            transport.connect(host, Integer.parseInt(port), username, password);
            transport.sendMessage(msg, msg.getRecipients(MimeMessage.RecipientType.TO));
        } else
            Transport.send(msg);
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Could not send email");
    }
}
Also used : DefaultAdminProvider(org.jivesoftware.openfire.admin.DefaultAdminProvider) User(org.jivesoftware.openfire.user.User) javax.mail(javax.mail) DataHandler(javax.activation.DataHandler) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) JID(org.xmpp.packet.JID) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Date(java.util.Date) java.util(java.util) org.jivesoftware.util(org.jivesoftware.util) UserManager(org.jivesoftware.openfire.user.UserManager) SimpleDateFormat(java.text.SimpleDateFormat)

Example 7 with UserManager

use of org.jivesoftware.openfire.user.UserManager in project Openfire by igniterealtime.

the class JustMarriedPlugin method addNewUserToOthersRoster.

private static void addNewUserToOthersRoster(User newUser, RosterItem otherItem, String currentUser) {
    otherItem.getJid();
    UserManager userManager = UserManager.getInstance();
    // Is this user registered with our OF server?
    String username = otherItem.getJid().getNode();
    if (username != null && username.length() > 0 && userManager.isRegisteredUser(username) && XMPPServer.getInstance().isLocal(XMPPServer.getInstance().createJID(currentUser, null))) {
        try {
            User otherUser = userManager.getUser(username);
            Roster otherRoster = otherUser.getRoster();
            RosterItem oldUserOnOthersRoster = otherRoster.getRosterItem(XMPPServer.getInstance().createJID(currentUser, null));
            try {
                if (!oldUserOnOthersRoster.isOnlyShared()) {
                    RosterItem justCreated = otherRoster.createRosterItem(XMPPServer.getInstance().createJID(newUser.getUsername(), null), oldUserOnOthersRoster.getNickname(), oldUserOnOthersRoster.getGroups(), true, true);
                    justCreated.setAskStatus(oldUserOnOthersRoster.getAskStatus());
                    justCreated.setRecvStatus(oldUserOnOthersRoster.getRecvStatus());
                    justCreated.setSubStatus(oldUserOnOthersRoster.getSubStatus());
                    otherRoster.updateRosterItem(justCreated);
                }
            } catch (UserAlreadyExistsException e) {
                Log.error("Could not create roster item for user " + newUser.getUsername(), e);
            } catch (SharedGroupException e) {
                Log.error(e);
            }
        } catch (UserNotFoundException e) {
            Log.error("Could not create roster item for user " + newUser.getUsername() + " because it is a contact from a shared group", e);
        }
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) RosterItem(org.jivesoftware.openfire.roster.RosterItem) User(org.jivesoftware.openfire.user.User) Roster(org.jivesoftware.openfire.roster.Roster) UserManager(org.jivesoftware.openfire.user.UserManager) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) SharedGroupException(org.jivesoftware.openfire.SharedGroupException)

Example 8 with UserManager

use of org.jivesoftware.openfire.user.UserManager in project Openfire by igniterealtime.

the class JustMarriedPlugin method changeName.

public static boolean changeName(String currentUserName, String newUserName, boolean deleteOldUser, String newEmail, String newRealName) {
    UserManager userManager = UserManager.getInstance();
    try {
        User currentUser = userManager.getUser(currentUserName);
        // Old user found, create new one
        String password = AuthFactory.getPassword(currentUserName);
        String newName = (newRealName == null || newRealName.length() == 0) ? currentUser.getName() : newRealName;
        String newMail = (newEmail == null || newEmail.length() == 0) ? currentUser.getEmail() : newEmail;
        User newUser = userManager.createUser(newUserName, password, currentUser.getName(), newMail);
        newUser.setName(newName);
        newUser.setNameVisible(currentUser.isNameVisible());
        newUser.setEmailVisible(currentUser.isEmailVisible());
        newUser.setCreationDate(currentUser.getCreationDate());
        copyRoster(currentUser, newUser, currentUserName);
        copyProperties(currentUser, newUser);
        copyToGroups(currentUserName, newUserName);
        copyVCard(currentUserName, newUserName);
        if (deleteOldUser) {
            deleteUser(currentUser);
        }
    } catch (UserNotFoundException e) {
        Log.error("Could not find user " + currentUserName, e);
        return false;
    } catch (UserAlreadyExistsException e) {
        Log.error("Could not create user " + newUserName, e);
        return false;
    }
    return true;
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) User(org.jivesoftware.openfire.user.User) UserManager(org.jivesoftware.openfire.user.UserManager) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException)

Example 9 with UserManager

use of org.jivesoftware.openfire.user.UserManager in project Openfire by igniterealtime.

the class JustMarriedController method changeName.

/**
	 * Change name.
	 *
	 * @param currentUserName
	 *            the current user name
	 * @param newUserName
	 *            the new user name
	 * @param deleteOldUser
	 *            the delete old user
	 * @param newEmail
	 *            the new email
	 * @param newRealName
	 *            the new real name
	 * @return true, if successful
	 * @throws ServiceException
	 *             the service exception
	 */
public static boolean changeName(String currentUserName, String newUserName, boolean deleteOldUser, String newEmail, String newRealName) throws ServiceException {
    UserManager userManager = UserManager.getInstance();
    try {
        User currentUser = userManager.getUser(currentUserName);
        // Old user found, create new one
        String password = AuthFactory.getPassword(currentUserName);
        String newName = (newRealName == null || newRealName.length() == 0) ? currentUser.getName() : newRealName;
        String newMail = (newEmail == null || newEmail.length() == 0) ? currentUser.getEmail() : newEmail;
        User newUser = userManager.createUser(newUserName, password, currentUser.getName(), newMail);
        newUser.setName(newName);
        newUser.setNameVisible(currentUser.isNameVisible());
        newUser.setEmailVisible(currentUser.isEmailVisible());
        newUser.setCreationDate(currentUser.getCreationDate());
        copyRoster(currentUser, newUser, currentUserName);
        copyProperties(currentUser, newUser);
        copyToGroups(currentUserName, newUserName);
        copyVCard(currentUserName, newUserName);
        if (deleteOldUser) {
            deleteUser(currentUser);
        }
    } catch (UserNotFoundException e) {
        throw new ServiceException("Could not find user", currentUserName, ExceptionType.USER_NOT_FOUND_EXCEPTION, Response.Status.NOT_FOUND, e);
    } catch (UserAlreadyExistsException e) {
        throw new ServiceException("Could not create new user", newUserName, ExceptionType.USER_ALREADY_EXISTS_EXCEPTION, Response.Status.CONFLICT, e);
    }
    return true;
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) User(org.jivesoftware.openfire.user.User) ServiceException(org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException) UserManager(org.jivesoftware.openfire.user.UserManager) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException)

Example 10 with UserManager

use of org.jivesoftware.openfire.user.UserManager in project Openfire by igniterealtime.

the class JustMarriedController method addNewUserToOthersRoster.

/**
	 * Adds the new user to others roster.
	 *
	 * @param newUser
	 *            the new user
	 * @param otherItem
	 *            the other item
	 * @param currentUser
	 *            the current user
	 * @throws ServiceException
	 *             the service exception
	 */
private static void addNewUserToOthersRoster(User newUser, RosterItem otherItem, String currentUser) throws ServiceException {
    otherItem.getJid();
    UserManager userManager = UserManager.getInstance();
    // Is this user registered with our OF server?
    String username = otherItem.getJid().getNode();
    if (username != null && username.length() > 0 && userManager.isRegisteredUser(username) && XMPPServer.getInstance().isLocal(XMPPServer.getInstance().createJID(currentUser, null))) {
        try {
            User otherUser = userManager.getUser(username);
            Roster otherRoster = otherUser.getRoster();
            RosterItem oldUserOnOthersRoster = otherRoster.getRosterItem(XMPPServer.getInstance().createJID(currentUser, null));
            try {
                if (!oldUserOnOthersRoster.isOnlyShared()) {
                    RosterItem justCreated = otherRoster.createRosterItem(XMPPServer.getInstance().createJID(newUser.getUsername(), null), oldUserOnOthersRoster.getNickname(), oldUserOnOthersRoster.getGroups(), true, true);
                    justCreated.setAskStatus(oldUserOnOthersRoster.getAskStatus());
                    justCreated.setRecvStatus(oldUserOnOthersRoster.getRecvStatus());
                    justCreated.setSubStatus(oldUserOnOthersRoster.getSubStatus());
                    otherRoster.updateRosterItem(justCreated);
                }
            } catch (UserAlreadyExistsException e) {
                throw new ServiceException("Could not create roster item for user ", newUser.getUsername(), ExceptionType.USER_ALREADY_EXISTS_EXCEPTION, Response.Status.CONFLICT, e);
            } catch (SharedGroupException e) {
                throw new ServiceException("Could not create roster item, because it is a contact from a shared group", newUser.getUsername(), ExceptionType.USER_ALREADY_EXISTS_EXCEPTION, Response.Status.BAD_REQUEST, e);
            }
        } catch (UserNotFoundException e) {
            throw new ServiceException("Could not create roster item for user " + newUser.getUsername() + "  because it is a contact from a shared group.", newUser.getUsername(), ExceptionType.USER_NOT_FOUND_EXCEPTION, Response.Status.NOT_FOUND, e);
        }
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) RosterItem(org.jivesoftware.openfire.roster.RosterItem) User(org.jivesoftware.openfire.user.User) Roster(org.jivesoftware.openfire.roster.Roster) ServiceException(org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException) UserManager(org.jivesoftware.openfire.user.UserManager) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) SharedGroupException(org.jivesoftware.openfire.SharedGroupException)

Aggregations

UserManager (org.jivesoftware.openfire.user.UserManager)13 User (org.jivesoftware.openfire.user.User)10 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)10 UserAlreadyExistsException (org.jivesoftware.openfire.user.UserAlreadyExistsException)7 JID (org.xmpp.packet.JID)5 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 Element (org.dom4j.Element)2 SharedGroupException (org.jivesoftware.openfire.SharedGroupException)2 GroupNotFoundException (org.jivesoftware.openfire.group.GroupNotFoundException)2 ServiceException (org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException)2 Roster (org.jivesoftware.openfire.roster.Roster)2 RosterItem (org.jivesoftware.openfire.roster.RosterItem)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Properties (java.util.Properties)1 TreeMap (java.util.TreeMap)1