Package org.jivesoftware.smackx.mam
Class MamManager
java.lang.Object
org.jivesoftware.smack.Manager
org.jivesoftware.smackx.mam.MamManager
A Manager for Message Archive Management (MAM, XEP-0313).
Get an instance of a manager for a message archive
In order to work withMamManager
you need to obtain an instance for a particular archive.
To get the instance for the default archive on the user's server, use the getInstanceFor(XMPPConnection)
method.
XMPPConnection connection = ...
MamManager mamManager = MamManager.getInstanceFor(connection);
If you want to retrieve a manager for a different archive use getInstanceFor(XMPPConnection, Jid)
, which takes the archive's XMPP address as second argument.
Check if MAM is supported
After you got your manager instance, you probably first want to check if MAM is supported. Simply useisSupported()
to check if there is a MAM archive available.
boolean isSupported = mamManager.isSupported();
Message Archive Preferences
After you have verified that the MAM is supported, you probably want to configure the archive first before using it. One of the most important preference is to enable MAM for your account. Some servers set up new accounts with MAM disabled by default. You can do so by callingenableMamForAllMessages()
.
Retrieve current preferences
The archive's preferences can be retrieved usingretrieveArchivingPreferences()
.
Update preferences
UseMamManager.MamPrefsResult.asMamPrefs()
to get a modifiable MamManager.MamPrefs
instance.
After performing the desired changes, use updateArchivingPreferences(MamPrefs)
to update the preferences.
Query the message archive
Querying a message archive involves a two step process. First you need specify the query's arguments, for example a date range. The query arguments of a particular query are represented by aMamManager.MamQueryArgs
instance, which can be build using MamManager.MamQueryArgs.Builder
.
After you have build such an instance, use queryArchive(MamQueryArgs)
to issue the query.
MamQueryArgs mamQueryArgs = MamQueryArgs.builder()
.limitResultsToJid(jid)
.setResultPageSizeTo(10)
.queryLastPage()
.build();
MamQuery mamQuery = mamManager.queryArchive(mamQueryArgs);
On success queryArchive(MamQueryArgs)
returns a MamManager.MamQuery
instance.
The instance will hold one page of the queries result set.
Use MamManager.MamQuery.getMessages()
to retrieve the messages of the archive belonging to the page.
You can get the whole page including all metadata using MamManager.MamQuery.getPage()
.
Paging through the results
Because the matching result set could be potentially very big, a MAM service will probably not return all matching messages. Instead the results are possibly send in multiple pages. To check if the result was complete or if there are further pages, useMamManager.MamQuery.isComplete()
.
If this method returns false
, then you may want to page through the archive.
MamManager.MamQuery
provides convince methods to do so: MamManager.MamQuery.pageNext(int)
and MamManager.MamQuery.pagePrevious(int)
.
MamQuery nextPageMamQuery = mamQuery.pageNext(10);
Get the supported form fields
You can useretrieveFormFields()
to retrieve a list of the supported additional form fields by this archive.
Those fields can be used for further restrict a query.- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final class
static final class
MAM preferences result class.final class
static final class
static final class
-
Method Summary
Modifier and TypeMethodDescriptionthe XMPP address of this MAM archive.static MamManager
getInstanceFor
(XMPPConnection connection) Get a MamManager for the MAM archive of the local entity (the "user") of the given connection.static MamManager
getInstanceFor
(XMPPConnection connection, Jid archiveAddress) static MamManager
getInstanceFor
(MultiUserChat multiUserChat) Get a MamManager for the MAM archive of the givenMultiUserChat
.Returns the MAM namespace used by thisMamManager
.Lookup the archive's message ID of the latest message in the archive.boolean
boolean
Check if this MamManager's archive address supports MAM.queryArchive
(MamManager.MamQueryArgs mamQueryArgs) queryMostRecentPage
(Jid jid, int max) Get the preferences stored in the server.Get the form fields supported by the server.retrieveFormFields
(String node) Get the form fields supported by the server.setDefaultBehavior
(MamPrefsIQ.DefaultBehavior desiredDefaultBehavior) Update the preferences in the server.Methods inherited from class org.jivesoftware.smack.Manager
connection, getAuthenticatedConnectionOrThrow, schedule, schedule, scheduleBlocking
-
Method Details
-
getInstanceFor
Get a MamManager for the MAM archive of the local entity (the "user") of the given connection.- Parameters:
connection
- the XMPP connection to get the archive for.- Returns:
- the instance of MamManager.
-
getInstanceFor
Get a MamManager for the MAM archive of the givenMultiUserChat
. Note that not all MUCs support MAM, hence it is recommended to useisSupported()
to check if MAM is supported by the MUC.- Parameters:
multiUserChat
- the MultiUserChat to retrieve the MamManager for.- Returns:
- the MamManager for the given MultiUserChat.
- Since:
- 4.3.0
-
getInstanceFor
-
getArchiveAddress
the XMPP address of this MAM archive. Note that this method may returnnull
if this MamManager handles the local entity's archive and if the connection has never been authenticated at least once.- Returns:
- the XMPP address of this MAM archive or
null
. - Since:
- 4.3.0
-
getMamNamespace
public String getMamNamespace() throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, SmackException.NoResponseException, InterruptedExceptionReturns the MAM namespace used by thisMamManager
. If the archive does not support any MAM namespace supported by Smack, null is returned.- Returns:
- the MAM namespace used by this manager, null if MAM is not supported
- Throws:
SmackException.NoResponseException
- if there was no response from the remote entity.XMPPException.XMPPErrorException
- if there was an XMPP error returned.SmackException.NotConnectedException
- if the XMPP connection is not connected.InterruptedException
- if the calling thread was interrupted.
-
queryArchive
-
queryMostRecentPage
-
retrieveFormFields
public List<FormField> retrieveFormFields() throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NotLoggedInExceptionGet the form fields supported by the server.- Returns:
- the list of form fields.
- Throws:
SmackException.NoResponseException
- if there was no response from the remote entity.XMPPException.XMPPErrorException
- if there was an XMPP error returned.SmackException.NotConnectedException
- if the XMPP connection is not connected.InterruptedException
- if the calling thread was interrupted.SmackException.NotLoggedInException
- if the XMPP connection is not authenticated.
-
retrieveFormFields
public List<FormField> retrieveFormFields(String node) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NotLoggedInException Get the form fields supported by the server.- Parameters:
node
- The PubSub node name, can be null- Returns:
- the list of form fields.
- Throws:
SmackException.NoResponseException
- if there was no response from the remote entity.XMPPException.XMPPErrorException
- if there was an XMPP error returned.SmackException.NotConnectedException
- if the XMPP connection is not connected.InterruptedException
- if the calling thread was interrupted.SmackException.NotLoggedInException
- if the XMPP connection is not authenticated.
-
isSupported
public boolean isSupported() throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedExceptionCheck if this MamManager's archive address supports MAM.- Returns:
- true if MAM is supported,
false
otherwise. - Throws:
SmackException.NoResponseException
- if there was no response from the remote entity.XMPPException.XMPPErrorException
- if there was an XMPP error returned.SmackException.NotConnectedException
- if the XMPP connection is not connected.InterruptedException
- if the calling thread was interrupted.- Since:
- 4.2.1
- See Also:
-
isAdvancedConfigurationSupported
public boolean isAdvancedConfigurationSupported() throws InterruptedException, XMPPException, SmackException -
getAdvancedConfigurationCommand
-
getMessageUidOfLatestMessage
public String getMessageUidOfLatestMessage() throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, SmackException.NotLoggedInException, InterruptedExceptionLookup the archive's message ID of the latest message in the archive. Returnsnull
if the archive is empty.- Returns:
- the ID of the lastest message or
null
. - Throws:
SmackException.NoResponseException
- if there was no response from the remote entity.XMPPException.XMPPErrorException
- if there was an XMPP error returned.SmackException.NotConnectedException
- if the XMPP connection is not connected.SmackException.NotLoggedInException
- if the XMPP connection is not authenticated.InterruptedException
- if the calling thread was interrupted.- Since:
- 4.3.0
-
retrieveArchivingPreferences
public MamManager.MamPrefsResult retrieveArchivingPreferences() throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NotLoggedInExceptionGet the preferences stored in the server.- Returns:
- the MAM preferences result
- Throws:
SmackException.NoResponseException
- if there was no response from the remote entity.XMPPException.XMPPErrorException
- if there was an XMPP error returned.SmackException.NotConnectedException
- if the XMPP connection is not connected.InterruptedException
- if the calling thread was interrupted.SmackException.NotLoggedInException
- if the XMPP connection is not authenticated.
-
updateArchivingPreferences
public MamManager.MamPrefsResult updateArchivingPreferences(MamManager.MamPrefs mamPrefs) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NotLoggedInException Update the preferences in the server.- Parameters:
mamPrefs
- the MAM preferences to set the archive to- Returns:
- the currently active preferences after the operation.
- Throws:
SmackException.NoResponseException
- if there was no response from the remote entity.XMPPException.XMPPErrorException
- if there was an XMPP error returned.SmackException.NotConnectedException
- if the XMPP connection is not connected.InterruptedException
- if the calling thread was interrupted.SmackException.NotLoggedInException
- if the XMPP connection is not authenticated.- Since:
- 4.3.0
-
enableMamForAllMessages
-
enableMamForRosterMessages
-
setDefaultBehavior
-