|
23 | 23 | import java.util.Map;
|
24 | 24 | import java.util.Optional;
|
25 | 25 |
|
26 |
| -/** Represents an HTTP message, either an HTTP request or a part of a multipart HTTP request. */ |
| 26 | +/** |
| 27 | + * Represents an HTTP message, either an HTTP request or a part of a multipart HTTP request. |
| 28 | + */ |
27 | 29 | public interface HttpMessage {
|
28 |
| - /** |
29 |
| - * Returns the value of the {@code Content-Type} header, if any. |
30 |
| - * |
31 |
| - * @return the content type, if any. |
32 |
| - */ |
33 |
| - Optional<String> getContentType(); |
| 30 | + /** |
| 31 | + * Returns the value of the {@code Content-Type} header, if any. |
| 32 | + * |
| 33 | + * @return the content type, if any. |
| 34 | + */ |
| 35 | + Optional<String> getContentType(); |
34 | 36 |
|
35 |
| - /** |
36 |
| - * Returns the numeric value of the {@code Content-Length} header. |
37 |
| - * |
38 |
| - * @return the content length. |
39 |
| - */ |
40 |
| - long getContentLength(); |
| 37 | + /** |
| 38 | + * Returns the numeric value of the {@code Content-Length} header. |
| 39 | + * |
| 40 | + * @return the content length. |
| 41 | + */ |
| 42 | + long getContentLength(); |
41 | 43 |
|
42 |
| - /** |
43 |
| - * Returns the character encoding specified in the {@code Content-Type} header, or {@code |
44 |
| - * Optional.empty()} if there is no {@code Content-Type} header or it does not have the {@code |
45 |
| - * charset} parameter. |
46 |
| - * |
47 |
| - * @return the character encoding for the content type, if one is specified. |
48 |
| - */ |
49 |
| - Optional<String> getCharacterEncoding(); |
| 44 | + /** |
| 45 | + * Returns the character encoding specified in the {@code Content-Type} header, or {@code |
| 46 | + * Optional.empty()} if there is no {@code Content-Type} header or it does not have the {@code |
| 47 | + * charset} parameter. |
| 48 | + * |
| 49 | + * @return the character encoding for the content type, if one is specified. |
| 50 | + */ |
| 51 | + Optional<String> getCharacterEncoding(); |
50 | 52 |
|
51 |
| - /** |
52 |
| - * Returns an {@link InputStream} that can be used to read the body of this HTTP request. Every |
53 |
| - * call to this method on the same {@link HttpMessage} will return the same object. This method is |
54 |
| - * typically used to read binary data. If the body is text, the {@link #getReader()} method is |
55 |
| - * more appropriate. |
56 |
| - * |
57 |
| - * @return an {@link InputStream} that can be used to read the body of this HTTP request. |
58 |
| - * @throws IOException if a valid {@link InputStream} cannot be returned for some reason. |
59 |
| - * @throws IllegalStateException if {@link #getReader()} has already been called on this instance. |
60 |
| - */ |
61 |
| - InputStream getInputStream() throws IOException; |
| 53 | + /** |
| 54 | + * Returns an {@link InputStream} that can be used to read the body of this HTTP request. Every |
| 55 | + * call to this method on the same {@link HttpMessage} will return the same object. This method is |
| 56 | + * typically used to read binary data. If the body is text, the {@link #getReader()} method is |
| 57 | + * more appropriate. |
| 58 | + * |
| 59 | + * @return an {@link InputStream} that can be used to read the body of this HTTP request. |
| 60 | + * @throws IOException if a valid {@link InputStream} cannot be returned for some reason. |
| 61 | + * @throws IllegalStateException if {@link #getReader()} has already been called on this instance. |
| 62 | + */ |
| 63 | + InputStream getInputStream() throws IOException; |
62 | 64 |
|
63 |
| - /** |
64 |
| - * Returns a {@link BufferedReader} that can be used to read the text body of this HTTP request. |
65 |
| - * Every call to this method on the same {@link HttpMessage} will return the same object. |
66 |
| - * |
67 |
| - * @return a {@link BufferedReader} that can be used to read the text body of this HTTP request. |
68 |
| - * @throws IOException if a valid {@link BufferedReader} cannot be returned for some reason. |
69 |
| - * @throws IllegalStateException if {@link #getInputStream()} has already been called on this |
70 |
| - * instance. |
71 |
| - */ |
72 |
| - BufferedReader getReader() throws IOException; |
| 65 | + /** |
| 66 | + * Returns a {@link BufferedReader} that can be used to read the text body of this HTTP request. |
| 67 | + * Every call to this method on the same {@link HttpMessage} will return the same object. |
| 68 | + * |
| 69 | + * @return a {@link BufferedReader} that can be used to read the text body of this HTTP request. |
| 70 | + * @throws IOException if a valid {@link BufferedReader} cannot be returned for some reason. |
| 71 | + * @throws IllegalStateException if {@link #getInputStream()} has already been called on this |
| 72 | + * instance. |
| 73 | + */ |
| 74 | + BufferedReader getReader() throws IOException; |
73 | 75 |
|
74 |
| - /** |
75 |
| - * Returns a map describing the headers of this HTTP request, or this part of a multipart request. |
76 |
| - * If the headers look like this... |
77 |
| - * |
78 |
| - * <pre> |
79 |
| - * Content-Type: text/plain |
80 |
| - * Some-Header: some value |
81 |
| - * Some-Header: another value |
82 |
| - * </pre> |
83 |
| - * |
84 |
| - * ...then the returned value will map {@code "Content-Type"} to a one-element list containing |
85 |
| - * {@code "text/plain"}, and {@code "Some-Header"} to a two-element list containing {@code "some |
86 |
| - * value"} and {@code "another value"}. |
87 |
| - * |
88 |
| - * @return a map where each key is an HTTP header and the corresponding {@code List} value has one |
89 |
| - * element for each occurrence of that header. |
90 |
| - */ |
91 |
| - Map<String, List<String>> getHeaders(); |
| 76 | + /** |
| 77 | + * Returns a map describing the headers of this HTTP request, or this part of a multipart request. |
| 78 | + * If the headers look like this... |
| 79 | + * |
| 80 | + * <pre> |
| 81 | + * Content-Type: text/plain |
| 82 | + * Some-Header: some value |
| 83 | + * Some-Header: another value |
| 84 | + * </pre> |
| 85 | + * <p> |
| 86 | + * ...then the returned value will map {@code "Content-Type"} to a one-element list containing |
| 87 | + * {@code "text/plain"}, and {@code "Some-Header"} to a two-element list containing {@code "some |
| 88 | + * value"} and {@code "another value"}. |
| 89 | + * |
| 90 | + * @return a map where each key is an HTTP header and the corresponding {@code List} value has one |
| 91 | + * element for each occurrence of that header. |
| 92 | + */ |
| 93 | + Map<String, List<String>> getHeaders(); |
92 | 94 |
|
93 |
| - /** |
94 |
| - * Convenience method that returns the value of the first header with the given name. If the |
95 |
| - * headers look like this... |
96 |
| - * |
97 |
| - * <pre> |
98 |
| - * Content-Type: text/plain |
99 |
| - * Some-Header: some value |
100 |
| - * Some-Header: another value |
101 |
| - * </pre> |
102 |
| - * |
103 |
| - * ...then {@code getFirstHeader("Some-Header")} will return {@code Optional.of("some value")}, |
104 |
| - * and {@code getFirstHeader("Another-Header")} will return {@code Optional.empty()}. |
105 |
| - * |
106 |
| - * @param name an HTTP header name. |
107 |
| - * @return the first value of the given header, if present. |
108 |
| - */ |
109 |
| - default Optional<String> getFirstHeader(String name) { |
110 |
| - List<String> headers = getHeaders().get(name); |
111 |
| - if (headers == null || headers.isEmpty()) { |
112 |
| - return Optional.empty(); |
| 95 | + /** |
| 96 | + * Convenience method that returns the value of the first header with the given name. If the |
| 97 | + * headers look like this... |
| 98 | + * |
| 99 | + * <pre> |
| 100 | + * Content-Type: text/plain |
| 101 | + * Some-Header: some value |
| 102 | + * Some-Header: another value |
| 103 | + * </pre> |
| 104 | + * <p> |
| 105 | + * ...then {@code getFirstHeader("Some-Header")} will return {@code Optional.of("some value")}, |
| 106 | + * and {@code getFirstHeader("Another-Header")} will return {@code Optional.empty()}. |
| 107 | + * |
| 108 | + * @param name an HTTP header name. |
| 109 | + * @return the first value of the given header, if present. |
| 110 | + */ |
| 111 | + default Optional<String> getFirstHeader(String name) { |
| 112 | + List<String> headers = getHeaders().get(name); |
| 113 | + if (headers == null || headers.isEmpty()) { |
| 114 | + return Optional.empty(); |
| 115 | + } |
| 116 | + return Optional.of(headers.get(0)); |
113 | 117 | }
|
114 |
| - return Optional.of(headers.get(0)); |
115 |
| - } |
116 | 118 | }
|
0 commit comments