1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2025-09-10 18:59:41 +02:00

Importing.

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@1777 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Matt Tucker 2003-01-13 16:58:47 +00:00 committed by mtucker
parent d025187016
commit f3837581f8
25 changed files with 4522 additions and 0 deletions

View file

@ -0,0 +1,168 @@
/**
* $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;
/**
* Authentication packet, which can be used to login to a Jabber server as well as discover
* login information from the server.
*/
public class Authentication extends IQ {
private IQ.Type type;
private String username = null;
private String password = null;
private String resource = null;
/**
* Creates a new Authentication object with the specified type.
*
* @param type the Type of authentication packet.
*/
public Authentication(IQ.Type type) {
this.type = type;
}
public Authentication(String username, String password, String resource) {
this.username = username;
this.password = password;
this.resource = resource;
}
/**
* Returns the type of the authentication packet.
*
* @return
*/
public Type getType() {
return type;
}
/**
* Sets the type of the authentication packet.
*
* @param type
*/
public void setType(Type type) {
this.type = type;
}
/**
* Returns the username.
*
* @return the username.
*/
public String getUsername() {
return username;
}
/**
* Sets the username.
*
* @param username the username.
*/
public void setUsername(String username) {
this.username = username;
}
/**
* Returns the password.
*
* @return the password.
*/
public String getPassword() {
return password;
}
/**
* Sets the password.
*
* @param password the password.
*/
public void setPassword(String password) {
this.password = password;
}
/**
* Returns the resource.
*
* @return the resource.
*/
public String getResource() {
return resource;
}
/**
* Sets the resource.
*
* @param resource the resource.
*/
public void setResource(String resource) {
this.resource = resource;
}
public String getQueryXML() {
StringBuffer buf = new StringBuffer();
buf.append("<query xmlns='jabber:iq:auth'>");
if (username != null) {
buf.append("<username>").append( username).append("</username>");
}
if (password != null) {
buf.append("<password>").append(password).append("</password>");
}
if (resource != null) {
buf.append("<resource>").append(resource).append("</resource>");
}
buf.append("</query>");
return buf.toString();
}
}

View file

@ -0,0 +1,141 @@
/**
* $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;
/**
* Represents a XMPP error subpacket. Typically, a server responds to a request that has
* problems by sending the packet back and including an error packet. Each error has a code
* as well as as an optional text explanation. Typical error codes are as follows:
*
* <table border=1>
* <tr><td><b>Code</b></td><td><b>Description</b></td></tr>
* <tr><td> 302 </td><td> Redirect </td></tr>
* <tr><td> 400 </td><td> Bad Request </td></tr>
* <tr><td> 401 </td><td> Unauthorized </td></tr>
* <tr><td> 402 </td><td> Payment Required </td></tr>
* <tr><td> 403 </td><td> Forbidden </td></tr>
* <tr><td> 404 </td><td> Not Found </td></tr>
* <tr><td> 405 </td><td> Not Allowed </td></tr>
* <tr><td> 406 </td><td> Not Acceptable </td></tr>
* <tr><td> 407 </td><td> Registration Required </td></tr>
* <tr><td> 408 </td><td> Request Timeout </td></tr>
* <tr><td> 409 </td><td> Conflict </td></tr>
* <tr><td> 500 </td><td> Internal Server Error </td></tr>
* <tr><td> 501 </td><td> Not Implemented </td></tr>
* <tr><td> 502 </td><td> Remote Server Error </td></tr>
* <tr><td> 503 </td><td> Service Unavailable </td></tr>
* <tr><td> 504 </td><td> Remote Server Timeout </td></tr>
* </table>
*
* @author Matt Tucker
*/
public class Error {
private int code;
private String message;
/**
* Creates a new error with the specified code and no message..
*
* @param code the error code.
*/
public Error (int code) {
this.code = code;
this.message = null;
}
/**
* Creates a new error with the specified code and message.
*
* @param code the error code.
* @param message a message describing the error.
*/
public Error(int code, String message) {
this.code = code;
this.message = message;
}
/**
* Returns the error code.
*
* @return the error code.
*/
public int getCode() {
return code;
}
/**
* Returns the message describing the error, or null if there is no message.
*
* @return the message describing the error, or null if there is no message.
*/
public String getMessage() {
return message;
}
/**
* Returns the error as XML.
*
* @return the error as XML.
*/
public String toXML() {
StringBuffer buf = new StringBuffer();
buf.append("<error>");
buf.append("<code>").append(code).append("</code>");
if (message != null) {
buf.append("<message>").append(message).append("</message>");
}
buf.append("</error>");
return buf.toString();
}
}

View file

@ -0,0 +1,164 @@
/**
* $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;
/**
* A generic IQ packet.
*
* @author Matt Tucker
*/
public abstract class IQ extends Packet {
private Type type = Type.GET;
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
public String toXML() {
StringBuffer buf = new StringBuffer();
buf.append("<iq ");
if (getPacketID() != null) {
buf.append("id=\"" + getPacketID() + "\" ");
}
if (getTo() != null) {
buf.append("to=\"").append(getTo()).append("\" ");
}
if (getFrom() != null) {
buf.append("from=\"").append(getFrom()).append("\" ");
}
if (type == null) {
buf.append("type=\"get\">");
}
else {
buf.append("type=\"").append(getType()).append("\">");
}
// Add the query section if there is one.
String queryXML = getQueryXML();
if (queryXML != null) {
buf.append(queryXML);
}
buf.append("</iq>");
// Add packet properties, if any are defined.
String propertiesXML = getPropertiesXML();
if (propertiesXML != null) {
buf.append(propertiesXML);
}
// Add the error sub-packet, if there is one.
Error error = getError();
if (error != null) {
buf.append(error.toXML());
}
return buf.toString();
}
/**
* Implementations of this method should return the XML of the query section of
* an IQ packet if that section should exist, or <tt>null</tt> otherwise. This lets
* the majority of IQ XML writing be generic, with each sub-class providing just the
* packet specific XML.
*
* @return the query section of the IQ XML.
*/
public abstract String getQueryXML();
/**
* A class to represent the type of the IQ packet. Valid types are:
*
* <ul>
* <li>IQ.Type.GET
* <li>IQ.Type.SET
* <li>IQ.Type.RESULT
* <li>IQ.Type.ERROR
* </ul>
*/
public static class Type {
public static final Type GET = new Type("get");
public static final Type SET = new Type("set");
public static final Type RESULT = new Type("result");
public static final Type ERROR = new Type("error");
public static Type fromString(String type) {
if (type.equals(GET.toString())) {
return GET;
}
else if (type.equals(SET.toString())) {
return SET;
}
else if (type.equals(ERROR.toString())) {
return ERROR;
}
else if (type.equals(RESULT.toString())) {
return RESULT;
}
else {
return null;
}
}
private String value;
private Type(String value) {
this.value = value;
}
public String toString() {
return value;
}
}
}

View file

@ -0,0 +1,320 @@
/**
* $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:
*
* <table>
* <tr><td>&nbsp;</td><td><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;
}
}
}

View file

@ -0,0 +1,348 @@
/**
* $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;
import java.util.*;
import java.io.*;
/**
* Base class for XMPP packets. Every packet has a unique ID (which is automatically
* generated, but can be overriden). Optionally, the "to" and "from" fields can be set,
* as well as an arbitrary number of properties.
*
* Properties provide an easy mechanism for clients to share data. Each property has a
* String name, and a value that is a Java primitive (int, long, float, double, boolean)
* or any Serializable object (a Java object is Serializable when it implements the
* Serializable interface).
*
* @author Matt Tucker
*/
public abstract class Packet {
/**
* A prefix helps to make sure that ID's are unique across mutliple instances.
*/
private static String prefix = StringUtils.randomString(3);
/**
* Keeps track of the current increment, which is appended to the prefix to
* forum a unique ID.
*/
private static long id = 0;
/**
* Returns the next unique id. Each id made up of a short alphanumeric
* prefix along with a unique numeric value.
*
* @return the next id.
*/
private static synchronized String nextID() {
return prefix + Long.toString(id++);
}
private String packetID = nextID();
private String to = null;
private String from = null;
private Map properties = new HashMap();
private Error error = null;
/**
* Returns the unique ID of the packet.
*
* @return the packet's unique ID.
*/
public String getPacketID() {
return packetID;
}
/**
* Sets the unique ID of the packet.
*
* @param packetID the unique ID for the packet.
*/
public void setPacketID(String packetID) {
this.packetID = packetID;
}
/**
* Returns who the packet is being sent "to", or <tt>null</tt> if
* the value is not set. The XMPP protocol often makes the "to"
* attribute optional, so it does not always need to be set.
*
* @return who the packet is being sent to, or <tt>null</tt> if the
* value has not been set.
*/
public String getTo() {
return to;
}
/**
* Sets who the packet is being sent "to". The XMPP protocol often makes
* the "to" attribute optional, so it does not always need to be set.
*
* @param to who the packet is being sent to.
*/
public void setTo(String to) {
this.to = to;
}
/**
* Returns who the packet is being sent "from" or <tt>null</tt> if
* the value is not set. The XMPP protocol often makes the "from"
* attribute optional, so it does not always need to be set.
*
* @return who the packet is being sent from, or <tt>null</tt> if the
* valud has not been set.
*/
public String getFrom() {
return from;
}
/**
* Sets who the packet is being sent "from". The XMPP protocol often
* makes the "from" attribute optional, so it does not always need to
* be set.
*
* @param from who the packet is being sent to.
*/
public void setFrom(String from) {
this.from = from;
}
/**
* Returns the error associated with this packet, or <tt>null</tt> if there are
* no errors.
*
* @return the error sub-packet or <tt>null</tt> if there isn't an error.
*/
public Error getError() {
return error;
}
/**
* Sets the error for this packet.
*
* @param error the error to associate with this packet.
*/
public void setError(Error error) {
this.error = error;
}
/**
* Returns the packet property with the specified name or <tt>null</tt> if the
* property doesn't exist. Property values that were orginally primitives will
* be returned as their object equivalent. For example, an int property will be
* returned as an Integer, a double as a Double, etc.
*
* @param name the name of the property.
* @return the property, or <tt>null</tt> if the property doesn't exist.
*/
public synchronized Object getProperty(String name) {
return properties.get(name);
}
/**
* Sets a packet property with an int value.
*
* @param name the name of the property.
* @param value the value of the property.
*/
public void setProperty(String name, int value) {
setProperty(name, new Integer(value));
}
/**
* Sets a packet property with a long value.
*
* @param name the name of the property.
* @param value the value of the property.
*/
public void setProperty(String name, long value) {
setProperty(name, new Long(value));
}
/**
* Sets a packet property with a float value.
*
* @param name the name of the property.
* @param value the value of the property.
*/
public void setProperty(String name, float value) {
setProperty(name, new Float(value));
}
/**
* Sets a packet property with a double value.
*
* @param name the name of the property.
* @param value the value of the property.
*/
public void setProperty(String name, double value) {
setProperty(name, new Double(value));
}
/**
* Sets a packet property with a bboolean value.
*
* @param name the name of the property.
* @param value the value of the property.
*/
public void setProperty(String name, boolean value) {
setProperty(name, new Boolean(value));
}
/**
* Sets a property with an Object as the value. The value must be Serializable
* or an IllegalArgumentException will be thrown.
*
* @param name the name of the property.
* @param value the value of the property.
*/
public synchronized void setProperty(String name, Object value) {
if (!(value instanceof Serializable)) {
throw new IllegalArgumentException("Value must be serialiazble");
}
properties.put(name, value);
}
/**
* Deletes a property.
*
* @param name the name of the property to delete.
*/
public synchronized void deleteProperty(String name) {
properties.remove(name);
}
/**
* Returns an Iterator for all the property names that are set.
*
* @return an Iterator for all property names.
*/
public synchronized Iterator getPropertyNames() {
return properties.keySet().iterator();
}
/**
* Returns the packet as XML. Every concrete extension of Packet must implement
* this method. In addition to writing out packet-specific data, each extension should
* also write out the error and the properties data if they are defined.
*
* @return the XML format of the packet as a String.
*/
public abstract String toXML();
/**
* Returns the properties portion of the packet as XML or <tt>null</tt> if there are
* no properties.
*
* @return the properties data as XML or <tt>null</tt> if there are no properties.
*/
protected synchronized String getPropertiesXML() {
// Return null if there are no properties.
if (properties.isEmpty()) {
return null;
}
StringBuffer buf = new StringBuffer();
buf.append("<x xmlns=\"http://www.jivesoftware.com/xmlns/xmpp/properties\">");
// Loop through all properties and write them out.
for (Iterator i=getPropertyNames(); i.hasNext(); ) {
String name = (String)i.next();
Object value = getProperty(name);
buf.append("<property>");
buf.append("<name>").append(StringUtils.escapeForXML(name)).append("</name>");
buf.append("<value type=\"");
if (value instanceof Integer) {
buf.append("integer\">").append(value).append("</value>");
}
else if (value instanceof Long) {
buf.append("long\">").append(value).append("</value>");
}
else if (value instanceof Float) {
buf.append("float\">").append(value).append("</value>");
}
else if (value instanceof Double) {
buf.append("double\">").append(value).append("</value>");
}
else if (value instanceof Boolean) {
buf.append("boolean\">").append(value).append("</value>");
}
else if (value instanceof String) {
buf.append("string\">");
buf.append(StringUtils.escapeForXML((String)value));
buf.append("</value>");
}
// Otherwise, it's a generic Serializable object. Serialized objects are in
// a binary format, which won't work well inside of XML. Therefore, we base-64
// encode the binary data before adding it to the SOAP payload.
else {
try {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteStream);
out.writeObject(value);
String encodedVal = StringUtils.encodeBase64(byteStream.toByteArray());
buf.append("java-object\">");
buf.append(encodedVal).append("</value");
}
catch (Exception e) {
}
}
buf.append("</property>");
}
buf.append("</x>");
return buf.toString();
}
}

View file

@ -0,0 +1,263 @@
/**
* $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.*;
/**
* Represents XMPP presence packets. Each packet will either be an "available" or
* "unavailable" message. A number of attributes are optional:
* <ul>
* <li>Status -- free-form text describing a user's presence (i.e., gone to lunch).
* <li>Priority -- non-negative numerical priority of a sender's resource. The
* highest resource priority is the default recipient of packets not addressed
* to a particular resource.
* <li>Mode -- one of four presence modes: chat, away, xa (extended away, and
* dnd (do not disturb).
* </ul>
*
* Note: XMPP presence packets are also used to subscribe and unsubscribe users from
* rosters. However, that functionality is controlled by the Roster object rather
* than Presence objects.
*
* @author Matt Tucker
*/
public class Presence extends Packet {
public static final Mode CHAT = new Mode("chat");
public static final Mode AWAY = new Mode("away");
public static final Mode EXTENDED_AWAY = new Mode("xa");
public static final Mode DO_NOT_DISTURB = new Mode("dnd");
public static final Mode INVISIBLE = new Mode("invisible");
private boolean available = true;
private String status = null;
private int priority = -1;
private Mode mode = null;
/**
* Creates a new presence update. Status, priority, and mode are left un-set.
*
* @param available true if this is an "available" presence packet, or false for
* "unavailable".
*/
public Presence(boolean available) {
this.available = available;
}
/**
* Creates a new presence update with a specified status, priority, and mode.
*
* @param available true if this is an "available" presence update, or false for
* "unavailable".
* @param status a text message describing the presence update.
* @param priority the priority of this presence update.
* @param mode the mode type for this presence update.
*/
public Presence(boolean available, String status, int priority, Mode mode) {
this.available = available;
this.status = status;
this.priority = priority;
this.mode = mode;
}
/**
* Returns true if this is an "available" presence update and false if "unavailable".
*
* @return true if an "available" presence update, false otherwise.
*/
public boolean isAvailable() {
return available;
}
/**
* Toggles the presence update between "available" and "unavailable".
*
* @param available true to make this an "available" presence update, or false
* for "unavailable".
*/
public void setAvailable(boolean available) {
this.available = available;
}
/**
* Returns the status message of the presence update, or <tt>null</tt> if there
* is not status. The status is free-form text describing a user's presence
* (i.e., "gone to lunch").
*
* @return the status message.
*/
public String getStatus() {
return status;
}
/**
* Sets the status message of the presence update. The status is free-form text
* describing a user's presence (i.e., gone to lunch).
*
* @param status the status message.
*/
public void setStatus(String status) {
this.status = status;
}
/**
* Returns the priority of the presence, or -1 if no priority has been set.
*
* @return the priority.
*/
public int getPriority() {
return priority;
}
/**
* Sets the priority of the presence.
*
* @param priority the priority of the presence.
*/
public void setPriority(int priority) {
this.priority = priority;
}
/**
* Returns the mode of the presence update, or <tt>null</tt> if no mode has been set.
*
* @return the mode.
*/
public Mode getMode() {
return mode;
}
/**
* Sets the mode of the presence update.
*
* @param mode the mode.
*/
public void setMode(Mode mode) {
this.mode = mode;
}
public String toXML() {
StringBuffer buf = new StringBuffer();
buf.append("<presence ");
if (getPacketID() != null) {
buf.append("id=\"").append(getPacketID()).append("\" ");
}
if (getTo() != null) {
buf.append("to=\"").append(getTo()).append("\" ");
}
if (getFrom() != null) {
buf.append("from=\"").append(getFrom()).append("\" ");
}
if (available) {
buf.append("type=\"available\">");
}
else {
buf.append("type=\"unavailable\">");
}
if (status != null) {
buf.append("<status>").append(status).append("</status>");
}
if (priority != -1) {
buf.append("<priority>").append(priority).append("</priority>");
}
if (mode != null) {
buf.append("<show>").append(mode).append("</show>");
}
buf.append("</presence>");
return buf.toString();
}
/**
* A typsafe enum class to represent the presecence mode.
*/
public static class Mode {
private String value;
private Mode(String value) {
this.value = value;
}
public String toString() {
return value;
}
/**
* Returns the Mode constant
*/
public static Mode fromString(String value) {
if (value == null) {
return null;
}
else if (value.equals("chat")) {
return CHAT;
}
else if (value.equals("away")) {
return AWAY;
}
else if (value.equals("xa")) {
return EXTENDED_AWAY;
}
else if (value.equals("dnd")) {
return DO_NOT_DISTURB;
}
else if (value.equals("invisible")) {
return INVISIBLE;
}
else {
return null;
}
}
}
}