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 * Bug reports contributed by Joao Paulo, Stephen Jones,
28 * John Zeng and Alstair Cole.
29 *
30 */
31/*******************************************************************************
32* Product of NIST/ITL Advanced Networking Technologies Division (ANTD).        *
33*******************************************************************************/
34package gov.nist.javax.sip.header;
35
36import gov.nist.core.NameValue;
37import gov.nist.core.NameValueList;
38import gov.nist.javax.sip.address.AddressImpl;
39
40import javax.sip.InvalidArgumentException;
41import javax.sip.header.ContactHeader;
42import java.text.ParseException;
43
44/**
45 * Contact Item.
46 *
47 * @see gov.nist.javax.sip.header.ContactList
48 *
49 * @author M. Ranganathan  <br/>
50 * @version 1.2 $Revision: 1.13 $ $Date: 2009/10/18 13:46:31 $
51 * @since 1.1
52 *
53 *
54 */
55public final  class Contact
56    extends AddressParametersHeader
57    implements javax.sip.header.ContactHeader {
58    /**
59     * Comment for <code>serialVersionUID</code>
60     */
61    private static final long serialVersionUID = 1677294871695706288L;
62    public static final String ACTION = ParameterNames.ACTION;
63    public static final String PROXY = ParameterNames.PROXY;
64    public static final String REDIRECT = ParameterNames.REDIRECT;
65    public static final String EXPIRES = ParameterNames.EXPIRES;
66    public static final String Q = ParameterNames.Q;
67
68    // This must be private or the toString will go for a loop!
69    private ContactList contactList;
70
71    /** wildCardFlag field.
72     */
73    protected boolean wildCardFlag;
74
75    /** Default constructor.
76     */
77    public Contact() {
78        super(NAME);
79    }
80
81    /** Set a parameter.
82    */
83    public void setParameter(String name, String value) throws ParseException {
84        NameValue nv = parameters.getNameValue(name);
85        if (nv != null) {
86            nv.setValueAsObject(value);
87        } else {
88            nv = new NameValue(name, value);
89            if (name.equalsIgnoreCase("methods"))
90                nv.setQuotedValue();
91            this.parameters.set(nv);
92        }
93    }
94
95    /**
96     * Encode body of the header into a cannonical String.
97     * @return string encoding of the header value.
98     */
99    protected String encodeBody() {
100        return encodeBody(new StringBuffer()).toString();
101    }
102
103    protected StringBuffer encodeBody(StringBuffer buffer) {
104        if (wildCardFlag) {
105            buffer.append('*');
106        }
107        else {
108            // Bug report by Joao Paulo
109            if (address.getAddressType() == AddressImpl.NAME_ADDR) {
110                address.encode(buffer);
111            } else {
112                // Encoding in canonical form must have <> around address.
113                buffer.append('<');
114                address.encode(buffer);
115                buffer.append('>');
116            }
117            if (!parameters.isEmpty()) {
118                buffer.append(SEMICOLON);
119                parameters.encode(buffer);
120            }
121        }
122
123        return buffer;
124    }
125
126    /** get the Contact list.
127     * @return ContactList
128     */
129    public ContactList getContactList() {
130        return contactList;
131    }
132
133    /** get the WildCardFlag field
134     * @return boolean
135     */
136    public boolean getWildCardFlag() {
137        return wildCardFlag;
138    }
139
140    /** get the address field.
141     * @return Address
142     */
143    public javax.sip.address.Address getAddress() {
144        // JAIN-SIP stores the wild card as an address!
145        return address;
146    }
147
148    /** get the parameters List
149     * @return NameValueList
150     */
151    public NameValueList getContactParms() {
152        return parameters;
153    }
154
155    /** get Expires parameter.
156     * @return the Expires parameter.
157     */
158    public int getExpires() {
159        return getParameterAsInt(EXPIRES);
160    }
161
162    /** Set the expiry time in seconds.
163    *@param expiryDeltaSeconds exipry time.
164    */
165
166    public void setExpires(int expiryDeltaSeconds) {
167        Integer deltaSeconds = Integer.valueOf(expiryDeltaSeconds);
168        this.parameters.set(EXPIRES, deltaSeconds);
169    }
170
171    /** get the Q-value
172     * @return float
173     */
174    public float getQValue() {
175        return getParameterAsFloat(Q);
176    }
177
178    /** set the Contact List
179     * @param cl ContactList to set
180     */
181    public void setContactList(ContactList cl) {
182        contactList = cl;
183    }
184
185    /**
186     * Set the wildCardFlag member
187     * @param w boolean to set
188     */
189    public void setWildCardFlag(boolean w) {
190        this.wildCardFlag = true;
191        this.address = new AddressImpl();
192        this.address.setWildCardFlag();
193    }
194
195    /**
196     * Set the address member
197     *
198     * @param address Address to set
199     */
200    public void setAddress(javax.sip.address.Address address) {
201        // Canonical form must have <> around the address.
202        if (address == null)
203            throw new NullPointerException("null address");
204        this.address = (AddressImpl) address;
205        this.wildCardFlag = false;
206    }
207
208    /**
209     * set the Q-value parameter
210     * @param qValue float to set
211     */
212    public void setQValue(float qValue) throws InvalidArgumentException {
213        if (qValue != -1 && (qValue < 0 || qValue > 1))
214            throw new InvalidArgumentException(
215                "JAIN-SIP Exception, Contact, setQValue(), "
216                    + "the qValue is not between 0 and 1");
217        this.parameters.set(Q, Float.valueOf(qValue));
218    }
219
220    public Object clone() {
221        Contact retval = (Contact) super.clone();
222        if (this.contactList != null)
223            retval.contactList = (ContactList) this.contactList.clone();
224        return retval;
225    }
226
227    /* (non-Javadoc)
228     * @see javax.sip.header.ContactHeader#setWildCard()
229     */
230    public void setWildCard() {
231       this.setWildCardFlag(true);
232
233    }
234
235    /* (non-Javadoc)
236     * @see javax.sip.header.ContactHeader#isWildCard()
237     */
238    public boolean isWildCard() {
239
240        return this.address.isWildcard();
241    }
242
243    public boolean equals(Object other) {
244        return (other instanceof ContactHeader) && super.equals(other);
245    }
246
247    public void removeSipInstanceParam() {
248        if (parameters != null)
249            parameters.delete(ParameterNames.SIP_INSTANCE);
250    }
251
252    public String getSipInstanceParam() {
253        return (String) parameters.getValue(ParameterNames.SIP_INSTANCE);
254    }
255
256    public void setSipInstanceParam(String value) {
257        this.parameters.set(ParameterNames.SIP_INSTANCE, value);
258    }
259
260    /**
261     *remove the pub-gruu value from the parameter list if it exists.
262     */
263    public void removePubGruuParam() {
264        if (parameters != null)
265            parameters.delete(ParameterNames.PUB_GRUU);
266    }
267
268    public String getPubGruuParam() {
269        return (String) parameters.getValue(ParameterNames.PUB_GRUU);
270    }
271
272    public void setPubGruuParam(String value)
273    {
274        this.parameters.set(ParameterNames.PUB_GRUU, value);
275    }
276
277    /**
278     *remove the pub-gruu value from the parameter list if it exists.
279     */
280    public void removeTempGruuParam() {
281        if (parameters != null)
282            parameters.delete(ParameterNames.TEMP_GRUU);
283    }
284
285    public String getTempGruuParam() {
286        return (String) parameters.getValue(ParameterNames.TEMP_GRUU);
287    }
288
289    public void setTempGruuParam(String value)
290    {
291        this.parameters.set(ParameterNames.TEMP_GRUU, value);
292    }
293}
294