1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2025-12-11 15:41:08 +01:00

Smack 1.5.1 + SMACK-73. Branch generated to include TLS support in HEAD.

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@2716 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Gaston Dombiak 2005-08-24 17:52:52 +00:00 committed by gato
parent ca9c6aea93
commit 171af4b325
276 changed files with 40441 additions and 0 deletions

View file

@ -0,0 +1,186 @@
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright 2003-2004 Jive Software.
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package org.jivesoftware.smack.packet;
import org.jivesoftware.smack.util.StringUtils;
/**
* Authentication packet, which can be used to login to a XMPP server as well
* as discover login information from the server.
*/
public class Authentication extends IQ {
private String username = null;
private String password = null;
private String digest = null;
private String resource = null;
/**
* Create a new authentication packet. By default, the packet will be in
* "set" mode in order to perform an actual authentication with the server.
* In order to send a "get" request to get the available authentication
* modes back from the server, change the type of the IQ packet to "get":
*
* <p><tt>setType(IQ.Type.GET);</tt>
*/
public Authentication() {
setType(IQ.Type.SET);
}
/**
* Returns the username, or <tt>null</tt> if the username hasn't been sent.
*
* @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 plain text password or <tt>null</tt> if the password hasn't
* been set.
*
* @return the password.
*/
public String getPassword() {
return password;
}
/**
* Sets the plain text password.
*
* @param password the password.
*/
public void setPassword(String password) {
this.password = password;
}
/**
* Returns the password digest or <tt>null</tt> if the digest hasn't
* been set. Password digests offer a more secure alternative for
* authentication compared to plain text. The digest is the hex-encoded
* SHA-1 hash of the connection ID plus the user's password. If the
* digest and password are set, digest authentication will be used. If
* only one value is set, the respective authentication mode will be used.
*
* @return the digest of the user's password.
*/
public String getDigest() {
return digest;
}
/**
* Sets the digest value using a connection ID and password. Password
* digests offer a more secure alternative for authentication compared to
* plain text. The digest is the hex-encoded SHA-1 hash of the connection ID
* plus the user's password. If the digest and password are set, digest
* authentication will be used. If only one value is set, the respective
* authentication mode will be used.
*
* @param connectionID the connection ID.
* @param password the password.
* @see org.jivesoftware.smack.XMPPConnection#getConnectionID()
*/
public void setDigest(String connectionID, String password) {
this.digest = StringUtils.hash(connectionID + password);
}
/**
* Sets the digest value directly. Password digests offer a more secure
* alternative for authentication compared to plain text. The digest is
* the hex-encoded SHA-1 hash of the connection ID plus the user's password.
* If the digest and password are set, digest authentication will be used.
* If only one value is set, the respective authentication mode will be used.
*
* @param digest the digest, which is the SHA-1 hash of the connection ID
* the user's password, encoded as hex.
* @see org.jivesoftware.smack.XMPPConnection#getConnectionID()
*/
public void setDigest(String digest) {
this.digest = digest;
}
/**
* Returns the resource or <tt>null</tt> if the resource hasn't been set.
*
* @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 getChildElementXML() {
StringBuffer buf = new StringBuffer();
buf.append("<query xmlns=\"jabber:iq:auth\">");
if (username != null) {
if (username.equals("")) {
buf.append("<username/>");
}
else {
buf.append("<username>").append( username).append("</username>");
}
}
if (digest != null) {
if (digest.equals("")) {
buf.append("<digest/>");
}
else {
buf.append("<digest>").append(digest).append("</digest>");
}
}
if (password != null && digest == null) {
if (password.equals("")) {
buf.append("<password/>");
}
else {
buf.append("<password>").append(password).append("</password>");
}
}
if (resource != null) {
if (resource.equals("")) {
buf.append("<resource/>");
}
else {
buf.append("<resource>").append(resource).append("</resource>");
}
}
buf.append("</query>");
return buf.toString();
}
}

View file

@ -0,0 +1,134 @@
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright 2003-2004 Jive Software.
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package org.jivesoftware.smack.packet;
import java.util.*;
/**
* Default implementation of the PacketExtension interface. Unless a PacketExtensionProvider
* is registered with {@link org.jivesoftware.smack.provider.ProviderManager ProviderManager},
* instances of this class will be returned when getting packet extensions.<p>
*
* This class provides a very simple representation of an XML sub-document. Each element
* is a key in a Map with its CDATA being the value. For example, given the following
* XML sub-document:
*
* <pre>
* &lt;foo xmlns="http://bar.com"&gt;
* &lt;color&gt;blue&lt;/color&gt;
* &lt;food&gt;pizza&lt;/food&gt;
* &lt;/foo&gt;</pre>
*
* In this case, getValue("color") would return "blue", and getValue("food") would
* return "pizza". This parsing mechanism mechanism is very simplistic and will not work
* as desired in all cases (for example, if some of the elements have attributes. In those
* cases, a custom PacketExtensionProvider should be used.
*
* @author Matt Tucker
*/
public class DefaultPacketExtension implements PacketExtension {
private String elementName;
private String namespace;
private Map map;
/**
* Creates a new generic packet extension.
*
* @param elementName the name of the element of the XML sub-document.
* @param namespace the namespace of the element.
*/
public DefaultPacketExtension(String elementName, String namespace) {
this.elementName = elementName;
this.namespace = namespace;
}
/**
* Returns the XML element name of the extension sub-packet root element.
*
* @return the XML element name of the packet extension.
*/
public String getElementName() {
return elementName;
}
/**
* Returns the XML namespace of the extension sub-packet root element.
*
* @return the XML namespace of the packet extension.
*/
public String getNamespace() {
return namespace;
}
public String toXML() {
StringBuffer buf = new StringBuffer();
buf.append("<").append(elementName).append(" xmlns=\"").append(namespace).append("\">");
for (Iterator i=getNames(); i.hasNext(); ) {
String name = (String)i.next();
String value = getValue(name);
buf.append("<").append(name).append(">");
buf.append(value);
buf.append("</").append(name).append(">");
}
buf.append("</").append(elementName).append(">");
return buf.toString();
}
/**
* Returns an Iterator for the names that can be used to get
* values of the packet extension.
*
* @return an Iterator for the names.
*/
public synchronized Iterator getNames() {
if (map == null) {
return Collections.EMPTY_LIST.iterator();
}
return Collections.unmodifiableMap(new HashMap(map)).keySet().iterator();
}
/**
* Returns a packet extension value given a name.
*
* @param name the name.
* @return the value.
*/
public synchronized String getValue(String name) {
if (map == null) {
return null;
}
return (String)map.get(name);
}
/**
* Sets a packet extension value using the given name.
*
* @param name the name.
* @param value the value.
*/
public synchronized void setValue(String name, String value) {
if (map == null) {
map = new HashMap();
}
map.put(name, value);
}
}

View file

@ -0,0 +1,167 @@
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright 2003-2004 Jive Software.
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package org.jivesoftware.smack.packet;
import org.jivesoftware.smack.util.StringUtils;
/**
* The base IQ (Info/Query) packet. IQ packets are used to get and set information
* on the server, including authentication, roster operations, and creating
* accounts. Each IQ packet has a specific type that indicates what type of action
* is being taken: "get", "set", "result", or "error".<p>
*
* IQ packets can contain a single child element that exists in a specific XML
* namespace. The combination of the element name and namespace determines what
* type of IQ packet it is. Some example IQ subpacket snippets:<ul>
*
* <li>&lt;query xmlns="jabber:iq:auth"&gt; -- an authentication IQ.
* <li>&lt;query xmlns="jabber:iq:private"&gt; -- a private storage IQ.
* <li>&lt;pubsub xmlns="http://jabber.org/protocol/pubsub"&gt; -- a pubsub IQ.
* </ul>
*
* @author Matt Tucker
*/
public abstract class IQ extends Packet {
private Type type = Type.GET;
/**
* Returns the type of the IQ packet.
*
* @return the type of the IQ packet.
*/
public Type getType() {
return type;
}
/**
* Sets the type of the IQ packet.
*
* @param type the type of the IQ packet.
*/
public void setType(Type type) {
if (type == null) {
this.type = Type.GET;
}
else {
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(StringUtils.escapeForXML(getTo())).append("\" ");
}
if (getFrom() != null) {
buf.append("from=\"").append(StringUtils.escapeForXML(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 = getChildElementXML();
if (queryXML != null) {
buf.append(queryXML);
}
// Add the error sub-packet, if there is one.
XMPPError error = getError();
if (error != null) {
buf.append(error.toXML());
}
buf.append("</iq>");
return buf.toString();
}
/**
* Returns the sub-element XML section of the IQ packet, or <tt>null</tt> if there
* isn't one. Packet extensions <b>must</b> be included, if any are defined.<p>
*
* Extensions of this class must override this method.
*
* @return the child element section of the IQ XML.
*/
public abstract String getChildElementXML();
/**
* A class to represent the type of the IQ packet. The 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");
/**
* Converts a String into the corresponding types. Valid String values
* that can be converted to types are: "get", "set", "result", and "error".
*
* @param type the String value to covert.
* @return the corresponding Type.
*/
public static Type fromString(String type) {
if (type == null) {
return null;
}
type = type.toLowerCase();
if (GET.toString().equals(type)) {
return GET;
}
else if (SET.toString().equals(type)) {
return SET;
}
else if (ERROR.toString().equals(type)) {
return ERROR;
}
else if (RESULT.toString().equals(type)) {
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,273 @@
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright 2003-2004 Jive Software.
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
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>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.
* </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>XMPPError</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 {
private Type 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 to the recipient of the message.
*/
public Message(String to) {
if (to == null) {
throw new IllegalArgumentException("Parameter cannot be null");
}
setTo(to);
}
/**
* Creates a new message of the specified type to a recipient.
*
* @param to the user to send the message to.
* @param type the message type.
*/
public Message(String to, Type type) {
if (to == null || type == null) {
throw new IllegalArgumentException("Parameters cannot be null.");
}
setTo(to);
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
* of "chat" messages. If no thread id is set, <tt>null</tt> will be returned.
*
* @return the thread id of the message, or <tt>null</tt> if it doesn't exist.
*/
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");
if (getPacketID() != null) {
buf.append(" id=\"").append(getPacketID()).append("\"");
}
if (getTo() != null) {
buf.append(" to=\"").append(StringUtils.escapeForXML(getTo())).append("\"");
}
if (getFrom() != null) {
buf.append(" from=\"").append(StringUtils.escapeForXML(getFrom())).append("\"");
}
if (type != 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 == Type.ERROR) {
XMPPError error = getError();
if (error != null) {
buf.append(error.toXML());
}
}
// Add packet extensions, if any are defined.
buf.append(getExtensionsXML());
buf.append("</message>");
return buf.toString();
}
/**
* Represents the type of a message.
*/
public static class Type {
/**
* (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.
*/
public static Type fromString(String type) {
if (type == null) {
return NORMAL;
}
type = type.toLowerCase();
if (CHAT.toString().equals(type)) {
return CHAT;
}
else if (GROUP_CHAT.toString().equals(type)) {
return GROUP_CHAT;
}
else if (HEADLINE.toString().equals(type)) {
return HEADLINE;
}
else if (ERROR.toString().equals(type)) {
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,423 @@
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright 2003-2004 Jive Software.
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
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 {
/**
* Constant used as packetID to indicate that a packet has no id. To indicate that a packet
* has no id set this constant as the packet's id. When the packet is asked for its id the
* answer will be <tt>null</tt>.
*/
public static final String ID_NOT_AVAILABLE = "ID_NOT_AVAILABLE";
/**
* A prefix helps to make sure that ID's are unique across mutliple instances.
*/
private static String prefix = StringUtils.randomString(5) + "-";
/**
* 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 = null;
private String to = null;
private String from = null;
private List packetExtensions = null;
private Map properties = null;
private XMPPError error = null;
/**
* Returns the unique ID of the packet. The returned value could be <tt>null</tt> when
* ID_NOT_AVAILABLE was set as the packet's id.
*
* @return the packet's unique ID or <tt>null</tt> if the packet's id is not available.
*/
public String getPacketID() {
if (ID_NOT_AVAILABLE.equals(packetID)) {
return null;
}
if (packetID == null) {
packetID = nextID();
}
return packetID;
}
/**
* Sets the unique ID of the packet. To indicate that a packet has no id
* pass the constant ID_NOT_AVAILABLE as the packet's id value.
*
* @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 XMPPError getError() {
return error;
}
/**
* Sets the error for this packet.
*
* @param error the error to associate with this packet.
*/
public void setError(XMPPError error) {
this.error = error;
}
/**
* Returns an Iterator for the packet extensions attached to the packet.
*
* @return an Iterator for the packet extensions.
*/
public synchronized Iterator getExtensions() {
if (packetExtensions == null) {
return Collections.EMPTY_LIST.iterator();
}
return Collections.unmodifiableList(new ArrayList(packetExtensions)).iterator();
}
/**
* Returns the first packet extension that matches the specified element name and
* namespace, or <tt>null</tt> if it doesn't exist. Packet extensions are
* are arbitrary XML sub-documents in standard XMPP packets. By default, a
* DefaultPacketExtension instance will be returned for each extension. However,
* PacketExtensionProvider instances can be registered with the
* {@link org.jivesoftware.smack.provider.ProviderManager ProviderManager}
* class to handle custom parsing. In that case, the type of the Object
* will be determined by the provider.
*
* @param elementName the XML element name of the packet extension.
* @param namespace the XML element namespace of the packet extension.
* @return the extension, or <tt>null</tt> if it doesn't exist.
*/
public synchronized PacketExtension getExtension(String elementName, String namespace) {
if (packetExtensions == null || elementName == null || namespace == null) {
return null;
}
for (Iterator i=packetExtensions.iterator(); i.hasNext(); ) {
PacketExtension ext = (PacketExtension)i.next();
if (elementName.equals(ext.getElementName()) && namespace.equals(ext.getNamespace())) {
return ext;
}
}
return null;
}
/**
* Adds a packet extension to the packet.
*
* @param extension a packet extension.
*/
public synchronized void addExtension(PacketExtension extension) {
if (packetExtensions == null) {
packetExtensions = new ArrayList();
}
packetExtensions.add(extension);
}
/**
* Removes a packet extension from the packet.
*
* @param extension the packet extension to remove.
*/
public synchronized void removeExtension(PacketExtension extension) {
if (packetExtensions != null) {
packetExtensions.remove(extension);
}
}
/**
* 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) {
if (properties == null) {
return null;
}
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");
}
if (properties == null) {
properties = new HashMap();
}
properties.put(name, value);
}
/**
* Deletes a property.
*
* @param name the name of the property to delete.
*/
public synchronized void deleteProperty(String name) {
if (properties == null) {
return;
}
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() {
if (properties == null) {
return Collections.EMPTY_LIST.iterator();
}
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, every sub-class
* should also write out the error and the extensions data if they are defined.
*
* @return the XML format of the packet as a String.
*/
public abstract String toXML();
/**
* Returns the extension sub-packets (including properties data) as an XML
* String, or the Empty String if there are no packet extensions.
*
* @return the extension sub-packets as XML or the Empty String if there
* are no packet extensions.
*/
protected synchronized String getExtensionsXML() {
StringBuffer buf = new StringBuffer();
// Add in all standard extension sub-packets.
Iterator extensions = getExtensions();
while (extensions.hasNext()) {
PacketExtension extension = (PacketExtension)extensions.next();
buf.append(extension.toXML());
}
// Add in packet properties.
if (properties != null && !properties.isEmpty()) {
buf.append("<properties 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.
else {
ByteArrayOutputStream byteStream = null;
ObjectOutputStream out = null;
try {
byteStream = new ByteArrayOutputStream();
out = new ObjectOutputStream(byteStream);
out.writeObject(value);
buf.append("java-object\">");
String encodedVal = StringUtils.encodeBase64(byteStream.toByteArray());
buf.append(encodedVal).append("</value>");
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (out != null) {
try { out.close(); } catch (Exception e) { }
}
if (byteStream != null) {
try { byteStream.close(); } catch (Exception e) { }
}
}
}
buf.append("</property>");
}
buf.append("</properties>");
}
return buf.toString();
}
}

View file

@ -0,0 +1,56 @@
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright 2003-2004 Jive Software.
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package org.jivesoftware.smack.packet;
/**
* Interface to represent packet extensions. A packet extension is an XML subdocument
* with a root element name and namespace. Packet extensions are used to provide
* extended functionality beyond what is in the base XMPP specification. Examples of
* packet extensions include message events, message properties, and extra presence data.
* IQ packets cannot contain packet extensions.
*
* @see DefaultPacketExtension
* @see org.jivesoftware.smack.provider.PacketExtensionProvider
* @author Matt Tucker
*/
public interface PacketExtension {
/**
* Returns the root element name.
*
* @return the element name.
*/
public String getElementName();
/**
* Returns the root element XML namespace.
*
* @return the namespace.
*/
public String getNamespace();
/**
* Returns the XML reppresentation of the PacketExtension.
*
* @return the packet extension as XML.
*/
public String toXML();
}

View file

@ -0,0 +1,327 @@
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright 2003-2004 Jive Software.
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package org.jivesoftware.smack.packet;
import org.jivesoftware.smack.util.StringUtils;
/**
* Represents XMPP presence packets. Every presence packet has a type, which is one of
* the following values:
* <ul>
* <li><tt>Presence.Type.AVAILABLE</tt> -- (Default) indicates the user is available to
* receive messages.
* <li><tt>Presence.Type.UNAVAILABLE</tt> -- the user is unavailable to receive messages.
* <li><tt>Presence.Type.SUBSCRIBE</tt> -- request subscription to recipient's presence.
* <li><tt>Presence.Type.SUBSCRIBED</tt> -- grant subscription to sender's presence.
* <li><tt>Presence.Type.UNSUBSCRIBE</tt> -- request removal of subscription to sender's
* presence.
* <li><tt>Presence.Type.UNSUBSCRIBED</tt> -- grant removal of subscription to sender's
* presence.
* <li><tt>Presence.Type.ERROR</tt> -- the presence packet contains an error message.
* </ul><p>
*
* 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 five presence modes: available (the default), chat, away,
* xa (extended away, and dnd (do not disturb).
* </ul><p>
*
* Presence packets are used for two purposes. First, to notify the server of our
* the clients current presence status. Second, they are used to subscribe and
* unsubscribe users from the roster.
*
* @see RosterPacket
* @author Matt Tucker
*/
public class Presence extends Packet {
private Type type = Type.AVAILABLE;
private String status = null;
private int priority = -1;
private Mode mode = Mode.AVAILABLE;
/**
* Creates a new presence update. Status, priority, and mode are left un-set.
*
* @param type the type.
*/
public Presence(Type type) {
this.type = type;
}
/**
* Creates a new presence update with a specified status, priority, and mode.
*
* @param type the type.
* @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(Type type, String status, int priority, Mode mode) {
this.type = type;
this.status = status;
this.priority = priority;
this.mode = mode;
}
/**
* Returns the type of this presence packet.
*
* @return the type of the presence packet.
*/
public Type getType() {
return type;
}
/**
* Sets the type of the presence packet.
*
* @param type the type of the presence packet.
*/
public void setType(Type type) {
this.type = type;
}
/**
* Returns the status message of the presence update, or <tt>null</tt> if there
* is not a 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. The valid range is -128 through 128.
*
* @param priority the priority of the presence.
* @throws IllegalArgumentException if the priority is outside the valid range.
*/
public void setPriority(int priority) {
if (priority < -128 || priority > 128) {
throw new IllegalArgumentException("Priority value " + priority +
" is not valid. Valid range is -128 through 128.");
}
this.priority = priority;
}
/**
* Returns the mode of the presence update.
*
* @return the mode.
*/
public Mode getMode() {
return mode;
}
/**
* Sets the mode of the presence update. For the standard "available" state, set
* the mode to <tt>null</tt>.
*
* @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(StringUtils.escapeForXML(getTo())).append("\"");
}
if (getFrom() != null) {
buf.append(" from=\"").append(StringUtils.escapeForXML(getFrom())).append("\"");
}
if (type != Type.AVAILABLE) {
buf.append(" type=\"").append(type).append("\"");
}
buf.append(">");
if (status != null) {
buf.append("<status>").append(status).append("</status>");
}
if (priority != -1) {
buf.append("<priority>").append(priority).append("</priority>");
}
if (mode != null && mode != Mode.AVAILABLE) {
buf.append("<show>").append(mode).append("</show>");
}
buf.append(this.getExtensionsXML());
// Add the error sub-packet, if there is one.
XMPPError error = getError();
if (error != null) {
buf.append(error.toXML());
}
buf.append("</presence>");
return buf.toString();
}
public String toString() {
StringBuffer buf = new StringBuffer();
buf.append(type);
if (mode != null) {
buf.append(": ").append(mode);
}
if (status != null) {
buf.append(" (").append(status).append(")");
}
return buf.toString();
}
/**
* A typsafe enum class to represent the presecence type.
*/
public static class Type {
public static final Type AVAILABLE = new Type("available");
public static final Type UNAVAILABLE = new Type("unavailable");
public static final Type SUBSCRIBE = new Type("subscribe");
public static final Type SUBSCRIBED = new Type("subscribed");
public static final Type UNSUBSCRIBE = new Type("unsubscribe");
public static final Type UNSUBSCRIBED = new Type("unsubscribed");
public static final Type ERROR = new Type("error");
private String value;
private Type(String value) {
this.value = value;
}
public String toString() {
return value;
}
/**
* Returns the type constant associated with the String value.
*/
public static Type fromString(String value) {
if (value == null) {
return AVAILABLE;
}
value = value.toLowerCase();
if ("unavailable".equals(value)) {
return UNAVAILABLE;
}
else if ("subscribe".equals(value)) {
return SUBSCRIBE;
}
else if ("subscribed".equals(value)) {
return SUBSCRIBED;
}
else if ("unsubscribe".equals(value)) {
return UNSUBSCRIBE;
}
else if ("unsubscribed".equals(value)) {
return UNSUBSCRIBED;
}
else if ("error".equals(value)) {
return ERROR;
}
// Default to available.
else {
return AVAILABLE;
}
}
}
/**
* A typsafe enum class to represent the presence mode.
*/
public static class Mode {
public static final Mode AVAILABLE = new Mode("available");
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 String value;
private Mode(String value) {
this.value = value;
}
public String toString() {
return value;
}
/**
* Returns the mode constant associated with the String value.
*/
public static Mode fromString(String value) {
if (value == null) {
return AVAILABLE;
}
value = value.toLowerCase();
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 AVAILABLE;
}
}
}
}

View file

@ -0,0 +1,113 @@
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright 2003-2004 Jive Software.
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package org.jivesoftware.smack.packet;
import java.util.Map;
import java.util.Iterator;
/**
* Represents registration packets. An empty GET query will cause the server to return information
* about it's registration support. SET queries can be used to create accounts or update
* existing account information. XMPP servers may require a number of attributes to be set
* when creating a new account. The standard account attributes are as follows:
* <ul>
* <li>name -- the user's name.
* <li>first -- the user's first name.
* <li>last -- the user's last name.
* <li>email -- the user's email address.
* <li>city -- the user's city.
* <li>state -- the user's state.
* <li>zip -- the user's ZIP code.
* <li>phone -- the user's phone number.
* <li>url -- the user's website.
* <li>date -- the date the registration took place.
* <li>misc -- other miscellaneous information to associate with the account.
* <li>text -- textual information to associate with the account.
* <li>remove -- empty flag to remove account.
* </ul>
*
* @author Matt Tucker
*/
public class Registration extends IQ {
private String instructions = null;
private Map attributes = null;
/**
* Returns the registration instructions, or <tt>null</tt> if no instructions
* have been set. If present, instructions should be displayed to the end-user
* that will complete the registration process.
*
* @return the registration instructions, or <tt>null</tt> if there are none.
*/
public String getInstructions() {
return instructions;
}
/**
* Sets the registration instructions.
*
* @param instructions the registration instructions.
*/
public void setInstructions(String instructions) {
this.instructions = instructions;
}
/**
* Returns the map of String key/value pairs of account attributes.
*
* @return the account attributes.
*/
public Map getAttributes() {
return attributes;
}
/**
* Sets the account attributes. The map must only contain String key/value pairs.
*
* @param attributes the account attributes.
*/
public void setAttributes(Map attributes) {
this.attributes = attributes;
}
public String getChildElementXML() {
StringBuffer buf = new StringBuffer();
buf.append("<query xmlns=\"jabber:iq:register\">");
if (instructions != null) {
buf.append("<instructions>").append(instructions).append("</instructions>");
}
if (attributes != null && attributes.size() > 0) {
Iterator fieldNames = attributes.keySet().iterator();
while (fieldNames.hasNext()) {
String name = (String)fieldNames.next();
String value = (String)attributes.get(name);
buf.append("<").append(name).append(">");
buf.append(value);
buf.append("</").append(name).append(">");
}
}
// Add packet extensions, if any are defined.
buf.append(getExtensionsXML());
buf.append("</query>");
return buf.toString();
}
}

View file

@ -0,0 +1,348 @@
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright 2003-2004 Jive Software.
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package org.jivesoftware.smack.packet;
import java.util.*;
/**
* Represents XMPP roster packets.
*
* @author Matt Tucker
*/
public class RosterPacket extends IQ {
private List rosterItems = new ArrayList();
/**
* Adds a roster item to the packet.
*
* @param item a roster item.
*/
public void addRosterItem(Item item) {
synchronized (rosterItems) {
rosterItems.add(item);
}
}
/**
* Returns the number of roster items in this roster packet.
*
* @return the number of roster items.
*/
public int getRosterItemCount() {
synchronized (rosterItems) {
return rosterItems.size();
}
}
/**
* Returns an Iterator for the roster items in the packet.
*
* @return and Iterator for the roster items in the packet.
*/
public Iterator getRosterItems() {
synchronized (rosterItems) {
List entries = Collections.unmodifiableList(new ArrayList(rosterItems));
return entries.iterator();
}
}
public String getChildElementXML() {
StringBuffer buf = new StringBuffer();
buf.append("<query xmlns=\"jabber:iq:roster\">");
synchronized (rosterItems) {
for (int i=0; i<rosterItems.size(); i++) {
Item entry = (Item)rosterItems.get(i);
buf.append(entry.toXML());
}
}
buf.append("</query>");
return buf.toString();
}
/**
* A roster item, which consists of a JID, their name, the type of subscription, and
* the groups the roster item belongs to.
*/
public static class Item {
private String user;
private String name;
private ItemType itemType;
private ItemStatus itemStatus;
private List groupNames;
/**
* Creates a new roster item.
*
* @param user the user.
* @param name the user's name.
*/
public Item(String user, String name) {
this.user = user;
this.name = name;
itemType = null;
itemStatus = null;
groupNames = new ArrayList();
}
/**
* Returns the user.
*
* @return the user.
*/
public String getUser() {
return user;
}
/**
* Returns the user's name.
*
* @return the user's name.
*/
public String getName() {
return name;
}
/**
* Sets the user's name.
*
* @param name the user's name.
*/
public void setName(String name) {
this.name = name;
}
/**
* Returns the roster item type.
*
* @return the roster item type.
*/
public ItemType getItemType() {
return itemType;
}
/**
* Sets the roster item type.
*
* @param itemType the roster item type.
*/
public void setItemType(ItemType itemType) {
this.itemType = itemType;
}
/**
* Returns the roster item status.
*
* @return the roster item status.
*/
public ItemStatus getItemStatus() {
return itemStatus;
}
/**
* Sets the roster item status.
*
* @param itemStatus the roster item status.
*/
public void setItemStatus(ItemStatus itemStatus) {
this.itemStatus = itemStatus;
}
/**
* Returns an Iterator for the group names (as Strings) that the roster item
* belongs to.
*
* @return an Iterator for the group names.
*/
public Iterator getGroupNames() {
synchronized (groupNames) {
return Collections.unmodifiableList(groupNames).iterator();
}
}
/**
* Adds a group name.
*
* @param groupName the group name.
*/
public void addGroupName(String groupName) {
synchronized (groupNames) {
if (!groupNames.contains(groupName)) {
groupNames.add(groupName);
}
}
}
/**
* Removes a group name.
*
* @param groupName the group name.
*/
public void removeGroupName(String groupName) {
synchronized (groupNames) {
groupNames.remove(groupName);
}
}
public String toXML() {
StringBuffer buf = new StringBuffer();
buf.append("<item jid=\"").append(user).append("\"");
if (name != null) {
buf.append(" name=\"").append(name).append("\"");
}
if (itemType != null) {
buf.append(" subscription=\"").append(itemType).append("\"");
}
if (itemStatus != null) {
buf.append(" ask=\"").append(itemStatus).append("\"");
}
buf.append(">");
synchronized (groupNames) {
for (int i=0; i<groupNames.size(); i++) {
String groupName = (String)groupNames.get(i);
buf.append("<group>").append(groupName).append("</group>");
}
}
buf.append("</item>");
return buf.toString();
}
}
/**
* The subscription status of a roster item. An optional element that indicates
* the subscription status if a change request is pending.
*/
public static class ItemStatus {
/**
* Request to subcribe.
*/
public static final ItemStatus SUBSCRIPTION_PENDING = new ItemStatus("subscribe");
/**
* Request to unsubscribe.
*/
public static final ItemStatus UNSUBCRIPTION_PENDING = new ItemStatus("unsubscribe");
public static ItemStatus fromString(String value) {
if (value == null) {
return null;
}
value = value.toLowerCase();
if ("unsubscribe".equals(value)) {
return SUBSCRIPTION_PENDING;
}
else if ("subscribe".equals(value)) {
return SUBSCRIPTION_PENDING;
}
else {
return null;
}
}
private String value;
/**
* Returns the item status associated with the specified string.
*
* @param value the item status.
*/
private ItemStatus(String value) {
this.value = value;
}
public String toString() {
return value;
}
}
/**
* The subscription type of a roster item.
*/
public static class ItemType {
/**
* The user and subscriber have no interest in each other's presence.
*/
public static final ItemType NONE = new ItemType("none");
/**
* The user is interested in receiving presence updates from the subscriber.
*/
public static final ItemType TO = new ItemType("to");
/**
* The subscriber is interested in receiving presence updates from the user.
*/
public static final ItemType FROM = new ItemType("from");
/**
* The user and subscriber have a mutual interest in each other's presence.
*/
public static final ItemType BOTH = new ItemType("both");
/**
* The user wishes to stop receiving presence updates from the subscriber.
*/
public static final ItemType REMOVE = new ItemType("remove");
public static ItemType fromString(String value) {
if (value == null) {
return null;
}
value = value.toLowerCase();
if ("none".equals(value)) {
return NONE;
}
else if ("to".equals(value)) {
return TO;
}
else if ("from".equals(value)) {
return FROM;
}
else if ("both".equals(value)) {
return BOTH;
}
else if ("remove".equals(value)) {
return REMOVE;
}
else {
return null;
}
}
private String value;
/**
* Returns the item type associated with the specified string.
*
* @param value the item type.
*/
public ItemType(String value) {
this.value = value;
}
public String toString() {
return value;
}
}
}

View file

@ -0,0 +1,117 @@
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright 2003-2004 Jive Software.
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package org.jivesoftware.smack.packet;
/**
* Represents a XMPP error sub-packet. 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:<p>
*
* <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 XMPPError </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 XMPPError {
private int code;
private String message;
/**
* Creates a new error with the specified code and no message..
*
* @param code the error code.
*/
public XMPPError(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 XMPPError(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 code=\"").append(code).append("\">");
if (message != null) {
buf.append(message);
}
buf.append("</error>");
return buf.toString();
}
public String toString() {
StringBuffer txt = new StringBuffer();
txt.append("(").append(code).append(")");
if (message != null) {
txt.append(" ").append(message);
}
return txt.toString();
}
}

View file

@ -0,0 +1 @@
<body>XML packets that are part of the XMPP protocol.</body>