OfflineMessageStore.java 6.29 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
Matt Tucker's avatar
Matt Tucker committed
6
 * Copyright (C) 2004 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
Matt Tucker's avatar
Matt Tucker committed
8 9
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
Matt Tucker's avatar
Matt Tucker committed
10
 */
Matt Tucker's avatar
Matt Tucker committed
11

Matt Tucker's avatar
Matt Tucker committed
12 13
package org.jivesoftware.messenger;

14 15 16
import org.jivesoftware.database.SequenceManager;
import org.jivesoftware.database.DbConnectionManager;
import org.jivesoftware.util.*;
17
import org.jivesoftware.messenger.container.BasicModule;
18 19 20 21 22 23 24
import org.xmpp.packet.Message;
import org.dom4j.io.SAXReader;
import org.dom4j.DocumentFactory;

import java.util.*;
import java.sql.*;
import java.sql.Connection;
Matt Tucker's avatar
Matt Tucker committed
25 26 27 28 29 30 31 32 33 34

/**
 * Represents the user's offline message storage. A message store holds messages that were sent
 * to the user while they were unavailable. The user can retrieve their messages by setting
 * their presence to "available". The messages will then be delivered normally.
 * Offline message storage is optional in which case, a null implementation is returned that
 * always throws UnauthorizedException adding messages to the store.
 *
 * @author Iain Shigeoka
 */
35
public class OfflineMessageStore extends BasicModule {
36 37 38 39 40 41 42 43 44 45 46 47

    private static final String INSERT_OFFLINE =
        "INSERT INTO jiveOffline (username, messageID, creationDate, messageSize, message) " +
        "VALUES (?, ?, ?, ?, ?)";
    private static final String LOAD_OFFLINE =
        "SELECT message FROM jiveOffline WHERE username=?";
    private static final String SELECT_SIZE_OFFLINE =
        "SELECT SUM(messageSize) FROM jiveOffline WHERE username=?";
    private static final String DELETE_OFFLINE =
        "DELETE FROM jiveOffline WHERE username=?";

    /**
48
     * Returns the instance of <CODE>OfflineMessageStore</CODE> being used by the XMPPServer.
49
     *
50
     * @return the instance of <CODE>OfflineMessageStore</CODE> being used by the XMPPServer.
51 52
     */
    public static OfflineMessageStore getInstance() {
53
        return XMPPServer.getInstance().getOfflineMessageStore();
54 55 56 57 58
    }

    private SAXReader saxReader = new SAXReader();
    private DocumentFactory docFactory = new DocumentFactory();

59 60
    public OfflineMessageStore() {
        super("Offline Message Store");
61 62
    }

Matt Tucker's avatar
Matt Tucker committed
63
    /**
64 65
     * Adds a message to this message store. Messages will be stored and made
     * available for later delivery.
Matt Tucker's avatar
Matt Tucker committed
66
     *
67
     * @param message the message to store.
Matt Tucker's avatar
Matt Tucker committed
68
     */
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    public void addMessage(Message message) {
        if (message == null) {
            return;
        }
        String username = message.getFrom().getNode();
        // If the username is null (such as when an anonymous user), don't store.
        if (username == null) {
            return;
        }

        long messageID = SequenceManager.nextID(JiveConstants.OFFLINE);

        // Get the message in XML format. We add the element to a new document so
        // that we can easily parse the message from the database later.
        String msgXML = docFactory.createDocument(message.getElement()).asXML();

        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(INSERT_OFFLINE);
            pstmt.setString(1, username);
            pstmt.setLong(2, messageID);
            pstmt.setString(3, StringUtils.dateToMillis(new java.util.Date()));
            pstmt.setInt(4, msgXML.length());
            pstmt.setString(5, msgXML);
            pstmt.executeUpdate();
        }

        catch (Exception e) {
            Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
        }
        finally {
            try { if (pstmt != null) { pstmt.close(); } }
            catch (Exception e) { Log.error(e); }
            try { if (con != null) { con.close(); } }
            catch (Exception e) { Log.error(e); }
        }
    }
Matt Tucker's avatar
Matt Tucker committed
108 109

    /**
110 111
     * Returns a Collection of all messages in the store for a user.
     * Messages are deleted after being selected from the database.
Matt Tucker's avatar
Matt Tucker committed
112
     *
113
     * @param username the username of the user who's messages you'd like to receive
Matt Tucker's avatar
Matt Tucker committed
114 115
     * @return An iterator of packets containing all offline messages
     */
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    public Collection<Message> getMessages(String username) {
        List<Message> messages = new ArrayList<Message>();
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(LOAD_OFFLINE);
            pstmt.setString(1, username);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                String msgXML = rs.getString(1);
                messages.add(new Message(saxReader.read(msgXML).getRootElement()));
            }
            rs.close();
            pstmt.close();

            pstmt = con.prepareStatement(DELETE_OFFLINE);
            pstmt.setString(1, username);
            pstmt.executeUpdate();
        }
        catch (Exception e) {
            Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
        }
        finally {
            try { if (pstmt != null) { pstmt.close(); } }
            catch (Exception e) { Log.error(e); }
            try { if (con != null) { con.close(); } }
            catch (Exception e) { Log.error(e); }
        }
        return messages;
    }
Matt Tucker's avatar
Matt Tucker committed
147 148

    /**
149 150
     * Returns the approximate size (in bytes) of the XML messages stored for
     * a particular user.
Matt Tucker's avatar
Matt Tucker committed
151
     *
152 153
     * @param username the username of the user.
     * @return the approximate size of stored messages (in bytes).
Matt Tucker's avatar
Matt Tucker committed
154
     */
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    public int getSize(String username) {
        int size = 0;
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(SELECT_SIZE_OFFLINE);
            pstmt.setString(1, username);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                size = rs.getInt(1);
            }
            rs.close();
        }
        catch (Exception e) {
            Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
        }
        finally {
            try { if (pstmt != null) { pstmt.close(); } }
            catch (Exception e) { Log.error(e); }
            try { if (con != null) { con.close(); } }
            catch (Exception e) { Log.error(e); }
        }
        return size;
    }
Matt Tucker's avatar
Matt Tucker committed
180
}