1. /*
  2. * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HeaderGroup.java,v 1.8 2004/05/13 04:03:25 mbecke Exp $
  3. * $Revision: 1.8 $
  4. * $Date: 2004/05/13 04:03:25 $
  5. *
  6. * ====================================================================
  7. *
  8. * Copyright 2003-2004 The Apache Software Foundation
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * ====================================================================
  22. *
  23. * This software consists of voluntary contributions made by many
  24. * individuals on behalf of the Apache Software Foundation. For more
  25. * information on the Apache Software Foundation, please see
  26. * <http://www.apache.org/>.
  27. *
  28. */
  29. package org.apache.commons.httpclient;
  30. import java.util.ArrayList;
  31. import java.util.Iterator;
  32. import java.util.List;
  33. /**
  34. * A class for combining a set of headers. This class allows for multiple
  35. * headers with the same name and keeps track of the order in which headers were
  36. * added.
  37. *
  38. * @author Michael Becke
  39. *
  40. * @since 2.0beta1
  41. */
  42. public class HeaderGroup {
  43. /** The list of headers for this group, in the order in which they were added */
  44. private List headers;
  45. /**
  46. * Constructor for HeaderGroup.
  47. */
  48. public HeaderGroup() {
  49. this.headers = new ArrayList();
  50. }
  51. /**
  52. * Removes any contained headers.
  53. */
  54. public void clear() {
  55. headers.clear();
  56. }
  57. /**
  58. * Adds the given header to the group. The order in which this header was
  59. * added is preserved.
  60. *
  61. * @param header the header to add
  62. */
  63. public void addHeader(Header header) {
  64. headers.add(header);
  65. }
  66. /**
  67. * Removes the given header.
  68. *
  69. * @param header the header to remove
  70. */
  71. public void removeHeader(Header header) {
  72. headers.remove(header);
  73. }
  74. /**
  75. * Sets all of the headers contained within this group overriding any
  76. * existing headers. The headers are added in the order in which they appear
  77. * in the array.
  78. *
  79. * @param headers the headers to set
  80. */
  81. public void setHeaders(Header[] headers) {
  82. clear();
  83. for (int i = 0; i < headers.length; i++) {
  84. addHeader(headers[i]);
  85. }
  86. }
  87. /**
  88. * Gets a header representing all of the header values with the given name.
  89. * If more that one header with the given name exists the values will be
  90. * combined with a "," as per RFC 2616.
  91. *
  92. * <p>Header name comparison is case insensitive.
  93. *
  94. * @param name the name of the header(s) to get
  95. * @return a header with a condensed value or <code>null</code> if no
  96. * headers by the given name are present
  97. */
  98. public Header getCondensedHeader(String name) {
  99. Header[] headers = getHeaders(name);
  100. if (headers.length == 0) {
  101. return null;
  102. } else if (headers.length == 1) {
  103. return new Header(headers[0].getName(), headers[0].getValue());
  104. } else {
  105. StringBuffer valueBuffer = new StringBuffer(headers[0].getValue());
  106. for (int i = 1; i < headers.length; i++) {
  107. valueBuffer.append(", ");
  108. valueBuffer.append(headers[i].getValue());
  109. }
  110. return new Header(name.toLowerCase(), valueBuffer.toString());
  111. }
  112. }
  113. /**
  114. * Gets all of the headers with the given name. The returned array
  115. * maintains the relative order in which the headers were added.
  116. *
  117. * <p>Header name comparison is case insensitive.
  118. *
  119. * @param name the name of the header(s) to get
  120. *
  121. * @return an array of length >= 0
  122. */
  123. public Header[] getHeaders(String name) {
  124. ArrayList headersFound = new ArrayList();
  125. for (Iterator headerIter = headers.iterator(); headerIter.hasNext();) {
  126. Header header = (Header) headerIter.next();
  127. if (header.getName().equalsIgnoreCase(name)) {
  128. headersFound.add(header);
  129. }
  130. }
  131. return (Header[]) headersFound.toArray(new Header[headersFound.size()]);
  132. }
  133. /**
  134. * Gets the first header with the given name.
  135. *
  136. * <p>Header name comparison is case insensitive.
  137. *
  138. * @param name the name of the header to get
  139. * @return the first header or <code>null</code>
  140. */
  141. public Header getFirstHeader(String name) {
  142. for (Iterator headerIter = headers.iterator(); headerIter.hasNext();) {
  143. Header header = (Header) headerIter.next();
  144. if (header.getName().equalsIgnoreCase(name)) {
  145. return header;
  146. }
  147. }
  148. return null;
  149. }
  150. /**
  151. * Gets the last header with the given name.
  152. *
  153. * <p>Header name comparison is case insensitive.
  154. *
  155. * @param name the name of the header to get
  156. * @return the last header or <code>null</code>
  157. */
  158. public Header getLastHeader(String name) {
  159. // start at the end of the list and work backwards
  160. for (int i = headers.size() - 1; i >= 0; i--) {
  161. Header header = (Header) headers.get(i);
  162. if (header.getName().equalsIgnoreCase(name)) {
  163. return header;
  164. }
  165. }
  166. return null;
  167. }
  168. /**
  169. * Gets all of the headers contained within this group.
  170. *
  171. * @return an array of length >= 0
  172. */
  173. public Header[] getAllHeaders() {
  174. return (Header[]) headers.toArray(new Header[headers.size()]);
  175. }
  176. /**
  177. * Tests if headers with the given name are contained within this group.
  178. *
  179. * <p>Header name comparison is case insensitive.
  180. *
  181. * @param name the header name to test for
  182. * @return <code>true</code> if at least one header with the name is
  183. * contained, <code>false</code> otherwise
  184. */
  185. public boolean containsHeader(String name) {
  186. for (Iterator headerIter = headers.iterator(); headerIter.hasNext();) {
  187. Header header = (Header) headerIter.next();
  188. if (header.getName().equalsIgnoreCase(name)) {
  189. return true;
  190. }
  191. }
  192. return false;
  193. }
  194. /**
  195. * Returns an iterator over this group of headers.
  196. *
  197. * @return iterator over this group of headers.
  198. *
  199. * @since 3.0
  200. */
  201. public Iterator getIterator() {
  202. return this.headers.iterator();
  203. }
  204. }