/** * Remove all header entries with the supplied name * * @param name * the header to remove */ public void removeHeader(String name) { List list = getHeaderList(name); // it's possible we might not have the named header. This is a nop if the header doesn't exist. if (list != null) { list.clear(); } }
/** * Add a new value to the header with the supplied name. * * @param name * the name of the header to add a new value for * @param value * another value */ public void addHeader(String name, String value) { List list = getHeaderList(name); if (list == null) { list = new ArrayList(); headers.put(name.toLowerCase(), list); } list.add(new InternetHeader(name, value)); }
/** * Return all the values for the specified header. * * @param name * the header to return * @return the values for that header, or null if the header is not present */ public String[] getHeader(String name) { List headers = getHeaderList(name); if (headers == null) { return null; } else { String[] result = new String[headers.size()]; for (int i = 0; i < headers.size(); i++) { InternetHeader header = (InternetHeader) headers.get(i); result[i] = header.getValue(); } return result; } }
/** * Return the values for the specified header as a single String. If the * header has more than one value then all values are concatenated together * separated by the supplied delimiter. * * @param name * the header to return * @param delimiter * the delimiter used in concatenation * @return the header as a single String */ public String getHeader(String name, String delimiter) { List list = getHeaderList(name); if (list == null) { return null; } else if (list.isEmpty()) { return ""; } else if (list.size() == 1 || delimiter == null) { return ((InternetHeader) list.get(0)).getValue(); } else { StringBuffer buf = new StringBuffer(20 * list.size()); buf.append(((InternetHeader) list.get(0)).getValue()); for (int i = 1; i < list.size(); i++) { buf.append(delimiter); buf.append(((InternetHeader) list.get(i)).getValue()); } return buf.toString(); } }
List list = getHeaderList(lastHeaderName); Header h = (Header) list.remove(list.size() - 1); list.add(new InternetHeader(lastHeaderName, (h.getValue() + value.toString()).trim()));