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:
028: package gov.nist.siplite.header;
029:
030: import gov.nist.core.*;
031: import java.util.*;
032: import gov.nist.siplite.address.*;
033:
034: /**
035: * Parameters header. Suitable for extension by headers that have parameters.
036: *
037: *
038: * <a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
039: *
040: * @version JAIN-SIP-1.1
041: *
042: */
043: public abstract class ParametersHeader extends Header {
044: /** Contents of the parameter list. */
045: protected NameValueList parameters;
046:
047: /** Default constructor. */
048: protected ParametersHeader() {
049: this .parameters = new NameValueList();
050: }
051:
052: /**
053: * Constructor with initial header name.
054: * @param hdrName an initial header name
055: */
056: protected ParametersHeader(String hdrName) {
057: super (hdrName);
058: this .parameters = new NameValueList();
059: }
060:
061: /**
062: * Returns the value of the named parameter, or null if it is not set. A
063: * zero-length String indicates flag parameter.
064: *
065: * @param name name of parameter to retrieve
066: * @return the value of specified parameter
067: */
068: public String getParameter(String name) {
069: return this .parameters.getParameter(name);
070:
071: }
072:
073: /**
074: * Returns the parameter as an object (dont convert to string).
075: *
076: * @param name is the name of the parameter to get.
077: * @return the object associated with the name.
078: *
079: */
080: public Object getParameterValue(String name) {
081: return this .parameters.getValue(name);
082: }
083:
084: /**
085: * Returns an Vector over the names (Strings) of all parameters present
086: * in this ParametersHeader.
087: *
088: * @return an Iterator over all the parameter names
089: */
090:
091: public Vector getParameterNames() {
092: return parameters.getNames();
093: }
094:
095: /**
096: * Returns true if you have a parameter and false otherwise.
097: *
098: * @return true if the parameters list is non-empty.
099: */
100:
101: public boolean hasParameters() {
102: return parameters != null && !parameters.isEmpty();
103: }
104:
105: /**
106: * Removes the specified parameter from Parameters of this ParametersHeader.
107: * This method returns silently if the parameter is not part of the
108: * ParametersHeader.
109: *
110: * @param name - a String specifying the parameter name
111: */
112:
113: public void removeParameter(String name) {
114: this .parameters.delete(name);
115: }
116:
117: /**
118: * Sets the value of the specified parameter. If the parameter already had
119: *
120: * a value it will be overwritten. A zero-length String indicates flag
121: *
122: * parameter.
123: *
124: *
125: *
126: * @param name - a String specifying the parameter name
127: *
128: * @param value - a String specifying the parameter value
129: *
130: * @throws ParseException which signals that an error has been reached
131: *
132: * unexpectedly while parsing the parameter name or value.
133: *
134: */
135: public void setParameter(String name, String value) {
136: NameValue nv = parameters.getNameValue(name);
137:
138: if (nv != null) {
139: nv.setValue(value);
140: } else {
141: nv = new NameValue(name, value);
142: }
143:
144: this .parameters.set(nv);
145: }
146:
147: /**
148: * Sets the value of the specified parameter. If the parameter already had
149: *
150: * a value it will be overwritten. A zero-length String indicates flag
151: *
152: * parameter.
153: *
154: *
155: *
156: * @param name - a String specifying the parameter name
157: *
158: * @param value - a String specifying the parameter value
159: *
160: * @throws ParseException which signals that an error has been reached
161: *
162: * unexpectedly while parsing the parameter name or value.
163: *
164: */
165: public void setQuotedParameter(String name, String value)
166: throws ParseException {
167: NameValue nv = parameters.getNameValue(name);
168: if (nv != null) {
169: nv.setValue(value);
170: nv.setQuotedValue();
171: } else {
172: nv = new NameValue(name, value);
173: nv.setQuotedValue();
174: this .parameters.set(nv);
175: }
176: }
177:
178: /**
179: * Sets the value of the specified parameter. If the parameter already had
180: *
181: * a value it will be overwritten.
182: *
183: *
184: * @param name - a String specifying the parameter name
185: *
186: * @param value - an int specifying the parameter value
187: *
188: * @throws ParseException which signals that an error has been reached
189: *
190: * unexpectedly while parsing the parameter name or value.
191: *
192: */
193: protected void setParameter(String name, int value) {
194: Integer val = new Integer(value);
195: NameValue nv = parameters.getNameValue(name);
196: if (nv != null) {
197: nv.setValue(val);
198: } else {
199: nv = new NameValue(name, val);
200: this .parameters.set(nv);
201: }
202: }
203:
204: /**
205: * Sets the value of the specified parameter. If the parameter already had
206: *
207: * a value it will be overwritten.
208: *
209: *
210: * @param name - a String specifying the parameter name
211: *
212: * @param value - a boolean specifying the parameter value
213: *
214: * @throws ParseException which signals that an error has been reached
215: *
216: * unexpectedly while parsing the parameter name or value.
217: *
218: */
219: protected void setParameter(String name, boolean value) {
220: Boolean val = new Boolean(value);
221: NameValue nv = parameters.getNameValue(name);
222: if (nv != null) {
223: nv.setValue(val);
224: } else {
225: nv = new NameValue(name, val);
226: this .parameters.set(nv);
227: }
228: }
229:
230: /**
231: * Sets the value of the specified parameter. If the parameter already had
232: *
233: * a value it will be overwritten. A zero-length String indicates flag
234: *
235: * parameter.
236: *
237: *
238: *
239: * @param name - a String specifying the parameter name
240: *
241: * @param value - a String specifying the parameter value
242: *
243: * @throws ParseException which signals that an error has been reached
244: *
245: * unexpectedly while parsing the parameter name or value.
246: *
247: */
248: protected void setParameter(String name, Object value) {
249: NameValue nv = parameters.getNameValue(name);
250: if (nv != null) {
251: nv.setValue(value);
252: } else {
253: nv = new NameValue(name, value);
254: this .parameters.set(nv);
255: }
256: }
257:
258: /**
259: * Returns true if has a parameter.
260: *
261: * @param parameterName is the name of the parameter.
262: *
263: * @return true if the parameter exists and false if not.
264: */
265: public boolean hasParameter(String parameterName) {
266: return this .parameters.hasNameValue(parameterName);
267: }
268:
269: /**
270: * Removes all parameters.
271: */
272: public void removeParameters() {
273: this .parameters = new NameValueList();
274: }
275:
276: /**
277: * get the parameter list.
278: * @return parameter list
279: */
280: public NameValueList getParameters() {
281: return parameters;
282: }
283:
284: /**
285: * Sets the parameter given a name and value.
286: *
287: * @param nameValue - the name value of the parameter to set.
288: */
289: public void setParameter(NameValue nameValue) {
290: this .parameters.set(nameValue);
291: }
292:
293: /**
294: * Sets the parameter list.
295: *
296: * @param parameters the name value list to set as the parameter list.
297: */
298: public void setParameters(NameValueList parameters) {
299: this .parameters = parameters;
300: }
301:
302: /**
303: * Gets the parameter as an integer value.
304: *
305: * @param parameterName -- the parameter name to fetch.
306: *
307: * @return -1 if the parameter is not defined in the header.
308: */
309: protected int getParameterAsInt(String parameterName) {
310: if (this .getParameterValue(parameterName) != null) {
311: try {
312: if (this .getParameterValue(parameterName) instanceof String) {
313: return Integer.parseInt(this
314: .getParameter(parameterName));
315: } else {
316: return ((Integer) getParameterValue(parameterName))
317: .intValue();
318: }
319: } catch (NumberFormatException ex) {
320: return -1;
321: }
322: } else
323: return -1;
324: }
325:
326: /**
327: * Gets the parameter as an integer when it is entered as a hex.
328: *
329: * @param parameterName -- The parameter name to fetch.
330: *
331: * @return -1 if the parameter is not defined in the header.
332: */
333: protected int getParameterAsHexInt(String parameterName) {
334: if (this .getParameterValue(parameterName) != null) {
335: try {
336: if (this .getParameterValue(parameterName) instanceof String) {
337: return Integer.parseInt(this
338: .getParameter(parameterName), 16);
339: } else {
340: return ((Integer) getParameterValue(parameterName))
341: .intValue();
342: }
343: } catch (NumberFormatException ex) {
344: return -1;
345: }
346: } else
347: return -1;
348: }
349:
350: /**
351: * Gets the parameter as a long value.
352: *
353: * @param parameterName -- the parameter name to fetch.
354: *
355: * @return -1 if the parameter is not defined or the parameter as a long.
356: */
357:
358: protected long getParameterAsLong(String parameterName) {
359: if (this .getParameterValue(parameterName) != null) {
360: try {
361: if (this .getParameterValue(parameterName) instanceof String) {
362: return Long.parseLong(this
363: .getParameter(parameterName));
364: } else {
365: return ((Long) getParameterValue(parameterName))
366: .longValue();
367: }
368: } catch (NumberFormatException ex) {
369: return -1;
370: }
371: } else
372: return -1;
373: }
374:
375: /**
376: * Gets the parameter value as a URI.
377: *
378: * @param parameterName -- the parameter name
379: *
380: * @return value of the parameter as a URI or null if the parameter
381: * not present.
382: */
383: protected URI getParameterAsURI(String parameterName) {
384: Object val = getParameterValue(parameterName);
385: if (val instanceof URI)
386: return (URI) val;
387: else {
388: try {
389: return new URI((String) val);
390: } catch (ParseException ex) {
391: // catch (URISyntaxException ex) {
392: return null;
393: }
394: }
395: }
396:
397: /**
398: * Gets the parameter value as a boolean.
399: *
400: * @param parameterName the parameter name
401: * @return boolean value of the parameter.
402: */
403: protected boolean getParameterAsBoolean(String parameterName) {
404: Object val = getParameterValue(parameterName);
405: if (val == null) {
406: return false;
407: } else if (val instanceof Boolean) {
408: return ((Boolean) val).booleanValue();
409: } else if (val instanceof String) {
410: return equalsIgnoreCase((String) val, "true");
411: } else
412: return false;
413: }
414:
415: /**
416: * This is for the benifit of the TCK.
417: *
418: * @param parameterName the parameter name
419: * @return the name value pair for the given parameter name.
420: */
421: public NameValue getNameValue(String parameterName) {
422: return parameters.getNameValue(parameterName);
423: }
424:
425: /**
426: * Encodes the contents as a string.
427: * @return encoded string of object contents.
428: */
429: protected abstract String encodeBody();
430:
431: /**
432: * Encodes the parameters as a string.
433: * @return encoded string of object contents.
434: */
435: protected String encodeWithSep() {
436: if (parameters == null) {
437: return "";
438: } else {
439: return parameters.encodeWithSep();
440: }
441: }
442: }
|