KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smack > packet > Message


1 /**
2  * $RCSfile$
3  * $Revision: 2408 $
4  * $Date: 2004-11-02 20:53:30 -0300 (Tue, 02 Nov 2004) $
5  *
6  * Copyright 2003-2004 Jive Software.
7  *
8  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */

20
21 package org.jivesoftware.smack.packet;
22
23 import org.jivesoftware.smack.util.StringUtils;
24
25 /**
26  * Represents XMPP message packets. A message can be one of several types:
27  *
28  * <ul>
29  * <li>Message.Type.NORMAL -- (Default) a normal text message used in email like interface.
30  * <li>Message.Type.CHAT -- a typically short text message used in line-by-line chat interfaces.
31  * <li>Message.Type.GROUP_CHAT -- a chat message sent to a groupchat server for group chats.
32  * <li>Message.Type.HEADLINE -- a text message to be displayed in scrolling marquee displays.
33  * <li>Message.Type.ERROR -- indicates a messaging error.
34  * </ul>
35  *
36  * For each message type, different message fields are typically used as follows:
37  * <p>
38  * <table border="1">
39  * <tr><td>&nbsp;</td><td colspan="5"><b>Message type</b></td></tr>
40  * <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>
41  * <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>
42  * <tr><td><i>thread</i></td> <td>OPTIONAL</td><td>SHOULD</td><td>OPTIONAL</td><td>OPTIONAL</td><td>SHOULD NOT</td></tr>
43  * <tr><td><i>body</i></td> <td>SHOULD</td><td>SHOULD</td><td>SHOULD</td><td>SHOULD</td><td>SHOULD NOT</td></tr>
44  * <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>
45  * </table>
46  *
47  * @author Matt Tucker
48  */

49 public class Message extends Packet {
50
51     private Type type = Type.NORMAL;
52     private String JavaDoc subject = null;
53     private String JavaDoc body = null;
54     private String JavaDoc thread = null;
55
56     /**
57      * Creates a new, "normal" message.
58      */

59     public Message() {
60     }
61
62     /**
63      * Creates a new "normal" message to the specified recipient.
64      *
65      * @param to the recipient of the message.
66      */

67     public Message(String JavaDoc to) {
68         if (to == null) {
69             throw new IllegalArgumentException JavaDoc("Parameter cannot be null");
70         }
71         setTo(to);
72     }
73
74     /**
75      * Creates a new message of the specified type to a recipient.
76      *
77      * @param to the user to send the message to.
78      * @param type the message type.
79      */

80     public Message(String JavaDoc to, Type type) {
81         if (to == null || type == null) {
82             throw new IllegalArgumentException JavaDoc("Parameters cannot be null.");
83         }
84         setTo(to);
85         this.type = type;
86     }
87
88     /**
89      * Returns the type of the message.
90      *
91      * @return the type of the message.
92      */

93     public Type getType() {
94         return type;
95     }
96
97     /**
98      * Sets the type of the message.
99      *
100      * @param type the type of the message.
101      */

102     public void setType(Type type) {
103         if (type == null) {
104             throw new IllegalArgumentException JavaDoc("Type cannot be null.");
105         }
106         this.type = type;
107     }
108
109     /**
110      * Returns the subject of the message, or null if the subject has not been set.
111      * The subject is a short description of message contents.
112      *
113      * @return the subject of the message.
114      */

115     public String JavaDoc getSubject() {
116         return subject;
117     }
118
119     /**
120      * Sets the subject of the message. The subject is a short description of
121      * message contents.
122      *
123      * @param subject the subject of the message.
124      */

125     public void setSubject(String JavaDoc subject) {
126         this.subject = subject;
127     }
128
129     /**
130      * Returns the body of the message, or null if the body has not been set. The body
131      * is the main message contents.
132      *
133      * @return the body of the message.
134      */

135     public String JavaDoc getBody() {
136         return body;
137     }
138
139     /**
140      * Sets the body of the message. The body is the main message contents.
141      * @param body
142      */

143     public void setBody(String JavaDoc body) {
144         this.body = body;
145     }
146
147     /**
148      * Returns the thread id of the message, which is a unique identifier for a sequence
149      * of "chat" messages. If no thread id is set, <tt>null</tt> will be returned.
150      *
151      * @return the thread id of the message, or <tt>null</tt> if it doesn't exist.
152      */

153     public String JavaDoc getThread() {
154         return thread;
155     }
156
157     /**
158      * Sets the thread id of the message, which is a unique identifier for a sequence
159      * of "chat" messages.
160      *
161      * @param thread the thread id of the message.
162      */

163     public void setThread(String JavaDoc thread) {
164         this.thread = thread;
165     }
166
167     public String JavaDoc toXML() {
168         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
169         buf.append("<message");
170         if (getPacketID() != null) {
171             buf.append(" id=\"").append(getPacketID()).append("\"");
172         }
173         if (getTo() != null) {
174             buf.append(" to=\"").append(StringUtils.escapeForXML(getTo())).append("\"");
175         }
176         if (getFrom() != null) {
177             buf.append(" from=\"").append(StringUtils.escapeForXML(getFrom())).append("\"");
178         }
179         if (type != Type.NORMAL) {
180             buf.append(" type=\"").append(type).append("\"");
181         }
182         buf.append(">");
183         if (subject != null) {
184             buf.append("<subject>").append(StringUtils.escapeForXML(subject)).append("</subject>");
185         }
186         if (body != null) {
187             buf.append("<body>").append(StringUtils.escapeForXML(body)).append("</body>");
188         }
189         if (thread != null) {
190             buf.append("<thread>").append(thread).append("</thread>");
191         }
192         // Append the error subpacket if the message type is an error.
193
if (type == Type.ERROR) {
194             XMPPError error = getError();
195             if (error != null) {
196                 buf.append(error.toXML());
197             }
198         }
199         // Add packet extensions, if any are defined.
200
buf.append(getExtensionsXML());
201         buf.append("</message>");
202         return buf.toString();
203     }
204
205     /**
206      * Represents the type of a message.
207      */

208     public static class Type {
209
210         /**
211          * (Default) a normal text message used in email like interface.
212          */

213         public static final Type NORMAL = new Type("normal");
214
215         /**
216          * Typically short text message used in line-by-line chat interfaces.
217          */

218         public static final Type CHAT = new Type("chat");
219
220         /**
221          * Chat message sent to a groupchat server for group chats.
222          */

223         public static final Type GROUP_CHAT = new Type("groupchat");
224
225         /**
226          * Text message to be displayed in scrolling marquee displays.
227          */

228         public static final Type HEADLINE = new Type("headline");
229
230         /**
231          * indicates a messaging error.
232          */

233         public static final Type ERROR = new Type("error");
234
235         /**
236          * Converts a String value into its Type representation.
237          *
238          * @param type the String value.
239          * @return the Type corresponding to the String.
240          */

241         public static Type fromString(String JavaDoc type) {
242             if (type == null) {
243                 return NORMAL;
244             }
245             type = type.toLowerCase();
246             if (CHAT.toString().equals(type)) {
247                 return CHAT;
248             }
249             else if (GROUP_CHAT.toString().equals(type)) {
250                 return GROUP_CHAT;
251             }
252             else if (HEADLINE.toString().equals(type)) {
253                 return HEADLINE;
254             }
255             else if (ERROR.toString().equals(type)) {
256                 return ERROR;
257             }
258             else {
259                 return NORMAL;
260             }
261         }
262
263         private String JavaDoc value;
264
265         private Type(String JavaDoc value) {
266             this.value = value;
267         }
268
269         public String JavaDoc toString() {
270             return value;
271         }
272     }
273 }
Popular Tags