1 31 32 package org.apache.commons.httpclient; 33 34 import java.util.ArrayList ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 38 47 public class HeaderGroup { 48 49 50 private List headers; 51 52 55 public HeaderGroup() { 56 this.headers = new ArrayList (); 57 } 58 59 62 public void clear() { 63 headers.clear(); 64 } 65 66 72 public void addHeader(Header header) { 73 headers.add(header); 74 } 75 76 81 public void removeHeader(Header header) { 82 headers.remove(header); 83 } 84 85 92 public void setHeaders(Header[] headers) { 93 clear(); 94 95 for (int i = 0; i < headers.length; i++) { 96 addHeader(headers[i]); 97 } 98 } 99 100 111 public Header getCondensedHeader(String name) { 112 Header[] headers = getHeaders(name); 113 114 if (headers.length == 0) { 115 return null; 116 } else if (headers.length == 1) { 117 return new Header(headers[0].getName(), headers[0].getValue()); 118 } else { 119 StringBuffer valueBuffer = new StringBuffer (headers[0].getValue()); 120 121 for (int i = 1; i < headers.length; i++) { 122 valueBuffer.append(", "); 123 valueBuffer.append(headers[i].getValue()); 124 } 125 126 return new Header(name.toLowerCase(), valueBuffer.toString()); 127 } 128 } 129 130 140 public Header[] getHeaders(String name) { 141 ArrayList headersFound = new ArrayList (); 142 143 for (Iterator headerIter = headers.iterator(); headerIter.hasNext();) { 144 Header header = (Header) headerIter.next(); 145 if (header.getName().equalsIgnoreCase(name)) { 146 headersFound.add(header); 147 } 148 } 149 150 return (Header[]) headersFound.toArray(new Header[headersFound.size()]); 151 } 152 153 161 public Header getFirstHeader(String name) { 162 for (Iterator headerIter = headers.iterator(); headerIter.hasNext();) { 163 Header header = (Header) headerIter.next(); 164 if (header.getName().equalsIgnoreCase(name)) { 165 return header; 166 } 167 } 168 169 return null; 170 } 171 172 180 public Header getLastHeader(String name) { 181 for (int i = headers.size() - 1; i >= 0; i--) { 183 Header header = (Header) headers.get(i); 184 if (header.getName().equalsIgnoreCase(name)) { 185 return header; 186 } 187 } 188 189 return null; 190 } 191 192 197 public Header[] getAllHeaders() { 198 return (Header[]) headers.toArray(new Header[headers.size()]); 199 } 200 201 210 public boolean containsHeader(String name) { 211 for (Iterator headerIter = headers.iterator(); headerIter.hasNext();) { 212 Header header = (Header) headerIter.next(); 213 if (header.getName().equalsIgnoreCase(name)) { 214 return true; 215 } 216 } 217 218 return false; 219 } 220 221 } 222 | Popular Tags |