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.address;
028:
029: import gov.nist.core.*;
030: import gov.nist.siplite.parser.*;
031:
032: /**
033: * User information part of a URL.
034: *
035: * <a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
036: */
037: public final class UserInfo {
038:
039: /**
040: * user field
041: */
042: private String user;
043:
044: /**
045: * password field
046: */
047: private String password;
048:
049: /**
050: * userType field
051: */
052: protected int userType;
053:
054: /**
055: * Constant field
056: */
057: public final static int TELEPHONE_SUBSCRIBER = 1;
058:
059: /**
060: * constant field
061: */
062: public final static int USER = 2;
063:
064: /**
065: * Default constructor
066: */
067: public UserInfo() {
068: super ();
069: }
070:
071: /**
072: * Compare for equality.
073: * @param obj Object to set
074: * @return true if the two headers are equals, false otherwise.
075: */
076: public boolean equals(Object obj) {
077: if (!getClass().getName().equals(obj.getClass().getName())) {
078: return false;
079: }
080: UserInfo other = (UserInfo) obj;
081: if (this .userType != other.userType) {
082: return false;
083: }
084: String u1 = this .user;
085: String u2 = other.user;
086: if (u1 == null)
087: u1 = "";
088: if (u2 == null)
089: u2 = "";
090: if (!u1.toLowerCase().equals(u2.toLowerCase())) {
091: return false;
092: }
093: u1 = this .password;
094: u2 = other.password;
095: if (u1 == null)
096: u1 = "";
097: if (u2 == null)
098: u2 = "";
099: if (!u1.equals(u2)) {
100: return false;
101: }
102: return (true);
103: }
104:
105: /**
106: * Encode the user information as a string.
107: * @return String
108: */
109: public String encode() {
110: if (password != null) {
111: return new StringBuffer().append(user).append(
112: Separators.COLON).append(password).toString();
113: } else {
114: return user;
115: }
116: }
117:
118: /**
119: * Clear the password field.
120: */
121: public void clearPassword() {
122: this .password = null;
123: }
124:
125: /**
126: * Gets the user type (which can be set to TELEPHONE_SUBSCRIBER or USER).
127: * @return the type of user.
128: */
129: public int getUserType() {
130: return userType;
131: }
132:
133: /**
134: * Get the user field.
135: * @return String
136: */
137: public String getUser() {
138: return user;
139: }
140:
141: /**
142: * Get the password field.
143: * @return String
144: */
145: public String getPassword() {
146: return password;
147: }
148:
149: /**
150: * Set the user member.
151: * @param user String to set
152: * @throws IllegalArgumentException if user name contains invalid
153: * characters
154: */
155: public void setUser(String user) throws IllegalArgumentException {
156: if (false == Lexer.isValidUserName(user)) {
157: throw new IllegalArgumentException("User name '" + user
158: + "' contains " + "illegal characters");
159: }
160: this .user = user;
161: // add this (taken form sip_messageParser)
162: // otherwise comparison of two SipUrl will fail because this
163: // parameter is not set (whereas it is set in sip_messageParser).
164: if (user != null
165: && (user.indexOf(Separators.POUND) >= 0 || user
166: .indexOf(Separators.SEMICOLON) >= 0)) {
167: setUserType(UserInfo.TELEPHONE_SUBSCRIBER);
168: } else {
169: setUserType(UserInfo.USER);
170: }
171: }
172:
173: /**
174: * Set the password member.
175: * @param p String to set
176: * @throws IllegalArgumentException if user name contains invalid
177: * characters
178: */
179: public void setPassword(String p) throws IllegalArgumentException {
180: // IMPL_NOTE: check for the password validity
181: password = p;
182: }
183:
184: /**
185: * Set the user type (to TELEPHONE_SUBSCRIBER or USER).
186: * @param type int to set
187: * @throws IllegalArgumentException if type is not in range.
188: */
189: public void setUserType(int type) throws IllegalArgumentException {
190: if (type != TELEPHONE_SUBSCRIBER && type != USER) {
191: throw new IllegalArgumentException("Parameter not in range");
192: }
193: userType = type;
194: }
195:
196: /**
197: * Copies the current instance.
198: * @return a copy of the current instance
199: */
200: public Object clone() {
201: UserInfo retval = new UserInfo();
202: try {
203: retval.setUser(user);
204: retval.setPassword(password);
205: } catch (IllegalArgumentException e) {
206: // intentionally ignored
207: // it shoild be impossible to get here due to this.user
208: // and this.password are verified values
209: }
210:
211: return retval;
212: }
213:
214: }
|