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: package gov.nist.siplite.header;
026:
027: import java.util.Hashtable;
028:
029: /**
030: * A mapping class that returns the Header for a given header name.
031: */
032: public class NameMap {
033: /** Contents of the name mapping table. */
034: static Hashtable nameMap;
035:
036: static {
037: initializeNameMap();
038: }
039:
040: /**
041: * Adds an entry to the name map table.
042: * @param headerName the header field label key
043: * @param clazz the class that handles the processing of the named header
044: */
045: protected static void putNameMap(String headerName, Class clazz) {
046: nameMap.put(headerName.toLowerCase(), clazz);
047: }
048:
049: /**
050: * Checks if the parameter is a short form of some header's name
051: * and if this is true, expands it to the full form.
052: * @param headerName the name to expand
053: * @return the expanded name or the source name if it was not expanded
054: */
055: public static String expandHeaderName(String headerName) {
056: /*
057: * Support for short forms of header names.
058: *
059: * i call-id; m contact; e content-encoding; l content-length;
060: * c content-type; f from; s subject; k supported; t to; v via;
061: * o event; r refer-to
062: */
063: String shortNames = "imelcfsktvoura";
064: String[] fullNames = { Header.CALL_ID, Header.CONTACT,
065: Header.CONTENT_ENCODING, Header.CONTENT_LENGTH,
066: Header.CONTENT_TYPE, Header.FROM, Header.SUBJECT,
067: Header.SUPPORTED, Header.TO, Header.VIA, Header.EVENT,
068: Header.ALLOW_EVENTS, Header.REFER_TO,
069: Header.ACCEPT_CONTACT };
070: String name = headerName;
071:
072: if (name.length() == 1) {
073: int idx = shortNames.indexOf(name.toLowerCase().charAt(0));
074: if (idx != -1) {
075: name = fullNames[idx];
076: }
077: }
078:
079: return name;
080: }
081:
082: /**
083: * Looks up the class to use from the header field label.
084: * @param headerName the key for looking up class handler
085: * @return the class handler for requested header field
086: */
087: public static Class getClassFromName(String headerName) {
088: return (Class) nameMap.get(headerName.toLowerCase());
089: }
090:
091: /**
092: * Checks if requested header is supported.
093: * @param headerName the key for looking up class handler
094: * @return true if the header has a class handler
095: */
096: public static boolean isHeaderSupported(String headerName) {
097: return nameMap.containsKey(headerName);
098: }
099:
100: /**
101: * Initializes the header to class mapping table.
102: */
103: private static void initializeNameMap() {
104: nameMap = new Hashtable();
105:
106: putNameMap(Header.CSEQ, CSeqHeader.clazz); // 1
107:
108: putNameMap(Header.RECORD_ROUTE, RecordRouteHeader.clazz); // 2
109:
110: putNameMap(Header.VIA, ViaHeader.clazz); // 3
111:
112: putNameMap(Header.FROM, FromHeader.clazz); // 4
113:
114: putNameMap(Header.CALL_ID, CallIdHeader.clazz); // 5
115:
116: putNameMap(Header.MAX_FORWARDS, MaxForwardsHeader.clazz); // 6
117:
118: putNameMap(Header.PROXY_AUTHENTICATE,
119: ProxyAuthenticateHeader.clazz); // 7
120:
121: putNameMap(Header.CONTENT_TYPE, ContentTypeHeader.clazz); // 8
122:
123: putNameMap(Header.CONTENT_LENGTH, ContentLengthHeader.clazz); // 9
124:
125: putNameMap(Header.ROUTE, RouteHeader.clazz); // 10
126:
127: putNameMap(Header.CONTACT, ContactHeader.clazz); // 11
128:
129: putNameMap(Header.WWW_AUTHENTICATE, WWWAuthenticateHeader.clazz); // 12
130:
131: putNameMap(Header.PROXY_AUTHORIZATION,
132: ProxyAuthorizationHeader.clazz); // 13
133:
134: putNameMap(Header.DATE, DateHeader.clazz); // 14
135:
136: putNameMap(Header.EXPIRES, ExpiresHeader.clazz); // 15
137:
138: putNameMap(Header.AUTHORIZATION, AuthorizationHeader.clazz); // 16
139:
140: putNameMap(Header.TO, ToHeader.clazz); // 17 -- mg
141:
142: putNameMap(Header.EVENT, EventHeader.clazz); // 18
143:
144: putNameMap(Header.SUBSCRIPTION_STATE,
145: SubscriptionStateHeader.clazz);
146:
147: putNameMap(Header.RSEQ, RSeqHeader.clazz); // 20
148:
149: putNameMap(Header.RACK, RAckHeader.clazz); // 21
150:
151: putNameMap(Header.ACCEPT_CONTACT, AcceptContactHeader.clazz); // 22
152: }
153:
154: }
|