2003-01-13 17:58:47 +01:00
|
|
|
/**
|
|
|
|
* $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
|
2003-01-17 21:34:50 +01:00
|
|
|
* contact webmaster@jivesoftware.com.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
/**
|
2003-08-15 21:36:20 +02:00
|
|
|
* The base IQ (Info/Query) packet. IQ packets are used to get and set information
|
2003-01-16 16:41:25 +01:00
|
|
|
* on the server, including authentication, roster operations, and creating
|
|
|
|
* accounts. Each IQ packet has a specific type that indicates what type of action
|
2003-08-15 21:36:20 +02:00
|
|
|
* 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><query xmlns="jabber:iq:auth"> -- an authentication IQ.
|
|
|
|
* <li><query xmlns="jabber:iq:private"> -- a private storage IQ.
|
|
|
|
* <li><pubsub xmlns="http://jabber.org/protocol/pubsub"> -- a pubsub IQ.
|
|
|
|
* </ul>
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
|
|
|
* @author Matt Tucker
|
|
|
|
*/
|
2003-08-04 23:59:07 +02:00
|
|
|
public abstract class IQ extends Packet {
|
2003-01-13 17:58:47 +01:00
|
|
|
|
|
|
|
private Type type = Type.GET;
|
|
|
|
|
2003-01-16 16:41:25 +01:00
|
|
|
/**
|
|
|
|
* Returns the type of the IQ packet.
|
|
|
|
*
|
|
|
|
* @return the type of the IQ packet.
|
|
|
|
*/
|
2003-01-13 17:58:47 +01:00
|
|
|
public Type getType() {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2003-01-16 16:41:25 +01:00
|
|
|
/**
|
|
|
|
* Sets the type of the IQ packet.
|
|
|
|
*
|
|
|
|
* @param type the type of the IQ packet.
|
|
|
|
*/
|
2003-01-13 17:58:47 +01:00
|
|
|
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.
|
2003-05-14 23:51:03 +02:00
|
|
|
String queryXML = getChildElementXML();
|
2003-01-13 17:58:47 +01:00
|
|
|
if (queryXML != null) {
|
|
|
|
buf.append(queryXML);
|
|
|
|
}
|
|
|
|
// Add the error sub-packet, if there is one.
|
2003-03-07 20:47:08 +01:00
|
|
|
XMPPError error = getError();
|
2003-01-13 17:58:47 +01:00
|
|
|
if (error != null) {
|
|
|
|
buf.append(error.toXML());
|
|
|
|
}
|
2003-08-05 21:39:16 +02:00
|
|
|
buf.append("</iq>");
|
2003-01-13 17:58:47 +01:00
|
|
|
return buf.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-05-14 23:51:03 +02:00
|
|
|
* Returns the sub-element XML section of the IQ packet, or <tt>null</tt> if there
|
2003-01-16 16:41:25 +01:00
|
|
|
* isn't one.<p>
|
|
|
|
*
|
|
|
|
* Generally, extensions of this class should override this method. This lets
|
|
|
|
* the majority of IQ XML writing be generic, with each sub-class providing just
|
|
|
|
* the packet-specific XML.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
|
|
|
* @return the query section of the IQ XML.
|
|
|
|
*/
|
2003-08-04 23:59:07 +02:00
|
|
|
public abstract String getChildElementXML();
|
2003-01-13 17:58:47 +01:00
|
|
|
|
|
|
|
/**
|
2003-01-16 16:41:25 +01:00
|
|
|
* A class to represent the type of the IQ packet. The types are:
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
|
|
|
* <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");
|
|
|
|
|
2003-01-16 16:41:25 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2003-01-13 17:58:47 +01:00
|
|
|
public static Type fromString(String type) {
|
2003-06-19 16:40:48 +02:00
|
|
|
if (type == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
type = type.toLowerCase();
|
2003-01-15 16:02:57 +01:00
|
|
|
if (GET.toString().equals(type)) {
|
2003-01-13 17:58:47 +01:00
|
|
|
return GET;
|
|
|
|
}
|
2003-01-15 16:02:57 +01:00
|
|
|
else if (SET.toString().equals(type)) {
|
2003-01-13 17:58:47 +01:00
|
|
|
return SET;
|
|
|
|
}
|
2003-01-15 16:02:57 +01:00
|
|
|
else if (ERROR.toString().equals(type)) {
|
2003-01-13 17:58:47 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
2003-01-15 16:02:57 +01:00
|
|
|
else if (RESULT.toString().equals(type)) {
|
2003-01-13 17:58:47 +01:00
|
|
|
return RESULT;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private String value;
|
|
|
|
|
|
|
|
private Type(String value) {
|
|
|
|
this.value = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String toString() {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|