refactored xmpp uri parsing to expose all params
This commit is contained in:
parent
1dad70fbc3
commit
4e0f05f0a1
|
@ -1,15 +1,19 @@
|
||||||
package eu.siacs.conversations.utils;
|
package eu.siacs.conversations.utils;
|
||||||
|
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.util.Log;
|
import android.support.annotation.NonNull;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableList;
|
||||||
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.net.URLDecoder;
|
import java.net.URLDecoder;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import eu.siacs.conversations.Config;
|
|
||||||
import rocks.xmpp.addr.Jid;
|
import rocks.xmpp.addr.Jid;
|
||||||
|
|
||||||
public class XmppUri {
|
public class XmppUri {
|
||||||
|
@ -17,9 +21,7 @@ public class XmppUri {
|
||||||
protected Uri uri;
|
protected Uri uri;
|
||||||
protected String jid;
|
protected String jid;
|
||||||
private List<Fingerprint> fingerprints = new ArrayList<>();
|
private List<Fingerprint> fingerprints = new ArrayList<>();
|
||||||
private String body;
|
private Map<String,String> parameters = Collections.emptyMap();
|
||||||
private String name;
|
|
||||||
private String action;
|
|
||||||
private boolean safeSource = true;
|
private boolean safeSource = true;
|
||||||
|
|
||||||
private static final String OMEMO_URI_PARAM = "omemo-sid-";
|
private static final String OMEMO_URI_PARAM = "omemo-sid-";
|
||||||
|
@ -66,7 +68,6 @@ public class XmppUri {
|
||||||
try {
|
try {
|
||||||
jid = Jid.of(lameUrlDecode(segments.get(1))).toString();
|
jid = Jid.of(lameUrlDecode(segments.get(1))).toString();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.d(Config.LOGTAG, "parsing failed ", e);
|
|
||||||
jid = null;
|
jid = null;
|
||||||
}
|
}
|
||||||
} else if (segments.size() >= 3) {
|
} else if (segments.size() >= 3) {
|
||||||
|
@ -74,33 +75,24 @@ public class XmppUri {
|
||||||
jid = segments.get(1) + "@" + segments.get(2);
|
jid = segments.get(1) + "@" + segments.get(2);
|
||||||
}
|
}
|
||||||
if (segments.size() > 1 && "j".equalsIgnoreCase(segments.get(0))) {
|
if (segments.size() > 1 && "j".equalsIgnoreCase(segments.get(0))) {
|
||||||
action = ACTION_JOIN;
|
this.parameters = ImmutableMap.of(ACTION_JOIN, "");
|
||||||
}
|
}
|
||||||
fingerprints = parseFingerprints(uri.getQuery(), '&');
|
final Map<String,String> parameters = parseParameters(uri.getQuery(), '&');
|
||||||
|
this.fingerprints = parseFingerprints(parameters);
|
||||||
} else if ("xmpp".equalsIgnoreCase(scheme)) {
|
} else if ("xmpp".equalsIgnoreCase(scheme)) {
|
||||||
// sample: xmpp:foo@bar.com
|
// sample: xmpp:foo@bar.com
|
||||||
|
this.parameters = parseParameters(uri.getQuery(), ';');
|
||||||
final String query = uri.getQuery();
|
|
||||||
|
|
||||||
if (hasAction(query, ACTION_JOIN)) {
|
|
||||||
this.action = ACTION_JOIN;
|
|
||||||
} else if (hasAction(query, ACTION_MESSAGE)) {
|
|
||||||
this.action = ACTION_MESSAGE;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (uri.getAuthority() != null) {
|
if (uri.getAuthority() != null) {
|
||||||
jid = uri.getAuthority();
|
jid = uri.getAuthority();
|
||||||
} else {
|
} else {
|
||||||
String[] parts = uri.getSchemeSpecificPart().split("\\?");
|
final String[] parts = uri.getSchemeSpecificPart().split("\\?");
|
||||||
if (parts.length > 0) {
|
if (parts.length > 0) {
|
||||||
jid = parts[0];
|
jid = parts[0];
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.fingerprints = parseFingerprints(uri.getQuery());
|
this.fingerprints = parseFingerprints(parameters);
|
||||||
this.body = parseParameter("body", uri.getQuery());
|
|
||||||
this.name = parseParameter("name", uri.getQuery());
|
|
||||||
} else if ("imto".equalsIgnoreCase(scheme)) {
|
} else if ("imto".equalsIgnoreCase(scheme)) {
|
||||||
// sample: imto://xmpp/foo@bar.com
|
// sample: imto://xmpp/foo@bar.com
|
||||||
try {
|
try {
|
||||||
|
@ -117,6 +109,35 @@ public class XmppUri {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static Map<String,String> parseParameters(final String query, final char seperator) {
|
||||||
|
final ImmutableMap.Builder<String,String> builder = new ImmutableMap.Builder<>();
|
||||||
|
final String[] pairs = query == null ? new String[0] : query.split(String.valueOf(seperator));
|
||||||
|
for (String pair : pairs) {
|
||||||
|
final String[] parts = pair.split("=", 2);
|
||||||
|
if (parts.length == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
final String key = parts[0].toLowerCase(Locale.US);
|
||||||
|
final String value;
|
||||||
|
if (parts.length == 2) {
|
||||||
|
String decoded;
|
||||||
|
try {
|
||||||
|
decoded = URLDecoder.decode(parts[1],"UTF-8");
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
decoded = "";
|
||||||
|
}
|
||||||
|
value = decoded;
|
||||||
|
} else {
|
||||||
|
value = "";
|
||||||
|
}
|
||||||
|
builder.put(key, value);
|
||||||
|
}
|
||||||
|
return builder.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@NonNull
|
||||||
public String toString() {
|
public String toString() {
|
||||||
if (uri != null) {
|
if (uri != null) {
|
||||||
return uri.toString();
|
return uri.toString();
|
||||||
|
@ -124,58 +145,25 @@ public class XmppUri {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Fingerprint> parseFingerprints(String query) {
|
private static List<Fingerprint> parseFingerprints(Map<String,String> parameters) {
|
||||||
return parseFingerprints(query, ';');
|
ImmutableList.Builder<Fingerprint> builder = new ImmutableList.Builder<>();
|
||||||
}
|
for (Map.Entry<String, String> parameter : parameters.entrySet()) {
|
||||||
|
final String key = parameter.getKey();
|
||||||
private List<Fingerprint> parseFingerprints(String query, char seperator) {
|
final String value = parameter.getValue().toLowerCase(Locale.US);
|
||||||
List<Fingerprint> fingerprints = new ArrayList<>();
|
if (key.startsWith(OMEMO_URI_PARAM)) {
|
||||||
String[] pairs = query == null ? new String[0] : query.split(String.valueOf(seperator));
|
|
||||||
for (String pair : pairs) {
|
|
||||||
String[] parts = pair.split("=", 2);
|
|
||||||
if (parts.length == 2) {
|
|
||||||
String key = parts[0].toLowerCase(Locale.US);
|
|
||||||
String value = parts[1].toLowerCase(Locale.US);
|
|
||||||
if (key.startsWith(OMEMO_URI_PARAM)) {
|
|
||||||
try {
|
|
||||||
int id = Integer.parseInt(key.substring(OMEMO_URI_PARAM.length()));
|
|
||||||
fingerprints.add(new Fingerprint(FingerprintType.OMEMO, value, id));
|
|
||||||
} catch (Exception e) {
|
|
||||||
//ignoring invalid device id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return fingerprints;
|
|
||||||
}
|
|
||||||
|
|
||||||
private String parseParameter(String key, String query) {
|
|
||||||
for (String pair : query == null ? new String[0] : query.split(";")) {
|
|
||||||
final String[] parts = pair.split("=", 2);
|
|
||||||
if (parts.length == 2 && key.equals(parts[0].toLowerCase(Locale.US))) {
|
|
||||||
try {
|
try {
|
||||||
return URLDecoder.decode(parts[1], "UTF-8");
|
final int id = Integer.parseInt(key.substring(OMEMO_URI_PARAM.length()));
|
||||||
} catch (UnsupportedEncodingException e) {
|
builder.add(new Fingerprint(FingerprintType.OMEMO, value, id));
|
||||||
return null;
|
} catch (Exception e) {
|
||||||
|
//ignoring invalid device id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return builder.build();
|
||||||
}
|
|
||||||
|
|
||||||
private boolean hasAction(String query, String action) {
|
|
||||||
for (String pair : query == null ? new String[0] : query.split(";")) {
|
|
||||||
final String[] parts = pair.split("=", 2);
|
|
||||||
if (parts.length == 1 && parts[0].toLowerCase(Locale.US).startsWith(action)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isAction(final String action) {
|
public boolean isAction(final String action) {
|
||||||
return this.action != null && this.action.equals(action);
|
return parameters.containsKey(action);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Jid getJid() {
|
public Jid getJid() {
|
||||||
|
@ -199,11 +187,11 @@ public class XmppUri {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getBody() {
|
public String getBody() {
|
||||||
return body;
|
return parameters.get("body");
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return parameters.get("name");
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Fingerprint> getFingerprints() {
|
public List<Fingerprint> getFingerprints() {
|
||||||
|
@ -218,7 +206,7 @@ public class XmppUri {
|
||||||
OMEMO
|
OMEMO
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getFingerprintUri(String base, List<XmppUri.Fingerprint> fingerprints, char seperator) {
|
public static String getFingerprintUri(String base, List<XmppUri.Fingerprint> fingerprints, char separator) {
|
||||||
StringBuilder builder = new StringBuilder(base);
|
StringBuilder builder = new StringBuilder(base);
|
||||||
builder.append('?');
|
builder.append('?');
|
||||||
for (int i = 0; i < fingerprints.size(); ++i) {
|
for (int i = 0; i < fingerprints.size(); ++i) {
|
||||||
|
@ -230,7 +218,7 @@ public class XmppUri {
|
||||||
builder.append('=');
|
builder.append('=');
|
||||||
builder.append(fingerprints.get(i).fingerprint);
|
builder.append(fingerprints.get(i).fingerprint);
|
||||||
if (i != fingerprints.size() - 1) {
|
if (i != fingerprints.size() - 1) {
|
||||||
builder.append(seperator);
|
builder.append(separator);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
|
@ -247,9 +235,10 @@ public class XmppUri {
|
||||||
this.deviceId = deviceId;
|
this.deviceId = deviceId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return type.toString() + ": " + fingerprint + (deviceId != 0 ? " " + String.valueOf(deviceId) : "");
|
return type.toString() + ": " + fingerprint + (deviceId != 0 ? " " + deviceId : "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue