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 * Product of NIST/ITL Advanced Networking Technologies Division (ANTD).       *
28 ******************************************************************************/
29package gov.nist.javax.sip.header;
30
31import gov.nist.core.HostPort;
32import gov.nist.javax.sip.address.AddressImpl;
33import gov.nist.javax.sip.parser.Parser;
34
35import javax.sip.header.FromHeader;
36import java.text.ParseException;
37
38/**
39 * From SIP Header.
40 *
41 * @version 1.2 $Revision: 1.9 $ $Date: 2009/07/17 18:57:31 $
42 * @since 1.1
43 *
44 * @author M. Ranganathan   <br/>
45 *
46 *
47 */
48public final class From
49    extends AddressParametersHeader
50    implements javax.sip.header.FromHeader {
51
52    /**
53     * Comment for <code>serialVersionUID</code>
54     */
55    private static final long serialVersionUID = -6312727234330643892L;
56
57    /** Default constructor
58     */
59    public From() {
60        super(NAME);
61    }
62
63    /** Generate a FROM header from a TO header
64     */
65    public From(To to) {
66        super(NAME);
67        address = to.address;
68        parameters = to.parameters;
69    }
70
71    /**
72     * Encode the header content into a String.
73     *
74     * @return String
75     */
76    protected String encodeBody() {
77        return encodeBody(new StringBuffer()).toString();
78    }
79
80    protected StringBuffer encodeBody(StringBuffer buffer) {
81        if (address.getAddressType() == AddressImpl.ADDRESS_SPEC) {
82            buffer.append(LESS_THAN);
83        }
84        address.encode(buffer);
85        if (address.getAddressType() == AddressImpl.ADDRESS_SPEC) {
86            buffer.append(GREATER_THAN);
87        }
88        if (!parameters.isEmpty()) {
89            buffer.append(SEMICOLON);
90            parameters.encode(buffer);
91        }
92        return buffer;
93    }
94
95    /**
96     * Conveniance accessor function to get the hostPort field from the address.
97     * Warning -- this assumes that the embedded URI is a SipURL.
98     *
99     * @return hostport field
100     */
101    public HostPort getHostPort() {
102        return address.getHostPort();
103    }
104
105    /**
106     * Get the display name from the address.
107     * @return Display name
108     */
109    public String getDisplayName() {
110        return address.getDisplayName();
111    }
112
113    /**
114     * Get the tag parameter from the address parm list.
115     * @return tag field
116     */
117    public String getTag() {
118        if (parameters == null)
119            return null;
120        return getParameter(ParameterNames.TAG);
121    }
122
123    /** Boolean function
124     * @return true if the Tag exist
125     */
126    public boolean hasTag() {
127        return hasParameter(ParameterNames.TAG);
128    }
129
130    /** remove Tag member
131     */
132    public void removeTag() {
133        parameters.delete(ParameterNames.TAG);
134    }
135
136    /**
137     * Set the address member
138     * @param address Address to set
139     */
140    public void setAddress(javax.sip.address.Address address) {
141        this.address = (AddressImpl) address;
142    }
143
144    /**
145     * Set the tag member
146     * @param t tag to set. From tags are mandatory.
147     */
148    public void setTag(String t) throws ParseException {
149        // JvB: check that it is a valid token
150        Parser.checkToken(t);
151        this.setParameter(ParameterNames.TAG, t);
152    }
153
154    /** Get the user@host port string.
155     */
156    public String getUserAtHostPort() {
157        return address.getUserAtHostPort();
158    }
159
160    public boolean equals(Object other) {
161        return (other instanceof FromHeader) && super.equals(other);
162    }
163
164}
165