2003-01-13 17:58:47 +01:00
|
|
|
/**
|
|
|
|
* $RCSfile$
|
|
|
|
* $Revision$
|
|
|
|
* $Date$
|
|
|
|
*
|
2007-02-12 01:59:05 +01:00
|
|
|
* Copyright 2003-2007 Jive Software.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* 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
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* 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.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
package org.jivesoftware.smack.packet;
|
|
|
|
|
2004-05-23 00:53:21 +02:00
|
|
|
import org.jivesoftware.smack.util.StringUtils;
|
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
/**
|
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) {
|
2003-11-19 17:51:19 +01:00
|
|
|
if (type == null) {
|
|
|
|
this.type = Type.GET;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.type = type;
|
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public String toXML() {
|
2006-07-18 07:14:33 +02:00
|
|
|
StringBuilder buf = new StringBuilder();
|
2003-01-13 17:58:47 +01:00
|
|
|
buf.append("<iq ");
|
|
|
|
if (getPacketID() != null) {
|
|
|
|
buf.append("id=\"" + getPacketID() + "\" ");
|
|
|
|
}
|
|
|
|
if (getTo() != null) {
|
2004-05-23 00:53:21 +02:00
|
|
|
buf.append("to=\"").append(StringUtils.escapeForXML(getTo())).append("\" ");
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
if (getFrom() != null) {
|
2004-05-23 00:53:21 +02:00
|
|
|
buf.append("from=\"").append(StringUtils.escapeForXML(getFrom())).append("\" ");
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
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
|
2004-03-29 23:31:08 +02:00
|
|
|
* isn't one. Packet extensions <b>must</b> be included, if any are defined.<p>
|
2003-01-16 16:41:25 +01:00
|
|
|
*
|
2003-08-18 16:46:32 +02:00
|
|
|
* Extensions of this class must override this method.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2003-08-18 16:46:32 +02:00
|
|
|
* @return the child element section of the IQ XML.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
2003-08-04 23:59:07 +02:00
|
|
|
public abstract String getChildElementXML();
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2010-03-24 20:44:02 +01:00
|
|
|
/**
|
|
|
|
* Convenience method to create a new empty {@link Type#RESULT IQ.Type.RESULT}
|
|
|
|
* IQ based on a {@link Type#GET IQ.Type.GET} or {@link Type#SET IQ.Type.SET}
|
|
|
|
* IQ. The new packet will be initialized with:<ul>
|
|
|
|
* <li>The sender set to the recipient of the originating IQ.
|
|
|
|
* <li>The recipient set to the sender of the originating IQ.
|
|
|
|
* <li>The type set to {@link Type#RESULT IQ.Type.RESULT}.
|
|
|
|
* <li>The id set to the id of the originating IQ.
|
|
|
|
* <li>No child element of the IQ element.
|
|
|
|
* </ul>
|
|
|
|
*
|
|
|
|
* @param iq the {@link Type#GET IQ.Type.GET} or {@link Type#SET IQ.Type.SET} IQ packet.
|
|
|
|
* @throws IllegalArgumentException if the IQ packet does not have a type of
|
|
|
|
* {@link Type#GET IQ.Type.GET} or {@link Type#SET IQ.Type.SET}.
|
|
|
|
* @return a new {@link Type#RESULT IQ.Type.RESULT} IQ based on the originating IQ.
|
|
|
|
*/
|
|
|
|
public static IQ createResultIQ(final IQ request) {
|
|
|
|
if (!(request.getType() == Type.GET || request.getType() == Type.SET)) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"IQ must be of type 'set' or 'get'. Original IQ: " + request.toXML());
|
|
|
|
}
|
|
|
|
final IQ result = new IQ() {
|
|
|
|
public String getChildElementXML() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
result.setType(Type.RESULT);
|
|
|
|
result.setPacketID(request.getPacketID());
|
|
|
|
result.setFrom(request.getTo());
|
|
|
|
result.setTo(request.getFrom());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience method to create a new {@link Type#ERROR IQ.Type.ERROR} IQ
|
|
|
|
* based on a {@link Type#GET IQ.Type.GET} or {@link Type#SET IQ.Type.SET}
|
|
|
|
* IQ. The new packet will be initialized with:<ul>
|
|
|
|
* <li>The sender set to the recipient of the originating IQ.
|
|
|
|
* <li>The recipient set to the sender of the originating IQ.
|
|
|
|
* <li>The type set to {@link Type#ERROR IQ.Type.ERROR}.
|
|
|
|
* <li>The id set to the id of the originating IQ.
|
|
|
|
* <li>The child element contained in the associated originating IQ.
|
|
|
|
* <li>The provided {@link XMPPError XMPPError}.
|
|
|
|
* </ul>
|
|
|
|
*
|
|
|
|
* @param iq the {@link Type#GET IQ.Type.GET} or {@link Type#SET IQ.Type.SET} IQ packet.
|
|
|
|
* @param error the error to associate with the created IQ packet.
|
|
|
|
* @throws IllegalArgumentException if the IQ packet does not have a type of
|
|
|
|
* {@link Type#GET IQ.Type.GET} or {@link Type#SET IQ.Type.SET}.
|
|
|
|
* @return a new {@link Type#ERROR IQ.Type.ERROR} IQ based on the originating IQ.
|
|
|
|
*/
|
|
|
|
public static IQ createErrorResponse(final IQ request, final XMPPError error) {
|
|
|
|
if (!(request.getType() == Type.GET || request.getType() == Type.SET)) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"IQ must be of type 'set' or 'get'. Original IQ: " + request.toXML());
|
|
|
|
}
|
|
|
|
final IQ result = new IQ() {
|
|
|
|
public String getChildElementXML() {
|
|
|
|
return request.getChildElementXML();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
result.setType(Type.ERROR);
|
|
|
|
result.setPacketID(request.getPacketID());
|
|
|
|
result.setFrom(request.getTo());
|
|
|
|
result.setTo(request.getFrom());
|
|
|
|
result.setError(error);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|