Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / xmpp / Message.java
blob372873db803ed0c1f46101d139cd7e6cefa89e9c
1 // Copyright 2009 Google Inc. All Rights Reserved.
3 package com.google.appengine.api.xmpp;
5 import java.util.Arrays;
7 /**
8 * Class that represents an XMPP message. {@code Message} objects can
9 * be constructed (via {@link MessageBuilder} and sent as outgoing
10 * messages, or can be parsed as incoming messages by {@link
11 * XMPPService#parseMessage}.
14 public class Message {
15 private final MessageType messageType;
16 private final boolean asXml;
17 private final String body;
18 private final String stanza;
19 private final JID fromJid;
20 private final JID[] recipientJids;
22 /**
23 * Constructor for an XMPP message.
25 * @param messageType Type of the message.
26 * @param asXml Whether the body should be interpreted as XML.
27 * @param body The body of the message.
28 * @param fromJid The JID to use as the sender.
29 * @param recipientJids Recipients of the message.
30 * @param stanza The full XMP message stanza, or {@code null} if not
31 * available.
33 Message(MessageType messageType, boolean asXml, String body, String stanza,
34 JID fromJid,
35 JID... recipientJids) {
36 this.messageType = messageType;
37 this.asXml = asXml;
38 this.body = body;
39 this.stanza = stanza;
40 this.fromJid = fromJid;
41 this.recipientJids = recipientJids;
44 public MessageType getMessageType() {
45 return messageType;
48 public boolean isXml() {
49 return asXml;
52 public String getBody() {
53 return body;
56 public JID getFromJid() {
57 return fromJid;
60 public JID[] getRecipientJids() {
61 return recipientJids;
64 /**
65 * This returns the entire XML message stanza for incoming XMPP
66 * messages. For outgoing messages, this field will be ignored.
68 public String getStanza() {
69 return stanza;
72 @Override
73 public String toString() {
74 String formattedStanza;
75 if (stanza == null) {
76 formattedStanza = "null";
77 } else {
78 formattedStanza = "\"" + stanza.replace("\"", "\\\"") + "\"";
81 String formattedBody;
82 if (body == null) {
83 formattedBody = "null";
84 } else {
85 formattedBody = "\"" + body.replace("\"", "\\\"") + "\"";
88 return String.format(
89 "Message(messageType=%s, asXml=%b, body=%s, stanza=%s, " +
90 "fromJid=%s, recipientJids=%s)",
91 messageType, asXml, formattedBody, formattedStanza, fromJid,
92 Arrays.toString(recipientJids));