ServiceDiscoveryResult.java

  1package eu.siacs.conversations.entities;
  2
  3import android.content.ContentValues;
  4import android.database.Cursor;
  5import android.util.Base64;
  6import com.google.common.base.Strings;
  7import eu.siacs.conversations.xml.Element;
  8import eu.siacs.conversations.xml.Namespace;
  9import eu.siacs.conversations.xmpp.forms.Data;
 10import eu.siacs.conversations.xmpp.forms.Field;
 11import im.conversations.android.xmpp.model.stanza.Iq;
 12import java.nio.charset.StandardCharsets;
 13import java.security.MessageDigest;
 14import java.security.NoSuchAlgorithmException;
 15import java.util.ArrayList;
 16import java.util.Collections;
 17import java.util.Comparator;
 18import java.util.List;
 19import org.json.JSONArray;
 20import org.json.JSONException;
 21import org.json.JSONObject;
 22
 23public class ServiceDiscoveryResult {
 24    public static final String TABLENAME = "discovery_results";
 25    public static final String HASH = "hash";
 26    public static final String VER = "ver";
 27    public static final String RESULT = "result";
 28    protected final String hash;
 29    protected final byte[] ver;
 30    protected final List<String> features;
 31    protected final List<Data> forms;
 32    private final List<Identity> identities;
 33
 34    public ServiceDiscoveryResult(final Iq packet) {
 35        this.identities = new ArrayList<>();
 36        this.features = new ArrayList<>();
 37        this.forms = new ArrayList<>();
 38        this.hash = "sha-1"; // We only support sha-1 for now
 39
 40        final List<Element> elements = packet.query().getChildren();
 41
 42        for (final Element element : elements) {
 43            if (element.getName().equals("identity")) {
 44                Identity id = new Identity(element);
 45                if (id.getType() != null && id.getCategory() != null) {
 46                    identities.add(id);
 47                }
 48            } else if (element.getName().equals("feature")) {
 49                if (element.getAttribute("var") != null) {
 50                    features.add(element.getAttribute("var"));
 51                }
 52            } else if (element.getName().equals("x")
 53                    && element.getAttribute("xmlns").equals(Namespace.DATA)) {
 54                forms.add(Data.parse(element));
 55            }
 56        }
 57        this.ver = this.mkCapHash();
 58    }
 59
 60    private ServiceDiscoveryResult(String hash, byte[] ver, JSONObject o) throws JSONException {
 61        this.identities = new ArrayList<>();
 62        this.features = new ArrayList<>();
 63        this.forms = new ArrayList<>();
 64        this.hash = hash;
 65        this.ver = ver;
 66
 67        JSONArray identities = o.optJSONArray("identities");
 68        if (identities != null) {
 69            for (int i = 0; i < identities.length(); i++) {
 70                this.identities.add(new Identity(identities.getJSONObject(i)));
 71            }
 72        }
 73        JSONArray features = o.optJSONArray("features");
 74        if (features != null) {
 75            for (int i = 0; i < features.length(); i++) {
 76                this.features.add(features.getString(i));
 77            }
 78        }
 79        JSONArray forms = o.optJSONArray("forms");
 80        if (forms != null) {
 81            for (int i = 0; i < forms.length(); i++) {
 82                this.forms.add(createFormFromJSONObject(forms.getJSONObject(i)));
 83            }
 84        }
 85    }
 86
 87    private ServiceDiscoveryResult() {
 88        this.hash = "sha-1";
 89        this.features = Collections.emptyList();
 90        this.identities = Collections.emptyList();
 91        this.ver = null;
 92        this.forms = Collections.emptyList();
 93    }
 94
 95    public static ServiceDiscoveryResult empty() {
 96        return new ServiceDiscoveryResult();
 97    }
 98
 99    public ServiceDiscoveryResult(Cursor cursor) throws JSONException {
100        this(
101                cursor.getString(cursor.getColumnIndexOrThrow(HASH)),
102                Base64.decode(cursor.getString(cursor.getColumnIndexOrThrow(VER)), Base64.DEFAULT),
103                new JSONObject(cursor.getString(cursor.getColumnIndexOrThrow(RESULT))));
104    }
105
106    private static String clean(String s) {
107        return s.replace("<", "&lt;");
108    }
109
110    private static String blankNull(String s) {
111        return s == null ? "" : clean(s);
112    }
113
114    private static Data createFormFromJSONObject(JSONObject o) {
115        Data data = new Data();
116        JSONArray names = o.names();
117        for (int i = 0; i < names.length(); ++i) {
118            try {
119                String name = names.getString(i);
120                JSONArray jsonValues = o.getJSONArray(name);
121                ArrayList<String> values = new ArrayList<>(jsonValues.length());
122                for (int j = 0; j < jsonValues.length(); ++j) {
123                    values.add(jsonValues.getString(j));
124                }
125                data.put(name, values);
126            } catch (Exception e) {
127                e.printStackTrace();
128            }
129        }
130        return data;
131    }
132
133    private static JSONObject createJSONFromForm(Data data) {
134        JSONObject object = new JSONObject();
135        for (Field field : data.getFields()) {
136            try {
137                JSONArray jsonValues = new JSONArray();
138                for (String value : field.getValues()) {
139                    jsonValues.put(value);
140                }
141                object.put(field.getFieldName(), jsonValues);
142            } catch (Exception e) {
143                e.printStackTrace();
144            }
145        }
146        try {
147            JSONArray jsonValues = new JSONArray();
148            jsonValues.put(data.getFormType());
149            object.put(Data.FORM_TYPE, jsonValues);
150        } catch (Exception e) {
151            e.printStackTrace();
152        }
153        return object;
154    }
155
156    public Identity getIdentity(String category, String type) {
157        for (Identity id : this.getIdentities()) {
158            if ((category == null || id.getCategory().equals(category)) &&
159                    (type == null || id.getType().equals(type))) {
160                return id;
161            }
162        }
163
164        return null;
165    }
166
167    public boolean hasIdentity(String category, String type) {
168        return getIdentity(category, type) != null;
169    }
170
171    public String getVer() {
172        return Base64.encodeToString(this.ver, Base64.NO_WRAP);
173    }
174
175    public List<Identity> getIdentities() {
176        return this.identities;
177    }
178
179    public List<String> getFeatures() {
180        return this.features;
181    }
182
183    public String getExtendedDiscoInformation(final String formType, final String name) {
184        for (final Data form : this.forms) {
185            if (formType.equals(form.getFormType())) {
186                for (final Field field : form.getFields()) {
187                    if (name.equals(field.getFieldName())) {
188                        return field.getValue();
189                    }
190                }
191            }
192        }
193        return null;
194    }
195
196    private byte[] mkCapHash() {
197        StringBuilder s = new StringBuilder();
198
199        List<Identity> identities = this.getIdentities();
200        Collections.sort(identities);
201
202        for (Identity id : identities) {
203            s.append(blankNull(id.getCategory()))
204                    .append("/")
205                    .append(blankNull(id.getType()))
206                    .append("/")
207                    .append(blankNull(id.getLang()))
208                    .append("/")
209                    .append(blankNull(id.getName()))
210                    .append("<");
211        }
212
213        final List<String> features = this.getFeatures();
214        Collections.sort(features);
215        for (final String feature : features) {
216            s.append(clean(feature)).append("<");
217        }
218
219        Collections.sort(forms, Comparator.comparing(Data::getFormType));
220        for (final Data form : forms) {
221            s.append(clean(form.getFormType())).append("<");
222            final List<Field> fields = form.getFields();
223            Collections.sort(
224                    fields, Comparator.comparing(lhs -> Strings.nullToEmpty(lhs.getFieldName())));
225            for (final Field field : fields) {
226                s.append(Strings.nullToEmpty(field.getFieldName())).append("<");
227                final List<String> values = field.getValues();
228                Collections.sort(values, Comparator.comparing(ServiceDiscoveryResult::blankNull));
229                for (final String value : values) {
230                    s.append(blankNull(value)).append("<");
231                }
232            }
233        }
234
235        MessageDigest md;
236        try {
237            md = MessageDigest.getInstance("SHA-1");
238        } catch (NoSuchAlgorithmException e) {
239            return null;
240        }
241
242        return md.digest(s.toString().getBytes(StandardCharsets.UTF_8));
243    }
244
245    private JSONObject toJSON() {
246        try {
247            JSONObject o = new JSONObject();
248
249            JSONArray ids = new JSONArray();
250            for (Identity id : this.getIdentities()) {
251                ids.put(id.toJSON());
252            }
253            o.put("identities", ids);
254
255            o.put("features", new JSONArray(this.getFeatures()));
256
257            JSONArray forms = new JSONArray();
258            for (Data data : this.forms) {
259                forms.put(createJSONFromForm(data));
260            }
261            o.put("forms", forms);
262
263            return o;
264        } catch (JSONException e) {
265            return null;
266        }
267    }
268
269    public ContentValues getContentValues() {
270        final ContentValues values = new ContentValues();
271        values.put(HASH, this.hash);
272        values.put(VER, getVer());
273        JSONObject jsonObject = toJSON();
274        values.put(RESULT, jsonObject == null ? "" : jsonObject.toString());
275        return values;
276    }
277
278    public static class Identity implements Comparable<Identity> {
279        protected final String type;
280        protected final String lang;
281        protected final String name;
282        final String category;
283
284        Identity(final String category, final String type, final String lang, final String name) {
285            this.category = category;
286            this.type = type;
287            this.lang = lang;
288            this.name = name;
289        }
290
291        Identity(final Element el) {
292            this(
293                    el.getAttribute("category"),
294                    el.getAttribute("type"),
295                    el.getAttribute("xml:lang"),
296                    el.getAttribute("name"));
297        }
298
299        Identity(final JSONObject o) {
300
301            this(
302                    o.optString("category", null),
303                    o.optString("type", null),
304                    o.optString("lang", null),
305                    o.optString("name", null));
306        }
307
308        public String getCategory() {
309            return this.category;
310        }
311
312        public String getType() {
313            return this.type;
314        }
315
316        public String getLang() {
317            return this.lang;
318        }
319
320        public String getName() {
321            return this.name;
322        }
323
324        JSONObject toJSON() {
325            try {
326                JSONObject o = new JSONObject();
327                o.put("category", this.getCategory());
328                o.put("type", this.getType());
329                o.put("lang", this.getLang());
330                o.put("name", this.getName());
331                return o;
332            } catch (JSONException e) {
333                return null;
334            }
335        }
336
337        @Override
338        public int compareTo(final Identity o) {
339            int r = blankNull(this.getCategory()).compareTo(blankNull(o.getCategory()));
340            if (r == 0) {
341                r = blankNull(this.getType()).compareTo(blankNull(o.getType()));
342            }
343            if (r == 0) {
344                r = blankNull(this.getLang()).compareTo(blankNull(o.getLang()));
345            }
346            if (r == 0) {
347                r = blankNull(this.getName()).compareTo(blankNull(o.getName()));
348            }
349
350            return r;
351        }
352    }
353}