Skip to content

Commit c71a165

Browse files
committed
fixes #139: Reduce code duplication for Stable/Unique stanza IDs
Code duplication (and thus complexity) is reduced by centralizing where an appropriate stanza ID is obtained.
1 parent 0eabde7 commit c71a165

File tree

3 files changed

+19
-43
lines changed

3 files changed

+19
-43
lines changed

src/java/com/reucon/openfire/plugin/archive/impl/JdbcPersistenceManager.java

+3-17
Original file line numberDiff line numberDiff line change
@@ -353,14 +353,14 @@ public Collection<ArchivedMessage> findMessages(Date startDate, Date endDate, JI
353353
final String first;
354354
final String last;
355355
if ( useStableID ) {
356-
final String firstSid = firstMessage.getStableId();
356+
final String firstSid = firstMessage.getStableId(owner);
357357
if ( firstSid != null && !firstSid.isEmpty() ) {
358358
first = firstSid;
359359
} else {
360360
// Issue #98: Fall-back to using the database-identifier. Although not a stable-id, it at least gives the client the option to paginate.
361361
first = firstMessage.getId().toString();
362362
}
363-
final String lastSid = lastMessage.getStableId();
363+
final String lastSid = lastMessage.getStableId(owner);
364364
if ( lastSid != null && !lastSid.isEmpty()) {
365365
last = lastSid;
366366
} else {
@@ -609,26 +609,12 @@ public static ArchivedMessage getArchivedMessage( long messageId, JID owner )
609609
}
610610

611611
static protected ArchivedMessage asArchivedMessage(JID owner, String fromJID, String fromJIDResource, String toJID, String toJIDResource, Date sentDate, String body, String stanza, Long id) throws DocumentException {
612-
String sid;
613-
if (stanza != null) {
614-
try {
615-
final Document doc = DocumentHelper.parseText(stanza);
616-
final Message message = new Message(doc.getRootElement());
617-
sid = StanzaIDUtil.findFirstUniqueAndStableStanzaID(message, owner.toBareJID());
618-
} catch (Exception e) {
619-
Log.warn("An exception occurred while parsing message with ID {}", id, e);
620-
sid = null;
621-
}
622-
} else {
623-
sid = null;
624-
}
625-
626612
final JID from = new JID(fromJID + ( fromJIDResource == null || fromJIDResource.isEmpty() ? "" : "/" + fromJIDResource ));
627613
final JID to = new JID(toJID + ( toJIDResource == null || toJIDResource.isEmpty() ? "" : "/" + toJIDResource ));
628614

629615
final ArchivedMessage.Direction direction = ArchivedMessage.Direction.getDirection(owner, to);
630616
final JID with = direction == Direction.from ? from : to;
631-
return new ArchivedMessage(id, sentDate, direction, with, sid, body, stanza);
617+
return new ArchivedMessage(id, sentDate, direction, with, body, stanza);
632618
}
633619

634620
private static Long dateToMillis(Date date) {

src/java/com/reucon/openfire/plugin/archive/impl/MucMamPersistenceManager.java

+3-18
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,14 @@ public Collection<ArchivedMessage> findMessages(Date startDate, Date endDate, JI
100100
final String first;
101101
final String last;
102102
if ( useStableID ) {
103-
final String firstSid = firstMessage.getStableId();
103+
final String firstSid = firstMessage.getStableId(owner);
104104
if ( firstSid != null && !firstSid.isEmpty() ) {
105105
first = firstSid;
106106
} else {
107107
// Issue #98: Fall-back to using the database-identifier. Although not a stable-id, it at least gives the client the option to paginate.
108108
first = firstMessage.getId().toString();
109109
}
110-
final String lastSid = lastMessage.getStableId();
110+
final String lastSid = lastMessage.getStableId(owner);
111111
if ( lastSid != null && !lastSid.isEmpty()) {
112112
last = lastSid;
113113
} else {
@@ -235,22 +235,7 @@ static protected ArchivedMessage asArchivedMessage(JID roomJID, String senderJID
235235
with = roomJID;
236236
}
237237

238-
String sid;
239-
if (stanza != null) {
240-
try {
241-
final Document doc = DocumentHelper.parseText(stanza);
242-
final Message message = new Message(doc.getRootElement());
243-
sid = StanzaIDUtil.findFirstUniqueAndStableStanzaID(message, roomJID.toBareJID());
244-
} catch (Exception e) {
245-
Log.warn("An exception occurred while parsing message with ID {}", id, e);
246-
sid = null;
247-
}
248-
} else {
249-
sid = null;
250-
}
251-
252-
final ArchivedMessage archivedMessage = new ArchivedMessage(id, sentDate, ArchivedMessage.Direction.from, with, sid, body, stanza);
253-
return archivedMessage;
238+
return new ArchivedMessage(id, sentDate, ArchivedMessage.Direction.from, with, body, stanza);
254239
}
255240

256241
static Long parseIdentifier( String value, MUCRoom room, boolean useStableID )

src/java/com/reucon/openfire/plugin/archive/model/ArchivedMessage.java

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.reucon.openfire.plugin.archive.model;
22

3+
import com.reucon.openfire.plugin.archive.util.StanzaIDUtil;
34
import org.dom4j.*;
45
import org.jivesoftware.database.JiveID;
56
import org.jivesoftware.openfire.XMPPServer;
@@ -69,15 +70,11 @@ public static Direction getDirection(@Nonnull final JID owner, @Nonnull final JI
6970
@Nullable
7071
private final Message stanza;
7172

72-
@Nullable
73-
private final String stableId;
74-
75-
public ArchivedMessage(@Nullable final Long id, @Nonnull final Date time, @Nonnull final Direction direction, @Nonnull final JID with, @Nullable final String stableId, @Nullable final String body, @Nullable final String stanza) throws DocumentException {
73+
public ArchivedMessage(@Nullable final Long id, @Nonnull final Date time, @Nonnull final Direction direction, @Nonnull final JID with, @Nullable final String body, @Nullable final String stanza) throws DocumentException {
7674
this.id = id;
7775
this.time = time;
7876
this.direction = direction;
7977
this.with = with;
80-
this.stableId = stableId;
8178
this.body = body;
8279

8380
if ( stanza != null) {
@@ -171,16 +168,24 @@ public JID getWith() {
171168
* @return a stable and unique stanza-id value.
172169
*/
173170
@Nullable
174-
public String getStableId()
171+
public String getStableId(final JID owner)
175172
{
176-
return stableId;
173+
if (this.stanza == null) {
174+
return null;
175+
}
176+
177+
try {
178+
return StanzaIDUtil.findFirstUniqueAndStableStanzaID(this.stanza, owner.toBareJID());
179+
} catch (Exception e) {
180+
Log.warn("An exception occurred while parsing message with ID {}", id, e);
181+
return null;
182+
}
177183
}
178184

179185
public String toString() {
180186
StringBuilder sb = new StringBuilder();
181187

182188
sb.append("ArchivedMessage[id=").append(id).append(",");
183-
sb.append("stableId=").append(stableId).append(",");
184189
sb.append("time=").append(time).append(",");
185190
sb.append("direction=").append(direction).append("]");
186191

0 commit comments

Comments
 (0)