Jid.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
 27import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
 28import java.io.Serializable;
 29
 30/**
 31 * Represents the JID as described in <a href="https://tools.ietf.org/html/rfc7622">Extensible Messaging and Presence Protocol (XMPP): Address Format</a>.
 32 * <p>
 33 * A JID consists of three parts:
 34 * <p>
 35 * [ localpart "@" ] domainpart [ "/" resourcepart ]
 36 * </p>
 37 * The easiest way to create a JID is to use the {@link #of(CharSequence)} method:
 38 * ```java
 39 * Jid jid = Jid.of("juliet@capulet.lit/balcony");
 40 * ```
 41 * You can then get the parts from it via the respective methods:
 42 * ```java
 43 * String local = jid.getLocal(); // juliet
 44 * String domain = jid.getDomain(); // capulet.lit
 45 * String resource = jid.getResource(); // balcony
 46 * ```
 47 * Implementations of this interface should override <code>equals()</code> and <code>hashCode()</code>, so that different instances with the same value are equal:
 48 * ```java
 49 * Jid.of("romeo@capulet.lit/balcony").equals(Jid.of("romeo@capulet.lit/balcony")); // true
 50 * ```
 51 * The default implementation of this class also supports <a href="https://xmpp.org/extensions/xep-0106.html">XEP-0106: JID Escaping</a>, i.e.
 52 * ```java
 53 * Jid.of("d'artagnan@musketeers.lit")
 54 * ```
 55 * is escaped as <code>d\\27artagnan@musketeers.lit</code>.
 56 * <p>
 57 * Implementations of this interface should be thread-safe and immutable.
 58 *
 59 * @author Christian Schudt
 60 * @see <a href="https://tools.ietf.org/html/rfc7622">RFC 7622 - Extensible Messaging and Presence Protocol (XMPP): Address Format</a>
 61 */
 62public interface Jid extends Comparable<Jid>, Serializable, CharSequence {
 63
 64    /**
 65     * The maximal length of a full JID, which is 3071.
 66     * <blockquote>
 67     * <p><cite><a href="https://tools.ietf.org/html/rfc7622#section-3.1">3.1.  Fundamentals</a></cite></p>
 68     * <p>Each allowable portion of a JID (localpart, domainpart, and
 69     * resourcepart) is 1 to 1023 octets in length, resulting in a maximum
 70     * total size (including the '@' and '/' separators) of 3071 octets.
 71     * </p>
 72     * </blockquote>
 73     * Note that the length is based on bytes, not characters.
 74     *
 75     * @see #MAX_BARE_JID_LENGTH
 76     */
 77    int MAX_FULL_JID_LENGTH = 3071;
 78
 79    /**
 80     * The maximal length of a bare JID, which is 2047 (1023 + 1 + 1023).
 81     * Note that the length is based on bytes, not characters.
 82     *
 83     * @see #MAX_FULL_JID_LENGTH
 84     */
 85    int MAX_BARE_JID_LENGTH = 2047;
 86
 87    /**
 88     * The service discovery feature used for determining support of JID escaping (<code>jid\20escaping</code>).
 89     */
 90    String ESCAPING_FEATURE = "jid\\20escaping";
 91
 92    /**
 93     * Returns a full JID with a domain and resource part, e.g. <code>capulet.com/balcony</code>
 94     *
 95     * @param local    The local part.
 96     * @param domain   The domain.
 97     * @param resource The resource part.
 98     * @return The JID.
 99     * @throws NullPointerException     If the domain is null.
100     * @throws IllegalArgumentException If the domain, local or resource part are not valid.
101     */
102    static Jid of(CharSequence local, CharSequence domain, CharSequence resource) {
103        return new FullJid(local, domain, resource);
104    }
105
106    /**
107     * Creates a bare JID with only the domain part, e.g. <code>capulet.com</code>
108     *
109     * @param domain The domain.
110     * @return The JID.
111     * @throws NullPointerException     If the domain is null.
112     * @throws IllegalArgumentException If the domain or local part are not valid.
113     */
114    static Jid ofDomain(CharSequence domain) {
115        return new FullJid(null, domain, null);
116    }
117
118    /**
119     * Creates a bare JID with a local and domain part, e.g. <code>juliet@capulet.com</code>
120     *
121     * @param local  The local part.
122     * @param domain The domain.
123     * @return The JID.
124     * @throws NullPointerException     If the domain is null.
125     * @throws IllegalArgumentException If the domain or local part are not valid.
126     */
127    static Jid ofLocalAndDomain(CharSequence local, CharSequence domain) {
128        return new FullJid(local, domain, null);
129    }
130
131    /**
132     * Creates a full JID with a domain and resource part, e.g. <code>capulet.com/balcony</code>
133     *
134     * @param domain   The domain.
135     * @param resource The resource part.
136     * @return The JID.
137     * @throws NullPointerException     If the domain is null.
138     * @throws IllegalArgumentException If the domain or resource are not valid.
139     */
140    static Jid ofDomainAndResource(CharSequence domain, CharSequence resource) {
141        return new FullJid(null, domain, resource);
142    }
143
144    /**
145     * Creates a JID from an unescaped string. The format must be
146     * <blockquote><p>[ localpart "@" ] domainpart [ "/" resourcepart ]</p></blockquote>
147     * The input string will be escaped.
148     *
149     * @param jid The JID.
150     * @return The JID.
151     * @throws NullPointerException     If the jid is null.
152     * @throws IllegalArgumentException If the jid could not be parsed or is not valid.
153     * @see <a href="https://xmpp.org/extensions/xep-0106.html">XEP-0106: JID Escaping</a>
154     */
155    static Jid of(CharSequence jid) {
156        if (jid instanceof Jid) {
157            return (Jid) jid;
158        }
159        return FullJid.of(jid.toString(), false);
160    }
161
162    /**
163     * Creates a JID from a escaped JID string. The format must be
164     * <blockquote><p>[ localpart "@" ] domainpart [ "/" resourcepart ]</p></blockquote>
165     * This method should be used, when parsing JIDs from the XMPP stream.
166     *
167     * @param jid The JID.
168     * @return The JID.
169     * @throws NullPointerException     If the jid is null.
170     * @throws IllegalArgumentException If the jid could not be parsed or is not valid.
171     * @see <a href="https://xmpp.org/extensions/xep-0106.html">XEP-0106: JID Escaping</a>
172     */
173    static Jid ofEscaped(CharSequence jid) {
174        return FullJid.of(jid.toString(), true);
175    }
176
177    /**
178     * Checks if the JID is a full JID.
179     * <blockquote>
180     * <p>The term "full JID" refers to an XMPP address of the form &lt;localpart@domainpart/resourcepart&gt; (for a particular authorized client or device associated with an account) or of the form &lt;domainpart/resourcepart&gt; (for a particular resource or script associated with a server).</p>
181     * </blockquote>
182     *
183     * @return True, if the JID is a full JID; otherwise false.
184     */
185    boolean isFullJid();
186
187    /**
188     * Checks if the JID is a bare JID.
189     * <blockquote>
190     * <p>The term "bare JID" refers to an XMPP address of the form &lt;localpart@domainpart&gt; (for an account at a server) or of the form &lt;domainpart&gt; (for a server).</p>
191     * </blockquote>
192     *
193     * @return True, if the JID is a bare JID; otherwise false.
194     */
195    boolean isBareJid();
196
197    /**
198     * Checks if the JID is a domain JID, i.e. if it has no local part.
199     *
200     * @return True, if the JID is a domain JID, i.e. if it has no local part.
201     */
202    boolean isDomainJid();
203
204    /**
205     * Gets the bare JID representation of this JID, i.e. removes the resource part.
206     * <blockquote>
207     * <p>The term "bare JID" refers to an XMPP address of the form &lt;localpart@domainpart&gt; (for an account at a server) or of the form &lt;domainpart&gt; (for a server).</p>
208     * </blockquote>
209     *
210     * @return The bare JID.
211     * @see #withResource(CharSequence)
212     */
213    Jid asBareJid();
214
215    /**
216     * Creates a new JID with a new local part and the same domain and resource part of the current JID.
217     *
218     * @param local The local part.
219     * @return The JID with a new local part.
220     * @throws IllegalArgumentException If the local is not a valid local part.
221     * @see #withResource(CharSequence)
222     */
223    Jid withLocal(CharSequence local);
224
225    /**
226     * Creates a new full JID with a resource and the same local and domain part of the current JID.
227     *
228     * @param resource The resource.
229     * @return The full JID with a resource.
230     * @throws IllegalArgumentException If the resource is not a valid resource part.
231     * @see #asBareJid()
232     * @see #withLocal(CharSequence)
233     */
234    Jid withResource(CharSequence resource);
235
236    /**
237     * Creates a new JID at a subdomain and at the same domain as this JID.
238     *
239     * @param subdomain The subdomain.
240     * @return The JID at a subdomain.
241     * @throws NullPointerException     If subdomain is null.
242     * @throws IllegalArgumentException If subdomain is not a valid subdomain name.
243     */
244    Jid atSubdomain(CharSequence subdomain);
245
246    /**
247     * Gets the local part of the JID, also known as the name or node.
248     * <blockquote>
249     * <p><cite><a href="https://tools.ietf.org/html/rfc7622#section-3.3">3.3.  Localpart</a></cite></p>
250     * <p>The localpart of a JID is an optional identifier placed before the
251     * domainpart and separated from the latter by the '@' character.
252     * Typically, a localpart uniquely identifies the entity requesting and
253     * using network access provided by a server (i.e., a local account),
254     * although it can also represent other kinds of entities (e.g., a
255     * chatroom associated with a multi-user chat service [XEP-0045]).  The
256     * entity represented by an XMPP localpart is addressed within the
257     * context of a specific domain (i.e., &lt;localpart@domainpart&gt;).</p>
258     * </blockquote>
259     *
260     * @return The local part or null.
261     * @see #getEscapedLocal()
262     */
263    String getLocal();
264
265    /**
266     * Gets the escaped local part of the JID.
267     *
268     * @return The escaped local part or null.
269     * @see #getLocal()
270     * @since 0.8.0
271     */
272    String getEscapedLocal();
273
274    /**
275     * Gets the domain part.
276     * <blockquote>
277     * <p><cite><a href="https://tools.ietf.org/html/rfc7622#section-3.2">3.2.  Domainpart</a></cite></p>
278     * <p>The domainpart is the primary identifier and is the only REQUIRED
279     * element of a JID (a mere domainpart is a valid JID).  Typically,
280     * a domainpart identifies the "home" server to which clients connect
281     * for XML routing and data management functionality.</p>
282     * </blockquote>
283     *
284     * @return The domain part.
285     */
286    String getDomain();
287
288    /**
289     * Gets the resource part.
290     * <blockquote>
291     * <p><cite><a href="https://tools.ietf.org/html/rfc7622#section-3.4">3.4.  Resourcepart</a></cite></p>
292     * <p>The resourcepart of a JID is an optional identifier placed after the
293     * domainpart and separated from the latter by the '/' character.  A
294     * resourcepart can modify either a &lt;localpart@domainpart&gt; address or a
295     * mere &lt;domainpart&gt; address.  Typically, a resourcepart uniquely
296     * identifies a specific connection (e.g., a device or location) or
297     * object (e.g., an occupant in a multi-user chatroom [XEP-0045])
298     * belonging to the entity associated with an XMPP localpart at a domain
299     * (i.e., &lt;localpart@domainpart/resourcepart&gt;).</p>
300     * </blockquote>
301     *
302     * @return The resource part or null.
303     */
304    String getResource();
305
306    /**
307     * Returns the JID in escaped form as described in <a href="https://xmpp.org/extensions/xep-0106.html">XEP-0106: JID Escaping</a>.
308     *
309     * @return The escaped JID.
310     * @see #toString()
311     */
312    String toEscapedString();
313}