Template for HTTP Headers. Variables that are unresolved are ignored and Literals are not encoded.
| 34 | * encoded. |
| 35 | */ |
| 36 | public final class HeaderTemplate { |
| 37 | |
| 38 | private final String name; |
| 39 | private final List<Template> values = new CopyOnWriteArrayList<>(); |
| 40 | |
| 41 | public static HeaderTemplate create(String name, Iterable<String> values) { |
| 42 | if (name == null || name.isEmpty()) { |
| 43 | throw new IllegalArgumentException("name is required."); |
| 44 | } |
| 45 | |
| 46 | if (values == null) { |
| 47 | throw new IllegalArgumentException("values are required"); |
| 48 | } |
| 49 | |
| 50 | return new HeaderTemplate(name, values, Util.UTF_8); |
| 51 | } |
| 52 | |
| 53 | public static HeaderTemplate literal(String name, Iterable<String> values) { |
| 54 | if (name == null || name.isEmpty()) { |
| 55 | throw new IllegalArgumentException("name is required."); |
| 56 | } |
| 57 | |
| 58 | if (values == null) { |
| 59 | throw new IllegalArgumentException("values are required"); |
| 60 | } |
| 61 | |
| 62 | return new HeaderTemplate(name, values, Util.UTF_8, true); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Append values to a Header Template. |
| 67 | * |
| 68 | * @param headerTemplate to append to. |
| 69 | * @param values to append. |
| 70 | * @return a new Header Template with the values added. |
| 71 | */ |
| 72 | public static HeaderTemplate append(HeaderTemplate headerTemplate, Iterable<String> values) { |
| 73 | LinkedHashSet<String> headerValues = new LinkedHashSet<>(headerTemplate.getValues()); |
| 74 | headerValues.addAll( |
| 75 | StreamSupport.stream(values.spliterator(), false) |
| 76 | .filter(Util::isNotBlank) |
| 77 | .collect(Collectors.toCollection(LinkedHashSet::new))); |
| 78 | return create(headerTemplate.getName(), headerValues); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Append values to a Header Template, as literals |
| 83 | * |
| 84 | * @param headerTemplate to append to. |
| 85 | * @param values to append. |
| 86 | * @return a new Header Template with the values added. |
| 87 | */ |
| 88 | public static HeaderTemplate appendLiteral( |
| 89 | HeaderTemplate headerTemplate, Iterable<String> values) { |
| 90 | LinkedHashSet<String> headerValues = new LinkedHashSet<>(headerTemplate.getValues()); |
| 91 | headerValues.addAll( |
| 92 | StreamSupport.stream(values.spliterator(), false) |
| 93 | .filter(Util::isNotBlank) |
nothing calls this directly
no outgoing calls
no test coverage detected