2016-01-10 14:56:55 -05:00
|
|
|
package eu.siacs.conversations.entities;
|
|
|
|
|
2016-01-10 16:25:26 -05:00
|
|
|
import android.content.ContentValues;
|
2016-01-12 21:53:38 -05:00
|
|
|
import android.database.Cursor;
|
2016-01-10 16:25:26 -05:00
|
|
|
import android.util.Base64;
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
import java.lang.Comparable;
|
|
|
|
import java.security.MessageDigest;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
2016-01-10 14:56:55 -05:00
|
|
|
import java.util.ArrayList;
|
2016-01-10 16:25:26 -05:00
|
|
|
import java.util.Collections;
|
2016-02-03 11:19:05 -05:00
|
|
|
import java.util.Comparator;
|
2016-01-10 16:25:26 -05:00
|
|
|
import java.util.List;
|
2016-01-10 16:43:48 -05:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
2016-01-10 14:56:55 -05:00
|
|
|
|
|
|
|
import eu.siacs.conversations.xml.Element;
|
2016-02-03 11:19:05 -05:00
|
|
|
import eu.siacs.conversations.xmpp.forms.Data;
|
2016-03-31 08:21:56 -04:00
|
|
|
import eu.siacs.conversations.xmpp.forms.Field;
|
2016-01-10 14:56:55 -05:00
|
|
|
import eu.siacs.conversations.xmpp.stanzas.IqPacket;
|
|
|
|
|
|
|
|
public class ServiceDiscoveryResult {
|
2016-01-12 21:53:38 -05:00
|
|
|
public static final String TABLENAME = "discovery_results";
|
|
|
|
public static final String HASH = "hash";
|
|
|
|
public static final String VER = "ver";
|
|
|
|
public static final String RESULT = "result";
|
2016-01-10 16:25:26 -05:00
|
|
|
|
|
|
|
protected static String blankNull(String s) {
|
|
|
|
return s == null ? "" : s;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class Identity implements Comparable {
|
2016-01-10 14:56:55 -05:00
|
|
|
protected final String category;
|
|
|
|
protected final String type;
|
2016-01-10 16:25:26 -05:00
|
|
|
protected final String lang;
|
2016-01-10 14:56:55 -05:00
|
|
|
protected final String name;
|
|
|
|
|
2016-01-10 16:25:26 -05:00
|
|
|
public Identity(final String category, final String type, final String lang, final String name) {
|
2016-01-10 14:56:55 -05:00
|
|
|
this.category = category;
|
|
|
|
this.type = type;
|
2016-01-10 16:25:26 -05:00
|
|
|
this.lang = lang;
|
2016-01-10 14:56:55 -05:00
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Identity(final Element el) {
|
2016-01-12 21:53:38 -05:00
|
|
|
this(
|
|
|
|
el.getAttribute("category"),
|
|
|
|
el.getAttribute("type"),
|
|
|
|
el.getAttribute("xml:lang"),
|
|
|
|
el.getAttribute("name")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Identity(final JSONObject o) {
|
2016-05-15 06:35:51 -04:00
|
|
|
|
2016-01-12 21:53:38 -05:00
|
|
|
this(
|
|
|
|
o.optString("category", null),
|
|
|
|
o.optString("type", null),
|
|
|
|
o.optString("lang", null),
|
|
|
|
o.optString("name", null)
|
|
|
|
);
|
2016-01-10 14:56:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getCategory() {
|
|
|
|
return this.category;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getType() {
|
|
|
|
return this.type;
|
|
|
|
}
|
|
|
|
|
2016-01-10 16:25:26 -05:00
|
|
|
public String getLang() {
|
|
|
|
return this.lang;
|
|
|
|
}
|
|
|
|
|
2016-01-10 14:56:55 -05:00
|
|
|
public String getName() {
|
|
|
|
return this.name;
|
|
|
|
}
|
2016-01-10 16:25:26 -05:00
|
|
|
|
|
|
|
public int compareTo(Object other) {
|
|
|
|
Identity o = (Identity)other;
|
|
|
|
int r = blankNull(this.getCategory()).compareTo(blankNull(o.getCategory()));
|
|
|
|
if(r == 0) {
|
|
|
|
r = blankNull(this.getType()).compareTo(blankNull(o.getType()));
|
|
|
|
}
|
|
|
|
if(r == 0) {
|
|
|
|
r = blankNull(this.getLang()).compareTo(blankNull(o.getLang()));
|
|
|
|
}
|
|
|
|
if(r == 0) {
|
|
|
|
r = blankNull(this.getName()).compareTo(blankNull(o.getName()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
2016-01-10 16:43:48 -05:00
|
|
|
|
|
|
|
public JSONObject toJSON() {
|
|
|
|
try {
|
|
|
|
JSONObject o = new JSONObject();
|
|
|
|
o.put("category", this.getCategory());
|
|
|
|
o.put("type", this.getType());
|
|
|
|
o.put("lang", this.getLang());
|
|
|
|
o.put("name", this.getName());
|
|
|
|
return o;
|
|
|
|
} catch(JSONException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2016-01-10 14:56:55 -05:00
|
|
|
}
|
|
|
|
|
2016-01-12 21:53:38 -05:00
|
|
|
protected final String hash;
|
|
|
|
protected final byte[] ver;
|
2016-01-10 14:56:55 -05:00
|
|
|
protected final List<Identity> identities;
|
|
|
|
protected final List<String> features;
|
2016-02-03 11:19:05 -05:00
|
|
|
protected final List<Data> forms;
|
2016-01-10 14:56:55 -05:00
|
|
|
|
|
|
|
public ServiceDiscoveryResult(final IqPacket packet) {
|
|
|
|
this.identities = new ArrayList<>();
|
|
|
|
this.features = new ArrayList<>();
|
2016-02-03 11:19:05 -05:00
|
|
|
this.forms = new ArrayList<>();
|
2016-01-12 21:53:38 -05:00
|
|
|
this.hash = "sha-1"; // We only support sha-1 for now
|
2016-01-10 14:56:55 -05:00
|
|
|
|
|
|
|
final List<Element> elements = packet.query().getChildren();
|
|
|
|
|
|
|
|
for (final Element element : elements) {
|
|
|
|
if (element.getName().equals("identity")) {
|
|
|
|
Identity id = new Identity(element);
|
|
|
|
if (id.getType() != null && id.getCategory() != null) {
|
|
|
|
identities.add(id);
|
|
|
|
}
|
|
|
|
} else if (element.getName().equals("feature")) {
|
2016-01-10 16:25:26 -05:00
|
|
|
if (element.getAttribute("var") != null) {
|
|
|
|
features.add(element.getAttribute("var"));
|
|
|
|
}
|
2016-02-03 11:19:05 -05:00
|
|
|
} else if (element.getName().equals("x") && "jabber:x:data".equals(element.getAttribute("xmlns"))) {
|
|
|
|
forms.add(Data.parse(element));
|
2016-01-10 14:56:55 -05:00
|
|
|
}
|
|
|
|
}
|
2016-01-12 21:53:38 -05:00
|
|
|
this.ver = this.mkCapHash();
|
|
|
|
}
|
|
|
|
|
|
|
|
public ServiceDiscoveryResult(String hash, byte[] ver, JSONObject o) throws JSONException {
|
|
|
|
this.identities = new ArrayList<>();
|
|
|
|
this.features = new ArrayList<>();
|
2016-02-03 11:19:05 -05:00
|
|
|
this.forms = new ArrayList<>();
|
2016-01-12 21:53:38 -05:00
|
|
|
this.hash = hash;
|
|
|
|
this.ver = ver;
|
|
|
|
|
2016-05-19 04:41:56 -04:00
|
|
|
JSONArray identities = o.optJSONArray("identities");
|
2016-02-03 04:40:02 -05:00
|
|
|
if (identities != null) {
|
|
|
|
for (int i = 0; i < identities.length(); i++) {
|
|
|
|
this.identities.add(new Identity(identities.getJSONObject(i)));
|
|
|
|
}
|
2016-01-12 21:53:38 -05:00
|
|
|
}
|
|
|
|
JSONArray features = o.optJSONArray("features");
|
2016-02-03 04:40:02 -05:00
|
|
|
if (features != null) {
|
|
|
|
for (int i = 0; i < features.length(); i++) {
|
|
|
|
this.features.add(features.getString(i));
|
|
|
|
}
|
2016-01-12 21:53:38 -05:00
|
|
|
}
|
2016-05-19 04:41:56 -04:00
|
|
|
JSONArray forms = o.optJSONArray("forms");
|
|
|
|
if (forms != null) {
|
|
|
|
for(int i = 0; i < forms.length(); i++) {
|
|
|
|
this.forms.add(createFormFromJSONObject(forms.getJSONObject(i)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Data createFormFromJSONObject(JSONObject o) {
|
|
|
|
Data data = new Data();
|
|
|
|
JSONArray names = o.names();
|
|
|
|
for(int i = 0; i < names.length(); ++i) {
|
|
|
|
try {
|
|
|
|
String name = names.getString(i);
|
|
|
|
JSONArray jsonValues = o.getJSONArray(name);
|
|
|
|
ArrayList<String> values = new ArrayList<>(jsonValues.length());
|
|
|
|
for(int j = 0; j < jsonValues.length(); ++j) {
|
|
|
|
values.add(jsonValues.getString(j));
|
|
|
|
}
|
|
|
|
data.put(name, values);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static JSONObject createJSONFromForm(Data data) {
|
|
|
|
JSONObject object = new JSONObject();
|
|
|
|
for(Field field : data.getFields()) {
|
|
|
|
try {
|
|
|
|
JSONArray jsonValues = new JSONArray();
|
|
|
|
for(String value : field.getValues()) {
|
|
|
|
jsonValues.put(value);
|
|
|
|
}
|
|
|
|
object.put(field.getFieldName(), jsonValues);
|
|
|
|
} catch(Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
JSONArray jsonValues = new JSONArray();
|
|
|
|
jsonValues.put(data.getFormType());
|
|
|
|
object.put(Data.FORM_TYPE, jsonValues);
|
|
|
|
} catch(Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return object;
|
2016-01-12 21:53:38 -05:00
|
|
|
}
|
|
|
|
|
2016-02-03 04:40:02 -05:00
|
|
|
public String getVer() {
|
|
|
|
return new String(Base64.encode(this.ver, Base64.DEFAULT)).trim();
|
|
|
|
}
|
|
|
|
|
2016-01-12 21:53:38 -05:00
|
|
|
public ServiceDiscoveryResult(Cursor cursor) throws JSONException {
|
|
|
|
this(
|
|
|
|
cursor.getString(cursor.getColumnIndex(HASH)),
|
|
|
|
Base64.decode(cursor.getString(cursor.getColumnIndex(VER)), Base64.DEFAULT),
|
|
|
|
new JSONObject(cursor.getString(cursor.getColumnIndex(RESULT)))
|
|
|
|
);
|
2016-01-10 14:56:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public List<Identity> getIdentities() {
|
|
|
|
return this.identities;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<String> getFeatures() {
|
|
|
|
return this.features;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasIdentity(String category, String type) {
|
|
|
|
for(Identity id : this.getIdentities()) {
|
|
|
|
if((category == null || id.getCategory().equals(category)) &&
|
|
|
|
(type == null || id.getType().equals(type))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-10 16:25:26 -05:00
|
|
|
|
2016-03-31 15:56:59 -04:00
|
|
|
public String getExtendedDiscoInformation(String formType, String name) {
|
|
|
|
for(Data form : this.forms) {
|
|
|
|
if (formType.equals(form.getFormType())) {
|
|
|
|
for(Field field: form.getFields()) {
|
|
|
|
if (name.equals(field.getFieldName())) {
|
|
|
|
return field.getValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-01-12 21:53:38 -05:00
|
|
|
protected byte[] mkCapHash() {
|
2016-01-10 16:25:26 -05:00
|
|
|
StringBuilder s = new StringBuilder();
|
|
|
|
|
|
|
|
List<Identity> identities = this.getIdentities();
|
|
|
|
Collections.sort(identities);
|
|
|
|
|
|
|
|
for(Identity id : identities) {
|
|
|
|
s.append(
|
|
|
|
blankNull(id.getCategory()) + "/" +
|
|
|
|
blankNull(id.getType()) + "/" +
|
|
|
|
blankNull(id.getLang()) + "/" +
|
|
|
|
blankNull(id.getName()) + "<"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
List<String> features = this.getFeatures();
|
|
|
|
Collections.sort(features);
|
|
|
|
|
|
|
|
for (String feature : features) {
|
|
|
|
s.append(feature + "<");
|
|
|
|
}
|
|
|
|
|
2016-02-03 11:19:05 -05:00
|
|
|
Collections.sort(forms, new Comparator<Data>() {
|
|
|
|
@Override
|
|
|
|
public int compare(Data lhs, Data rhs) {
|
|
|
|
return lhs.getFormType().compareTo(rhs.getFormType());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
for(Data form : forms) {
|
2016-03-31 08:21:56 -04:00
|
|
|
s.append(form.getFormType() + "<");
|
|
|
|
List<Field> fields = form.getFields();
|
|
|
|
Collections.sort(fields, new Comparator<Field>() {
|
|
|
|
@Override
|
|
|
|
public int compare(Field lhs, Field rhs) {
|
|
|
|
return lhs.getFieldName().compareTo(rhs.getFieldName());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
for(Field field : fields) {
|
|
|
|
s.append(field.getFieldName()+"<");
|
|
|
|
List<String> values = field.getValues();
|
|
|
|
Collections.sort(values);
|
|
|
|
for(String value : values) {
|
|
|
|
s.append(value+"<");
|
|
|
|
}
|
|
|
|
}
|
2016-02-03 11:19:05 -05:00
|
|
|
}
|
2016-01-10 16:25:26 -05:00
|
|
|
|
|
|
|
MessageDigest md;
|
|
|
|
try {
|
|
|
|
md = MessageDigest.getInstance("SHA-1");
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return md.digest(s.toString().getBytes("UTF-8"));
|
|
|
|
} catch(UnsupportedEncodingException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-10 16:43:48 -05:00
|
|
|
public JSONObject toJSON() {
|
|
|
|
try {
|
|
|
|
JSONObject o = new JSONObject();
|
|
|
|
|
|
|
|
JSONArray ids = new JSONArray();
|
|
|
|
for(Identity id : this.getIdentities()) {
|
|
|
|
ids.put(id.toJSON());
|
|
|
|
}
|
2016-05-19 04:41:56 -04:00
|
|
|
o.put("identities", ids);
|
2016-01-10 16:43:48 -05:00
|
|
|
|
|
|
|
o.put("features", new JSONArray(this.getFeatures()));
|
|
|
|
|
2016-05-19 04:41:56 -04:00
|
|
|
JSONArray forms = new JSONArray();
|
|
|
|
for(Data data : this.forms) {
|
|
|
|
forms.put(createJSONFromForm(data));
|
|
|
|
}
|
|
|
|
o.put("forms", forms);
|
|
|
|
|
2016-01-10 16:43:48 -05:00
|
|
|
return o;
|
|
|
|
} catch(JSONException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-12 21:53:38 -05:00
|
|
|
public ContentValues getContentValues() {
|
|
|
|
final ContentValues values = new ContentValues();
|
|
|
|
values.put(HASH, this.hash);
|
2016-02-03 04:40:02 -05:00
|
|
|
values.put(VER, getVer());
|
2016-01-12 21:53:38 -05:00
|
|
|
values.put(RESULT, this.toJSON().toString());
|
|
|
|
return values;
|
|
|
|
}
|
2016-01-10 14:56:55 -05:00
|
|
|
}
|