Search in sources :

Example 1 with OfflineMessage

use of org.jivesoftware.openfire.OfflineMessage in project Openfire by igniterealtime.

the class Xep227Exporter method exportOfflineMessages.

/**
   * Adding offline messages, if there are some.
   * 
   * @param hostname
   *            host name
   * @param userElement
   *            DOM element
   * @param userName
   *            user name
   */
@SuppressWarnings("unchecked")
private void exportOfflineMessages(String hostname, Element userElement, String userName) {
    Collection<OfflineMessage> offlineMessages = offlineMessagesStore.getMessages(userName, false);
    if (!offlineMessages.isEmpty()) {
        Element offlineElement = userElement.addElement(OFFLINE_MESSAGES_ELEMENT_NAME);
        for (OfflineMessage offMessage : offlineMessages) {
            Element messageElement = offlineElement.addElement(new QName(MESSAGE_ELEMENT_NAME, JABBER_MSG_NS));
            for (Object att : offMessage.getElement().attributes()) {
                Attribute attribute = (Attribute) att;
                messageElement.addAttribute(attribute.getQName(), attribute.getValue());
            }
            for (Iterator<Element> iterator = offMessage.getElement().elementIterator(); iterator.hasNext(); ) {
                Element element = iterator.next();
                messageElement.add(element.createCopy(new QName(element.getName(), (element.getNamespace() == Namespace.NO_NAMESPACE ? JABBER_MSG_NS : element.getNamespace()))));
            }
            /**
         * Adding delay element
         */
            Element delayElement = messageElement.addElement("delay", "urn:xmpp:delay");
            delayElement.addAttribute(FROM_NAME, hostname);
            delayElement.addAttribute("stamp", XMPPDateTimeFormat.format(offMessage.getCreationDate()));
            delayElement.addText("Offline Storage");
        }
    }
}
Also used : Attribute(org.dom4j.Attribute) QName(org.dom4j.QName) Element(org.dom4j.Element) OfflineMessage(org.jivesoftware.openfire.OfflineMessage)

Example 2 with OfflineMessage

use of org.jivesoftware.openfire.OfflineMessage 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 OfflineMessage

use of org.jivesoftware.openfire.OfflineMessage in project Openfire by igniterealtime.

the class IQOfflineMessagesHandler method getItems.

@Override
public Iterator<DiscoItem> getItems(String name, String node, JID senderJID) {
    // Mark that offline messages shouldn't be sent when the user becomes available
    stopOfflineFlooding(senderJID);
    List<DiscoItem> answer = new ArrayList<>();
    for (OfflineMessage offlineMessage : messageStore.getMessages(senderJID.getNode(), false)) {
        answer.add(new DiscoItem(senderJID.asBareJID(), offlineMessage.getFrom().toString(), XMPPDateTimeFormat.format(offlineMessage.getCreationDate()), null));
    }
    return answer.iterator();
}
Also used : ArrayList(java.util.ArrayList) DiscoItem(org.jivesoftware.openfire.disco.DiscoItem) OfflineMessage(org.jivesoftware.openfire.OfflineMessage)

Example 4 with OfflineMessage

use of org.jivesoftware.openfire.OfflineMessage in project Openfire by igniterealtime.

the class IQOfflineMessagesHandler method handleIQ.

@Override
public IQ handleIQ(IQ packet) throws UnauthorizedException {
    IQ reply = IQ.createResultIQ(packet);
    Element offlineRequest = packet.getChildElement();
    JID from = packet.getFrom();
    if (offlineRequest.element("purge") != null) {
        // User requested to delete all offline messages
        messageStore.deleteMessages(from.getNode());
    } else if (offlineRequest.element("fetch") != null) {
        // Mark that offline messages shouldn't be sent when the user becomes available
        stopOfflineFlooding(from);
        // User requested to receive all offline messages
        for (OfflineMessage offlineMessage : messageStore.getMessages(from.getNode(), false)) {
            sendOfflineMessage(from, offlineMessage);
        }
    } else {
        for (Iterator it = offlineRequest.elementIterator("item"); it.hasNext(); ) {
            Element item = (Element) it.next();
            Date creationDate = null;
            try {
                creationDate = xmppDateTime.parseString(item.attributeValue("node"));
            } catch (ParseException e) {
                Log.error("Error parsing date", e);
            }
            if ("view".equals(item.attributeValue("action"))) {
                // User requested to receive specific message
                OfflineMessage offlineMsg = messageStore.getMessage(from.getNode(), creationDate);
                if (offlineMsg != null) {
                    sendOfflineMessage(from, offlineMsg);
                }
            } else if ("remove".equals(item.attributeValue("action"))) {
                // User requested to delete specific message
                if (messageStore.getMessage(from.getNode(), creationDate) != null) {
                    messageStore.deleteMessage(from.getNode(), creationDate);
                } else {
                    // If the requester is authorized but the node does not exist, the server MUST return a <item-not-found/> error.
                    reply.setError(PacketError.Condition.item_not_found);
                }
            }
        }
    }
    return reply;
}
Also used : JID(org.xmpp.packet.JID) Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ) Iterator(java.util.Iterator) ParseException(java.text.ParseException) OfflineMessage(org.jivesoftware.openfire.OfflineMessage) Date(java.util.Date)

Example 5 with OfflineMessage

use of org.jivesoftware.openfire.OfflineMessage in project Openfire by igniterealtime.

the class Xep227Exporter method importOffLineMessages.

/**
   * @param userElement
   * @param offlineMessages
   */
@SuppressWarnings("unchecked")
private void importOffLineMessages(Element userElement, List<OfflineMessage> offlineMessages) {
    Log.debug("importOffLineMessages");
    // TODO Auto-generated method stub
    Iterator<Element> messageIter = userElement.elementIterator(MESSAGE_ELEMENT_NAME);
    while (messageIter.hasNext()) {
        Element msgElement = messageIter.next();
        String creationDateStr = null;
        if (msgElement.element(DELAY_ELEMENT_NAME) != null) {
            creationDateStr = msgElement.element(DELAY_ELEMENT_NAME).attributeValue(STAMP_NAME);
        }
        Date creationDate = null;
        try {
            if (creationDateStr != null) {
                creationDate = dateformater.parse(creationDateStr);
            }
        } catch (ParseException e) {
            Log.warn("Date not parsable:" + e.getLocalizedMessage());
        }
        offlineMessages.add(new OfflineMessage(creationDate, msgElement));
    }
}
Also used : Element(org.dom4j.Element) ParseException(java.text.ParseException) Date(java.util.Date) OfflineMessage(org.jivesoftware.openfire.OfflineMessage)

Aggregations

OfflineMessage (org.jivesoftware.openfire.OfflineMessage)5 Element (org.dom4j.Element)4 ParseException (java.text.ParseException)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 StringprepException (gnu.inet.encoding.StringprepException)1 Iterator (java.util.Iterator)1 Attribute (org.dom4j.Attribute)1 QName (org.dom4j.QName)1 DiscoItem (org.jivesoftware.openfire.disco.DiscoItem)1 RosterItem (org.jivesoftware.openfire.roster.RosterItem)1 UserAlreadyExistsException (org.jivesoftware.openfire.user.UserAlreadyExistsException)1 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)1 IQ (org.xmpp.packet.IQ)1 JID (org.xmpp.packet.JID)1