1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2014-2017 Christian Schudt
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25package rocks.xmpp.addr;
26
27import java.util.Objects;
28
29/**
30 * Represents a malformed JID in order to handle the <code>jid-malformed</code> error.
31 * <p>
32 * This class is not intended to be publicly instantiable, but is used for malformed JIDs during parsing automatically.
33 *
34 * @author Christian Schudt
35 * @see <a href="https://xmpp.org/rfcs/rfc6120.html#stanzas-error-conditions-jid-malformed">RFC 6120, 8.3.3.8. jid-malformed</a>
36 */
37public final class MalformedJid extends AbstractJid {
38
39 private static final long serialVersionUID = -2896737611021417985L;
40
41 private final String localPart;
42
43 private final String domainPart;
44
45 private final String resourcePart;
46
47 private final Throwable cause;
48
49 static MalformedJid of(final String jid, final Throwable cause) {
50 // Do some basic parsing without any further checks or validation.
51 final StringBuilder sb = new StringBuilder(jid);
52 // 1. Remove any portion from the first '/' character to the end of the
53 // string (if there is a '/' character present).
54 final int indexOfResourceDelimiter = jid.indexOf('/');
55 final String resourcePart;
56 if (indexOfResourceDelimiter > -1) {
57 resourcePart = sb.substring(indexOfResourceDelimiter + 1);
58 sb.delete(indexOfResourceDelimiter, sb.length());
59 } else {
60 resourcePart = null;
61 }
62 // 2. Remove any portion from the beginning of the string to the first
63 // '@' character (if there is an '@' character present).
64 final int indexOfAt = jid.indexOf('@');
65 final String localPart;
66 if (indexOfAt > -1) {
67 localPart = sb.substring(0, indexOfAt);
68 sb.delete(0, indexOfAt + 1);
69 } else {
70 localPart = null;
71 }
72 return new MalformedJid(localPart, sb.toString(), resourcePart, cause);
73 }
74
75 private MalformedJid(final String localPart, final String domainPart, final String resourcePart, final Throwable cause) {
76 this.localPart = localPart;
77 this.domainPart = domainPart;
78 this.resourcePart = resourcePart;
79 this.cause = cause;
80 }
81
82 @Override
83 public final Jid asBareJid() {
84 return new MalformedJid(localPart, domainPart, null, cause);
85 }
86
87 @Override
88 public Jid withLocal(CharSequence local) {
89 return new MalformedJid(local.toString(), domainPart, resourcePart, cause);
90 }
91
92 @Override
93 public Jid withResource(CharSequence resource) {
94 return new MalformedJid(localPart, domainPart, resource.toString(), cause);
95 }
96
97 @Override
98 public Jid atSubdomain(CharSequence subdomain) {
99 return new MalformedJid(localPart, Objects.requireNonNull(subdomain) + "." + domainPart, resourcePart, cause);
100 }
101
102 @Override
103 public final String getLocal() {
104 return localPart;
105 }
106
107 @Override
108 public final String getEscapedLocal() {
109 return localPart;
110 }
111
112 @Override
113 public final String getDomain() {
114 return domainPart;
115 }
116
117 @Override
118 public final String getResource() {
119 return resourcePart;
120 }
121
122 /**
123 * Gets the cause why the JID is malformed.
124 *
125 * @return The cause.
126 */
127 public final Throwable getCause() {
128 return cause;
129 }
130}