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 androidx.annotation.NonNull;
33
34import im.conversations.android.xmpp.model.stanza.Stanza;
35
36public class InvalidJid implements Jid {
37
38 private final String value;
39
40 private InvalidJid(String jid) {
41 this.value = jid;
42 }
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 boolean isDomainJid() {
72 throw new AssertionError("Not implemented");
73 }
74
75 @Override
76 public Jid asBareJid() {
77 throw new AssertionError("Not implemented");
78 }
79
80
81 @Override
82 public Jid withResource(CharSequence charSequence) {
83 throw new AssertionError("Not implemented");
84 }
85
86 @Override
87 public String getLocal() {
88 throw new AssertionError("Not implemented");
89 }
90
91 @Override
92 public String getEscapedLocal() {
93 throw new AssertionError("Not implemented");
94 }
95
96 @Override
97 public Jid getDomain() {
98 throw new AssertionError("Not implemented");
99 }
100
101 @Override
102 public String getResource() {
103 throw new AssertionError("Not implemented");
104 }
105
106 @Override
107 public String toEscapedString() {
108 throw new AssertionError("Not implemented");
109 }
110
111 @Override
112 public int length() {
113 return value.length();
114 }
115
116 @Override
117 public char charAt(int index) {
118 return value.charAt(index);
119 }
120
121 @Override
122 public CharSequence subSequence(int start, int end) {
123 return value.subSequence(start, end);
124 }
125
126 @Override
127 public int compareTo(@NonNull Jid o) {
128 throw new AssertionError("Not implemented");
129 }
130
131 public static Jid getNullForInvalid(Jid jid) {
132 if (jid instanceof InvalidJid) {
133 return null;
134 } else {
135 return jid;
136 }
137 }
138
139 public static boolean isValid(Jid jid) {
140 return !(jid instanceof InvalidJid);
141 }
142
143 public static boolean hasValidFrom(Stanza stanza) {
144 final String from = stanza.getAttribute("from");
145 if (from == null) {
146 return false;
147 }
148 try {
149 Jid.ofEscaped(from);
150 return true;
151 } catch (IllegalArgumentException e) {
152 return false;
153 }
154 }
155}