The Headers class encapsulates a collection of HTTP headers. Header names are treated case-insensitively, although this class retains their original case. Header insertion order is maintained as well.
| 1146 | * their original case. Header insertion order is maintained as well. |
| 1147 | */ |
| 1148 | public static class Headers implements Iterable<Header> { |
| 1149 | |
| 1150 | // due to the requirements of case-insensitive name comparisons, |
| 1151 | // retaining the original case, and retaining header insertion order, |
| 1152 | // and due to the fact that the number of headers is generally |
| 1153 | // quite small (usually under 12 headers), we use a simple array with |
| 1154 | // linear access times, which proves to be more efficient and |
| 1155 | // straightforward than the alternatives |
| 1156 | protected Header[] headers = new Header[12]; |
| 1157 | protected int count; |
| 1158 | |
| 1159 | /** |
| 1160 | * Returns the number of added headers. |
| 1161 | * |
| 1162 | * @return the number of added headers |
| 1163 | */ |
| 1164 | public int size() { |
| 1165 | return count; |
| 1166 | } |
| 1167 | |
| 1168 | /** |
| 1169 | * Returns the value of the first header with the given name. |
| 1170 | * |
| 1171 | * @param name the header name (case insensitive) |
| 1172 | * @return the header value, or null if none exists |
| 1173 | */ |
| 1174 | public String get(String name) { |
| 1175 | for (int i = 0; i < count; i++) |
| 1176 | if (headers[i].getName().equalsIgnoreCase(name)) |
| 1177 | return headers[i].getValue(); |
| 1178 | return null; |
| 1179 | } |
| 1180 | |
| 1181 | /** |
| 1182 | * Returns the Date value of the header with the given name. |
| 1183 | * |
| 1184 | * @param name the header name (case insensitive) |
| 1185 | * @return the header value as a Date, or null if none exists |
| 1186 | * or if the value is not in any supported date format |
| 1187 | */ |
| 1188 | public Date getDate(String name) { |
| 1189 | try { |
| 1190 | String header = get(name); |
| 1191 | return header == null ? null : parseDate(header); |
| 1192 | } catch (IllegalArgumentException iae) { |
| 1193 | return null; |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | /** |
| 1198 | * Returns whether there exists a header with the given name. |
| 1199 | * |
| 1200 | * @param name the header name (case insensitive) |
| 1201 | * @return whether there exists a header with the given name |
| 1202 | */ |
| 1203 | public boolean contains(String name) { |
| 1204 | return get(name) != null; |
| 1205 | } |
nothing calls this directly
no outgoing calls
no test coverage detected