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