2003-01-13 17:58:47 +01:00
|
|
|
/**
|
|
|
|
* $RCSfile$
|
|
|
|
* $Revision$
|
|
|
|
* $Date$
|
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* Copyright 2003-2004 Jive Software.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
package org.jivesoftware.smack.packet;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.util.StringUtils;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents XMPP message packets. A message can be one of several types:
|
|
|
|
*
|
|
|
|
* <ul>
|
2004-03-11 16:43:43 +01:00
|
|
|
* <li>Message.Type.NORMAL -- (Default) a normal text message used in email like interface.
|
|
|
|
* <li>Message.Type.CHAT -- a typically short text message used in line-by-line chat interfaces.
|
|
|
|
* <li>Message.Type.GROUP_CHAT -- a chat message sent to a groupchat server for group chats.
|
|
|
|
* <li>Message.Type.HEADLINE -- a text message to be displayed in scrolling marquee displays.
|
|
|
|
* <li>Message.Type.ERROR -- indicates a messaging error.
|
2003-01-13 17:58:47 +01:00
|
|
|
* </ul>
|
|
|
|
*
|
|
|
|
* For each message type, different message fields are typically used as follows:
|
2003-01-15 01:30:12 +01:00
|
|
|
* <p>
|
|
|
|
* <table border="1">
|
|
|
|
* <tr><td> </td><td colspan="5"><b>Message type</b></td></tr>
|
2003-03-07 20:47:08 +01:00
|
|
|
* <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>XMPPError</b></td></tr>
|
2003-01-13 17:58:47 +01:00
|
|
|
* <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 {
|
|
|
|
|
2003-05-07 21:10:53 +02:00
|
|
|
private Type type = Type.NORMAL;
|
2003-01-13 17:58:47 +01:00
|
|
|
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.
|
|
|
|
*
|
2003-01-15 16:02:29 +01:00
|
|
|
* @param to the recipient of the message.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
2003-01-15 16:02:29 +01:00
|
|
|
public Message(String to) {
|
2003-05-07 21:10:53 +02:00
|
|
|
if (to == null) {
|
|
|
|
throw new IllegalArgumentException("Parameter cannot be null");
|
|
|
|
}
|
2003-01-15 16:02:29 +01:00
|
|
|
setTo(to);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new message of the specified type to a recipient.
|
|
|
|
*
|
2003-01-15 16:02:29 +01:00
|
|
|
* @param to the user to send the message to.
|
2003-01-13 17:58:47 +01:00
|
|
|
* @param type the message type.
|
|
|
|
*/
|
2003-01-15 16:02:29 +01:00
|
|
|
public Message(String to, Type type) {
|
|
|
|
if (to == null || type == null) {
|
2003-01-13 17:58:47 +01:00
|
|
|
throw new IllegalArgumentException("Parameters cannot be null.");
|
|
|
|
}
|
2003-01-15 16:02:29 +01:00
|
|
|
setTo(to);
|
2003-01-13 17:58:47 +01:00
|
|
|
this.type = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2003-05-07 21:10:53 +02:00
|
|
|
* of "chat" messages. If no thread id is set, <tt>null</tt> will be returned.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2003-05-07 21:10:53 +02:00
|
|
|
* @return the thread id of the message, or <tt>null</tt> if it doesn't exist.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String toXML() {
|
|
|
|
StringBuffer buf = new StringBuffer();
|
|
|
|
buf.append("<message");
|
2004-02-10 16:36:05 +01:00
|
|
|
if (getPacketID() != null) {
|
|
|
|
buf.append(" id=\"").append(getPacketID()).append("\"");
|
|
|
|
}
|
2003-01-15 16:02:29 +01:00
|
|
|
if (getTo() != null) {
|
2004-05-23 00:53:21 +02:00
|
|
|
buf.append(" to=\"").append(StringUtils.escapeForXML(getTo())).append("\"");
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2003-01-15 16:02:29 +01:00
|
|
|
if (getFrom() != null) {
|
2004-05-23 00:53:21 +02:00
|
|
|
buf.append(" from=\"").append(StringUtils.escapeForXML(getFrom())).append("\"");
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2003-05-07 21:10:53 +02:00
|
|
|
if (type != Type.NORMAL) {
|
2003-01-13 17:58:47 +01:00
|
|
|
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.
|
2003-05-07 21:10:53 +02:00
|
|
|
if (type == Type.ERROR) {
|
2003-03-07 20:47:08 +01:00
|
|
|
XMPPError error = getError();
|
2003-01-13 17:58:47 +01:00
|
|
|
if (error != null) {
|
|
|
|
buf.append(error.toXML());
|
|
|
|
}
|
|
|
|
}
|
2003-05-07 21:10:53 +02:00
|
|
|
// Add packet extensions, if any are defined.
|
2003-05-27 17:07:13 +02:00
|
|
|
buf.append(getExtensionsXML());
|
2003-01-13 17:58:47 +01:00
|
|
|
buf.append("</message>");
|
|
|
|
return buf.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents the type of a message.
|
|
|
|
*/
|
|
|
|
public static class Type {
|
|
|
|
|
2003-05-07 21:10:53 +02:00
|
|
|
/**
|
|
|
|
* (Default) a normal text message used in email like interface.
|
|
|
|
*/
|
|
|
|
public static final Type NORMAL = new Type("normal");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Typically short text message used in line-by-line chat interfaces.
|
|
|
|
*/
|
|
|
|
public static final Type CHAT = new Type("chat");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Chat message sent to a groupchat server for group chats.
|
|
|
|
*/
|
|
|
|
public static final Type GROUP_CHAT = new Type("groupchat");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Text message to be displayed in scrolling marquee displays.
|
|
|
|
*/
|
|
|
|
public static final Type HEADLINE = new Type("headline");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* indicates a messaging error.
|
|
|
|
*/
|
|
|
|
public static final Type ERROR = new Type("error");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a String value into its Type representation.
|
|
|
|
*
|
|
|
|
* @param type the String value.
|
|
|
|
* @return the Type corresponding to the String.
|
|
|
|
*/
|
2003-01-13 17:58:47 +01:00
|
|
|
public static Type fromString(String type) {
|
2003-06-19 16:40:48 +02:00
|
|
|
if (type == null) {
|
|
|
|
return NORMAL;
|
|
|
|
}
|
|
|
|
type = type.toLowerCase();
|
2003-01-15 16:02:29 +01:00
|
|
|
if (CHAT.toString().equals(type)) {
|
2003-01-13 17:58:47 +01:00
|
|
|
return CHAT;
|
|
|
|
}
|
2003-01-15 16:02:29 +01:00
|
|
|
else if (GROUP_CHAT.toString().equals(type)) {
|
2003-01-13 17:58:47 +01:00
|
|
|
return GROUP_CHAT;
|
|
|
|
}
|
2003-01-15 16:02:29 +01:00
|
|
|
else if (HEADLINE.toString().equals(type)) {
|
2003-01-13 17:58:47 +01:00
|
|
|
return HEADLINE;
|
|
|
|
}
|
2003-01-15 16:02:29 +01:00
|
|
|
else if (ERROR.toString().equals(type)) {
|
2003-01-13 17:58:47 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return NORMAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private String value;
|
|
|
|
|
|
|
|
private Type(String value) {
|
|
|
|
this.value = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String toString() {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
2003-05-07 21:10:53 +02:00
|
|
|
}
|