1package im.conversations.android.xmpp.model.stanza;
2
3import com.google.common.base.Strings;
4import im.conversations.android.annotation.XmlElement;
5import im.conversations.android.xmpp.model.Extension;
6import im.conversations.android.xmpp.model.error.Error;
7import java.util.Arrays;
8import java.util.Locale;
9
10@XmlElement
11public class Iq extends Stanza {
12
13 public static Iq TIMEOUT = new Iq(Type.TIMEOUT);
14
15 public Iq() {
16 super(Iq.class);
17 }
18
19 public Iq(final Type type) {
20 super(Iq.class);
21 this.setAttribute("type", type.toString().toLowerCase(Locale.ROOT));
22 }
23
24 public Iq(final Type type, final Extension... extensions) {
25 this(type);
26 this.addExtensions(Arrays.asList(extensions));
27 }
28
29 // TODO get rid of timeout
30 public enum Type {
31 SET,
32 GET,
33 ERROR,
34 RESULT,
35 TIMEOUT
36 }
37
38 public Type getType() {
39 return Type.valueOf(
40 Strings.nullToEmpty(this.getAttribute("type")).toUpperCase(Locale.ROOT));
41 }
42
43 @Override
44 public boolean isInvalid() {
45 final var id = getId();
46 if (Strings.isNullOrEmpty(id)) {
47 return true;
48 }
49 return super.isInvalid();
50 }
51
52 public Iq generateResponse(final Iq.Type type) {
53 final var packet = new Iq(type);
54 packet.setTo(this.getFrom());
55 packet.setId(this.getId());
56 return packet;
57 }
58
59 public String getErrorCondition() {
60 final Error error = getError();
61 final var condition = error == null ? null : error.getCondition();
62 return condition == null ? null : condition.getName();
63 }
64}