IqPacket.java

 1package eu.siacs.conversations.xmpp.stanzas;
 2
 3import eu.siacs.conversations.xml.Element;
 4
 5public class IqPacket extends AbstractStanza {
 6
 7	public static enum TYPE {
 8		ERROR,
 9		SET,
10		RESULT,
11		GET,
12		INVALID
13	}
14
15	public IqPacket(final TYPE type) {
16		super("iq");
17		if (type != TYPE.INVALID) {
18			this.setAttribute("type", type.toString().toLowerCase());
19		}
20	}
21
22	public IqPacket() {
23		super("iq");
24	}
25
26	public Element query() {
27		Element query = findChild("query");
28		if (query == null) {
29			query = addChild("query");
30		}
31		return query;
32	}
33
34	public Element query(final String xmlns) {
35		final Element query = query();
36		query.setAttribute("xmlns", xmlns);
37		return query();
38	}
39
40	public TYPE getType() {
41		final String type = getAttribute("type");
42		if (type == null) {
43			return TYPE.INVALID;
44		}
45		switch (type) {
46			case "error":
47				return TYPE.ERROR;
48			case "result":
49				return TYPE.RESULT;
50			case "set":
51				return TYPE.SET;
52			case "get":
53				return TYPE.GET;
54			default:
55				return TYPE.INVALID;
56		}
57	}
58
59	public IqPacket generateResponse(final TYPE type) {
60		final IqPacket packet = new IqPacket(type);
61		packet.setTo(this.getFrom());
62		packet.setId(this.getId());
63		return packet;
64	}
65
66}