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.javax.sip.SIPConstants;
32
33/**
34 * Status Line (for SIPReply) messages.
35 *
36 * @version 1.2 $Revision: 1.7 $ $Date: 2009/10/18 13:46:34 $
37 *
38 * @author M. Ranganathan   <br/>
39 *
40 *
41 */
42public final class StatusLine extends SIPObject implements SipStatusLine {
43
44    /**
45     * Comment for <code>serialVersionUID</code>
46     */
47    private static final long serialVersionUID = -4738092215519950414L;
48
49    protected boolean matchStatusClass;
50
51    /** sipVersion field
52     */
53    protected String sipVersion;
54
55    /** status code field
56     */
57    protected int statusCode;
58
59    /** reasonPhrase field
60     */
61    protected String reasonPhrase;
62
63    /** Match with a template.
64     * Match only the response class if the last two digits of the
65     * match templates are 0's
66     */
67
68    public boolean match(Object matchObj) {
69        if (!(matchObj instanceof StatusLine))
70            return false;
71        StatusLine sl = (StatusLine) matchObj;
72        // A pattern matcher has been registered.
73        if (sl.matchExpression != null)
74            return sl.matchExpression.match(this.encode());
75        // no patter matcher has been registered..
76        if (sl.sipVersion != null && !sl.sipVersion.equals(sipVersion))
77            return false;
78        if (sl.statusCode != 0) {
79            if (matchStatusClass) {
80                int hiscode = sl.statusCode;
81                String codeString = Integer.toString(sl.statusCode);
82                String mycode = Integer.toString(statusCode);
83                if (codeString.charAt(0) != mycode.charAt(0))
84                    return false;
85            } else {
86                if (statusCode != sl.statusCode)
87                    return false;
88            }
89        }
90        if (sl.reasonPhrase == null || reasonPhrase == sl.reasonPhrase)
91            return true;
92        return reasonPhrase.equals(sl.reasonPhrase);
93
94    }
95
96    /** set the flag on a match template.
97     *If this set to true, then the whole status code is matched (default
98     * behavior) else only the class of the response is matched.
99     */
100    public void setMatchStatusClass(boolean flag) {
101        matchStatusClass = flag;
102    }
103
104    /** Default Constructor
105     */
106    public StatusLine() {
107        reasonPhrase = null;
108        sipVersion = SIPConstants.SIP_VERSION_STRING;
109    }
110
111    /**
112     * Encode into a canonical form.
113     * @return String
114     */
115    public String encode() {
116        String encoding = SIPConstants.SIP_VERSION_STRING + SP + statusCode;
117        if (reasonPhrase != null)
118            encoding += SP + reasonPhrase;
119        encoding += NEWLINE;
120        return encoding;
121    }
122
123    /* (non-Javadoc)
124     * @see gov.nist.javax.sip.header.SipStatusLine#getSipVersion()
125     */
126    public String getSipVersion() {
127        return sipVersion;
128    }
129
130    /* (non-Javadoc)
131     * @see gov.nist.javax.sip.header.SipStatusLine#getStatusCode()
132     */
133    public int getStatusCode() {
134        return statusCode;
135    }
136
137    /* (non-Javadoc)
138     * @see gov.nist.javax.sip.header.SipStatusLine#getReasonPhrase()
139     */
140    public String getReasonPhrase() {
141        return reasonPhrase;
142    }
143
144    /* (non-Javadoc)
145     * @see gov.nist.javax.sip.header.SipStatusLine#setSipVersion(java.lang.String)
146     */
147    public void setSipVersion(String s) {
148        sipVersion = s;
149    }
150
151    /* (non-Javadoc)
152     * @see gov.nist.javax.sip.header.SipStatusLine#setStatusCode(int)
153     */
154    public void setStatusCode(int statusCode) {
155        this.statusCode = statusCode;
156    }
157
158    /* (non-Javadoc)
159     * @see gov.nist.javax.sip.header.SipStatusLine#setReasonPhrase(java.lang.String)
160     */
161    public void setReasonPhrase(String reasonPhrase) {
162        this.reasonPhrase = reasonPhrase;
163    }
164
165    /* (non-Javadoc)
166     * @see gov.nist.javax.sip.header.SipStatusLine#getVersionMajor()
167     */
168    public String getVersionMajor() {
169        if (sipVersion == null)
170            return null;
171        String major = null;
172        boolean slash = false;
173        for (int i = 0; i < sipVersion.length(); i++) {
174            if (sipVersion.charAt(i) == '.')
175                slash = false;
176            if (slash) {
177                if (major == null)
178                    major = "" + sipVersion.charAt(i);
179                else
180                    major += sipVersion.charAt(i);
181            }
182            if (sipVersion.charAt(i) == '/')
183                slash = true;
184        }
185        return major;
186    }
187
188    /* (non-Javadoc)
189     * @see gov.nist.javax.sip.header.SipStatusLine#getVersionMinor()
190     */
191    public String getVersionMinor() {
192        if (sipVersion == null)
193            return null;
194        String minor = null;
195        boolean dot = false;
196        for (int i = 0; i < sipVersion.length(); i++) {
197            if (dot) {
198                if (minor == null)
199                    minor = "" + sipVersion.charAt(i);
200                else
201                    minor += sipVersion.charAt(i);
202            }
203            if (sipVersion.charAt(i) == '.')
204                dot = true;
205        }
206        return minor;
207    }
208}
209/*
210 * $Log: StatusLine.java,v $
211 * Revision 1.7  2009/10/18 13:46:34  deruelle_jean
212 * FindBugs Fixes (Category Performance Warnings)
213 *
214 * Issue number:
215 * Obtained from:
216 * Submitted by: Jean Deruelle
217 * Reviewed by:
218 *
219 * Revision 1.6  2009/09/15 02:55:26  mranga
220 * Issue number:  222
221 * Add HeaderFactoryExt.createStatusLine(String) and HeaderFactoryExt.createRequestLine(String)
222 * Allows users to easily parse SipFrag bodies (for example NOTIFY bodies
223 * during call transfer).
224 *
225 * Revision 1.5  2009/07/17 18:57:38  emcho
226 * Converts indentation tabs to spaces so that we have a uniform indentation policy in the whole project.
227 *
228 * Revision 1.4  2006/07/13 09:01:48  mranga
229 * Issue number:
230 * Obtained from:
231 * Submitted by:  jeroen van bemmel
232 * Reviewed by:   mranga
233 * Moved some changes from jain-sip-1.2 to java.net
234 *
235 * CVS: ----------------------------------------------------------------------
236 * CVS: Issue number:
237 * CVS:   If this change addresses one or more issues,
238 * CVS:   then enter the issue number(s) here.
239 * CVS: Obtained from:
240 * CVS:   If this change has been taken from another system,
241 * CVS:   then name the system in this line, otherwise delete it.
242 * CVS: Submitted by:
243 * CVS:   If this code has been contributed to the project by someone else; i.e.,
244 * CVS:   they sent us a patch or a set of diffs, then include their name/email
245 * CVS:   address here. If this is your work then delete this line.
246 * CVS: Reviewed by:
247 * CVS:   If we are doing pre-commit code reviews and someone else has
248 * CVS:   reviewed your changes, include their name(s) here.
249 * CVS:   If you have not had it reviewed then delete this line.
250 *
251 * Revision 1.3  2006/06/19 06:47:27  mranga
252 * javadoc fixups
253 *
254 * Revision 1.2  2006/06/16 15:26:28  mranga
255 * Added NIST disclaimer to all public domain files. Clean up some javadoc. Fixed a leak
256 *
257 * Revision 1.1.1.1  2005/10/04 17:12:35  mranga
258 *
259 * Import
260 *
261 *
262 * Revision 1.2  2004/01/22 13:26:29  sverker
263 * Issue number:
264 * Obtained from:
265 * Submitted by:  sverker
266 * Reviewed by:   mranga
267 *
268 * Major reformat of code to conform with style guide. Resolved compiler and javadoc warnings. Added CVS tags.
269 *
270 * CVS: ----------------------------------------------------------------------
271 * CVS: Issue number:
272 * CVS:   If this change addresses one or more issues,
273 * CVS:   then enter the issue number(s) here.
274 * CVS: Obtained from:
275 * CVS:   If this change has been taken from another system,
276 * CVS:   then name the system in this line, otherwise delete it.
277 * CVS: Submitted by:
278 * CVS:   If this code has been contributed to the project by someone else; i.e.,
279 * CVS:   they sent us a patch or a set of diffs, then include their name/email
280 * CVS:   address here. If this is your work then delete this line.
281 * CVS: Reviewed by:
282 * CVS:   If we are doing pre-commit code reviews and someone else has
283 * CVS:   reviewed your changes, include their name(s) here.
284 * CVS:   If you have not had it reviewed then delete this line.
285 *
286 */
287