001: /*
002: * Portions Copyright 2000-2007 Sun Microsystems, Inc. All Rights
003: * Reserved. Use is subject to license terms.
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License version
008: * 2 only, as published by the Free Software Foundation.
009: *
010: * This program is distributed in the hope that it will be useful, but
011: * WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * General Public License version 2 for more details (a copy is
014: * included at /legal/license.txt).
015: *
016: * You should have received a copy of the GNU General Public License
017: * version 2 along with this work; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
022: * Clara, CA 95054 or visit www.sun.com if you need additional
023: * information or have any questions.
024: */
025: /*
026: */
027: package gov.nist.siplite.header;
028:
029: import gov.nist.core.*;
030: import gov.nist.siplite.parser.Lexer;
031:
032: /**
033: * The call identifer that goes into a callID header and a in-reply-to header.
034: * @see CallIdHeader
035: */
036: public final class CallIdentifier extends GenericObject {
037:
038: /**
039: * localId field
040: */
041: protected String localId;
042:
043: /**
044: * host field
045: */
046: protected String host;
047:
048: /**
049: * Default constructor
050: */
051: public CallIdentifier() {
052: }
053:
054: /**
055: * Constructor
056: * @param localId is the local id.
057: * @param host is the host.
058: */
059: public CallIdentifier(String localId, String host) {
060: this .localId = localId;
061: this .host = host;
062: }
063:
064: /**
065: * constructor
066: * @param cid String to set
067: * @throws IllegalArgumentException if cid is null or is not a token,
068: * or token@token
069: */
070: public CallIdentifier(String cid) throws IllegalArgumentException {
071: setCallIdHeader(cid);
072: }
073:
074: /**
075: * Get the encoded version of this id.
076: * @return String to set
077: */
078: public String encode() {
079: if (host != null) {
080: return localId + Separators.AT + host;
081: } else {
082: return localId;
083: }
084: }
085:
086: /**
087: * Compare two call identifiers for equality.
088: * @param other Object to set
089: * @return true if the two call identifiers are equals, false
090: * otherwise
091: */
092: public boolean equals(Object other) {
093: if (!other.getClass().equals(this .getClass())) {
094: return false;
095: }
096: CallIdentifier that = (CallIdentifier) other;
097: if (this .localId.compareTo(that.localId) != 0) {
098: return false;
099: }
100: if (this .host == that.host)
101: return true;
102: if ((this .host == null && that.host != null)
103: || (this .host != null && that.host == null))
104: return false;
105: if (Utils.compareToIgnoreCase(host, that.host) != 0) {
106: return false;
107: }
108: return true;
109: }
110:
111: /**
112: * get the LocalId field
113: * @return String
114: */
115: public String getLocalId() {
116: return localId;
117: }
118:
119: /**
120: * get the host field
121: * @return host member String
122: */
123: public String getHost() {
124: return host;
125: }
126:
127: /**
128: * Set the localId member
129: * @param localId String to set
130: */
131: public void setLocalId(String localId) {
132: this .localId = localId;
133: }
134:
135: /**
136: * set the callId field
137: * @param cid Strimg to set
138: * @throws IllegalArgumentException if cid is null or is not a token or
139: * token@token
140: */
141: public void setCallIdHeader(String cid)
142: throws IllegalArgumentException {
143: if (cid == null)
144: throw new IllegalArgumentException("NULL!");
145: int index = cid.indexOf('@');
146: if (index == -1) {
147: checkValue(cid);
148: localId = cid;
149: host = null;
150: } else {
151: if (index == 0 || index == cid.length() - 1) {
152: throw new IllegalArgumentException(
153: "CallIdHeader must be token@token or token");
154: }
155: String temp1 = cid.substring(0, index);
156: String temp2 = cid.substring(index + 1, cid.length());
157: checkValue(temp1);
158: checkValue(temp2);
159: localId = temp1;
160: host = temp2;
161: }
162: }
163:
164: /**
165: * Checks Call-Id value validity
166: *
167: * @param cid string to be checked
168: *
169: * @throws IllegalArgumentException in case of illegal symbol use
170: */
171: private void checkValue(String cid) throws IllegalArgumentException {
172: // RFC 3261 p.228
173: // Call-ID = ( "Call-ID" / "i" ) HCOLON callid
174: // callid = word [ "@" word ]
175: // word = 1*(alphanum / "-" / "." / "!" / "%" / "*" /
176: // "_" / "+" / "`" / "'" / "~" /
177: // "(" / ")" / "<" / ">" /
178: // ":" / "\" / DQUOTE /
179: // "/" / "[" / "]" / "?" /
180: // "{" / "}" )
181: // The word construct is used in
182: // Call-ID to allow most separators to be used.
183: String word = "-.!%*_+`'~()<>:\\\"/[]?{}";
184: char c;
185: for (int i = 0; i < cid.length(); i++) {
186: c = cid.charAt(i);
187: if (Lexer.isAlpha(c) || Lexer.isHexDigit(c)
188: || word.indexOf(c) != -1) {
189: continue;
190: }
191: throw new IllegalArgumentException("Wrong Call-Id value:"
192: + "illegal use of symbol '" + c + "' at '" + cid
193: + "'");
194: }
195: }
196:
197: /**
198: * Set the host member
199: * @param host String to set
200: */
201: public void setHost(String host) {
202: this .host = host;
203: }
204:
205: /**
206: * Clone - do a deep copy.
207: * @return Object CallIdentifier
208: */
209: public Object clone() {
210: CallIdentifier retval = new CallIdentifier();
211:
212: if (this .localId != null)
213: retval.localId = new String(this .localId);
214: if (this .host != null)
215: retval.host = new String(this .host);
216: return retval;
217: }
218:
219: /**
220: * Encodes the object. Calls encode().
221: * @return String canonical encoded version of this CallIdentifier.
222: */
223: public String toString() {
224: return encode();
225: }
226:
227: }
|