anotherim/src/main/java/eu/siacs/conversations/xmpp/stanzas/IqPacket.java

76 lines
1.3 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;
2015-08-13 16:25:10 +00:00
public class IqPacket extends AbstractAcknowledgeableStanza {
2014-10-22 16:38:44 +00:00
2015-08-13 16:25:10 +00:00
public enum TYPE {
2014-12-30 13:16:25 +00:00
ERROR,
SET,
RESULT,
GET,
INVALID,
TIMEOUT
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;
2015-08-28 09:41:41 +00:00
case "timeout":
return TYPE.TIMEOUT;
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;
}
@Override
public boolean valid() {
String id = getId();
return id != null && super.valid();
}
2014-10-22 16:38:44 +00:00
}