Monitoring Plugin request collection last page XEP-0059

Request:

<iq type='get' id='page1'>
  <retrieve xmlns='urn:xmpp:archive'
            with='room@app.shmakov.voximplant.com'
            start='2015-07-01T09:35:48.646Z'>
    <set xmlns='http://jabber.org/protocol/rsm'>
      <max>3</max>
      <before/>
    </set>
  </retrieve>
</iq>

Reponse (invalid):

<iq type="result" id="page1" to="111(app.shmakov)@voximplant.com/QIP">
     <chat xmlns="urn:xmpp:archive" with="room@app.shmakov.voximplant.com" start="2015-07-01T09:35:48.646Z"/>
</iq>

Should be valid:

<iq type="result" id="page1" to="111(app.shmakov)@voximplant.com/QIP">
     <chat xmlns="urn:xmpp:archive" with="room@app.shmakov.voximplant.com" start="2015-07-01T09:35:48.646Z">
          <to secs="204">
               <body>Pre-Pre-Last Message</body>
          </to>
          <to secs="205">
               <body>Pre-Last Message</body>
          </to>
          <to secs="206">
               <body>Last Message</body>
          </to>
          <set xmlns="http://jabber.org/protocol/rsm">
               <first index="102">102</first>
               <last>104</last>
               <count>105</count>
          </set>
     </chat>
</iq>

XEP-0059: Result Set Management

2.5 Requesting the Last Page in a Result Set

The requesting entity MAY ask for the last page in a result set by including in its request an empty element, and the maximum number of items to return.

Example 11. Requesting the Last Page of a Result Set

<iq type='set' from='stpeter@jabber.org/roundabout' to='users.jabber.org' id='page1'>
  <query xmlns='jabber:iq:search'>
    <nick>Pete</nick>
    <set xmlns='http://jabber.org/protocol/rsm'>
      <max>10</max>
      <before/>
    </set>
  </query>
</iq>
   

The problem in IQRetrieveHandler.java file at line 60:

toIndex = resultSet.getBefore().intValue();

tiIndex is Integer, getBefore is Long.

Then is empty it set to Long.MAX_VALUE at xep0059/XmppResultSet.java

if (setElement.elementText("before").isEmpty()) {
   this.before = Long.valueOf(9223372036854775807L);
}

and casing intValue from Long.MAX_VALUE give -1

So, It’s need fix following from:

if (resultSet.getIndex() != null) {
  fromIndex = resultSet.getIndex();
  toIndex = fromIndex + max;
} else if (resultSet.getAfter() != null) {
  fromIndex = resultSet.getAfter().intValue() + 1;
  toIndex = fromIndex + max;
} else if (resultSet.getBefore() != null) {
  toIndex = resultSet.getBefore().intValue();
  fromIndex = toIndex - max;
}

To:

if (resultSet.getIndex() != null) {
  fromIndex = resultSet.getIndex();
  toIndex = fromIndex + max;
} else if (resultSet.getAfter() != null) {
  fromIndex = resultSet.getAfter().intValue() + 1;
  toIndex = fromIndex + max;
} else if (resultSet.getBefore() != null) {
  if (resultSet.getBefore()!=Long.MAX_VALUE)
       toIndex = resultSet.getBefore().intValue();
  else        toIndex = conversation.getMessages().size();
  fromIndex = toIndex - max;
}

and

if (setElement.elementText("before").isEmpty()) {
     this.before = Long.MAX_VALUE;
}

Thanks!