Smack/source/org/jivesoftware/smack/packet/Message.java

321 lines
10 KiB
Java

/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
* ====================================================================
* The Jive Software License (based on Apache Software License, Version 1.1)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by
* Jive Software (http://www.jivesoftware.com)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Smack" and "Jive Software" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please
* contact webmaster@coolservlets.com.
*
* 5. Products derived from this software may not be called "Smack",
* nor may "Smack" appear in their name, without prior written
* permission of Jive Software.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*/
package org.jivesoftware.smack.packet;
import org.jivesoftware.smack.util.StringUtils;
/**
* Represents XMPP message packets. A message can be one of several types:
*
* <ul>
* <li>NORMAL -- (Default) a normal text message used in email like interface.
* <li>CHAT -- a typically short text message used in line-by-line chat interfaces.
* <li>GROUP_CHAT -- a chat message sent to a groupchat server for group chats.
* <li>HEADLINE -- a text message to be displayed in scrolling marquee displays.
* <li>ERROR -- indicates a messaging error.
* </ul>
*
* For each message type, different message fields are typically used as follows:
* <p>
* <table border="1">
* <tr><td>&nbsp;</td><td colspan="5"><b>Message type</b></td></tr>
* <tr><td><i>Field</i></td><td><b>Normal</b></td><td><b>Chat</b></td><td><b>Group Chat</b></td><td><b>Headline</b></td><td><b>Error</b></td></tr>
* <tr><td><i>subject</i></td> <td>SHOULD</td><td>SHOULD NOT</td><td>SHOULD NOT</td><td>SHOULD NOT</td><td>SHOULD NOT</td></tr>
* <tr><td><i>thread</i></td> <td>OPTIONAL</td><td>SHOULD</td><td>OPTIONAL</td><td>OPTIONAL</td><td>SHOULD NOT</td></tr>
* <tr><td><i>body</i></td> <td>SHOULD</td><td>SHOULD</td><td>SHOULD</td><td>SHOULD</td><td>SHOULD NOT</td></tr>
* <tr><td><i>error</i></td> <td>MUST NOT</td><td>MUST NOT</td><td>MUST NOT</td><td>MUST NOT</td><td>MUST</td></tr>
* </table>
*
* @author Matt Tucker
*/
public class Message extends Packet {
public static final Type NORMAL = new Type("normal");
public static final Type CHAT = new Type("chat");
public static final Type GROUP_CHAT = new Type("groupchat");
public static final Type HEADLINE = new Type("headline");
public static final Type ERROR = new Type("error");
private String sender = null;
private String recipient = null;
private Type type = NORMAL;
private String subject = null;
private String body = null;
private String thread = null;
/**
* Creates a new, "normal" message.
*/
public Message() {
}
/**
* Creates a new "normal" message to the specified recipient.
*
* @param recipient the recipient of the message.
*/
public Message(String recipient) {
this.recipient = recipient;
}
/**
* Creates a new message of the specified type to a recipient.
*
* @param recipient the user to send the message to.
* @param type the message type.
*/
public Message(String recipient, Type type) {
if (recipient == null || type == null) {
throw new IllegalArgumentException("Parameters cannot be null.");
}
this.recipient = recipient;
this.type = type;
}
/**
* Returns the sender of the message, or null if the sender has not been set.
*
* @return the sender of the message.
*/
public String getSender() {
return sender;
}
/**
* Sets the sender of the message.
*
* @param sender the sender of the message.
*/
public void setSender(String sender) {
this.sender = sender;
}
/**
* Returns the recipient of the message, or null if the recipient has not been set.
*
* @return the recipient of the message.
*/
public String getRecipient() {
return recipient;
}
/**
* Sets the recipient of the message.
*
* @param recipient the recipient of the message.
*/
public void setRecipient(String recipient) {
if (recipient == null) {
throw new IllegalArgumentException("Recipient cannot be null.");
}
this.recipient = recipient;
}
/**
* Returns the type of the message.
*
* @return the type of the message.
*/
public Type getType() {
return type;
}
/**
* Sets the type of the message.
*
* @param type the type of the message.
*/
public void setType(Type type) {
if (type == null) {
throw new IllegalArgumentException("Type cannot be null.");
}
this.type = type;
}
/**
* Returns the subject of the message, or null if the subject has not been set.
* The subject is a short description of message contents.
*
* @return the subject of the message.
*/
public String getSubject() {
return subject;
}
/**
* Sets the subject of the message. The subject is a short description of
* message contents.
*
* @param subject the subject of the message.
*/
public void setSubject(String subject) {
this.subject = subject;
}
/**
* Returns the body of the message, or null if the body has not been set. The body
* is the main message contents.
*
* @return the body of the message.
*/
public String getBody() {
return body;
}
/**
* Sets the body of the message. The body is the main message contents.
* @param body
*/
public void setBody(String body) {
this.body = body;
}
/**
* Returns the thread id of the message, which is a unique identifier for a sequence
* of "chat" messages.
*
* @return the thread id of the message.
*/
public String getThread() {
return thread;
}
/**
* Sets the thread id of the message, which is a unique identifier for a sequence
* of "chat" messages.
*
* @param thread the thread id of the message.
*/
public void setThread(String thread) {
this.thread = thread;
}
/**
* Returns the message as XML.
*
* @return the message as XML.
*/
public String toXML() {
StringBuffer buf = new StringBuffer();
buf.append("<message");
buf.append(" id=\"").append(getPacketID()).append("\"");
if (recipient != null) {
buf.append(" to=\"").append(recipient).append("\"");
}
if (sender != null) {
buf.append(" from=\"").append(sender).append("\"");
}
if (type != NORMAL) {
buf.append(" type=\"").append(type).append("\"");
}
buf.append(">");
if (subject != null) {
buf.append("<subject>").append(StringUtils.escapeForXML(subject)).append("</subject>");
}
if (body != null) {
buf.append("<body>").append(StringUtils.escapeForXML(body)).append("</body>");
}
if (thread != null) {
buf.append("<thread>").append(thread).append("</thread>");
}
// Append the error subpacket if the message type is an error.
if (type == ERROR) {
Error error = getError();
if (error != null) {
buf.append(error.toXML());
}
}
String properties = getPropertiesXML();
if (properties != null) {
buf.append(properties);
}
buf.append("</message>");
return buf.toString();
}
/**
* Represents the type of a message.
*/
public static class Type {
public static Type fromString(String type) {
if (type.equals(CHAT.toString())) {
return CHAT;
}
else if (type.equals(GROUP_CHAT.toString())) {
return GROUP_CHAT;
}
else if (type.equals(HEADLINE.toString())) {
return HEADLINE;
}
else if (type.equals(ERROR.toString())) {
return ERROR;
}
else {
return NORMAL;
}
}
private String value;
private Type(String value) {
this.value = value;
}
public String toString() {
return value;
}
}
}