001: /*
002: * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package javax.xml.soap;
031:
032: import java.util.*;
033:
034: public class MimeHeaders {
035: private final HashMap<String, ArrayList<MimeHeader>> _headers = new HashMap<String, ArrayList<MimeHeader>>();
036:
037: public MimeHeaders() {
038: }
039:
040: public void addHeader(String name, String value) {
041: if (name == null || "".equals(name))
042: throw new IllegalArgumentException();
043:
044: ArrayList<MimeHeader> list = _headers.get(name);
045:
046: if (list == null) {
047: list = new ArrayList<MimeHeader>();
048: _headers.put(name, list);
049: }
050:
051: list.add(new MimeHeader(name, value));
052: }
053:
054: public Iterator getAllHeaders() {
055: return new BlacklistHeaderIterator();
056: }
057:
058: public String[] getHeader(String name) {
059: ArrayList<MimeHeader> list = _headers.get(name);
060:
061: if (list == null)
062: return new String[0];
063:
064: String[] values = new String[list.size()];
065:
066: for (int i = 0; i < list.size(); i++)
067: values[i] = list.get(i).getValue();
068:
069: return values;
070: }
071:
072: public Iterator getMatchingHeaders(String[] names) {
073: return new MatchingHeaderIterator(names);
074: }
075:
076: public Iterator getNonMatchingHeaders(String[] names) {
077: return new BlacklistHeaderIterator(names);
078: }
079:
080: public void removeAllHeaders() {
081: _headers.clear();
082: }
083:
084: public void removeHeader(String name) {
085: _headers.remove(name);
086: }
087:
088: public void setHeader(String name, String value) {
089: if (name == null || "".equals(name))
090: throw new IllegalArgumentException();
091:
092: ArrayList<MimeHeader> list = _headers.get(name);
093:
094: if (list == null) {
095: list = new ArrayList<MimeHeader>();
096: _headers.put(name, list);
097: }
098:
099: if (list.size() > 0)
100: list.set(0, new MimeHeader(name, value));
101: else
102: list.add(new MimeHeader(name, value));
103: }
104:
105: private class BlacklistHeaderIterator implements Iterator {
106: private String[] _blackList;
107: private Iterator<Map.Entry<String, ArrayList<MimeHeader>>> _topIterator;
108: private Iterator<MimeHeader> _bottomIterator;
109:
110: public BlacklistHeaderIterator() {
111: this (null);
112: }
113:
114: public BlacklistHeaderIterator(String[] blackList) {
115: _blackList = blackList;
116: _topIterator = _headers.entrySet().iterator();
117: }
118:
119: private void prepareIterator() {
120: if (_bottomIterator == null || !_bottomIterator.hasNext()) {
121: _bottomIterator = null;
122:
123: while (_topIterator.hasNext()) {
124: Map.Entry<String, ArrayList<MimeHeader>> entry = _topIterator
125: .next();
126:
127: boolean ok = true;
128:
129: if (_blackList != null) {
130: for (int i = 0; i < _blackList.length; i++) {
131: if (entry.getKey().equals(_blackList[i]))
132: ok = false;
133: }
134: }
135:
136: if (ok) {
137: _bottomIterator = entry.getValue().iterator();
138: break;
139: }
140: }
141: }
142: }
143:
144: public Object next() {
145: prepareIterator();
146:
147: if (_bottomIterator == null)
148: throw new NoSuchElementException();
149:
150: return _bottomIterator.next();
151: }
152:
153: public boolean hasNext() {
154: prepareIterator();
155:
156: if (_bottomIterator == null)
157: return false;
158:
159: return _bottomIterator.hasNext();
160: }
161:
162: public void remove() {
163: throw new UnsupportedOperationException();
164: }
165: }
166:
167: private class MatchingHeaderIterator implements Iterator {
168: private int _name = -1;
169: private String[] _names;
170: private Iterator<MimeHeader> _iterator;
171:
172: public MatchingHeaderIterator(String[] names) {
173: _names = names;
174: }
175:
176: private void prepareIterator() {
177: if (_iterator == null || !_iterator.hasNext()) {
178: _iterator = null;
179:
180: for (_name++; _name < _names.length; _name++) {
181: ArrayList<MimeHeader> list = _headers
182: .get(_names[_name]);
183:
184: if (list != null) {
185: _iterator = list.iterator();
186:
187: if (_iterator.hasNext())
188: break;
189:
190: _iterator = null;
191: }
192: }
193: }
194: }
195:
196: public Object next() {
197: prepareIterator();
198:
199: if (_iterator == null)
200: throw new NoSuchElementException();
201:
202: return _iterator.next();
203: }
204:
205: public boolean hasNext() {
206: prepareIterator();
207:
208: if (_iterator == null)
209: return false;
210:
211: return _iterator.hasNext();
212: }
213:
214: public void remove() {
215: throw new UnsupportedOperationException();
216: }
217: }
218: }
|