Search in sources :

Example 1 with UserNotFoundException

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

the class OpenfireExporter method importUsers.

/*
   * (non-Javadoc)
   * 
   * @see
   * org.jivesoftware.openfire.plugin.InExporter#importUsers(org.dom4j.Document,
   * java.lang.String)
   */
@SuppressWarnings("unchecked")
private List<String> importUsers(Document document, String previousDomain, boolean isUserProviderReadOnly) {
    Log.debug("importUsers");
    List<String> invalidUsers = new ArrayList<String>();
    Element users = document.getRootElement();
    Iterator<Element> usersIter = users.elementIterator("User");
    while (usersIter.hasNext()) {
        Element user = usersIter.next();
        String userName = null;
        String password = null;
        String email = null;
        String name = null;
        List<RosterItem> rosterItems = new ArrayList<RosterItem>();
        Iterator<Element> userElements = user.elementIterator();
        while (userElements.hasNext()) {
            Element userElement = userElements.next();
            String nameElement = userElement.getName();
            if ("Username".equals(nameElement)) {
                userName = userElement.getText();
            } else if ("Password".equals(nameElement)) {
                password = userElement.getText();
            } else if ("Name".equals(nameElement)) {
                name = userElement.getText();
            } else if ("Email".equals(nameElement)) {
                email = userElement.getText();
            } else if ("Roster".equals(nameElement)) {
                Iterator<Element> rosterIter = userElement.elementIterator("Item");
                while (rosterIter.hasNext()) {
                    Element rosterElement = rosterIter.next();
                    String jid = rosterElement.attributeValue("jid");
                    String askstatus = rosterElement.attributeValue("askstatus");
                    String recvstatus = rosterElement.attributeValue("recvstatus");
                    String substatus = rosterElement.attributeValue("substatus");
                    String nickname = rosterElement.attributeValue("name");
                    List<String> groups = new ArrayList<String>();
                    Iterator<Element> groupIter = rosterElement.elementIterator("Group");
                    while (groupIter.hasNext()) {
                        Element group = groupIter.next();
                        String groupName = group.getText();
                        if (groupName != null && groupName.trim().length() > 0) {
                            groups.add(groupName);
                        }
                    }
                    // used for migration
                    if (previousDomain != null) {
                        jid = jid.replace(previousDomain, serverName);
                    }
                    rosterItems.add(new RosterItem(new JID(jid), RosterItem.SubType.getTypeFromInt(Integer.parseInt(substatus)), RosterItem.AskType.getTypeFromInt(Integer.parseInt(askstatus)), RosterItem.RecvType.getTypeFromInt(Integer.parseInt(recvstatus)), nickname, groups));
                }
            }
        }
        if ((userName != null) && (password != null)) {
            try {
                userName = Stringprep.nodeprep(userName);
                if (isUserProviderReadOnly) {
                    userManager.createUser(userName, password, name, email);
                }
                // Check to see user exists before adding their roster, this is for
                // read-only user providers.
                userManager.getUser(userName);
                for (RosterItem ri : rosterItems) {
                    rosterItemProvider.createItem(userName, ri);
                }
            } catch (StringprepException se) {
                Log.info("Invalid username " + userName);
                invalidUsers.add(userName);
            } catch (UserAlreadyExistsException e) {
                Log.info("User already exists " + userName);
                invalidUsers.add(userName);
            } catch (UserNotFoundException e) {
                Log.info("User not found " + userName);
                invalidUsers.add(userName);
            }
        }
    }
    return invalidUsers;
}
Also used : StringprepException(gnu.inet.encoding.StringprepException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) RosterItem(org.jivesoftware.openfire.roster.RosterItem) JID(org.xmpp.packet.JID) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException)

Example 2 with UserNotFoundException

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

the class Xep227Exporter method importUser.

/**
   * @param user
   * @param previousDomain
   * @param isUserProviderReadOnly
   * @param invalidUsers
   */
@SuppressWarnings("unchecked")
private void importUser(Element user, String previousDomain, boolean isUserProviderReadOnly, List<String> invalidUsers) {
    Log.debug("importUser");
    List<RosterItem> rosterItems = new ArrayList<RosterItem>();
    List<OfflineMessage> offlineMessages = new ArrayList<OfflineMessage>();
    Element vCardElement = null;
    String userName = user.attributeValue(NAME_NAME);
    String password = user.attributeValue(PASSWORD_NAME);
    Iterator<Element> userElements = user.elementIterator();
    while (userElements.hasNext()) {
        Element userElement = userElements.next();
        String nameElement = userElement.getName();
        if (OFFLINE_MESSAGES_ELEMENT_NAME.equals(nameElement)) {
            importOffLineMessages(userElement, offlineMessages);
        } else if (QUERY_ELEMENT_NAME.equals(nameElement) && JABBER_IQ_ROSTER_NS.equals(userElement.getNamespaceURI())) {
            importUserRoster(userElement, rosterItems, previousDomain);
        } else if (V_CARD_NAME.equals(nameElement) && VCARD_TEMP_NS.equals(userElement.getNamespaceURI())) {
            vCardElement = userElement;
        }
    }
    if (userName != null) {
        try {
            userName = Stringprep.nodeprep(userName);
            if (!isUserProviderReadOnly && (password != null)) {
                userManager.createUser(userName, password, userName, null);
            }
            if (!isUserProviderReadOnly && vCardElement != null) {
                try {
                    vCardManager.setVCard(userName, vCardElement);
                } catch (Exception e) {
                    Log.warn("Error updating VCard:" + userName + ":" + e.getMessage());
                    Log.debug("", e);
                }
            }
            // Check to see user exists before adding their roster, this is for
            // read-only user providers.
            userManager.getUser(userName);
            for (RosterItem ri : rosterItems) {
                rosterItemProvider.createItem(userName, ri);
            }
            for (OfflineMessage offlineMessage : offlineMessages) {
                offlineMessagesStore.addMessage(offlineMessage);
            }
        } catch (StringprepException se) {
            Log.info("Invalid username " + userName);
            invalidUsers.add(userName);
        } catch (UserAlreadyExistsException e) {
            Log.info("User already exists " + userName);
            invalidUsers.add(userName);
        } catch (UserNotFoundException e) {
            Log.info("User not found " + userName);
            invalidUsers.add(userName);
        } catch (Exception e) {
            Log.warn("Error updating User:" + userName + ":" + e.getLocalizedMessage());
            invalidUsers.add(userName);
        }
    }
}
Also used : StringprepException(gnu.inet.encoding.StringprepException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) RosterItem(org.jivesoftware.openfire.roster.RosterItem) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) OfflineMessage(org.jivesoftware.openfire.OfflineMessage) StringprepException(gnu.inet.encoding.StringprepException) ParseException(java.text.ParseException) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException)

Example 3 with UserNotFoundException

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

the class UserServicePlugin method addRosterItem.

/**
	 * Add new roster item for specified user
	 *
	 * @param username
	 *            the username of the local user to add roster item to.
	 * @param itemJID
	 *            the JID of the roster item to be added.
	 * @param itemName
	 *            the nickname of the roster item.
	 * @param subscription
	 *            the type of subscription of the roster item. Possible values
	 *            are: -1(remove), 0(none), 1(to), 2(from), 3(both).
	 * @param groupNames
	 *            the name of a group to place contact into.
	 * @throws UserNotFoundException
	 *             if the user does not exist in the local server.
	 * @throws UserAlreadyExistsException
	 *             if roster item with the same JID already exists.
	 * @throws SharedGroupException
	 *             if roster item cannot be added to a shared group.
	 */
public void addRosterItem(String username, String itemJID, String itemName, String subscription, String groupNames) throws UserNotFoundException, UserAlreadyExistsException, SharedGroupException {
    getUser(username);
    Roster r = rosterManager.getRoster(username);
    JID j = new JID(itemJID);
    try {
        r.getRosterItem(j);
        throw new UserAlreadyExistsException(j.toBareJID());
    } catch (UserNotFoundException e) {
    // Roster item does not exist. Try to add it.
    }
    if (r != null) {
        List<String> groups = new ArrayList<String>();
        if (groupNames != null) {
            StringTokenizer tkn = new StringTokenizer(groupNames, ",");
            while (tkn.hasMoreTokens()) {
                groups.add(tkn.nextToken());
            }
        }
        RosterItem ri = r.createRosterItem(j, itemName, groups, false, true);
        if (subscription == null) {
            subscription = "0";
        }
        ri.setSubStatus(RosterItem.SubType.getTypeFromInt(Integer.parseInt(subscription)));
        r.updateRosterItem(ri);
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) RosterItem(org.jivesoftware.openfire.roster.RosterItem) StringTokenizer(java.util.StringTokenizer) Roster(org.jivesoftware.openfire.roster.Roster) JID(org.xmpp.packet.JID) ArrayList(java.util.ArrayList) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException)

Example 4 with UserNotFoundException

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

the class UserServiceLegacy method userSerivceRequest.

@GET
@Path("/userservice")
public Response userSerivceRequest() throws IOException {
    // Printwriter for writing out responses to browser
    PrintWriter out = response.getWriter();
    if (!plugin.getAllowedIPs().isEmpty()) {
        // Get client's IP address
        String ipAddress = request.getHeader("x-forwarded-for");
        if (ipAddress == null) {
            ipAddress = request.getHeader("X_FORWARDED_FOR");
            if (ipAddress == null) {
                ipAddress = request.getHeader("X-Forward-For");
                if (ipAddress == null) {
                    ipAddress = request.getRemoteAddr();
                }
            }
        }
        if (!plugin.getAllowedIPs().contains(ipAddress)) {
            Log.warn("User service rejected service to IP address: " + ipAddress);
            replyError("RequestNotAuthorised", response, out);
            return Response.status(200).build();
        }
    }
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    String name = request.getParameter("name");
    String email = request.getParameter("email");
    String type = request.getParameter("type");
    String secret = request.getParameter("secret");
    String groupNames = request.getParameter("groups");
    String item_jid = request.getParameter("item_jid");
    String sub = request.getParameter("subscription");
    // Check that our plugin is enabled.
    if (!plugin.isEnabled()) {
        Log.warn("User service plugin is disabled: " + request.getQueryString());
        replyError("UserServiceDisabled", response, out);
        return Response.status(200).build();
    }
    // Check this request is authorised
    if (secret == null || !secret.equals(plugin.getSecret())) {
        Log.warn("An unauthorised user service request was received: " + request.getQueryString());
        replyError("RequestNotAuthorised", response, out);
        return Response.status(200).build();
    }
    // Some checking is required on the username
    if (username == null && !"grouplist".equals(type)) {
        replyError("IllegalArgumentException", response, out);
        return Response.status(200).build();
    }
    if ((type.equals("add_roster") || type.equals("update_roster") || type.equals("delete_roster")) && (item_jid == null || !(sub == null || sub.equals("-1") || sub.equals("0") || sub.equals("1") || sub.equals("2") || sub.equals("3")))) {
        replyError("IllegalArgumentException", response, out);
        return Response.status(200).build();
    }
    // Check the request type and process accordingly
    try {
        if ("grouplist".equals(type)) {
            String message = "";
            for (String groupname : plugin.getAllGroups()) {
                message += "<groupname>" + groupname + "</groupname>";
            }
            replyMessage(message, response, out);
        } else {
            username = username.trim().toLowerCase();
            username = JID.escapeNode(username);
            username = Stringprep.nodeprep(username);
            if ("add".equals(type)) {
                plugin.createUser(username, password, name, email, groupNames);
                replyMessage("ok", response, out);
            } else if ("delete".equals(type)) {
                plugin.deleteUser(username);
                replyMessage("ok", response, out);
            } else if ("enable".equals(type)) {
                plugin.enableUser(username);
                replyMessage("ok", response, out);
            } else if ("disable".equals(type)) {
                plugin.disableUser(username);
                replyMessage("ok", response, out);
            } else if ("update".equals(type)) {
                plugin.updateUser(username, password, name, email, groupNames);
                replyMessage("ok", response, out);
            } else if ("add_roster".equals(type)) {
                plugin.addRosterItem(username, item_jid, name, sub, groupNames);
                replyMessage("ok", response, out);
            } else if ("update_roster".equals(type)) {
                plugin.updateRosterItem(username, item_jid, name, sub, groupNames);
                replyMessage("ok", response, out);
            } else if ("delete_roster".equals(type)) {
                plugin.deleteRosterItem(username, item_jid);
                replyMessage("ok", response, out);
            } else if ("usergrouplist".equals(type)) {
                String message = "";
                for (String groupname : plugin.getUserGroups(username)) {
                    message += "<groupname>" + groupname + "</groupname>";
                }
                replyMessage(message, response, out);
            } else {
                Log.warn("The userService servlet received an invalid request of type: " + type);
            // TODO Do something
            }
        }
    } catch (UserAlreadyExistsException e) {
        replyError("UserAlreadyExistsException", response, out);
    } catch (UserNotFoundException e) {
        replyError("UserNotFoundException", response, out);
    } catch (IllegalArgumentException e) {
        replyError("IllegalArgumentException", response, out);
    } catch (SharedGroupException e) {
        replyError("SharedGroupException", response, out);
    } catch (Exception e) {
        replyError(e.toString(), response, out);
    }
    return Response.status(200).build();
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) SharedGroupException(org.jivesoftware.openfire.SharedGroupException) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) IOException(java.io.IOException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) SharedGroupException(org.jivesoftware.openfire.SharedGroupException) PrintWriter(java.io.PrintWriter) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 5 with UserNotFoundException

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

the class UserCreationPlugin method manageSub.

private boolean manageSub(JID target, boolean isSending, Presence.Type type, Roster roster) throws UserAlreadyExistsException, SharedGroupException {
    RosterItem item = null;
    RosterItem.AskType oldAsk;
    RosterItem.SubType oldSub = null;
    RosterItem.RecvType oldRecv;
    boolean newItem = false;
    try {
        if (roster.isRosterItem(target)) {
            item = roster.getRosterItem(target);
        } else {
            if (Presence.Type.unsubscribed == type || Presence.Type.unsubscribe == type || Presence.Type.subscribed == type) {
                // subscription approval from an unknown user
                return false;
            }
            item = roster.createRosterItem(target, false, true);
            item.setGroups(Arrays.asList("Friends"));
            roster.updateRosterItem(item);
            newItem = true;
        }
        // Get a snapshot of the item state
        oldAsk = item.getAskStatus();
        oldSub = item.getSubStatus();
        oldRecv = item.getRecvStatus();
        // Update the item state based in the received presence type
        updateState(item, type, isSending);
        // Update the roster IF the item state has changed
        if (oldAsk != item.getAskStatus() || oldSub != item.getSubStatus() || oldRecv != item.getRecvStatus()) {
            roster.updateRosterItem(item);
        } else if (newItem) {
            // Do not push items with a state of "None + Pending In"
            if (item.getSubStatus() != RosterItem.SUB_NONE || item.getRecvStatus() != RosterItem.RECV_SUBSCRIBE) {
                roster.broadcast(item, false);
            }
        }
    } catch (UserNotFoundException e) {
        // Should be there because we just checked that it's an item
        Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
    }
    return oldSub != item.getSubStatus();
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) RosterItem(org.jivesoftware.openfire.roster.RosterItem)

Aggregations

UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)118 JID (org.xmpp.packet.JID)50 Element (org.dom4j.Element)28 Roster (org.jivesoftware.openfire.roster.Roster)27 RosterItem (org.jivesoftware.openfire.roster.RosterItem)26 User (org.jivesoftware.openfire.user.User)25 UserAlreadyExistsException (org.jivesoftware.openfire.user.UserAlreadyExistsException)23 IQ (org.xmpp.packet.IQ)15 ArrayList (java.util.ArrayList)14 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)12 SharedGroupException (org.jivesoftware.openfire.SharedGroupException)11 Group (org.jivesoftware.openfire.group.Group)10 UserManager (org.jivesoftware.openfire.user.UserManager)10 Workgroup (org.jivesoftware.xmpp.workgroup.Workgroup)10 Presence (org.xmpp.packet.Presence)10 NotFoundException (org.jivesoftware.util.NotFoundException)9 SQLException (java.sql.SQLException)8 List (java.util.List)8 IOException (java.io.IOException)7 Connection (java.sql.Connection)7