/*
* $Header: /usr/local/cvsroot/3rdParty_sources/commons-httpclient/org/apache/commons/httpclient/HeaderGroup.java,v 1.1.2.1 2016/04/04 12:40:24 marcin Exp $
* $Revision: 1.1.2.1 $
* $Date: 2016/04/04 12:40:24 $
*
* ====================================================================
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
*
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(String name) {
Header[] headers = getHeaders(name);
if (headers.length == 0) {
return null;
} else if (headers.length == 1) {
return new Header(headers[0].getName(), headers[0].getValue());
} else {
StringBuffer valueBuffer = new StringBuffer(headers[0].getValue());
for (int i = 1; i < headers.length; i++) {
valueBuffer.append(", ");
valueBuffer.append(headers[i].getValue());
}
return new Header(name.toLowerCase(), 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(String name) { ArrayList headersFound = new ArrayList(); for (Iterator headerIter = headers.iterator(); headerIter.hasNext();) { Header header = (Header) headerIter.next(); if (header.getName().equalsIgnoreCase(name)) { headersFound.add(header); } } return (Header[]) headersFound.toArray(new Header[headersFound.size()]); } /** * Gets the first header with the given name. * *
Header name comparison is case insensitive.
*
* @param name the name of the header to get
* @return the first header or null
*/
public Header getFirstHeader(String name) {
for (Iterator headerIter = headers.iterator(); headerIter.hasNext();) {
Header header = (Header) headerIter.next();
if (header.getName().equalsIgnoreCase(name)) {
return header;
}
}
return null;
}
/**
* Gets the last header with the given name.
*
*
Header name comparison is case insensitive.
*
* @param name the name of the header to get
* @return the last header or null
*/
public Header getLastHeader(String name) {
// start at the end of the list and work backwards
for (int i = headers.size() - 1; i >= 0; i--) {
Header 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 (Header[]) headers.toArray(new Header[headers.size()]);
}
/**
* Tests if headers with the given name are contained within this group.
*
*
Header name comparison is case insensitive.
*
* @param name the header name to test for
* @return true
if at least one header with the name is
* contained, false
otherwise
*/
public boolean containsHeader(String name) {
for (Iterator headerIter = headers.iterator(); headerIter.hasNext();) {
Header header = (Header) headerIter.next();
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 3.0
*/
public Iterator getIterator() {
return this.headers.iterator();
}
}