InvalidJid.java

  1/*
  2 * Copyright (c) 2018, Daniel Gultsch All rights reserved.
  3 *
  4 * Redistribution and use in source and binary forms, with or without modification,
  5 * are permitted provided that the following conditions are met:
  6 *
  7 * 1. Redistributions of source code must retain the above copyright notice, this
  8 * list of conditions and the following disclaimer.
  9 *
 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
 11 * this list of conditions and the following disclaimer in the documentation and/or
 12 * other materials provided with the distribution.
 13 *
 14 * 3. Neither the name of the copyright holder nor the names of its contributors
 15 * may be used to endorse or promote products derived from this software without
 16 * specific prior written permission.
 17 *
 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28 */
 29
 30package eu.siacs.conversations.xmpp;
 31
 32import android.support.annotation.NonNull;
 33
 34import eu.siacs.conversations.xmpp.stanzas.AbstractStanza;
 35import rocks.xmpp.addr.Jid;
 36
 37public class InvalidJid implements Jid {
 38
 39	private final String value;
 40
 41	private InvalidJid(String jid) {
 42		this.value = jid;
 43	}
 44	public  static Jid of(String jid, boolean fallback) {
 45		final int pos = jid.indexOf('/');
 46		if (fallback && pos >= 0 && jid.length() >= pos + 1) {
 47			if (jid.substring(pos+1).trim().isEmpty()) {
 48				return Jid.ofEscaped(jid.substring(0,pos));
 49			}
 50		}
 51		return new InvalidJid(jid);
 52	}
 53
 54	@Override
 55	@NonNull
 56	public String toString() {
 57		return value;
 58	}
 59
 60	@Override
 61	public boolean isFullJid() {
 62		throw new AssertionError("Not implemented");
 63	}
 64
 65	@Override
 66	public boolean isBareJid() {
 67		throw new AssertionError("Not implemented");
 68	}
 69
 70	@Override
 71	public Jid asBareJid() {
 72		throw new AssertionError("Not implemented");
 73	}
 74
 75	@Override
 76	public Jid withLocal(CharSequence charSequence) {
 77		throw new AssertionError("Not implemented");
 78	}
 79
 80	@Override
 81	public Jid withResource(CharSequence charSequence) {
 82		throw new AssertionError("Not implemented");
 83	}
 84
 85	@Override
 86	public Jid atSubdomain(CharSequence charSequence) {
 87		throw new AssertionError("Not implemented");
 88	}
 89
 90	@Override
 91	public String getLocal() {
 92		throw new AssertionError("Not implemented");
 93	}
 94
 95	@Override
 96	public String getEscapedLocal() {
 97		throw new AssertionError("Not implemented");
 98	}
 99
100	@Override
101	public String getDomain() {
102		throw new AssertionError("Not implemented");
103	}
104
105	@Override
106	public String getResource() {
107		throw new AssertionError("Not implemented");
108	}
109
110	@Override
111	public String toEscapedString() {
112		throw new AssertionError("Not implemented");
113	}
114
115	@Override
116	public int length() {
117		return value.length();
118	}
119
120	@Override
121	public char charAt(int index) {
122		return value.charAt(index);
123	}
124
125	@Override
126	public CharSequence subSequence(int start, int end) {
127		return value.subSequence(start, end);
128	}
129
130	@Override
131	public int compareTo(@NonNull Jid o) {
132		throw new AssertionError("Not implemented");
133	}
134
135	public static Jid getNullForInvalid(Jid jid) {
136		if (jid != null && jid instanceof InvalidJid) {
137			return null;
138		} else {
139			return jid;
140		}
141	}
142
143	public static boolean isValid(Jid jid) {
144		return !(jid != null && jid instanceof InvalidJid);
145	}
146
147	public static boolean hasValidFrom(AbstractStanza stanza) {
148		final String from = stanza.getAttribute("from");
149		if (from == null) {
150			return false;
151		}
152		try {
153			Jid.ofEscaped(from);
154			return true;
155		} catch (IllegalArgumentException e) {
156			return false;
157		}
158	}
159}