MalformedJid.java

  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
 27/**
 28 * Represents a malformed JID in order to handle the <code>jid-malformed</code> error.
 29 * <p>
 30 * This class is not intended to be publicly instantiable, but is used for malformed JIDs during parsing automatically.
 31 *
 32 * @author Christian Schudt
 33 * @see <a href="https://xmpp.org/rfcs/rfc6120.html#stanzas-error-conditions-jid-malformed">RFC 6120, 8.3.3.8.  jid-malformed</a>
 34 */
 35public final class MalformedJid extends AbstractJid {
 36
 37    private static final long serialVersionUID = -2896737611021417985L;
 38
 39    private final String localPart;
 40
 41    private final String domainPart;
 42
 43    private final String resourcePart;
 44
 45    private final Throwable cause;
 46
 47    static MalformedJid of(final String jid, final Throwable cause) {
 48        // Do some basic parsing without any further checks or validation.
 49        final StringBuilder sb = new StringBuilder(jid);
 50        // 1.  Remove any portion from the first '/' character to the end of the
 51        // string (if there is a '/' character present).
 52        final int indexOfResourceDelimiter = jid.indexOf('/');
 53        final String resourcePart;
 54        if (indexOfResourceDelimiter > -1) {
 55            resourcePart = sb.substring(indexOfResourceDelimiter + 1);
 56            sb.delete(indexOfResourceDelimiter, sb.length());
 57        } else {
 58            resourcePart = null;
 59        }
 60        // 2.  Remove any portion from the beginning of the string to the first
 61        // '@' character (if there is an '@' character present).
 62        final int indexOfAt = jid.indexOf('@');
 63        final String localPart;
 64        if (indexOfAt > -1) {
 65            localPart = sb.substring(0, indexOfAt);
 66            sb.delete(0, indexOfAt + 1);
 67        } else {
 68            localPart = null;
 69        }
 70        return new MalformedJid(localPart, sb.toString(), resourcePart, cause);
 71    }
 72
 73    private MalformedJid(final String localPart, final String domainPart, final String resourcePart, final Throwable cause) {
 74        this.localPart = localPart;
 75        this.domainPart = domainPart;
 76        this.resourcePart = resourcePart;
 77        this.cause = cause;
 78    }
 79
 80    @Override
 81    public final Jid asBareJid() {
 82        return new MalformedJid(localPart, domainPart, null, cause);
 83    }
 84
 85    @Override
 86    public Jid withLocal(CharSequence local) {
 87        return new MalformedJid(local.toString(), domainPart, resourcePart, cause);
 88    }
 89
 90    @Override
 91    public Jid withResource(CharSequence resource) {
 92        return new MalformedJid(localPart, domainPart, resource.toString(), cause);
 93    }
 94
 95    @Override
 96    public Jid atSubdomain(CharSequence subdomain) {
 97        if (subdomain == null) {
 98            throw new NullPointerException();
 99        }
100        return new MalformedJid(localPart, subdomain + "." + domainPart, resourcePart, cause);
101    }
102
103    @Override
104    public final String getLocal() {
105        return localPart;
106    }
107
108    @Override
109    public final String getEscapedLocal() {
110        return localPart;
111    }
112
113    @Override
114    public final String getDomain() {
115        return domainPart;
116    }
117
118    @Override
119    public final String getResource() {
120        return resourcePart;
121    }
122
123    /**
124     * Gets the cause why the JID is malformed.
125     *
126     * @return The cause.
127     */
128    public final Throwable getCause() {
129        return cause;
130    }
131}