001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.transport;
038:
039: import java.util.Collection;
040: import java.util.HashMap;
041: import java.util.LinkedList;
042: import java.util.List;
043: import java.util.Map;
044: import java.util.Set;
045: import java.io.*;
046:
047: /**
048: * HTTP request and response headers are represented by this class which implements
049: * the interface {@link java.util.Map}<
050: * {@link java.lang.String},{@link java.util.List}<{@link java.lang.String}>>.
051: * The keys are case-insensitive Strings representing the header names and
052: * the value associated with each key is a {@link List}<{@link String}> with one
053: * element for each occurence of the header name in the request or response.
054: * <p>
055: * For example, if a response header instance contains one key "HeaderName" with two values "value1 and value2"
056: * then this object is output as two header lines:
057: * <blockquote><pre>
058: * HeaderName: value1
059: * HeaderName: value2
060: * </blockquote></pre>
061: * <p>
062: * All the normal {@link java.util.Map} methods are provided, but the following
063: * additional convenience methods are most likely to be used:
064: * <ul>
065: * <li>{@link #getFirst(String)} returns a single valued header or the first value of
066: * a multi-valued header.</li>
067: * <li>{@link #add(String,String)} adds the given header value to the list for the given key</li>
068: * <li>{@link #set(String,String)} sets the given header field to the single value given
069: * overwriting any existing values in the value list.
070: * </ul><p>
071: * All methods in this class accept <code>null</code> values for keys and values. However, null
072: * keys will never will be present in HTTP request headers, and will not be output/sent in response headers.
073: * Null values can be represented as either a null entry for the key (i.e. the list is null) or
074: * where the key has a list, but one (or more) of the list's values is null. Null values are output
075: * as a header line containing the key but no associated value.
076: * @since 1.6
077: */
078: public class Headers implements Map<String, List<String>> {
079:
080: HashMap<String, List<String>> map;
081:
082: public Headers() {
083: map = new HashMap<String, List<String>>(32);
084: }
085:
086: /* Normalize the key by converting to following form.
087: * First char upper case, rest lower case.
088: * key is presumed to be ASCII
089: */
090: private String normalize(String key) {
091: if (key == null) {
092: return null;
093: }
094: int len = key.length();
095: if (len == 0) {
096: return key;
097: }
098: char[] b = new char[len];
099: String s = null;
100: b = key.toCharArray();
101: if (b[0] >= 'a' && b[0] <= 'z') {
102: b[0] = (char) (b[0] - ('a' - 'A'));
103: }
104: for (int i = 1; i < len; i++) {
105: if (b[i] >= 'A' && b[i] <= 'Z') {
106: b[i] = (char) (b[i] + ('a' - 'A'));
107: }
108: }
109: s = new String(b);
110: return s;
111: }
112:
113: public int size() {
114: return map.size();
115: }
116:
117: public boolean isEmpty() {
118: return map.isEmpty();
119: }
120:
121: public boolean containsKey(Object key) {
122: if (key == null) {
123: return false;
124: }
125: if (!(key instanceof String)) {
126: return false;
127: }
128: return map.containsKey(normalize((String) key));
129: }
130:
131: public boolean containsValue(Object value) {
132: return map.containsValue(value);
133: }
134:
135: public List<String> get(Object key) {
136: return map.get(normalize((String) key));
137: }
138:
139: /**
140: * returns the first value from the List of String values
141: * for the given key (if at least one exists).
142: * @param key the key to search for
143: * @return the first string value associated with the key
144: */
145: public String getFirst(String key) {
146: List<String> l = map.get(normalize((String) key));
147: if (l == null) {
148: return null;
149: }
150: return l.get(0);
151: }
152:
153: public List<String> put(String key, List<String> value) {
154: return map.put(normalize(key), value);
155: }
156:
157: /**
158: * adds the given value to the list of headers
159: * for the given key. If the mapping does not
160: * already exist, then it is created
161: * @param key the header name
162: * @param value the header value to add to the header
163: */
164: public void add(String key, String value) {
165: String k = normalize(key);
166: List<String> l = map.get(k);
167: if (l == null) {
168: l = new LinkedList<String>();
169: map.put(k, l);
170: }
171: l.add(value);
172: }
173:
174: /**
175: * sets the given value as the sole header value
176: * for the given key. If the mapping does not
177: * already exist, then it is created
178: * @param key the header name
179: * @param value the header value to set.
180: */
181: public void set(String key, String value) {
182: LinkedList<String> l = new LinkedList<String>();
183: l.add(value);
184: put(key, l);
185: }
186:
187: public List<String> remove(Object key) {
188: return map.remove(normalize((String) key));
189: }
190:
191: public void putAll(Map<? extends String, ? extends List<String>> t) {
192: for (Map.Entry<? extends String, ? extends List<String>> entry : t
193: .entrySet()) {
194: put(entry.getKey(), entry.getValue());
195: }
196: }
197:
198: public void clear() {
199: map.clear();
200: }
201:
202: public Set<String> keySet() {
203: return map.keySet();
204: }
205:
206: public Collection<List<String>> values() {
207: return map.values();
208: }
209:
210: public Set<Map.Entry<String, List<String>>> entrySet() {
211: return map.entrySet();
212: }
213:
214: public boolean equals(Object o) {
215: return map.equals(o);
216: }
217:
218: public int hashCode() {
219: return map.hashCode();
220: }
221:
222: @Override
223: public String toString() {
224: return map.toString();
225: }
226: }
|