Smack

org.jivesoftware.smack.packet
Class Message

java.lang.Object
  extended by org.jivesoftware.smack.packet.Packet
      extended by org.jivesoftware.smack.packet.Message

public class Message
extends Packet

Represents XMPP message packets. A message can be one of several types:

For each message type, different message fields are typically used as follows:

 Message type
FieldNormalChatGroup ChatHeadlineXMPPError
subject SHOULDSHOULD NOTSHOULD NOTSHOULD NOTSHOULD NOT
thread OPTIONALSHOULDOPTIONALOPTIONALSHOULD NOT
body SHOULDSHOULDSHOULDSHOULDSHOULD NOT
error MUST NOTMUST NOTMUST NOTMUST NOTMUST

Author:
Matt Tucker

Nested Class Summary
static class Message.Body
          Represents a message body, its language and the content of the message.
static class Message.Subject
          Represents a message subject, its language and the content of the subject.
static class Message.Type
          Represents the type of a message.
 
Field Summary
 
Fields inherited from class org.jivesoftware.smack.packet.Packet
DEFAULT_LANGUAGE, ID_NOT_AVAILABLE
 
Constructor Summary
Message()
          Creates a new, "normal" message.
Message(String to)
          Creates a new "normal" message to the specified recipient.
Message(String to, Message.Type type)
          Creates a new message of the specified type to a recipient.
 
Method Summary
 Message.Body addBody(String language, String body)
          Adds a body with a corresponding language.
 Message.Subject addSubject(String language, String subject)
          Adds a subject with a corresponding language.
 boolean equals(Object o)
           
 Collection<Message.Body> getBodies()
          Returns a set of all bodies in this Message, including the default message body accessible from getBody().
 String getBody()
          Returns the default body of the message, or null if the body has not been set.
 String getBody(String language)
          Returns the body corresponding to the language.
 Collection<String> getBodyLanguages()
          Returns all the languages being used for the bodies, not including the default body.
 String getLanguage()
          Returns the xml:lang of this Message.
 String getSubject()
          Returns the default subject of the message, or null if the subject has not been set.
 String getSubject(String language)
          Returns the subject corresponding to the language.
 Collection<String> getSubjectLanguages()
          Returns all the languages being used for the subjects, not including the default subject.
 Collection<Message.Subject> getSubjects()
          Returns a set of all subjects in this Message, including the default message subject accessible from getSubject().
 String getThread()
          Returns the thread id of the message, which is a unique identifier for a sequence of "chat" messages.
 Message.Type getType()
          Returns the type of the message.
 int hashCode()
           
 boolean removeBody(Message.Body body)
          Removes the body from the message and returns true if the body was removed.
 boolean removeBody(String language)
          Removes the body with the given language from the message.
 boolean removeSubject(Message.Subject subject)
          Removes the subject from the message and returns true if the subject was removed.
 boolean removeSubject(String language)
          Removes the subject with the given language from the message.
 void setBody(String body)
          Sets the body of the message.
 void setLanguage(String language)
          Sets the xml:lang of this Message.
 void setSubject(String subject)
          Sets the subject of the message.
 void setThread(String thread)
          Sets the thread id of the message, which is a unique identifier for a sequence of "chat" messages.
 void setType(Message.Type type)
          Sets the type of the message.
 String toXML()
          Returns the packet as XML.
 
Methods inherited from class org.jivesoftware.smack.packet.Packet
addExtension, deleteProperty, getDefaultLanguage, getError, getExtension, getExtension, getExtensions, getExtensionsXML, getFrom, getPacketID, getProperty, getPropertyNames, getTo, getXmlns, nextID, removeExtension, setDefaultXmlns, setError, setFrom, setPacketID, setProperty, setTo
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Message

public Message()
Creates a new, "normal" message.


Message

public Message(String to)
Creates a new "normal" message to the specified recipient.

Parameters:
to - the recipient of the message.

Message

public Message(String to,
               Message.Type type)
Creates a new message of the specified type to a recipient.

Parameters:
to - the user to send the message to.
type - the message type.
Method Detail

getType

public Message.Type getType()
Returns the type of the message. If no type has been set this method will return Message.Type.normal.

Returns:
the type of the message.

setType

public void setType(Message.Type type)
Sets the type of the message.

Parameters:
type - the type of the message.
Throws:
IllegalArgumentException - if null is passed in as the type

getSubject

public String getSubject()
Returns the default subject of the message, or null if the subject has not been set. The subject is a short description of message contents.

The default subject of a message is the subject that corresponds to the message's language. (see getLanguage()) or if no language is set to the applications default language (see Packet.getDefaultLanguage()).

Returns:
the subject of the message.

getSubject

public String getSubject(String language)
Returns the subject corresponding to the language. If the language is null, the method result will be the same as getSubject(). Null will be returned if the language does not have a corresponding subject.

Parameters:
language - the language of the subject to return.
Returns:
the subject related to the passed in language.

getSubjects

public Collection<Message.Subject> getSubjects()
Returns a set of all subjects in this Message, including the default message subject accessible from getSubject().

Returns:
a collection of all subjects in this message.

setSubject

public void setSubject(String subject)
Sets the subject of the message. The subject is a short description of message contents.

Parameters:
subject - the subject of the message.

addSubject

public Message.Subject addSubject(String language,
                                  String subject)
Adds a subject with a corresponding language.

Parameters:
language - the language of the subject being added.
subject - the subject being added to the message.
Returns:
the new Message.Subject
Throws:
NullPointerException - if the subject is null, a null pointer exception is thrown

removeSubject

public boolean removeSubject(String language)
Removes the subject with the given language from the message.

Parameters:
language - the language of the subject which is to be removed
Returns:
true if a subject was removed and false if it was not.

removeSubject

public boolean removeSubject(Message.Subject subject)
Removes the subject from the message and returns true if the subject was removed.

Parameters:
subject - the subject being removed from the message.
Returns:
true if the subject was successfully removed and false if it was not.

getSubjectLanguages

public Collection<String> getSubjectLanguages()
Returns all the languages being used for the subjects, not including the default subject.

Returns:
the languages being used for the subjects.

getBody

public String getBody()
Returns the default body of the message, or null if the body has not been set. The body is the main message contents.

The default body of a message is the body that corresponds to the message's language. (see getLanguage()) or if no language is set to the applications default language (see Packet.getDefaultLanguage()).

Returns:
the body of the message.

getBody

public String getBody(String language)
Returns the body corresponding to the language. If the language is null, the method result will be the same as getBody(). Null will be returned if the language does not have a corresponding body.

Parameters:
language - the language of the body to return.
Returns:
the body related to the passed in language.
Since:
3.0.2

getBodies

public Collection<Message.Body> getBodies()
Returns a set of all bodies in this Message, including the default message body accessible from getBody().

Returns:
a collection of all bodies in this Message.
Since:
3.0.2

setBody

public void setBody(String body)
Sets the body of the message. The body is the main message contents.

Parameters:
body - the body of the message.

addBody

public Message.Body addBody(String language,
                            String body)
Adds a body with a corresponding language.

Parameters:
language - the language of the body being added.
body - the body being added to the message.
Returns:
the new Message.Body
Throws:
NullPointerException - if the body is null, a null pointer exception is thrown
Since:
3.0.2

removeBody

public boolean removeBody(String language)
Removes the body with the given language from the message.

Parameters:
language - the language of the body which is to be removed
Returns:
true if a body was removed and false if it was not.

removeBody

public boolean removeBody(Message.Body body)
Removes the body from the message and returns true if the body was removed.

Parameters:
body - the body being removed from the message.
Returns:
true if the body was successfully removed and false if it was not.
Since:
3.0.2

getBodyLanguages

public Collection<String> getBodyLanguages()
Returns all the languages being used for the bodies, not including the default body.

Returns:
the languages being used for the bodies.
Since:
3.0.2

getThread

public String getThread()
Returns the thread id of the message, which is a unique identifier for a sequence of "chat" messages. If no thread id is set, null will be returned.

Returns:
the thread id of the message, or null if it doesn't exist.

setThread

public void setThread(String thread)
Sets the thread id of the message, which is a unique identifier for a sequence of "chat" messages.

Parameters:
thread - the thread id of the message.

getLanguage

public String getLanguage()
Returns the xml:lang of this Message.

Returns:
the xml:lang of this Message.
Since:
3.0.2

setLanguage

public void setLanguage(String language)
Sets the xml:lang of this Message.

Parameters:
language - the xml:lang of this Message.
Since:
3.0.2

toXML

public String toXML()
Description copied from class: Packet
Returns the packet as XML. Every concrete extension of Packet must implement this method. In addition to writing out packet-specific data, every sub-class should also write out the error and the extensions data if they are defined.

Specified by:
toXML in class Packet
Returns:
the XML format of the packet as a String.

equals

public boolean equals(Object o)
Overrides:
equals in class Packet

hashCode

public int hashCode()
Overrides:
hashCode in class Packet

Smack

Copyright © 2003-2007 Jive Software.