XmppUri.java

 1package eu.siacs.conversations.utils;
 2
 3import android.net.Uri;
 4
 5import java.io.UnsupportedEncodingException;
 6import java.net.URLDecoder;
 7
 8import eu.siacs.conversations.xmpp.jid.InvalidJidException;
 9import eu.siacs.conversations.xmpp.jid.Jid;
10
11public class XmppUri {
12
13	protected String jid;
14	protected boolean muc;
15	protected String fingerprint;
16
17	public XmppUri(String uri) {
18		try {
19			parse(Uri.parse(uri));
20		} catch (IllegalArgumentException e) {
21			try {
22				jid = Jid.fromString(uri).toBareJid().toString();
23			} catch (InvalidJidException e2) {
24				jid = null;
25			}
26		}
27	}
28
29	public XmppUri(Uri uri) {
30		parse(uri);
31	}
32
33	protected void parse(Uri uri) {
34		String scheme = uri.getScheme();
35		if ("xmpp".equalsIgnoreCase(scheme)) {
36			// sample: xmpp:jid@foo.com
37			muc = "join".equalsIgnoreCase(uri.getQuery());
38			if (uri.getAuthority() != null) {
39				jid = uri.getAuthority();
40			} else {
41				jid = uri.getSchemeSpecificPart().split("\\?")[0];
42			}
43			fingerprint = parseFingerprint(uri.getQuery());
44		} else if ("imto".equalsIgnoreCase(scheme)) {
45			// sample: imto://xmpp/jid@foo.com
46			try {
47				jid = URLDecoder.decode(uri.getEncodedPath(), "UTF-8").split("/")[1];
48			} catch (final UnsupportedEncodingException ignored) {
49				jid = null;
50			}
51		} else {
52			try {
53				jid = Jid.fromString(uri.toString()).toBareJid().toString();
54			} catch (final InvalidJidException ignored) {
55				jid = null;
56			}
57		}
58	}
59
60	protected  String parseFingerprint(String query) {
61		if (query == null) {
62			return null;
63		} else {
64			final String NEEDLE = "otr-fingerprint=";
65			int index = query.indexOf(NEEDLE);
66			if (index >= 0 && query.length() >= (NEEDLE.length() + index + 40)) {
67				return query.substring(index + NEEDLE.length(), index + NEEDLE.length() + 40);
68			} else {
69				return null;
70			}
71		}
72	}
73
74	public Jid getJid() {
75		try {
76			return this.jid == null ? null :Jid.fromString(this.jid.toLowerCase());
77		} catch (InvalidJidException e) {
78			return null;
79		}
80	}
81
82	public String getFingerprint() {
83		return this.fingerprint;
84	}
85}