Header name comparison is case insensitive.
*
* @param name the name of the header(s) to get
* @return a header with a condensed value or null
if no
* headers by the given name are present
*/
public Header getCondensedHeader(final String name) {
final Header[] hdrs = getHeaders(name);
if (hdrs.length == 0) {
return null;
} else if (hdrs.length == 1) {
return hdrs[0];
} else {
final CharArrayBuffer valueBuffer = new CharArrayBuffer(128);
valueBuffer.append(hdrs[0].getValue());
for (int i = 1; i < hdrs.length; i++) {
valueBuffer.append(", ");
valueBuffer.append(hdrs[i].getValue());
}
return new BasicHeader(name.toLowerCase(Locale.ENGLISH), valueBuffer.toString());
}
}
/**
* Gets all of the headers with the given name. The returned array
* maintains the relative order in which the headers were added.
*
*
Header name comparison is case insensitive.
*
* @param name the name of the header(s) to get
*
* @return an array of length >= 0
*/
public Header[] getHeaders(final String name) {
final List Header name comparison is case insensitive.
*
* @param name the name of the header to get
* @return the first header or Header name comparison is case insensitive.
*
* @param name the name of the header to get
* @return the last header or Header name comparison is case insensitive.
*
* @param name the header name to test for
* @return null
*/
public Header getFirstHeader(final String name) {
// HTTPCORE-361 : we don't use the for-each syntax, i.e.
// for (Header header : headers)
// as that creates an Iterator that needs to be garbage-collected
for (int i = 0; i < this.headers.size(); i++) {
final Header header = this.headers.get(i);
if (header.getName().equalsIgnoreCase(name)) {
return header;
}
}
return null;
}
/**
* Gets the last header with the given name.
*
* null
*/
public Header getLastHeader(final String name) {
// start at the end of the list and work backwards
for (int i = headers.size() - 1; i >= 0; i--) {
final Header header = headers.get(i);
if (header.getName().equalsIgnoreCase(name)) {
return header;
}
}
return null;
}
/**
* Gets all of the headers contained within this group.
*
* @return an array of length >= 0
*/
public Header[] getAllHeaders() {
return headers.toArray(new Header[headers.size()]);
}
/**
* Tests if headers with the given name are contained within this group.
*
* true
if at least one header with the name is
* contained, false
otherwise
*/
public boolean containsHeader(final String name) {
// HTTPCORE-361 : we don't use the for-each syntax, i.e.
// for (Header header : headers)
// as that creates an Iterator that needs to be garbage-collected
for (int i = 0; i < this.headers.size(); i++) {
final Header header = this.headers.get(i);
if (header.getName().equalsIgnoreCase(name)) {
return true;
}
}
return false;
}
/**
* Returns an iterator over this group of headers.
*
* @return iterator over this group of headers.
*
* @since 4.0
*/
public HeaderIterator iterator() {
return new BasicListHeaderIterator(this.headers, null);
}
/**
* Returns an iterator over the headers with a given name in this group.
*
* @param name the name of the headers over which to iterate, or
* null
for all headers
*
* @return iterator over some headers in this group.
*
* @since 4.0
*/
public HeaderIterator iterator(final String name) {
return new BasicListHeaderIterator(this.headers, name);
}
/**
* Returns a copy of this object
*
* @return copy of this object
*
* @since 4.0
*/
public HeaderGroup copy() {
final HeaderGroup clone = new HeaderGroup();
clone.headers.addAll(this.headers);
return clone;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public String toString() {
return this.headers.toString();
}
}