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		switch (type) {
43			case "error":
44				return TYPE.ERROR;
45			case "result":
46				return TYPE.RESULT;
47			case "set":
48				return TYPE.SET;
49			case "get":
50				return TYPE.GET;
51			default:
52				return TYPE.INVALID;
53		}
54	}
55
56	public IqPacket generateResponse(final TYPE type) {
57		final IqPacket packet = new IqPacket(type);
58		packet.setTo(this.getFrom());
59		packet.setId(this.getId());
60		return packet;
61	}
62
63}