| 41 | import org.openqa.selenium.io.Read; |
| 42 | |
| 43 | abstract class HttpMessage<M extends HttpMessage<M>> { |
| 44 | |
| 45 | private final Map<String, List<String>> headers = new HashMap<>(); |
| 46 | private final Map<String, Object> attributes = new HashMap<>(); |
| 47 | private Contents.Supplier content = Contents.empty(); |
| 48 | |
| 49 | /** |
| 50 | * Retrieves a user-defined attribute of this message. Attributes are stored as simple key-value |
| 51 | * pairs and are not included in a message's serialized form. |
| 52 | * |
| 53 | * @param key attribute name |
| 54 | * @return attribute object |
| 55 | */ |
| 56 | @Nullable |
| 57 | public Object getAttribute(String key) { |
| 58 | return attributes.get(key); |
| 59 | } |
| 60 | |
| 61 | public M setAttribute(String key, Object value) { |
| 62 | attributes.put(key, value); |
| 63 | return self(); |
| 64 | } |
| 65 | |
| 66 | public M removeAttribute(String key) { |
| 67 | attributes.remove(key); |
| 68 | return self(); |
| 69 | } |
| 70 | |
| 71 | public Iterable<String> getAttributeNames() { |
| 72 | return Set.copyOf(attributes.keySet()); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Calls the {@code action} for all headers set. |
| 77 | * |
| 78 | * @param action the action to call |
| 79 | */ |
| 80 | public void forEachHeader(BiConsumer<String, String> action) { |
| 81 | headers.forEach((name, values) -> values.forEach((value) -> action.accept(name, value))); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Returns an iterable with all the names of the headers set. |
| 86 | * |
| 87 | * @return an iterable view of the header names |
| 88 | */ |
| 89 | public Iterable<String> getHeaderNames() { |
| 90 | return Collections.unmodifiableCollection(headers.keySet()); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Returns an iterable of the values of headers with the {@code name} (case-insensitive). |
| 95 | * |
| 96 | * @param name the name of the header, case-insensitive |
| 97 | * @return an iterable view of the values |
| 98 | */ |
| 99 | public Iterable<String> getHeaders(String name) { |
| 100 | return Collections.unmodifiableCollection( |