Commit d3afd192 authored by Matt Tucker's avatar Matt Tucker Committed by matt

Added support for delayed delivery (JM-103).


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@896 b35dd754-fafc-0310-a699-88a17e54d16e
parent afdefcb4
...@@ -17,18 +17,21 @@ import org.jivesoftware.util.*; ...@@ -17,18 +17,21 @@ import org.jivesoftware.util.*;
import org.jivesoftware.messenger.container.BasicModule; import org.jivesoftware.messenger.container.BasicModule;
import org.xmpp.packet.Message; import org.xmpp.packet.Message;
import org.dom4j.io.SAXReader; import org.dom4j.io.SAXReader;
import org.dom4j.Element;
import java.util.*; import java.util.*;
import java.util.Date;
import java.sql.*; import java.sql.*;
import java.sql.Connection; import java.sql.Connection;
import java.io.StringReader; import java.io.StringReader;
import java.text.SimpleDateFormat;
/** /**
* Represents the user's offline message storage. A message store holds messages that were sent * Represents the user's offline message storage. A message store holds messages that were
* to the user while they were unavailable. The user can retrieve their messages by setting * sent to the user while they were unavailable. The user can retrieve their messages by
* their presence to "available". The messages will then be delivered normally. * 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 * Offline message storage is optional, in which case a null implementation is returned that
* always throws UnauthorizedException adding messages to the store. * always throws UnauthorizedException when adding messages to the store.
* *
* @author Iain Shigeoka * @author Iain Shigeoka
*/ */
...@@ -38,16 +41,20 @@ public class OfflineMessageStore extends BasicModule { ...@@ -38,16 +41,20 @@ public class OfflineMessageStore extends BasicModule {
"INSERT INTO jiveOffline (username, messageID, creationDate, messageSize, message) " + "INSERT INTO jiveOffline (username, messageID, creationDate, messageSize, message) " +
"VALUES (?, ?, ?, ?, ?)"; "VALUES (?, ?, ?, ?, ?)";
private static final String LOAD_OFFLINE = private static final String LOAD_OFFLINE =
"SELECT message FROM jiveOffline WHERE username=?"; "SELECT message, creationDate FROM jiveOffline WHERE username=?";
private static final String SELECT_SIZE_OFFLINE = private static final String SELECT_SIZE_OFFLINE =
"SELECT SUM(messageSize) FROM jiveOffline WHERE username=?"; "SELECT SUM(messageSize) FROM jiveOffline WHERE username=?";
private static final String SELECT_SIZE_ALL_OFFLINE =
"SELECT SUM(messageSize) FROM jiveOffline";
private static final String DELETE_OFFLINE = private static final String DELETE_OFFLINE =
"DELETE FROM jiveOffline WHERE username=?"; "DELETE FROM jiveOffline WHERE username=?";
private SimpleDateFormat dateFormat;
/** /**
* Returns the instance of <CODE>OfflineMessageStore</CODE> being used by the XMPPServer. * Returns the instance of <tt>OfflineMessageStore</tt> being used by the XMPPServer.
* *
* @return the instance of <CODE>OfflineMessageStore</CODE> being used by the XMPPServer. * @return the instance of <tt>OfflineMessageStore</tt> being used by the XMPPServer.
*/ */
public static OfflineMessageStore getInstance() { public static OfflineMessageStore getInstance() {
return XMPPServer.getInstance().getOfflineMessageStore(); return XMPPServer.getInstance().getOfflineMessageStore();
...@@ -55,8 +62,13 @@ public class OfflineMessageStore extends BasicModule { ...@@ -55,8 +62,13 @@ public class OfflineMessageStore extends BasicModule {
private SAXReader saxReader = new SAXReader(); private SAXReader saxReader = new SAXReader();
/**
* Constructs a new offline message store.
*/
public OfflineMessageStore() { public OfflineMessageStore() {
super("Offline Message Store"); super("Offline Message Store");
dateFormat = new SimpleDateFormat("yyyyMMdd'T'hh:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
} }
/** /**
...@@ -122,7 +134,15 @@ public class OfflineMessageStore extends BasicModule { ...@@ -122,7 +134,15 @@ public class OfflineMessageStore extends BasicModule {
ResultSet rs = pstmt.executeQuery(); ResultSet rs = pstmt.executeQuery();
while (rs.next()) { while (rs.next()) {
String msgXML = rs.getString(1); String msgXML = rs.getString(1);
messages.add(new Message(saxReader.read(new StringReader(msgXML)).getRootElement())); Date creationDate = new Date(Long.parseLong(rs.getString(2).trim()));
Message message = new Message(saxReader.read(new StringReader(msgXML)).getRootElement());
// Add a delayed delivery (JEP-0091) element to the message.
Element delay = message.addChildElement("x", "jabber:x:delay");
delay.addElement("from").setText(XMPPServer.getInstance().getServerInfo().getName());
synchronized (dateFormat) {
delay.addElement("stamp").setText(dateFormat.format(creationDate));
}
messages.add(message);
} }
rs.close(); rs.close();
pstmt.close(); pstmt.close();
...@@ -175,4 +195,35 @@ public class OfflineMessageStore extends BasicModule { ...@@ -175,4 +195,35 @@ public class OfflineMessageStore extends BasicModule {
} }
return size; return size;
} }
/**
* Returns the approximate size (in bytes) of the XML messages stored for all
* users.
*
* @return the approximate size of all stored messages (in bytes).
*/
public int getSize() {
int size = 0;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(SELECT_SIZE_ALL_OFFLINE);
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;
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment