conversations-classic/src/main/java/eu/siacs/conversations/xmpp/stanzas/IqPacket.java

67 lines
1.2 KiB
Java
Raw Normal View History

2014-10-22 16:38:44 +00:00
package eu.siacs.conversations.xmpp.stanzas;
import eu.siacs.conversations.xml.Element;
public class IqPacket extends AbstractStanza {
2014-12-30 13:16:25 +00:00
public static enum TYPE {
ERROR,
SET,
RESULT,
GET,
INVALID
2014-10-22 16:38:44 +00:00
}
2014-12-30 13:16:25 +00:00
public IqPacket(final TYPE type) {
2014-10-22 16:38:44 +00:00
super("iq");
2014-12-30 13:16:25 +00:00
if (type != TYPE.INVALID) {
this.setAttribute("type", type.toString().toLowerCase());
2014-10-22 16:38:44 +00:00
}
}
public IqPacket() {
super("iq");
}
public Element query() {
Element query = findChild("query");
if (query == null) {
query = addChild("query");
}
return query;
}
public Element query(final String xmlns) {
final Element query = query();
2014-10-22 16:38:44 +00:00
query.setAttribute("xmlns", xmlns);
return query();
}
2014-12-30 13:16:25 +00:00
public TYPE getType() {
final String type = getAttribute("type");
if (type == null) {
return TYPE.INVALID;
}
switch (type) {
case "error":
2014-12-30 13:16:25 +00:00
return TYPE.ERROR;
case "result":
2014-12-30 13:16:25 +00:00
return TYPE.RESULT;
case "set":
2014-12-30 13:16:25 +00:00
return TYPE.SET;
case "get":
2014-12-30 13:16:25 +00:00
return TYPE.GET;
default:
2014-12-30 13:16:25 +00:00
return TYPE.INVALID;
2014-10-22 16:38:44 +00:00
}
}
2014-12-30 13:16:25 +00:00
public IqPacket generateResponse(final TYPE type) {
final IqPacket packet = new IqPacket(type);
2014-10-22 16:38:44 +00:00
packet.setTo(this.getFrom());
packet.setId(this.getId());
return packet;
}
}