1 /*
2 * Conditions Of Use
3 *
4 * This software was developed by employees of the National Institute of
5 * Standards and Technology (NIST), an agency of the Federal Government.
6 * Pursuant to title 15 Untied States Code Section 105, works of NIST
7 * employees are not subject to copyright protection in the United States
8 * and are considered to be in the public domain.  As a result, a formal
9 * license is not needed to use the software.
10 *
11 * This software is provided by NIST as a service and is expressly
12 * provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
13 * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
14 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
15 * AND DATA ACCURACY.  NIST does not warrant or make any representations
16 * regarding the use of the software or the results thereof, including but
17 * not limited to the correctness, accuracy, reliability or usefulness of
18 * the software.
19 *
20 * Permission to use this software is contingent upon your acceptance
21 * of the terms of this agreement
22 *
23 * .
24 *
25 */
26 /*
27  * Acknowledgement -- Lamine Brahimi
28  * Submitted a bug fix for a this class.
29  */
30 /*******************************************************************************
31  * Product of NIST/ITL Advanced Networking Technologies Division (ANTD).       *
32  *******************************************************************************/
33 package gov.nist.javax.sip.address;
34 
35 /**
36  * User information part of a URL.
37  *
38  * @version 1.2 $Revision: 1.8 $ $Date: 2009/07/17 18:57:23 $
39  * @author M. Ranganathan   <br/>
40  *
41  */
42 public final class UserInfo extends NetObject {
43 
44 
45     private static final long serialVersionUID = 7268593273924256144L;
46 
47     /** user field
48      */
49     protected String user;
50 
51     /** password field
52      */
53     protected String password;
54 
55     /** userType field
56          */
57     protected int userType;
58 
59     /** Constant field
60      */
61     public final static int TELEPHONE_SUBSCRIBER = 1;
62 
63     /** constant field
64      */
65     public final static int USER = 2;
66 
67     /** Default constructor
68      */
69     public UserInfo() {
70         super();
71     }
72 
73     /**
74      * Compare for equality.
75      * @param obj Object to set
76      * @return true if the two headers are equals, false otherwise.
77      */
78     public boolean equals(Object obj) {
79         if (getClass() != obj.getClass()) {
80             return false;
81         }
82         UserInfo other = (UserInfo) obj;
83         if (this.userType != other.userType) {
84             return false;
85         }
86         if (!this.user.equalsIgnoreCase(other.user)) {
87             return false;
88         }
89         if (this.password != null && other.password == null)
90             return false;
91 
92         if (other.password != null && this.password == null)
93             return false;
94 
95         if (this.password == other.password)
96             return true;
97 
98         return (this.password.equals(other.password));
99     }
100 
101     /**
102      * Encode the user information as a string.
103      * @return String
104      */
105     public String encode() {
106         return encode(new StringBuffer()).toString();
107     }
108 
109     public StringBuffer encode(StringBuffer buffer) {
110         if (password != null)
111             buffer.append(user).append(COLON).append(password);
112         else
113             buffer.append(user);
114 
115         return buffer;
116     }
117 
118     /** Clear the password field.
119     */
120     public void clearPassword() {
121         this.password = null;
122     }
123 
124     /**
125      * Gets the user type (which can be set to TELEPHONE_SUBSCRIBER or USER)
126      * @return the type of user.
127      */
128     public int getUserType() {
129         return userType;
130     }
131 
132     /** get the user field.
133      * @return String
134      */
135     public String getUser() {
136         return user;
137     }
138 
139     /** get the password field.
140      * @return String
141      */
142     public String getPassword() {
143         return password;
144     }
145 
146     /**
147      * Set the user member
148      * @param user String to set
149      */
150     public void setUser(String user) {
151         this.user = user;
152         // BUG Fix submitted by Lamine Brahimi
153         // add this (taken form sip_messageParser)
154         // otherwise comparison of two SipUrl will fail because this
155         // parameter is not set (whereas it is set in sip_messageParser).
156         if (user != null
157             && (user.indexOf(POUND) >= 0 || user.indexOf(SEMICOLON) >= 0)) {
158             setUserType(TELEPHONE_SUBSCRIBER);
159         } else {
160             setUserType(USER);
161         }
162     }
163 
164     /**
165      * Set the password member
166      * @param p String to set
167      */
168     public void setPassword(String p) {
169         password = p;
170     }
171 
172     /**
173      * Set the user type (to TELEPHONE_SUBSCRIBER or USER).
174      * @param type int to set
175      * @throws IllegalArgumentException if type is not in range.
176      */
177     public void setUserType(int type) throws IllegalArgumentException {
178         if (type != TELEPHONE_SUBSCRIBER && type != USER) {
179             throw new IllegalArgumentException("Parameter not in range");
180         }
181         userType = type;
182     }
183 }
184