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.message;
028:
029: import gov.nist.siplite.header.*;
030: import java.util.Hashtable;
031:
032: /**
033: * A map of which of the standard headers may appear as a list
034: */
035:
036: class ListMap {
037: /**
038: * A table that indicates whether a header has a list representation or
039: * not (to catch adding of the non-list form when a list exists.)
040: * Entries in this table allow you to look up the list form of a header
041: * (provided it has a list form).
042: */
043: private static Hashtable headerListTable;
044:
045: /** Static initializer */
046: static {
047: initializeListMap();
048: }
049:
050: /**
051: * Builds a table mapping between objects that have a list form
052: * and the class of such objects.
053: */
054: static private void initializeListMap() {
055: headerListTable = new Hashtable();
056:
057: headerListTable.put(ExtensionHeader.clazz, new HeaderList()
058: .getClass());
059:
060: headerListTable.put(ParameterLessHeader.clazz, new HeaderList()
061: .getClass());
062:
063: headerListTable.put(ContactHeader.clazz, new ContactList()
064: .getClass());
065:
066: headerListTable.put(ViaHeader.clazz, new ViaList().getClass());
067:
068: headerListTable.put(WWWAuthenticateHeader.clazz,
069: new WWWAuthenticateList().getClass());
070:
071: headerListTable.put(RouteHeader.clazz, new RouteList()
072: .getClass());
073:
074: headerListTable.put(ProxyAuthenticateHeader.clazz,
075: new ProxyAuthenticateList().getClass());
076:
077: headerListTable.put(ProxyAuthorizationHeader.clazz,
078: new HeaderList().getClass());
079:
080: headerListTable.put(RecordRouteHeader.clazz,
081: new RecordRouteList().getClass());
082: }
083:
084: /**
085: * Returns true if this has an associated list object.
086: * @param sipHeader the requested header to be checked
087: * @return true if list is present
088: */
089: static protected boolean hasList(Header sipHeader) {
090: if (sipHeader instanceof HeaderList)
091: return false;
092: else {
093: Class headerClass = sipHeader.getClass();
094: return headerListTable.get(headerClass) != null;
095: }
096: }
097:
098: /**
099: * Returns true if this has an associated list object.
100: * @param sipHdrClass the class to be checked
101: * @return true if listis present
102: */
103: static protected boolean hasList(Class sipHdrClass) {
104: return headerListTable.get(sipHdrClass) != null;
105: }
106:
107: /**
108: * Gets the associated list class.
109: * @param sipHdrClass the class to be checked
110: * @return the list class
111: */
112: static protected Class getListClass(Class sipHdrClass) {
113: return (Class) headerListTable.get(sipHdrClass);
114: }
115:
116: /**
117: * Returns a list object for this header if it has an associated
118: * list object.
119: * @param sipHeader the requested header with associated list
120: * @return list object
121: */
122: static protected HeaderList getList(Header sipHeader) {
123: try {
124: Class headerClass = sipHeader.getClass();
125: Class listClass = (Class) headerListTable.get(headerClass);
126: HeaderList shl = (HeaderList) listClass.newInstance();
127: shl.setHeaderName(sipHeader.getHeaderName());
128: return shl;
129: } catch (InstantiationException ex) {
130: ex.printStackTrace();
131: } catch (IllegalAccessException ex) {
132: ex.printStackTrace();
133: }
134: return null;
135: }
136: }
|