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.header;
028:
029: import gov.nist.core.*;
030: import gov.nist.siplite.address.*;
031:
032: /**
033: * RequestLine of SIP Request.
034: *
035: */
036: public class RequestLine extends GenericObject {
037:
038: /**
039: * Uri field. Note that this can be a SIP URI or a generic URI
040: * like tel URI.
041: */
042: protected URI uri;
043:
044: /**
045: * Method field.
046: */
047: protected String method;
048:
049: /**
050: * SipVersion field
051: */
052: protected String sipVersion;
053:
054: /** Class handle. */
055: public static Class clazz;
056:
057: static {
058: clazz = new RequestLine().getClass();
059: }
060:
061: /**
062: * Default constructor.
063: */
064: public RequestLine() {
065: sipVersion = "SIP/2.0";
066: }
067:
068: /**
069: * Set the SIP version.
070: * @param sipVersion -- the SIP version to set.
071: */
072: public void setSIPVersion(String sipVersion) {
073: this .sipVersion = sipVersion;
074: }
075:
076: /**
077: * Encodes the request line as a String.
078: *
079: * @return requestLine encoded as a string.
080: */
081: public String encode() {
082: StringBuffer encoding = new StringBuffer();
083: if (method != null) {
084: encoding.append(method);
085: encoding.append(Separators.SP);
086: }
087: if (uri != null) {
088: encoding.append(uri.encode());
089: encoding.append(Separators.SP);
090: }
091: encoding.append(sipVersion + Separators.NEWLINE);
092: return encoding.toString();
093: }
094:
095: /**
096: * Gets the Request-URI.
097: * @return the request URI
098: */
099: public URI getUri() {
100: return uri;
101: }
102:
103: /**
104: * Constructor given the request URI and the method.
105: * @param requestURI the request URI
106: * @param method the operation to perform
107: */
108: public RequestLine(URI requestURI, String method) {
109: this .uri = requestURI;
110: this .method = method;
111: this .sipVersion = "SIP/2.0";
112: }
113:
114: /**
115: * Get the Method
116: *
117: * @return method string.
118: */
119: public String getMethod() {
120: return method;
121: }
122:
123: /**
124: * Get the SIP version.
125: *
126: * @return String
127: */
128: public String getSipVersion() {
129: return sipVersion;
130: }
131:
132: /**
133: * Set the uri member.
134: * @param uri URI to set.
135: */
136: public void setUri(URI uri) {
137: this .uri = uri;
138: }
139:
140: /**
141: * Set the method member
142: *
143: * @param method String to set
144: */
145: public void setMethod(String method) {
146: this .method = method;
147: }
148:
149: /**
150: * Set the sipVersion member
151: *
152: * @param s String to set
153: */
154: public void setSipVersion(String s) {
155: sipVersion = s;
156: }
157:
158: /**
159: * Get the major verrsion number.
160: *
161: * @return String major version number
162: */
163: public String getVersionMajor() {
164: if (sipVersion == null)
165: return null;
166: String major = null;
167: boolean slash = false;
168: for (int i = 0; i < sipVersion.length(); i++) {
169: if (sipVersion.charAt(i) == '.')
170: break;
171: if (slash) {
172: if (major == null)
173: major = "" + sipVersion.charAt(i);
174: else
175: major += sipVersion.charAt(i);
176: }
177: if (sipVersion.charAt(i) == '/')
178: slash = true;
179: }
180: return major;
181: }
182:
183: /**
184: * Get the minor version number.
185: *
186: * @return String minor version number
187: *
188: */
189: public String getVersionMinor() {
190: if (sipVersion == null)
191: return null;
192: String minor = null;
193: boolean dot = false;
194: for (int i = 0; i < sipVersion.length(); i++) {
195: if (dot) {
196: if (minor == null)
197: minor = "" + sipVersion.charAt(i);
198: else
199: minor += sipVersion.charAt(i);
200: }
201: if (sipVersion.charAt(i) == '.')
202: dot = true;
203: }
204: return minor;
205: }
206:
207: /**
208: * Compare for equality.
209: *
210: * @param other object to compare with. We assume that all fields
211: * are set.
212: * @return true if the object matches
213: */
214: public boolean equals(Object other) {
215: boolean retval;
216: if (!other.getClass().equals(this .getClass())) {
217: return false;
218: }
219: RequestLine that = (RequestLine) other;
220: try {
221: retval = this .method.equals(that.method)
222: && this .uri.equals(that.uri)
223: && this .sipVersion.equals(that.sipVersion);
224: } catch (NullPointerException ex) {
225: retval = false;
226: }
227: return retval;
228:
229: }
230:
231: /**
232: * Clone this request.
233: * @return copt of the current object
234: */
235: public Object clone() {
236: RequestLine retval = new RequestLine();
237: if (this .uri != null)
238: retval.uri = (URI) this .uri.clone();
239: if (this .method != null)
240: retval.method = new String(this .method);
241: if (this .sipVersion != null)
242: retval.sipVersion = new String(this .sipVersion);
243: return (Object) retval;
244: }
245:
246: }
|