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
 45	public  static Jid of(String jid, boolean fallback) {
 46		final int pos = jid.indexOf('/');
 47		if (fallback && pos >= 0 && jid.length() >= pos + 1) {
 48			if (jid.substring(pos+1).trim().isEmpty()) {
 49				return Jid.ofEscaped(jid.substring(0,pos));
 50			}
 51		}
 52		return new InvalidJid(jid);
 53	}
 54
 55	@Override
 56	@NonNull
 57	public String toString() {
 58		return value;
 59	}
 60
 61	@Override
 62	public boolean isFullJid() {
 63		throw new AssertionError("Not implemented");
 64	}
 65
 66	@Override
 67	public boolean isBareJid() {
 68		throw new AssertionError("Not implemented");
 69	}
 70
 71	@Override
 72	public Jid asBareJid() {
 73		throw new AssertionError("Not implemented");
 74	}
 75
 76	@Override
 77	public Jid withLocal(CharSequence charSequence) {
 78		throw new AssertionError("Not implemented");
 79	}
 80
 81	@Override
 82	public Jid withResource(CharSequence charSequence) {
 83		throw new AssertionError("Not implemented");
 84	}
 85
 86	@Override
 87	public Jid atSubdomain(CharSequence charSequence) {
 88		throw new AssertionError("Not implemented");
 89	}
 90
 91	@Override
 92	public String getLocal() {
 93		throw new AssertionError("Not implemented");
 94	}
 95
 96	@Override
 97	public String getEscapedLocal() {
 98		throw new AssertionError("Not implemented");
 99	}
100
101	@Override
102	public String getDomain() {
103		throw new AssertionError("Not implemented");
104	}
105
106	@Override
107	public String getResource() {
108		throw new AssertionError("Not implemented");
109	}
110
111	@Override
112	public String toEscapedString() {
113		throw new AssertionError("Not implemented");
114	}
115
116	@Override
117	public int length() {
118		return value.length();
119	}
120
121	@Override
122	public char charAt(int index) {
123		return value.charAt(index);
124	}
125
126	@Override
127	public CharSequence subSequence(int start, int end) {
128		return value.subSequence(start, end);
129	}
130
131	@Override
132	public int compareTo(@NonNull Jid o) {
133		throw new AssertionError("Not implemented");
134	}
135
136	public static Jid getNullForInvalid(Jid jid) {
137		if (jid != null && jid instanceof InvalidJid) {
138			return null;
139		} else {
140			return jid;
141		}
142	}
143
144	public static boolean isValid(Jid jid) {
145		return !(jid != null && jid instanceof InvalidJid);
146	}
147
148	public static boolean hasValidFrom(AbstractStanza stanza) {
149		final String from = stanza.getAttribute("from");
150		if (from == null) {
151			return false;
152		}
153		try {
154			Jid.ofEscaped(from);
155			return true;
156		} catch (IllegalArgumentException e) {
157			return false;
158		}
159	}
160}